hg.layout = (function($) {
	/* Hintergrund */
	var $bg;
	var $bg_wrapper;
	var $page;
	var img_width;
	var img_height;
	var resize_possible = false;
	var force_scale = true;
	
	function start_resize()
	{
		$bg_wrapper.css('overflow', 'hidden');
		$bg.css('width', 'auto');
		$bg.css('min-width', '0px');
		img_width = $bg.width();
		img_height = $bg.height();
		if (img_width != 1600)
		{
			img_height = img_height * (1600 / img_width);
			img_width = 1600;
		}
		$bg.css('width', '100%');
		if (img_height && img_width)
		{
			resize_possible = true;
			handle_resize();
		}
	};
	
	function handle_resize()
	{
		if (!resize_possible) return;
		var screen_width = $(document).width();
		var screen_height = $(document).height();
		var bg_height = $bg_wrapper.height();
		var cur_width;
		var cur_height;
		
		var page_height = $page.outerHeight();
		var window_height = $(window).height();
		if (screen_height > page_height)
			screen_height = page_height;
		if (screen_height < window_height)
			screen_height = window_height;
		
		if (img_width/img_height > screen_width/screen_height)
		{
			if (force_scale || img_height < screen_height)
			{
				cur_width = (screen_height / img_height) * img_width;
				cur_height = screen_height;
			}
			else
			{
				cur_width = img_width;
				cur_height = img_height;
			}
		}
		else
		{
			if (force_scale || img_width < screen_width)
			{
				cur_width = screen_width;
				cur_height = (screen_width / img_width) * img_height;
			}
			else
			{
				cur_width = img_width;
				cur_height = img_height;
			}
		}
		$bg_wrapper.height(screen_height);
		if (cur_width == img_width && cur_height == img_height)
		{
			$bg.css({
				width: cur_width + 'px',
				height: cur_height + 'px',
				//top: '50%',
				top: '0px',
				left: '50%',
				//marginTop: '-' + (cur_height / 2) + 'px',
				marginTop: '0px',
				marginLeft: '-' + (cur_width / 2) + 'px'
			});
		}
		else
		{
			$bg.css({
				width: cur_width + 'px',
				height: cur_height + 'px',
				//top: '-'  + ((cur_height - screen_height) / 2) + 'px',
				top: '0px',
				left: '-'  + ((cur_width - screen_width) / 2) + 'px',
				//marginTop: '0px',
				marginTop: '0px',
				marginLeft: '0px'
			});
		}
	};
	
	function init()
	{
		$bg = $('#background');
		$bg_wrapper = $('#background-wrapper');
		$page = $('#page');
		if ($bg.width() && $bg.height())
			start_resize();
		$bg.bind('load', start_resize);
		$(window).bind('load', start_resize);
		$(window).bind('resize', handle_resize);
	};
	
	return {
		start_resize: start_resize,
		handle_resize: handle_resize,
		init: init
	};
})(jQuery);

hg.button = (function($) {
	var defaults = {
		interval: 50,
		callback: function () {},
		_interval: null
	};
	
	function _start($el, options)
	{
		return (function (event) {
			options._interval = window.setInterval(options.callback, options.interval);
		});
	}
	
	function _stop($el, options)
	{
		return (function (event) {
			if (options._interval)
				window.clearInterval(options._interval);
		});
	}
	
	function bind(el, _options)
	{
		var $el = el;
		var options = $.extend({}, defaults, _options);
		$el.bind('mousedown', _start($el, options));
		$el.bind('mouseleave mouseup', _stop($el, options));
	}
	
	return {
		bind: bind
	};
})(jQuery);

