/* BEGIN CONFIGURATOR CLASS DEFINITION */
function ChromeConfigurator() {
	// New Object
	CurrentDivisionID = "";
	CurrentModelID = "";
	CurrentStyleID = "";
	CurrentStyle = {};
	/*  */
	this.containerArray = ['ConfigMakeHolder','ConfigModelHolder','ConfigStylesHolder','ConfigStyleHolder','ConfigLoader'];
	this.buttonArray = ['MakesButton','ModelsButton','StylesButton','StyleButton'];
	this.ProgressStep = 0;
};

ChromeConfigurator.prototype.getStyle = function(StyleID) {
	if (typeof StyleID!='undefined') {
		CurrentStyleID = StyleID;
	}
	
	/* Hide the buttons. We'll show them again later if we need them. */
	$('ProgressStep4_Review').hide();
	$('ProgressStep4_WheelsTV').hide();
	$('ProgressStep4_360').style.visibility = 'hidden';
	
	var self = this;
	
	/* AJAX call parameters */
	var url = 'index.cfm';
	var ts = new Date();
	var pars = '?FuseAction=Configure.getStyle&StyleID=' + CurrentStyleID + '&TS=' + ts.toString();
	/* Make AJAX Call */
	Spry.Utils.loadURL(
		'GET', url+pars, true, null,
		{
			successCallback: function(req) {
				try {
					CurrentStyle = eval('(' + req.xhRequest.responseText + ')');
				}
				catch (error) {
					alert("URL: " + url+pars + '\n' + req.xhRequest.responseText);
					return;
				}
				if (CurrentStyle.evoxmedia.exmov.length || CurrentStyle.evoxmedia.inmov.length) {
					$('ProgressStep4_360').style.visibility = '';
					if (CurrentStyle.evoxmedia.exmov.length) {
						$('extSpinBtn').style.visibility = '';
						setTimeout(showExtSpin,750);
					} else {
						$('extSpinBtn').style.visibility = 'hidden';
					}
					if (CurrentStyle.evoxmedia.inmov.length) {
						$('intSpinBtn').style.visibility = '';
					} else {
						$('intSpinBtn').style.visibility = 'hidden';
					}
				} else {
					$('ProgressStep4_360').style.visibility = 'hidden';
				}
				/* Show/Hide Specific buttons */
				if (CurrentStyle.review.modeloverview.length > 7) {
					$('ProgressStep4_Review').show();
				} else {
					$('ProgressStep4_Review').hide();
				}
				if (CurrentStyle.testdrive.length) {
					$('ProgressStep4_WheelsTV').show();
				} else {
					$('ProgressStep4_WheelsTV').hide();
				}
				/* Load Spry Datasets */
				dsStyle.setDataFromArray(CurrentStyle.stylesummary);
				dsStyleExtColors.setDataFromArray(CurrentStyle.stylecolors);
				dsStyleEvoxColors.setDataFromArray(CurrentStyle.evoxmedia.colorarray);
				/* We only need 8 photos for now */
				if (CurrentStyle.evoxmedia.photoarray.length > 8) {
					CurrentStyle.evoxmedia.photoarray.length = 8;
				}
				dsStyleEvoxPhotos.setDataFromArray(CurrentStyle.evoxmedia.photoarray);
				/* Reset Int Color Spry DataSet */
				dsStyleIntColors.setDataFromArray([]);
				/* Reset Selected Options Spry DataSet */
				dsSelectedOptions.setDataFromArray([]);
				$('MPG_City').innerHTML = CurrentStyle.mpg.city;
				$('MPG_Hwy').innerHTML = CurrentStyle.mpg.hwy;
				if (typeof setMPGs == 'function') {
					setTimeout(function(){
						setMPGs(CurrentStyle.mpgs);
					},250);
				}
				$('Warranty').innerHTML = CurrentStyle.warranty;
				if (CurrentStyle.evoxmedia.photoarray.length) {
					$('motioncontainer').style.display="";
					//setTimeout("fillup()",200);
				} else {
					$('motioncontainer').style.display="none";
				}
				if (typeof chromeConfig.switchContainer == 'function') {
					setTimeout(function(){self.switchContainer('ConfigStyleHolder')},700);
				} else {
					alert('Something has gone wrong!');
				}
			}
		}
	);
};
ChromeConfigurator.prototype.getStyleIntColors = function(Ext1Code) {
	/* AJAX call parameters */
	var url = 'index.cfm';
	var ts = new Date();
	var pars = 'FuseAction=Configure.getIntColors&StyleID=' + CurrentStyleID + '&Ext1Code=' + Ext1Code + '&TS=' + ts.toString();
	/* Make AJAX Call */
	Spry.Utils.loadURL(
		'GET', url+'?'+pars, true, null,
		{
			successCallback: function(req) {
				try {
					var IntColors = eval('(' + req.xhRequest.responseText + ')');
					dsStyleIntColors.setDataFromArray(IntColors);
				}
				catch (error) {
					alert(req.xhRequest.responseText);
				}	
			}
		}
	);
};
ChromeConfigurator.prototype.getStyles = function(ModelID) {
	if (typeof ModelID!='undefined') {
		CurrentModelID = ModelID;
	}
	/* AJAX call parameters */
	var url = 'index.cfm';
	var ts = new Date();
	var pars = 'FuseAction=Configure.getStyles&ModelID=' + CurrentModelID + '&TS=' + ts.toString();
	/* Make AJAX Call */
	Spry.Utils.loadURL(
		'GET', url+'?'+pars, true, null,
		{
			successCallback: function(req) {
				stylesArray = eval('(' + req.xhRequest.responseText + ')');
				/* Load Styles DataSet */
				dsStyles.setDataFromArray(stylesArray);
				/* Get Distinct List Of BodyStyles */
				var bodyArray = [];
				var bodyObj = new Object();
				for (var i=0; i<stylesArray.length; i++) {
					bodyObj[stylesArray[i].primarybodystyle] = 'distinct';
				}
				for (var key in bodyObj) {
					bodyArray[bodyArray.length] = {primarybodystyle:key};
				}
				/* Load And Sort BodyStyle DataSet */
				dsBodyStyles.setDataFromArray(bodyArray);
				dsBodyStyles.sort('primarybodystyle', 'ascending');
				/* Get Filters Ready */
				if(typeof resetStylesFilter == 'function') {
					setTimeout(resetStylesFilter,20);
				}
				if (typeof chromeConfig.switchContainer == 'function') {
					setTimeout(function(){chromeConfig.switchContainer('ConfigStylesHolder')},500);
				} else {
					alert('Something has gone wrong!');
				}
			}
		}
	);
};
ChromeConfigurator.prototype.goBack = function() {
	var _step = Math.min(this.ProgressStep,4);
	if (_step > 1) {
		// console.log('this.switchContainer('+ this.containerArray[_step-2] + ')');
		this.switchContainer(this.containerArray[_step-2]);
	}
};
/* DISPLAY PROGRESS USING A CLASS FOR THE BUTTONS */
ChromeConfigurator.prototype.setProgress = function(step) {
	var _step = Math.min(step,4);
	/* Turn Off Class For All Buttons */
	for (var i=0; i<this.buttonArray.length; i++) {
		Spry.Utils.removeClassName(this.buttonArray[i],'ToStep');
		Spry.Utils.removeClassName(this.buttonArray[i],'DoneStep');
		$('ProgressStep' + (i+1)).hide();
	}
	for (var i=1; i<_step; i++) {
		Spry.Utils.addClassName(this.buttonArray[i-1],'DoneStep');
	}
	Spry.Utils.addClassName(this.buttonArray[_step-1],'ToStep');
	$('ProgressStep' + _step).show();
	this.ProgressStep = step;
};
ChromeConfigurator.prototype.switchContainer = function(container) {
	var showContainer = function() {
		$(container).style.display='';
		var cFade = new Spry.Effect.Fade(container,{duration:300, from:0, to:100});
		cFade.start();
	};
	/* Always Hide Floating Panels */
	var panels = {
		'options':$('ConfigOptionsHolder')
		,'features':$('ConfigFeaturesHolder')
	};
	for (var i in panels) {
		panels[i].style.display = 'none';
		panels[i].style.filter = 'alpha(opacity=0)';	// IE
		panels[i].style.opacity = .00; 				// FF
	}
	/* Turn Class Back On For The Right Button */
	switch (container) {
		case 'ConfigLoader':
			// this.setProgress('1');
			break;
		case 'ConfigMakeHolder':
			this.setProgress('1');
			break;
		case 'ConfigModelHolder':
			this.setProgress('2');
			break;
		case 'ConfigStylesHolder':
			this.setProgress('3');
			break;
		case 'ConfigStyleHolder':
			var _step = this.ProgressStep;
			this.setProgress('4');
			$('OptionFeatureBar').removeClassName('Step2');
			$('OptionFeatureBar').removeClassName('Step3');
			$('OptionFeatureBar').addClassName('Step1');
			if (_step >= 4) {
				/* 
				 Step 4 is the Detail. 
				 If we are on it or past, no need to do the fade thing below.
				 */
				return;
			}
			break;
		case 'ConfigOptionsHolder':
			/* Show Options Panel */
			this.setProgress('5');
			$('OptionFeatureBar').removeClassName('Step1');
			$('OptionFeatureBar').removeClassName('Step3');
			$('OptionFeatureBar').addClassName('Step2');
			showContainer();
			return; break;
		case 'ConfigFeaturesHolder':
			/* Show Features Panel */
			this.setProgress('6');
			$('OptionFeatureBar').removeClassName('Step1');
			$('OptionFeatureBar').removeClassName('Step2');
			$('OptionFeatureBar').addClassName('Step3');
			showContainer();
			return; break;
	}
	for (var i=0; i<this.containerArray.length; i++) {
		thisContainer = this.containerArray[i];
		if (typeof($(thisContainer)) == 'object' && $(thisContainer).style.display != 'none') {
			var thisFade = new Spry.Effect.Fade(thisContainer,{duration:300, from:100, to:0, finish:function(){$(thisContainer).style.display='none';showContainer();}});
			thisFade.start();
			return;
		}
	}
};
ChromeConfigurator.prototype.showMakes = function() {
	this.switchContainer('ConfigMakeHolder');
};
ChromeConfigurator.prototype.showModels = function(DivisionID) {
	if (typeof DivisionID!='undefined') {
		CurrentDivisionID = DivisionID;
	}
	if (!CurrentDivisionID.length) {
		alert('Please Choose A Brand!');
		return;
	}
	dsMakeModel.filter(function(dataSet, row, rowNumber) {
		if (row["divisionid"] == CurrentDivisionID)
			return row;		// Return the row to keep it in the data set.
		return null;		// Return null to remove the row from the data set.
	});
	this.switchContainer('ConfigModelHolder');
};

