// 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;

//var nextGallery // this is handled in _gallery.php now

var galleryFlashObject = null;

// 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');
	
	$('#view-by-thumb').click(function(){feature_view_by(this.id);});
	$('#view-by-text').click(function(){feature_view_by(this.id);});
	
	galleryFlashObject = getFlashMovieObject('galleryFlash');

	if (!galleryFlashObject && galleryMuted == false) {
		galleryMuted = true;
		$(".secondary-nav .mute").attr({className: "unmute", title: "Turn Sound On"});
	}

	t = setTimeout(function() {
			if (galleryPlaying) {
				if (!galleryMuted) {
					updateAudioVars(currentFeatureID);
					galleryFlashObject.play_audio(false);
				} else {
					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
	jMainImage.attr('src', featureData.feature[id].image);
	jMainImage.attr('alt', featureData.feature[id].name);
	
	// 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
	// update audio player data
	updateAudioVars(id);

	if (!galleryMuted) {
		galleryFlashObject.play_audio(false);
	} else {
		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({className:addClass, title:setTitle});
	$("#image-container ."+removeClass).attr({className: addClass, title: setTitle});

	// Update cookie
	updatePreferences();
	
	if (galleryPlaying) {
		if (!galleryMuted) {
			updateAudioVars(currentFeatureID);
			galleryFlashObject.play_audio(true);
		} else {
			autoplayTimerID = setTimeout("play_gallery(true)", gallerySlideShowTime);
		}
	}
}

function gallery_mute() {
	galleryMuted = !galleryMuted;
	
	if (!galleryMuted) {
		skipACall = false;
		clearTimeout(autoplayTimerID);
	}
	
	if(galleryFlashObject) {
		updateAudioVars(currentFeatureID);
		galleryFlashObject.mute_unmute(galleryMuted);
	
		var addClass = galleryMuted ? "unmute" : "mute";
		var removeClass = galleryMuted ? "mute" : "unmute";
		var setTitle = galleryMuted ? "Turn Sound On" : "Turn Sound Off";
		$(".controls ."+removeClass).attr({className: addClass, title: setTitle});
		$("#image-container ."+removeClass).attr({className: addClass, title: setTitle});

		// Update cookie
		updatePreferences();
		// skip a flash iteration of next pic?
		if (galleryPlaying && galleryMuted) {
			skipACall = true;
			autoplayTimerID = setTimeout("play_gallery(true)", gallerySlideShowTime);
		}
		
	} else {
		alert("You must have Flash enabled to listen to audio content.");
	}
}

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 updateAudioVars(id) {
	flashPassVar('audioYear', MODEL_YEAR, 'galleryFlash');
	flashPassVar('hasLong', featureData.feature[id].dual_audio, 'galleryFlash');
	flashPassVar('audio', featureData.feature[id].audio, 'galleryFlash');
}

function updatePreferences() {
	$.cookie("UserPrefGallery", galleryMuted+";"+galleryPlaying+";"+galleryThumbs, {expires:365,path:"/"});
}

