function mouseHitTest( o ) {
	if ( iPageX >= o.offsetLeft && iPageX <= (o.offsetLeft + o.offsetWidth) && iPageY >= o.offsetTop && iPageY <= o.offsetTop + o.offsetHeight ) {
		return true;
	}
	return false;
}

var iBackground = 0;
function showBackgroundImage() {
	var aSRC = background_image;
	
	$('body').append('<div id="bg"><img src="' + aSRC[0] + '" width="100%" alt="" /></div>');
	$('#bg').insertBefore('body:first');
}

function nextBackground() {
	var aSRC = background_image;
	iBackground++;
	if ( iBackground == aSRC.length ) {
		iBackground = 0;
	}
	$('#bg img').attr('src', aSRC[ iBackground ]);
}

function previousBackground() {
	var aSRC = background_image;
	iBackground--;
	if ( iBackground < 0 ) {
		iBackground = aSRC.length - 1;
	}
	$('#bg img').attr('src', aSRC[ iBackground ]);
}

var is_sliding = false;

function slideDown( o ) {
	if( !is_sliding ) {
		if ( o.css( 'bottom' ) == '0px' ) {
			is_sliding = true;
			o.animate( {
				bottom: '-' + (o.outerHeight() - 10) + 'px'
			}, 1000, function() {
				is_sliding = false;
			});
		}
	}
}

function slideUp( o ) {
	if( !is_sliding ) {
		if ( o.css( 'bottom' ) != '0px' ) {
			is_sliding = true;
			o.animate( {
				bottom: '0px'
			}, 1000, function() {
				is_sliding = false;
			});
		}
	}
}

function resizeBackground() {
	var o = document.getElementById('bg').getElementsByTagName('img')[0];
	o.parentNode.style.backgroundColor = '#000';
	
	if ( $('#bg img').attr('width') == '0' || $('#bg img').attr('height') == '0' ) {
		window.setTimeout('resizeBackground()', 10);
		return false;
	}
	o.parentNode.style.backgroundColor = '';
	if ( ( $(window).width() / $(window).height() ) > ( $('#bg img').attr('width') / $('#bg img').attr('height') ) ) {
		o.removeAttribute('height');
		o.setAttribute('width', '100%');
	} else {
		o.removeAttribute('width');
		o.setAttribute('height', '100%');
	}
}

var iPageX = iPageY = 0;
var menu_hide = false;
var background_image = [];
$(document).ready( function() {
	menu_hide = $('#menu').attr('rel');
	background_image = eval( $('body').attr('rel') );
	
	if ( !background_image ) {
		background_image = [];
	}
	
	if ( menu_hide == 'hidefromstart' ) {
		slideDown( $('#menu') );
	}
	
	if ( menu_hide == 'hide' || menu_hide == 'hidefromstart' ) {
		$('#menu').hover(
			function() {
				slideUp( $(this) );
			},
			function() {
				slideDown( $(this) );
			}
		);
	}
	
	if ( background_image.length ) {
		showBackgroundImage();
		if ( background_image.length > 1 ) {
			showMouseArrow();
		}
	}
	
	resizeBackground();
	
	$(window).bind("resize", function(){
		resizeBackground();
 	});
});

function showMouseArrow() {
	$('body').append('<img id="mousearrow" src="/images/right.gif" alt="" width="8" height="12" />');
	
	$('body').mousemove( function(e){
		iPageX = e.pageX;
		iPageY = e.pageY;
		
		if ( mouseHitTest( document.getElementById('menu') ) ) {
			$('#mousearrow').css( 'display', 'none' );
			return false;
		}
		
		$('#mousearrow').css( 'display', 'inline' );
		// Hvis musen er i højre side af skærmen
		if ( e.pageX > ($('body').width() / 2) ) {
			$('#mousearrow').attr('src', '/images/right.gif');
			$('#mousearrow').css( 'left', iPageX + 15 + 'px' );
		// Hvis musen er i venstre side af skærmen
		} else {
			$('#mousearrow').attr('src', '/images/left.gif');
			$('#mousearrow').css( 'left', iPageX - $('#mousearrow').width() - 12 + 'px' );
		}
		$('#mousearrow').css( 'top', iPageY + 'px' );
	});
	
	$('body').mouseover( function(e){
		$('#mousearrow').css( 'display', 'inline' );
	});
	
	$('body').mouseout( function(e){
		$('#mousearrow').css( 'display', 'none' );
	});
	
	$('body').click( function(e){
		// Hvis musen er i højre side af skærmen
		if ( e.pageX > ($('body').width() / 2) ) {
			nextBackground();
		// Hvis musen er i venstre side af skærmen
		} else {
			previousBackground();
		}
	})
}
