var flashModels;
var changeModelTimer;
var changeTrimTimer;
var changeColorTimer;
var resetColorTimer;
var flashReady = false;
var flashModelsChangedColorAuxiliaryFunctions = new Array(); // see flashModelsChangedColor function for details

$(function(){
	init360();
	//setTimeout(flashModelsActivate, 2000);
});

function init360() {
	flashModels = getFlashMovieObject('flash360s');
	$('.model-360s-container').mouseout(function(){flashModels.onMouseOutsideStage();}); // tell flash when the mouse leaves the stage
}

function change360sModel(model) {
	if (flashReady) {
		flashModels.change_model(model);
		//alert('changed model:'+model);
	} else {
		clearTimeout(changeModelTimer);
		changeModelTimer = setTimeout("change360sModel('"+model+"')", 100);
	}
}

/* Initially I thought that this function should accept model and year variables,
 and it could calculate a path something like '_m/sr_' + year.substr(2,2) + '/' + model '/360s/static.jpg',
 but the problem with that is the background will not match if it gets used on a different tool. So the unfortunate
 reality is that these static images are tool specific, so put them in the tool directories, and just send the path
 to this function. */
function change360sStaticModel(path) {
	if (flashReady) {
		flashModels.change_model_static(path);
		//alert('changed static model:'+path);
	} else {
		clearTimeout(changeModelTimer);
		changeModelTimer = setTimeout("change360sStaticModel('"+path+"')", 100);
	}
}

function change360sTrim(trim) {
	if (flashReady) {
		flashModels.change_trim(trim);
	} else {
		clearTimeout(changeTrimTimer);
		changeTrimTimer = setTimeout("change360sTrim('"+trim+"')", 100);
	}
}

function change360sColor(colorCode) {
	if (flashReady) {
		flashModels.change_color(colorCode);
	} else {
		clearTimeout(changeColorTimer);
		changeColorTimer = setTimeout("change360sColor('"+colorCode+"')", 100);
	}
}

function reset360sColor() {
	if (flashReady) {
		flashModels.reset_color();
	} else {
		clearTimeout(resetColorTimer);
		resetColorTimer = setTimeout("reset360sColor()", 100);
	}
}

var flashModelsActivate = function () {
	flashReady = true;
	return true;
}

/* this is the function which flash will call when the user
	uses the internal color picker to change the model color. 
	it will go through the function array 
	flashModelsChangedColorAuxiliaryFunctions calling each 
	function.
	Example:
		var my_func_name = function(code){
			// stuff
		}
		flashModelsChangedColorAuxiliaryFunctions[flashModelsChangedColorAuxiliaryFunctions.length] = my_func_name;
	*/
var flashModelsChangedColor = function(code) {
	//alert('flash has changed the color to: '+code);
	for (var i in flashModelsChangedColorAuxiliaryFunctions) {
		flashModelsChangedColorAuxiliaryFunctions[i](code);
	}
}
/*
if (!$.browser.msie) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
	if ($.browser.msie) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	document.Show.MouseX.value = tempX;
	document.Show.MouseY.value = tempY;
	return true;
}*/
