var banner_timeout = 7000;
var banner_timer;
var banner_locked = false;
var banner_images_length;
var banner_image_height;
var banner_current = 0;

function banner_select( position ) {
	if( ! banner_locked ) {
		window.clearTimeout( banner_timer );
		banner_locked = true;
		$( "a:eq(" + banner_current + ")", "div.banner div.link_layer" ).removeClass( "selected" );
		$( "a", "div.banner div.link_layer" ).addClass( "locked" );

	$( "h2:not(:eq(" + position + "))", "div.banner div.text_layer" ).fadeOut(1500);
	$( "h2:eq(" + position + ")", "div.banner div.text_layer" ).fadeIn(1500);

		$( "img:eq(" + banner_current + ")", "div.banner div.banners" ).animate(
			{
				"top": "-" + banner_image_height + "px"
			},
			{
				queue: false,
				duration: 1500,
				easing: "easeInOutSine"
			},
			function () {
			}
		);

		$( "img:eq(" + position + ")", "div.banner div.banners" ).animate(
			{
				"top": "0px"
			},
			{
				queue: false,
				duration: 1500,
				easing: "easeInOutSine",
				complete: function() {
					$( "img:eq(" + banner_current + ")", "div.banner div.banners" ).css( "top", banner_image_height + "px" );
					banner_current = position;
					$( "a:eq(" + position + ")", "div.banner div.link_layer" ).addClass( "selected" );
					banner_timer = window.setTimeout( "banner_next()", banner_timeout );
					banner_locked = false;
					$( "a", "div.banner div.link_layer" ).removeClass( "locked" );
				}
			},
			function () {
			}
		);
	}
}

function banner_next() {
	var next_position = banner_current + 1;

	if( next_position < banner_images_length ) {
		banner_select( next_position );
	} else {
		banner_select(0);
	}
}

$( document ).ready( function() {
	banner_images_length = $( "img", "div.banner div.banners" ).length;
	banner_image_height = parseInt( $( "img", "div.banner div.banners" ).height() );
	banner_timer = window.setTimeout( "banner_next()", banner_timeout );

	$( "img:eq(0)", "div.banner div.banners" ).css({
		"left": "0px",
		"position": "absolute",
		"top": "0px"
	});
	$( "img:not(:eq(0))", "div.banner div.banners" ).css({
		"left": "0px",
		"position": "absolute",
		"top": banner_image_height + "px"
	});

	$( "h2:eq(0)", "div.banner div.text_layer" ).css({
		"display": "block"
	});
	$( "h2:not(:eq(0))", "div.banner div.text_layer" ).css({
		"display": "none"
	});

	$( "a", "div.banner div.link_layer" ).each( function( index, element ) {
		$( this ).click( function( event ) {
			if( index != banner_current) {
				event.preventDefault();
				banner_select( index );
			}
		});
	});
});

