// JavaScript Document

//-- Global Variables pertaining to the page --//
var totalHandleLength = 717;
var ulInc=-113;

var galArr = [1069651329024,1051696562176,1055647596544,1055683248128,1055859408896,1055759794176,1057299103744,1055601459200,1049490358272,1054225727488,1052673835008,1050946830336,1052533325824,1051020230656,1051454341120];
var tmpArr = [1056192856064,1056223264768,1056193904640,1056204390400,1056225361920,1056194953216,1056196001792,1056198098944,1056201244672,1056203341824,1056221167616,1056227459072];	

//--- Element Declaration ---//


//-- Immediate execution --//
//maximizeWindow();


//--- On load execution ---//
$(window).ready(function () {
		/*** Init Page ***/	
	
	$('div.sliderGallery').each(function () {
		var totalSamples = $("#samplesTotal").val();
		var ul = $('ul', this);
		//var productWidth = ul.innerWidth() - $(this).outerWidth();
		productWidth = 48.42 * totalSamples
		var slider = $('.slider', this).slider({ 
		  handle: '.handle',
		  minValue: 0, 
		  maxValue: productWidth, 
		  slide: function (ev, ui) {
			ul.css('left', '-' + ui.value + 'px');
		  }, 
		  stop: function (ev, ui) {
			//console.log("ui.value : " + ui.value);
			ul.animate({ 'left' : '-' + ui.value + 'px' }, 900, 'linear');
		  }
		});
	});
	
	
	var gal = getURLParam("smp");
	if(gal!=null && gal !='') {	
		matchAndClick(galArr, parseInt(gal));
	} 
	
	var tmp = getURLParam("tmp");
	if(tmp!=null && tmp !='') {			
		matchAndClick(tmpArr, parseInt(tmp));

	} 
	
	var pd = getURLParam("pd");
	if(pd!=null && pd !='') {
		
		var lid = parseInt(pd);
		var matchedId = false;
		var pos = 1; //Default
		var lidArr = galArr; //Default
		
		//Search through the galArr first to see if id matches
		for (var x=0; x<galArr.length; x++) {
			if (galArr[x] == lid) {
				pos = x + 1;
				lidArr = galArr;
				matchedId = true;
				break;
			}
		}
		
		//If no match was found in galArr search through tmpArr.
		if (!matchedId) {
			//Search through the galArr first to see if id matches
			for (var x=0; x<tmpArr.length; x++) {
				if (tmpArr[x] == lid) {
					pos = x + 1;
					lidArr = tmpArr;
					matchedId = true;
					break;
				}
			}
			
		}
		matchAndClick(lidArr, pos);

	} 
	
	
	 //Manual click of thumb.
	//$("a.thumbLink:first").click();
});


//--- Page Functions---//

/*
* Click on the corret page design on load if parameter is passed for samples.
*/
function matchAndClick(lidArr, pos) {	
	var index = pos-1;
	var lid = lidArr[index];
	if (!lid) {
		lid = lidArr[0];
		pos = 1;
	}
	var thumb = $("#"+lid);
	clickedThumb(thumb, pos, lid) ;
}



function clickedThumb(thumb, pos, lid) {
	$("#displaySample").attr("src", "/samplesPreview.jsp?id="+lid);
	$(thumb).parent().siblings().each(function(){
		$(this).children().children().css("border", "5px solid #fff");
		//this.removeClass(sampleThumbOn);
	});
	$(thumb).children().css("border", "5px solid #3a80bd");
	scrollGallery(pos);
	
}

function scrollGallery(pos) {
	pos = pos-1;
	var lastScrollPos = getLastScrollablePos();	
	var handInc= (totalHandleLength/lastScrollPos);
	var ulPos = ulInc*pos;
	var handPos = handInc*pos;
	
	if (!shouldScrollGallery(pos)) {		
		ulPos = ulInc * lastScrollPos ; //calculate ulInc * lastPos
		handPos=totalHandleLength;
	} 
	
	$("ul.items").animate({ 'left' : ulPos+"px"  }, 900, 'linear');
	$("ul.items").css('left', ulPos +"px");
	$(".handle").animate({ 'left' : handPos+"px" }, 900, 'linear');
	$(".handle").css('left', handPos+"px");
	
}

function shouldScrollGallery(pos) {
	var lastScrollPos = getLastScrollablePos();
	if (pos >= lastScrollPos) {
		return false;	
	}
	return true;
}

function getLastScrollablePos() {
	var totalSamples =$("#samplesTotal").val();
	return totalSamples-8;
}
