jQuery(document).ready(function () {
	/* Hintergrund */
	var $bg = jQuery('#background');
	/* // Verschoben nach document.xsl
	if (jQuery(window).width() > 1600)
		$bg.css({
			marginLeft: '0px',
			left: '0px'
		});*/
	var img_width;
	var img_height;
	var resize_possible = false;
	var force_scale = true;
	function start_resize()
	{
		$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 = jQuery(window).width();
		var screen_height = jQuery(window).height();
		var cur_width;
		var cur_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;
			}
		}
		if (cur_width == img_width && cur_height == img_height)
		{
			$bg.css({
				width: cur_width + 'px',
				height: cur_height + 'px',
				top: '50%',
				left: '50%',
/*				marginTop: '-' + (cur_height / 2) + 'px', */
				marginLeft: '-' + (cur_width / 2) + 'px'
			});
		}
		else
		{
			$bg.css({
				width: cur_width + 'px',
				height: cur_height + 'px',
				top: '-'  + ((cur_height - screen_height) / 2) + 'px',
				left: '-'  + ((cur_width - screen_width) / 2) + 'px',
				marginTop: '0px',
				marginLeft: '0px'
			});
		}
	}
	if ($bg.width() && $bg.height())
		start_resize();
	$bg.bind('load', start_resize);
	jQuery(window).bind('load', start_resize);
	jQuery(window).bind('resize', handle_resize);
});

jQuery(document).ready(function () {
	/* Scroll */
	var $box = jQuery('#body');
	if (!$box.length) return;
	var $inner_scrollbox = jQuery('<div>');
	var $scrollbox = jQuery('<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: $box.height() + 'px'
	});
	$box.css({
		overflow: 'hidden'
	});
	var $scrollbar = jQuery('<div>');
	var $scrollbar_background = jQuery('<div class="scrollbar-background">');
	var $scrollbar_outer = jQuery('<div class="scrollbar">');
	var box_offset = $box.position();
	$scrollbar_outer.css({
		top: box_offset.top + 'px',
		left: (box_offset.left + $box.innerWidth() + 5) + 'px',
		height: ($box.innerHeight() - ($.support.boxModel ? 50 : 0)) + 'px'
	});
	$scrollbar_outer.append($scrollbar_background);
	$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_mousewheel(event, delta) {
		var scroll_delta = delta * 20;
		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
	}
	jQuery(document).bind('mousewheel', handle_mousewheel);
	//$box.bind('mousewheel', handle_mousewheel);
	//$scrollbar.bind('mousewheel', handle_mousewheel);
	function handle_keydown(event) {
		if (event.keyCode == 40) { // down
			handle_mousewheel(event, -1);
		} else if (event.keyCode == 38) { // up
			handle_mousewheel(event, 1);
		} else if (event.keyCode == 34) { // page down
			handle_mousewheel(event, -10);
		} else if (event.keyCode == 33) { // page up
			handle_mousewheel(event, 10);
		}
	}
	jQuery(document).bind('keydown', handle_keydown);
	function handle_resize() {
		$scrollbox.css({
			height: $box.height() + 'px'
		});
		$scrollbar_outer.css({
			height: ($box.innerHeight() - ($.support.boxModel ? 50 : 0)) + 'px'
		});
		$scrollbar_background.css({
			height: $scrollbar_outer.innerHeight() + '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();
	}
	jQuery(window).bind('load', handle_resize);
	jQuery(window).bind('load', function () { $scrollbar.slider('value', scroll_max); });
	jQuery(window).bind('resize', handle_resize);
	handle_resize();
});


