$(document).ready(function() {
	// Set the binds:
	setBinds();

	// Hide the login inputs:
	$('form#login').hide();
	// $('#login .show').show();

	// Hide all scroller modules past the third:
	$('#modules .module:gt(2)').hide();
});

function moveScroller(direction){
	// Get some initial data:
	var module_count = $('#modules').children().length;
	var first_index = $('#modules').children().index( $('#modules .module:visible:first') );

	if(direction == 'down'){
		if( first_index + 3 < module_count ){
			// Hide all the modules:
			$('#modules .module').hide();

			// Show the relevant modules:
			$('#modules .module:gt(' + first_index + ')').show();
			$('#modules .module:gt(' + (first_index + 2) + ')').hide();
			$('#modules .module:eq(' + (first_index + 3) + ')').fadeIn();
		}
	}

	if(direction == 'up'){
		if( first_index > 0 ){
			// Hide all the modules:
			$('#modules .module').hide();

			// Show the relevant modules:
			$('#modules .module:lt(' + (first_index + 3) + ')').show();
			$('#modules .module:lt(' + first_index + ')').hide();
			$('#modules .module:gt(' + (first_index + 1) + ')').hide();
			$('#modules .module:eq(' + (first_index - 1) + ')').fadeIn();
		}
	}

	return false;
}

function setBinds(){
	$('#scroller .up').bind('click', function(event){
		moveScroller('up');
	});

	$('#scroller .down').bind('click', function(event){
		moveScroller('down');
	});

	$('a.login-trigger').live('click', function(e){
		if (navigator.appName != 'Microsoft Internet Explorer') {
			$('form#login').fadeIn();
			$('.inputs input:first').focus();
			return false;
		}
	});

	$('.splash #side_tab a').bind('click', function(event){
		var clicked = $(this).attr('href').replace('/', '').replace('/', '');

		if(clicked == 'commercial'){
			setCommercial();
		} else if(clicked = 'consumer'){
			setConsumer();
		}

		return false;
	});

	$('.splash #nav ul li').bind('click', function(event){
		// Determine the index of the clicked link:
		var nav_clicked = $('#nav ul li').index($(this));

		// Determine the name of the selected section:
		var tab_clicked = '';

		if( $('#side_tab a:first img').attr('src') == '/images/misc/tab_on-commercial.gif' ){
			tab_clicked = 'commercial';
		} else{
			tab_clicked = 'consumer';
		}

		// Set all tabs to inactive:
		$('#nav a').removeClass('active');

		// Set the proper tab to active:
		$('#nav ul a:eq(' + nav_clicked + ')').addClass('active');

		// Hide the content:
		$('.content').hide();

		// Get the new content:
		$.get('/ajax/splash.php', { section: tab_clicked, index: nav_clicked }, function(data){
			$('.content').html(data);
		});

		// Show the content:
		$('.content').show();

		return false;
	});
}

function setCommercial(){
	// Hide the navigation and change the text and links:
	$('#nav ul a').hide();

	$('#nav ul a:eq(0)').attr('href', '/commercial/food_service/');
	$('#nav ul a:eq(0)').text('Food Service');

	$('#nav ul a:eq(1)').attr('href', '/commercial/ingredients/');
	$('#nav ul a:eq(1)').text('Ingredients');

	$('#nav ul a:eq(2)').attr('href', '/commercial/private_label/');
	$('#nav ul a:eq(2)').text('Private Label');

	$('#nav ul a:eq(3)').attr('href', '/commercial/export/');
	$('#nav ul a:eq(3)').text('Export');

	$('#nav ul a:eq(4)').attr('href', '/commercial/customer_service/');
	$('#nav ul a:eq(4)').text('Customer Service');

	$('#nav ul a:eq(5)').attr('href', '/commercial/resource_center/');
	$('#nav ul a:eq(5)').text('Resource Center');

	// Set all tabs to inactive:
	$('#nav a').removeClass('active');
	
	// Set the first tab to active:
	$('#nav ul a:eq(0)').addClass('active');

	// Set the side tabs to the proper on/off values:
	$('#side_tab img:first').attr('src', $('#side_tab img:first').attr('src').replace('_off-', '_on-') );
	$('#side_tab img:last').attr('src', $('#side_tab img:last').attr('src').replace('_on-', '_off-') );

	// Hide the content:
	$('.content').hide();

	// Get the new content:
	$.get('/ajax/splash.php', { section: 'commercial', index: 0 }, function(data){
		$('.content').html(data);
	});

	// Show the content:
	$('.content').show();

	// Fade the navigation back into view:
	$('#nav ul a').fadeIn();
}

function setConsumer(){
	// Hide the navigation, change the text and links, then show it again:
	$('#nav ul a').hide();

	$('#nav ul a:eq(0)').attr('href', '/consumer/products/');
	$('#nav ul a:eq(0)').text('Products');

	$('#nav ul a:eq(1)').attr('href', '/consumer/recipes/');
	$('#nav ul a:eq(1)').text('Recipes');

	$('#nav ul a:eq(2)').attr('href', '/consumer/baking_fun/');
	$('#nav ul a:eq(2)').text('Baking Fun');

	$('#nav ul a:eq(3)').attr('href', '/consumer/bakers_blog/');
	$('#nav ul a:eq(3)').text('Baker\'s Blog');

	$('#nav ul a:eq(4)').attr('href', 'http://www.clabbergirl.com/store/');
	$('#nav ul a:eq(4)').text('Online Store');

	$('#nav ul a:eq(5)').attr('href', '/consumer/newsletter/');
	$('#nav ul a:eq(5)').text('Newsletter');

	// Set all tabs to inactive:
	$('#nav a').removeClass('active');
	
	// Set the first tab to active:
	$('#nav ul a:eq(0)').addClass('active');

	// Set the side tabs to the proper on/off values:
	$('#side_tab img:first').attr('src', $('#side_tab img:first').attr('src').replace('_on-', '_off-') );
	$('#side_tab img:last').attr('src', $('#side_tab img:last').attr('src').replace('_off-', '_on-') );

	// Hide the content:
	$('.content').hide();

	// Get the new content:
	$.get('/ajax/splash.php', { section: 'consumer', index: 0 }, function(data){
		$('.content').html(data);
	});

	// Show the content:
	$('.content').show();

	// Fade the navigation back into view:
	$('#nav ul a').fadeIn();
}

