// var featureViewPath = ; set inline
var currentFeatureID = 0;
// var totalFeatures = ; set inline

var jThumbList = new Array(); // ids = feature_thumb_0 - N
var jTextList = new Array(); // ids = feature_text_0 - N

var jThumbDescription = null; // id = thumb-description
var jFeatureName = null; // id = feature-name
var jFeatureDescription = null; // id = feature-description
var jFeatureSku = null; // id = feature-sku
var jFeatureMsrp = null; // id = feature-msrp

var jThumbContainer = null; // thumb-container
var jFeatureListContainer = null; // feature-list-container

var jMainImage = null; // id = main

var instructionsTimerID;
var autoplayTimerID;

// flash bool
skipACall = false;

var gallerySlideShowTime = 8000; // the time in MS that a gallery will wait on each image when the audio is muted

$(function() {
	for(var i=0; i<totalFeatures; i++){ 
		jThumbList[i] = $('#feature_thumb_'+i);
		jTextList[i] = $('#feature_text_'+i);
	}
	
	jThumbDescription = $('#thumb-description');
	jFeatureName = $('#feature-name');
	jFeatureDescription = $('#feature-description');
	jFeatureSku = $('#feature-sku');
	jFeatureMsrp = $('#feature-msrp');
	
	jThumbContainer = $('#thumb-container');
	jFeatureListContainer = $('#feature-list-container');
	
	jMainImage = $('#main-image');
	jFlash = $('#flash-mgallery');
	
	$('#view-by-thumb').click(function(){feature_view_by(this.id);});
	$('#view-by-text').click(function(){feature_view_by(this.id);});

	t = setTimeout(function() {
			if (galleryPlaying) {
				autoplayTimerID = setTimeout("play_gallery(true)", gallerySlideShowTime);
			}
		}, 50);	
});

var gallery_shift = function (direction) {
	//alert('shifting '+direction);
	var nextID = (currentFeatureID + direction + totalFeatures) % totalFeatures; // allows wrapping
	feature_select(nextID);
}

function feature_view_by(what) {
	//alert('view by: '+what);
	thumbs = (what == 'view-by-thumb');
	jThumbContainer.css("display",thumbs ? 'block' : 'none');
	jFeatureListContainer.css("display",thumbs ? 'none' : 'block');
	galleryThumbs = thumbs;
	updatePreferences();
}

function feature_over(id) {
	clearTimeout(instructionsTimerID);
	id = id.replace(/^\D*(\d+)$/, "$1");
	jThumbDescription.html(featureData.feature[id].name);
	jThumbList[id].addClass('over');
}

function feature_out(id) {
	id = id.replace(/^\D*(\d+)$/, "$1");
	jThumbList[id].removeClass('over');
	instructionsTimerID = setTimeout(resetInstructions, 200);
}

var resetInstructions = function() {
	jThumbDescription.html("Click a thumbnail image below to view the full-size version");
}

function feature_select(id) {
	//alert('selecting feature '+id);
	id = typeof(id) == "string" ? id.replace(/^\D*(\d+)$/, "$1") : id;
	//alert(id);
	
	if (galleryPlaying) {
		gallery_pause();
	}

	feature_change(id);
}

function feature_change(id) {
	// update the classes
	jThumbList[currentFeatureID].removeClass('cur');
	jThumbList[id].addClass('cur');
	jTextList[currentFeatureID].removeClass('cur');
	jTextList[id].addClass('cur');
	
	// switch the image
	
	var imageUrl = featureData.feature[id].image;
	if (imageUrl.indexOf("kizashi_underbody_poster_swf_view") == -1
			&& imageUrl.indexOf("gv_seat_anim") == -1
			&& imageUrl.indexOf("skid_plates_anim") == -1
			&& imageUrl.indexOf("EQ_seat_up_down") == -1) {
		jMainImage.attr('src', featureData.feature[id].image);
		jMainImage.attr('alt', featureData.feature[id].name);
		jMainImage.show();
		jFlash.hide();
	} else {
		jMainImage.hide();
		jFlash.show();
	}
	
	// update the right side
	jFeatureName.html(featureData.feature[id].name);
	jFeatureDescription.html(featureData.feature[id].description);
	jFeatureSku.html(featureData.feature[id].sku);
	jFeatureMsrp.html("$"+featureData.feature[id].msrp+"*");
	
	// store new featureID
	currentFeatureID = eval(id); // make sure it is a number

	if (galleryPlaying) {
		autoplayTimerID = setTimeout("play_gallery(true)", gallerySlideShowTime);
	}
}

function gallery_pause() {
	galleryPlaying = !galleryPlaying;
	clearTimeout(autoplayTimerID);
	
	var addClass = galleryPlaying ? "pause" : "play";
	var removeClass = galleryPlaying ? "play" : "pause";
	var setTitle = galleryPlaying ? "Click To Pause" : "Click To Play";
	$(".controls ."+removeClass).attr({'class':addClass, title:setTitle});
	$("#image-container ."+removeClass).attr({'class': addClass, title: setTitle});

	// Update cookie
	updatePreferences();
	
	if (galleryPlaying) {
		autoplayTimerID = setTimeout("play_gallery(true)", gallerySlideShowTime);
	}
}

function play_gallery(notFlashCaller) {
	// was flash calling or the js?
	if (notFlashCaller == true) {
		skipACall = false;
	}
	if (galleryPlaying && !skipACall) {
		if (currentFeatureID >= totalFeatures - 1) {
			location.href = nextGallery;
		} else {
			var next_id = currentFeatureID + 1;
			feature_change(next_id);
		}
	}
}

function updatePreferences() {
	$.cookie("UserPrefGallery", galleryMuted+";"+galleryPlaying+";"+galleryThumbs, {expires:365,path:"/"});
}


