/*
INCLUDES
**********************************************/
// specify which javascripts to include on the webpage
//js('/files/javascript/SCRIPTNAME.js');


//js('/files/javascript/jquery.localscroll-1.2.7-min.js');
//js('/files/javascript/jquery.serialScroll-1.2.2-min.js');
//js('/files/javascript/jquery.scrollTo-1.4.2-min.js');
//js('/files/javascript/coda-slider.js');
js('/files/javascript/jquery.easing.1.3.js');
//js('/files/javascript/jquery.lavalamp.min.js');
js('/files/javascript/e-supplies.lavalamp.1.3.3.js');
js('/files/javascript/jcarousellite_1.0.1.min.js');

/*
GLOBAL VARIABLE OBJECT
**********************************************/
var globalVariables = {
	frontpageRotateTimeout: '5000' // frontpage carousel rotation setTimeout variable
};

// $('head').append('<style type="text/css">.carouselimages li { display: none;}</style>');

/*
ON LOAD
*********************************************/
// jQuery 'onReady' script - runs when the document have been loaded, but before images
$(function(){
	validateForms();
	toggleMainMenu();
	toggleAreaMenu();
	activateAjaxShopping();
	autocompletePostalCode();
	productlistImage();
	postPolls();
	quizWizzard();
	tracktrace();
	feedReader();
	changeProductImage();
	flashLoad();
	openPopups();
	showInlineHelp();
	variantSelection();
	// activate rotation on frontpage
	//frontpageRotation();
	// Activate LavaLamp
	/*activateLavaLamp();*/
	
	// activate projects carousel on portfolio page
	carouselActivation();
});

// Activate LavaLamp
function activateLavaLamp() {
	$("#mainMenuUl").lavaLamp({fx: "swing", speed: 333});
}


/*
// activate rotation on frontpage
function frontpageRotation(strSelector){
	if (typeof(strSelector) == 'undefined') {
		strSelector = '.jsRotation';
	}
	var objListItems = $(strSelector).find('.carouselimages li');
	var objListNumbers = $(strSelector).find('.carouselpicker li');
	
	if (objListItems.size() > 1){
//		objListItems.eq(0).fadeIn('slow');
		objListItems.eq(0).show();
		objListNumbers.eq(0).addClass('active');
		
		globalVariables.frontpageRotateTimeout = setTimeout('rotateFrontpageImages()',5000);
		
		objListNumbers.click(function(){
			clearTimeout(globalVariables.frontpageRotateTimeout);
			rotateFrontpageImages($('a',this).attr('href').replace('#',''));
			return false;
		});
	} else {
		objListNumbers.parent().hide();
	}
}

function rotateFrontpageImages(intNewItem){
	var objListNumbers = $('.jsRotation').find('.carouselpicker li');
	var objListItems = $('.jsRotation').find('.carouselimages li');
	
	var intActiveIndex = objListNumbers.index(objListNumbers.filter('.active'));
	
	if (objListNumbers.size() != (intActiveIndex+1) || typeof intNewItem != 'undefined'){ // if we are not at the end, go to next item
		if (typeof intNewItem != 'undefined'){
			// change active number
			objListNumbers.eq(intActiveIndex).removeClass('active');
			objListNumbers.eq(intNewItem).addClass('active');
			
			// change active item
			objListItems.eq(intActiveIndex).fadeOut('slow');
			objListItems.eq(intNewItem).fadeIn('slow');
		} else {
			// change active number and item
			objListNumbers.eq(intActiveIndex).removeClass('active').next().addClass('active');
			objListItems.eq(intActiveIndex).fadeOut('slow').next().fadeIn('slow');
		}
	} else { // we are at the end and we should go to the first item
		objListNumbers.eq(intActiveIndex).removeClass('active');
		objListNumbers.eq(0).addClass('active');
		
		objListItems.eq(intActiveIndex).fadeOut('slow')
		objListItems.eq(0).fadeIn('slow');
	}
	
	globalVariables.frontpageRotateTimeout = setTimeout('rotateFrontpageImages()',5000);
}
*/



function carouselActivation(){
	
	// control state and opacity of previous and next buttons
	function controlPanelnavigation(){
		// reset all disabled classes
		$('#slider').find('.projectbuttons img').removeClass('disabled').css('opacity','1');
		
		// if first panel is selected, disable the previous button
		if (objProjects.children().eq(0).hasClass('active')){
			$('#slider').find('.projectbuttons .left').addClass('disabled').css('opacity','0.5');
		}
		
		// if last panel is selected, disable the previous button
		if (objProjects.children(':last').hasClass('active')){
			$('#slider').find('.projectbuttons .right').addClass('disabled').css('opacity','0.5');
		}
	}
	
	function scrollPanelIntoView(intIndex){
		var intScrollTo = 0;
		
		objProjects.children('.active').find('.text').fadeOut('fast',function(){
			objProjects.children().removeClass('active').eq(intIndex).addClass('active').prevAll().each(function(){
				intScrollTo += $(this).width() * -1;
			});
			
			objProjects.animate({
				left: intScrollTo + 'px'
			},function(){
				objProjects.children('.active').find('.text').fadeIn('fast');
				window.location.hash = '#project' + intIndex;
				controlPanelnavigation();
			});
		});
	}
	
	// images sliding inside a project
	$('#slider').find('.panel').each(function(){
		var $this = $(this);
		var strPanelSelector = $this.attr('id');
		var arrItems = new Array;
		
		if ($this.find('.carouselpicker a').size() > 0){
			$this.find('.carouselpicker a').each(function(i){
				arrItems[i] = '#' + strPanelSelector + ' .item' + i;
			}).click(function(){
				if ($this.find('.carouselimages:animated').size() == 0){
					$(this).blur().parent().addClass('current').siblings().removeClass('current');
				}
			});
			
			$this.find('.jsRotation').jCarouselLite({
				btnGo: arrItems,
				//auto: 5000,
				speed: 800,
				visible: 1
			});
		}
	});
	
	// activate projects sliding
	var objProjects = $('#slider').find('.projectPanels'),
			intTotalwidth = 0;
			intMaxHeight = 0;
	
	// calculate total width of the projects
	objProjects.children().each(function(){
		intTotalwidth += $(this).width();
		
		if ($(this).height() > intMaxHeight) {
			intMaxHeight = $(this).height();
		}
	});
	
	// set the projects container to the total width of all projects and float all projects
	objProjects.css({
		width: intTotalwidth + 'px',
		height: intMaxHeight + 'px'
  }).children().css('float','left');
	
	// select either the specified project or the first one
	var intActiveProjectIndex = 0;
	if (window.location.hash.indexOf('#project') > -1){
		intActiveProjectIndex = parseInt(window.location.hash.replace('#project',''));
	}
	objProjects.children().eq(intActiveProjectIndex).addClass('active').siblings().find('.text').hide();
	scrollPanelIntoView(intActiveProjectIndex);
	
	controlPanelnavigation();
	
	$('#slider .projectbuttons .left').click(function(){
		if (!$(this).hasClass('disabled')){
			scrollPanelIntoView(objProjects.children().index(objProjects.children('.active')) - 1);
		}
	});
	$('#slider .projectbuttons .right').click(function(){
		if (!$(this).hasClass('disabled')){
			scrollPanelIntoView(objProjects.children().index(objProjects.children('.active')) + 1);
		}
	});
}
