/***************************************************
Handling all gallery image swap functions, including 
swapping flash videos for certain cars (i.e. Corvette)
Date: 	Jan 2005
Author: skube
***************************************************/
// Intial highlighted thumbnail (or current photo) will always be the first one 
var currPhoto = '01';
var hasFlash = true; // assume user can 
var server;

var thisLoc = location.toString();
var isLightWeight = thisLoc.indexOf('_lw') != -1;



// Base path and image prefixes (*** put in http://gm.ca/ for EDS, but NOT for itl.gm.ca QA site)
if (thisLoc.indexOf('itl') != -1) { // we are on ITL
	server = 'http://itl.gm.ca';
} else if (thisLoc.indexOf('preprod') != -1) { // we are on PREPROD
	server = 'http://preprod.gm.ca';
} else { // default to LIVE
	server = 'http://gm.ca';
}
//alert('testing: \nserver for basepath is : '+server+'\nthis location is: '+thisLoc);

var basePath = server + '/images/vehicles/commercial/';
var galImgPathPrefix = basePath + make + '_' + section + '_gall_';

// object ref
var galPhotoPrefix = 'galPhoto';	

function preLoadGalleryImgs() {
	for(var i=1;i<galNum+1;i++) {
		var num = i<10 ? '0'+i : i; // deal with the pesky leading zero
		// first the MAIN images ONLY if not VIDEOS
		galPhotoPrefix + num + ' = new Image()';
 		galPhotoPrefix + num + '.src = ' + galImgPathPrefix + num + '.jpg';
	}
}
preLoadGalleryImgs();


// Thumbnail href event 
function loadPhoto(num) {
	if (currPhoto == num) return; // do nothing if already on currPhoto
		// update main photo
		getRef('galPhoto01').src = galImgPathPrefix + num + '.jpg';
		getRef('galPhoto01').title = galAltArray[num-1][0];
		getRef('galPhoto01').alt = galAltArray[num-1][1];
		
		// update main photo caption (*** JAN 2005 COMMENTED OUT until all gallery alt tags have been entered properly)
		getRef('commPhotoCaption').innerHTML = galAltArray[num-1][0];

		updateCurr(num); // change currPhoto var and update thumbnails
}

// PREVIOUS button href event 
function prevPhoto() {
	// As long as not on first photo:
	if (currPhoto != '01') {
		finishNextPrev('back',currPhoto);
		photoNums();
	}else{
		galNum++;
		finishNextPrev('back',galNum);
		galNum--;
		photoNums();
		}
}
// NEXT button href event
function nextPhoto() {
	// As long as not on last photo:
	if (currPhoto != galNum) {
		finishNextPrev('fwd',currPhoto);
		photoNums();
	}else{
		finishNextPrev('fwd','00');
		photoNums();
	}
}
// Finish off what prevPhoto/nextPhoto started
function finishNextPrev(direction,startPos){
	var numPhoto = new Number(startPos);
	(direction == 'fwd') ? numPhoto++ : numPhoto--;
	var num = numPhoto<10 ? '0'+numPhoto : numPhoto; 	// deal with the pesky leading zero
	loadPhoto(num);
}


/***************** HELPER FUNCTIONs *******************/

// Updates currPhoto var and swaps thumbnails
function updateCurr(num) {
	currPhoto = num; 
}

function photoNums(thisNum) {
	var thisNum;
	if (currPhoto<10){
		var splitThisNum = currPhoto.split('0');
		thisNum = splitThisNum[1];
	}
	else{
		thisNum = currPhoto;
	}
	getRef('gallCounter').innerHTML = thisNum + '/' + galNum;
}