ChromeConfigurator.prototype.showFeatures = function() {
	if (!CurrentStyleID.length) {
		alert('Please Choose A Model And Style!');
		return;
	}
	var TS = new Date();
	var url = 'index.cfm?FuseAction=Configure.getStandardFeatures&StyleID=' + CurrentStyleID + '&TS=' + TS.toString();
	if(!CurrentStyle.FeaturesLoaded) {
		Spry.Utils.updateContent('FeaturesDiv', url, function(){CurrentStyle.FeaturesLoaded=true});	
	}
	this.switchContainer('ConfigFeaturesHolder');
};

ChromeConfigurator.prototype.showOptions = function() {
	if (!CurrentStyleID.length) {
		alert('Please Choose A Model And Style!');
		return;
	}
	var TS = new Date();
	var url = 'index.cfm?FuseAction=Configure.getOptions&StyleID=' + CurrentStyleID + '&TS=' + TS.toString();
	if(!CurrentStyle.OptionsLoaded) {
		Spry.Utils.updateContent('OptionsDiv', url, function(){CurrentStyle.OptionsLoaded=true});	
	}
	this.switchContainer('ConfigOptionsHolder');
};

ChromeConfigurator.prototype.showStyle = function(ModelID,StyleID) {
	if (typeof ModelID!='undefined') {
		CurrentModelID = ModelID;
	}
	if (typeof StyleID!='undefined') {
		CurrentStyleID = StyleID;
	}
	if (!CurrentStyleID.length) {
		alert('Please Choose A Model And Style!');
		return;
	}
	/* Only Change Style Info When New StyleID Passed In */
	if (typeof StyleID!='undefined') {
		this.switchContainer('ConfigLoader');
		this.getStyle(CurrentStyleID);
	} else {
		this.switchContainer('ConfigStyleHolder');
	}
};

