var isThumbnailClicked = false;



jQuery('#gallery').ready(function() {

	// setup initial view

	if(isSlideShow) galleryInit();

	// thumbnail click action

	jQuery('.thumb').click(

		function() {

			jQuery("#videoDisclaimerId").css({ display:"none" });

			clearInterval(slideshowTimer);

			var thisThumbId = jQuery(this).attr("id").split('thumbTainer')[1];

			if(imagePathLrg.length>0 && jQuery('#image'+thisThumbId)) {

				loadThisImage(thisThumbId); 

			}

			jQuery("#vehicleVideo").empty();

			jQuery("#vehPhoto").show();

			if(isThumbnailClicked == false)

				slideshowTimer = setTimeout("slideshow()",3000);

		}, 

		function() {

			var thisThumbId = jQuery(this).attr("id").split('thumbTainer')[1];

			if(document.getElementById('image'+thisThumbId)){

				if(thisThumbId!=loadedIndex){ jQuery('#gThumb'+thisThumbId).removeClass('tumbBorder'); }

			}

			if ((!activePopup) && (isThumbnailClicked == false)) {

				slideshowTimer = setTimeout("slideshow()",3000);

			}

		}

	);

});

galleryInit = function () {

	startIndex = 0;

	loadedIndex = 0;

	masterIndex = 0;

	loadThumbs(startIndex);

	loadThisImage(0);

	if (playMode == false)

		jQuery('#gThumb0').addClass('tumbBorder');

	setMorePhotosIfAny();

	if ((!playMode) && (isThumbnailClicked == false))

		slideshowTimer = setTimeout("slideshow()",3000);

}

setMorePhotosIfAny = function() {

	var morePhotos = eval(imagePathLrg.length - thumbCount);

	if (morePhotos > 0) {
	
		document.getElementById('totalImages').innerHTML = '<a class="moreImages" style="color:#11527F;" href="#" onClick="openPopup(); return false;">' + morePhotos + ' more </a>';

	}

}

loadThisImage = function(id){

	jQuery('#gThumb'+loadedIndex).removeClass('tumbBorder');

	if (jQuery('.tumbBorder').length == 0) 

		jQuery('#gThumb'+id).addClass('tumbBorder');

	loadedIndex = id;

	// determine masterIndex

	var thisImagePath = document.getElementById('image'+id).src;

	for(k=0;k<thumbCount;k++){

		if(thisImagePath==imagePathMed[k]){ masterIndex=k; break; }

	}

	document.getElementById('vehiclePhotoWrapper_0').innerHTML = '<img id="vehiclePhoto_0" src="'+imagePathLrg[masterIndex]+'">';

}

loadThumbs = function (index) {

	startIndex = index;	

	for(a=0;a<thumbCount;a++){

		document.getElementById('gThumb'+a).innerHTML = '<img id="image'+a+'" src="'+imagePathMed[index]+'" width="35" height="25">';

		jQuery('#thumbTainer'+a).css({ cursor:"pointer"});

		index++;

		

	}

}

slideshow = function (){

	if(loadedIndex < (thumbCount-1)){

		var nextImageId = Number(loadedIndex)+1;

		if(document.getElementById('image'+nextImageId)){

			loadThisImage(Number(loadedIndex)+1);

		}

	} else if(loadedIndex == (thumbCount-1)){

		loadThumbs(0);

		loadThisImage(0);

	}

	if(isThumbnailClicked == false)

		slideshowTimer = setTimeout("slideshow()",5000);

}

openPopup = function() {

	if (jQuery("#"+Photos.getInstance().getIframeId()+"").length == 0 ) {      

		var verticalElement = jQuery("#overviewTab")[0];  // reference to parent result item element

		activePopup = true;

		Photos.getInstance().open(verticalElement, vehicleID,true,'VehicleDetails',isPhotoAvailable);

    } else {

		alert ("There is currently another Photopopup form open.");

    }

}



jQuery("div.thumbImage").click(function() {

	isThumbnailClicked = true;

});