function newPopupWindow(url, title, w, h) { 
	popupWindow = window.open(url, title, "resizable=yes,toolbar=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,width=" + w + ",height=" + h);
	popupWindow.focus();
}

$(document).ready(function() {
	// Set the binds:
	setBinds();

	// Hide the login inputs:
	$('#login input, #login .inputs').hide();
	$('#login .show').show();

	// Hide all scroller modules past the third:
	$('#modules .module:gt(2)').hide();

	// Hide all folding archives except the first:
	$('.folding_archive ul').hide();
	$('.folding_archive ul:first').show();
	
	scroll_interval = 8000;//Set the auto scroll inteval (miliseconds).
	scroll_interval_id = setInterval ( "moveScroller('down')", scroll_interval );//Start a looping call to the moveScroller function, defaulted down.
	
});

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'){
		// Hide all the modules:
		$('#modules .module').hide();

		// Loop if necessary:
		if( first_index + 3 >= module_count ){
			var first_module = $('#modules .module:first').html();
			var html = $('<div class="module">' + first_module + '</div>').hide();
			$('#modules').append(html);
			$('#modules .module:first').remove();

			first_index = first_index - 1;
		}

		// 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'){
		// Hide all the modules:
		$('#modules .module').hide();

		if( first_index <= 0 ){
			var last_module = $('#modules .module:last').html();
			var html = $('<div class="module">' + last_module + '</div>').hide();
			$('#modules').prepend(html);
			$('#modules .module:last').remove();

			first_index = first_index + 1;
		}

		// 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;
}

//Starts the looping scroll counter over again after a click on an arrow
function pauseScroller(direction){
	clearInterval(scroll_interval_id);//Stops the counter
	scroll_interval_id = setInterval ( "moveScroller('"+direction+"')", scroll_interval );//Starts it over again in the direction clicked.
	moveScroller(direction);
}

function setBinds(){
	$('#scroller .up').bind('click', function(event){
		pauseScroller('up');
	});

	$('#scroller .down').bind('click', function(event){
		pauseScroller('down');
	});

	$('#login .show a:first').bind('click', function(event){
		$('#login .show').hide();
		$('#login input, #login .inputs').fadeIn();
		$('#login .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;
	});

	$('.folding_archive .archive_title').bind('click', function(){
		$(this).parent().siblings().children('ul').hide();
		$(this).parent().children('ul').fadeIn('fast');
	});
}

function setCommercial(){
	// Hide the navigation and change the text and links:
	$('#nav ul a').hide();

	$('#nav ul a:eq(0)').attr('href', '/commercial/foodservice/');
	$('#nav ul a:eq(0)').text('Foodservice');

	$('#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', 'http://www.clabbergirl-store.com/index.html');
	$('#nav ul a:eq(5)').text('Online Store');

	// Set all tabs to inactive:
	$('#nav a').removeClass('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-rb.php', { section: 'commercial', index: 'splash' }, 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-store.com/index.html');
	$('#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 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-rb.php', { section: 'consumer', index: 'splash' }, function(data){
		$('.content').html(data);
	});

	// Show the content:
	$('.content').show();

	// Fade the navigation back into view:
	$('#nav ul a').fadeIn();
}