ChromeConfigurator.prototype.showStyles = function(ModelID) {
	if (typeof ModelID!='undefined') {
		CurrentModelID = ModelID;
	}
	if (!CurrentModelID.length) {
		alert('Please Choose A Model!');
		return;
	}
	this.switchContainer('ConfigLoader');
	this.getStyles(CurrentModelID);
	// this.switchContainer('ConfigStylesHolder');
};

ChromeConfigurator.prototype.getChosenOptionIDs = function() {
	var optArray = [];
	if (typeof(CurrentStyle.ChosenOptions) == 'object') {
		for (var i in CurrentStyle.ChosenOptions) {
			optArray.push(i);
		}
	}
	return optArray;
}

ChromeConfigurator.prototype.toggleOption = function(_option,checkbox) {
	/* Start Collection If Neccesary */
	if (typeof(CurrentStyle.ChosenOptions) == 'undefined') {
		CurrentStyle.ChosenOptions = {};
	}
	/* Add Or Remove From Collection */
	if (typeof(CurrentStyle.ChosenOptions[_option.OptionCode]) == 'undefined') {
		CurrentStyle.ChosenOptions[_option.OptionCode] = _option;
		CurrentStyle.ChosenOptions[_option.OptionCode].checkbox = checkbox;
	} else {
		CurrentStyle.ChosenOptions[_option.OptionCode].checkbox.checked = false;
		delete CurrentStyle.ChosenOptions[_option.OptionCode];
	}
	/* Calculate Invoice And MSRP */
	var currentStyle = dsStyle.getCurrentRow();
	var _MSRP = (currentStyle.basemsrp + currentStyle.destination);
	var _Invoice = (currentStyle.invoice + currentStyle.destination);
	var _OptMSRP = 0;
	var _OptInvoice = 0;
	var OptArray = [];
	for (var i in CurrentStyle.ChosenOptions) {
		OptArray[OptArray.length] = CurrentStyle.ChosenOptions[i];
		_OptMSRP = _OptMSRP + CurrentStyle.ChosenOptions[i].MSRP;
		_OptInvoice = _OptInvoice + CurrentStyle.ChosenOptions[i].Invoice;
	}
	/* Update Selected Options Spry DataSet */
	dsSelectedOptions.setDataFromArray(OptArray);
	/* Update Totals On Screen */
	if ($('OptionsMSRP')) {
		$('OptionsMSRP').innerHTML = '$' + _OptMSRP.toFixed(0);
	}
	if ($('OptionsInvoice')) {
		$('OptionsInvoice').innerHTML = '$' + _OptInvoice.toFixed(0);
	}
	if ($('MSRPTotal')) {
		$('MSRPTotal').innerHTML = '$' + (_MSRP+_OptMSRP).toFixed(0);
	}
	if ($('InvoiceTotal')) {
		$('InvoiceTotal').innerHTML = '$' + (_Invoice+_OptInvoice).toFixed(0);
	}
	//alert('_MSRP='+_MSRP.toFixed(2)+'\n_Invoice='+_Invoice.toFixed(2));
};
/* END CONFIGURATOR CLASS DEFINITION */