hg.scroll = (function($) {
	function init()
	{
		/* Cleanup */
		$('.scrollbar').remove();
		/* Scroll */
		var $body = $('body');
		$body.css({
			overflow: 'hidden'
		});
		var $box = $('#content');
		if (!$box.length) return;
		var $inner_scrollbox = $('<div>');
		var $scrollbox = $('<div class="scrollbox">');
		$inner_scrollbox.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			width: '100%'
		});
		$inner_scrollbox.append($box.children().clone(true));
		$scrollbox.append($inner_scrollbox);
		$box.empty().append($scrollbox);
		$scrollbox.css({
			overflow: 'hidden',
			height: (Math.min($inner_scrollbox.height(), $(window).height() - 260)) + 'px'
		});
		$box.css({
			overflow: 'hidden'
		});
		var $scrollbar = $('<div>');
		var $scrollbar_up = $('<div class="scrollbar-up">');
		var $scrollbar_down = $('<div class="scrollbar-down">');
		var $scrollbar_outer = $('<div class="scrollbar">');
		var box_offset = $box.position();
		$scrollbar_outer.append($scrollbar_up);
		$scrollbar_outer.append($scrollbar_down);
		$scrollbar_outer.append($scrollbar);
		$box.after($scrollbar_outer);
		var scroll_max = $inner_scrollbox.height() - $scrollbox.height();
		function scroll_to(value) {
			$inner_scrollbox.css({
				top: '-' + (scroll_max - value) + 'px'
			});
		}
		function handle_slide(event, ui) {
			scroll_to(ui.value);
		}
		$scrollbar.slider({
			orientation: "vertical",
			min: 0,
			max: scroll_max,
			value: scroll_max,
			slide: handle_slide,
			change: handle_slide
		});
		function handle_scroll(delta) {
			var scroll_delta = delta * 5;
			var value = $scrollbar.slider('value');
			value += scroll_delta;
			if (value > scroll_max) value = scroll_max;
			else if (value < 0) value = 0;
			$scrollbar.slider('value', value);
			//scroll_to(value); // just to be sure, events of slider should be enough
		}
		$(document).bind('mousewheel', function (event, delta) { handle_scroll(delta * 4); });
		//$scrollbar_up.bind('click', function (event) { handle_scroll(4); } );
		hg.button.bind($scrollbar_up, {
				callback: function (event) { handle_scroll(1); }
		})
		//$scrollbar_down.bind('click', function (event) { handle_scroll(-4); } );
		hg.button.bind($scrollbar_down, {
				callback: function (event) { handle_scroll(-1); }
		})
		//$box.bind('mousewheel', handle_mousewheel);
		//$scrollbar.bind('mousewheel', handle_mousewheel);
		function handle_keydown(event) {
			if (event.keyCode == 40) { // down
				handle_scroll(-2);
			} else if (event.keyCode == 38) { // up
				handle_scroll(2);
			} else if (event.keyCode == 34) { // page down
				handle_scroll(-20);
			} else if (event.keyCode == 33) { // page up
				handle_scroll(20);
			}
		}
		$(document).bind('keydown', handle_keydown);
		function handle_resize() {
			box_offset = $box.position();
			$scrollbox.css({
				height: (Math.min($inner_scrollbox.height(), $(window).height() - 260)) + 'px'
			});
			$scrollbar_outer.css({
				top: box_offset.top + 'px',
				left: (box_offset.left + $box.innerWidth() + 20) + 'px'
			});
			var old_scroll_max = scroll_max;
			scroll_max = $inner_scrollbox.height() - $scrollbox.height();
			var diff = old_scroll_max - scroll_max;
			$scrollbar.slider('option', 'max', scroll_max);
			$scrollbar.slider('value', $scrollbar.slider('value') - diff);
			if ($box.height() >= $inner_scrollbox.height())
				$scrollbar_outer.hide();
			else
				$scrollbar_outer.show();
		}
		$(window).bind('load', handle_resize);
		$(window).bind('load', function () { $scrollbar.slider('value', scroll_max); });
		$(window).bind('resize', handle_resize);
		handle_resize();
		return {
			handle_resize: handle_resize,
			handle_scroll: handle_scroll,
			scroll_to: scroll_to
		};
	};
	
	return {
		init: init
	}
})(jQuery);


jQuery(document).ready(function () {
	hg.layout.init();
	hg.scroll.init();
});



