function activate( element ) {
	var selectedButton = jQuery( element );
	selectedButton.parent().parent().children().each( function() { jQuery( this ).children().removeClass( 'selected' ); } );
	selectedButton.addClass( 'selected' );
}

function removeScrollButtons() {
	jQuery( "a[class='left_button']" ).remove();
	jQuery( "a[class='right_button']" ).remove();
	jQuery( "div#fxWrapper" ).remove();
}

function loadSlidesFrom( button, source ) {
	activate( button );
	jQuery('#banner_container').load( source );
	removeScrollButtons();
	setTimeout( "configureSlidingBanner()", 250 );
	return false;
}

function hookUpBanner() {
	jQuery( '#leggera-range-button' ).click(
		function( event ) {
			loadSlidesFrom( this, '/controls/_leggera.html #range > *' );
			event.preventDefault();
		}
	);

	jQuery( '#leggera-pizzas-button' ).click(
		function( event ) {
			loadSlidesFrom( this, '/controls/_leggera.html #pizzas > *' );
			event.preventDefault();
		}
	);

	jQuery( '#leggera-starters-button' ).click(
		function( event ) {
			loadSlidesFrom( this, '/controls/_leggera.html #starters > *' );
			event.preventDefault();
		}
	);

	jQuery( '#leggera-dessert-button' ).click(
		function( event ) {
			loadSlidesFrom( this, '/controls/_leggera.html #deserts > *' );
			event.preventDefault();
		}
	);

	jQuery( '#lighter-drinks-button' ).click(
		function( event ) {
			loadSlidesFrom( this, '/controls/_leggera.html #drinks > *' );
			event.preventDefault();
		}
	);
}

function showButtons() {
	if (index > 0 && leftButton.is(":not(:visible)")) {
		leftButton.show("slide", { direction: "left" }, "fast");
	}
	if (index != banners.length - 1 && rightButton.is(":not(:visible)")) {
		rightButton.show("slide", { direction: "right" }, "fast");
	}
}

function hideButtons() {
	if (leftButton.is(":visible")) {
		leftButton.hide("slide", { direction: "left" }, "fast");
	}
	if (rightButton.is(":visible")) {
		rightButton.hide("slide", { direction: "right" }, "fast");
	}
}

function transition(direction) {
	return function() {
		index += direction;
		banners.animate(getAnimation(direction), 1000, "easeInOutCubic", showButtons);
		if (index < 1) {
			leftButton.hide();

			if (banners.is('.auto')) {
				clearInterval(interval);
				interval = setInterval(transition(1), 10000);
			}
		}
		if (index == banners.length - 1) {
			rightButton.hide();

			if (banners.is('.auto')) {
				clearInterval(interval);
				interval = setInterval(transition(-1), 10000);
			}
		}
		return false;
	}
}

function getAnimation(direction) {
	var operator = direction == 1 ? "-=" : "+=";
	return { left: operator + (banners.width()) }
}


function hookupLeggeraBanner() {
	index = 0;
	interval = null;
	
	function buildLeftButton() {
		var button = jQuery('<a class="left_button" href="#"></a>');
		button.click(transition(-1));
		button.hide();
		return button;
	}

	function buildRightButton() {
		var button = jQuery('<a class="right_button" href="#"></a>');
		button.click(transition(1));
		button.hide();
		return button;
	}

	banners = jQuery('div.slide');
	leftButton = buildLeftButton();
	rightButton = buildRightButton();
	banners.parent().parent().parent().prepend(rightButton).prepend(leftButton).parent().hover(showButtons, hideButtons);

	if (banners.is('.auto')) {
		interval = setInterval(transition(1), 10000);
	}
}


function configureSlidingBanner() {
	hookupLeggeraBanner();
	showButtons();
}

jQuery(hookUpBanner);