// =====================================
// This function calls all the other ones
// =====================================
function all(){
	loadSelectSearchEngine ();
	loadSelectedButtons ();
	loadCustomersRefs ();
	loadCustomersCases ();
}

// =====================================
// Show menu and change its picture
// =====================================
function showAndChangePict(ulId,imgId,pictureUrl) {
	show(ulId);
	mouseChangePict(imgId,pictureUrl);
	}

// =====================================
// Manages the show/hide menus
// =====================================
function show(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=6; i++) {
		if (document.getElementById('menuList'+i)) {
			document.getElementById('menuList'+i).style.display='none';
			}
		}
	if (d) {
		d.style.display='block';
		}
	}

// =====================================
// Switch picture contents
// =====================================
function mouseChangePict(objId,pictureUrl){
	document.getElementById(objId).src=pictureUrl;
	}

// =====================================
// Switch background contents
// =====================================
function mouseChangeBack(objId,pictureUrl){
	document.getElementById(objId).style.background="url("+pictureUrl+")";
	}

// =====================================
// Submit the search form
// =====================================
function formSubmit()
{
document.getElementById(searchFormId).submit()
}

// =====================================
// Reset a form
// =====================================
function formReset(formId)
{
document.getElementById(formId).reset()
}

// =====================================
// Show the next customer logo with 
// fade-in / fade-out effects
// =====================================
function animateCustRef () {
	logos = new Array ();
	href = new Array ();
	alt = new Array ();
	
	logos = new Array (
		'/assets/pictures/buttonCustomerRef/airbusLogo.jpg',
		'/assets/pictures/buttonCustomerRef/blurLogo.jpg',
		'/assets/pictures/buttonCustomerRef/ensembleStudiosLogo.jpg',
		'/assets/pictures/buttonCustomerRef/imsiDesignLogo.jpg',
		'/assets/pictures/buttonCustomerRef/misslerSoftwareLogo.jpg',
		'/assets/pictures/buttonCustomerRef/taylorJamesLogo.jpg',
		'/assets/pictures/buttonCustomerRef/configuraLogo.jpg'
		);
	
	href = new Array (
		'http://www.airbus.com',
		'http://www.blur.com',
		'http://www.ensemblestudios.com',
		'http://www.imsidesign.com',
		'http://www.topsolid.com',
		'http://www.taylorjames.com',
		'http://www.configura.com'
		);

	alt = new Array (
		'Airbus logo',
		'Blur logo',
		'Ensemble Studios logo',
		'ImsiDesign logo',
		'Missler Software logo',
		'TaylorJames logo',
		'Configura Sverige AB logo'
		);
	
	if ( typeof animateCustRef.counter == 'undefined' ) {
		// The initialization has not been made yet
		animateCustRef.counter = 1;	// must start at 1 as logo#0 is already loaded by default
		animateCustRef.fadeOut = true;
	}
	else {
	// Increase the counter modulo the number of logos
		animateCustRef.fadeOut = !animateCustRef.fadeOut;
		if (animateCustRef.fadeOut) {
			animateCustRef.counter = (animateCustRef.counter + 1) % logos.length;
		}
	}
	
	if (animateCustRef.fadeOut) {
		$('#logoId').fadeOut(1000, animateCustRef);
	}
	else {
		$('#logoId').attr('src', logos [animateCustRef.counter]);
		$('#logoId').attr('alt', alt [animateCustRef.counter]);
		$('#hrefId').attr('href', href [animateCustRef.counter]);
		$('#customerRefContainer').unbind('click');
		$('#customerRefContainer').bind('click', function () {
			window.location.href=href [animateCustRef.counter];
			} );	
		$('#logoId').fadeIn(1000, animateCustRef);
	}
}

// =====================================
// Preload search engine button +
// set animation
// =====================================
function loadSelectSearchEngine () {
	// preload background selection
	preLoadPicture = new Image();
	preLoadPicture = '/assets/pictures/buttonSearch/searchS.jpg';
	
	$('#searchButtonId').bind ('mouseenter', function () { $('#searchButtonId').attr('src','/assets/pictures/buttonSearch/searchS.jpg'); } );
	
	$('#searchButtonId').bind ('mouseleave', function () { $('#searchButtonId').attr('src','/assets/pictures/buttonSearch/searchU.jpg'); } );
}

// =====================================
// Preload button selection backgrounds
// =====================================
function loadSelectedButtons () {
	var preLoadPictures = new Array();
	for (i=0; i<9; i++) {
		preLoadPictures[i] = new Image();
	}
	preLoadPictures[0].src = '/assets/pictures/contextButtons/addCartS.jpg';
	preLoadPictures[1].src = '/assets/pictures/contextButtons/buttonCaseS.jpg';
	preLoadPictures[2].src = '/assets/pictures/contextButtons/buttonTutorialsS.jpg';
	preLoadPictures[3].src = '/assets/pictures/contextButtons/buttonFaqS.jpg';
	preLoadPictures[4].src = '/assets/pictures/contextButtons/buttonPurchaseS.jpg';
	preLoadPictures[5].src = '/assets/pictures/contextButtons/buttonResetS.jpg';
	preLoadPictures[6].src = '/assets/pictures/contextButtons/buttonSubmitS.jpg';
	preLoadPictures[7].src = '/assets/pictures/contextButtons/buttonTrialS.jpg';
	preLoadPictures[8].src = '/assets/pictures/contextButtons/viewCartS.jpg';
}

// =====================================
// (pre)Load the customers references background
// =====================================
function loadCustomersRefs () {
	// preload background selection
	preLoadPicture = new Image();
	preLoadPicture = '/assets/pictures/buttonCustomerRef/backgroundS.jpg';
	
	$('#customerRefContainer').bind ('mouseenter', function () {
		$('#customerRefContainer').css('background-image', 'url(/assets/pictures/buttonCustomerRef/backgroundS.jpg)');
		$('#customerRefContainer').css('cursor', 'hand');
		$('#customerRefContainer > table th').css('color', '#FF0000');
		} );
	
	$('#customerRefContainer').bind ('mouseleave', function () {
		$('#customerRefContainer').css('background-image', 'url(/assets/pictures/buttonCustomerRef/backgroundU.jpg)');
		$('#customerRefContainer').css('cursor', 'pointer');
		$('#customerRefContainer > table th').css('color', '#FFFFFF');
		} );
	
	animateCustRef ();
}

// =====================================
// Load the dynamic customer cases randomly
// =====================================
function loadCustomersCases () {
	// preload background selection
	preLoadPicture = new Image();
	preLoadPicture = '/assets/pictures/buttonCustomerCase/backgroundS.jpg'
	
	$('#customerCaseContainer').bind ('mouseenter', function () {
		$('#customerCaseContainer').css('background-image', 'url(/assets/pictures/buttonCustomerCase/backgroundS.jpg)');
		$('#customerCaseContainer').css('cursor', 'hand');
		$('#customerCaseContainer > table th').css('color', '#FF0000');
		} );
	
	$('#customerCaseContainer').bind ('mouseleave', function () {
		$('#customerCaseContainer').css('background-image', 'url(/assets/pictures/buttonCustomerCase/backgroundU.jpg)');
		$('#customerCaseContainer').css('cursor', 'pointer');
		$('#customerCaseContainer > table th').css('color', '#FFFFFF');
		} );
}

// Load dynamic elements when document is ready
$(document).ready(all);
