/**
 * This script belongs in 'Select Interior & Exterior' page of
 * the Vehicle Configurator.
 * This uses the methods in 'config_snapshot.js' to update the snapshot.
 */

var ALERT_ICON = "images/global/icon_glo_alert.gif";
var SPACER_ICON = "images/global/spacer.gif";

/**
 * Data that drives the selection process.
 * This will be **dynamically populated** by the JSP.
 * And preloaded images....
 */
var extColors=new Array();
var intColors=new Array();
var validCombinations=new Array();
var createdOriginalSnapshot = false;
var loadIntDefaultFlag=true;
willShow= new Array(0);
/*
 * Changes images from one another.
 */
function chngImg() {
	//alert("image change " + chngImg.arguments[0]);
	document[chngImg.arguments[0]].src = chngImg.arguments[1];
}

/**
 * Constructor for Color object.
 * Represents both Interiors and Exteriors.
 */

function intClothObj(idCode, contents, showthis)
{
	this.idCode=idCode;
	this.contents=contents;
	this.showthis=showthis;
}

function intLeatherObj(idCode, contents, showthis)
{
	this.idCode=idCode;
	this.contents=contents;
	this.showthis=showthis;
}
function Color( fieldName, idCode, description, msrp,
				detailImg, gloryImg, yourImg, swatchImg, notAvailableSwatchImg, additionalLabel,
				link360, isAvailable, whyNotAvailable ) {

	this.fieldName = fieldName;
	this.idCode = idCode;
	this.description = description;
	this.msrp = msrp;
	this.detailImg = detailImg;
	this.gloryImg = gloryImg;
	this.yourImg = yourImg;
	this.swatchImg = swatchImg;
	this.notAvailableSwatchImg = notAvailableSwatchImg;
	this.additionalLabel = additionalLabel ;
	this.link360 = link360;
	this.isAvailable = isAvailable;
	this.whyNotAvailable = whyNotAvailable;
}


/**
 * Constructor for ColorCombo object.
 * Represents an Exterior Interior combination.
 */
function ColorCombo( extIdCode, intIdCode ) {
	this.extIdCode = extIdCode;
	this.intIdCode = intIdCode;
}

/**
 * This method should be called when exterior color selection is changed.
 */
var clickedExtID=0;

function clickExterior( extCode, preventView ) {
	if(clickedExtID!=0)
	{
		var oldIdx=getExteriorIndex(clickedExtID);
		chngImg(clickedExtID, extColors[oldIdx].swatchImg);
		$('#ext' + oldIdx).removeClass('swatch_on');
	}
	
	var indx=getExteriorIndex(extCode);
	var onImg = extColors[indx].swatchImg; 
	onImg=onImg.substring(0, (onImg.length-4)) + "_on" + onImg.substring((onImg.length-4), onImg.length);
	//alert(extCode);
	//alert(onImg);
	chngImg(extCode, onImg);
	$('#ext' + indx).addClass('swatch_on');
	
	clickedExtID=extCode;
	var intCode = document.configForm.interiorCode.value;
	//alert(intCode);
	var validFlag = false;

	for( var nI = 0; nI < validCombinations.length; nI++) {
		if ( validCombinations[nI].extIdCode == extCode
				&& validCombinations[nI].intIdCode == intCode ) {
			validFlag = true;
			break;
		}
	}
	//alert(validFlag);
	document.configForm.exteriorCode.value = extCode;
	if ( validFlag == false ) {
		//
		// Switch to the first valid interior with this exterior
		//
		for( var nI = 0; nI < validCombinations.length; nI++) {
			if ( validCombinations[nI].extIdCode == extCode ) {
				document.configForm.interiorCode.value = validCombinations[nI].intIdCode;
				break;
			}
		}
	}
	//alert(document.configForm.interiorCode.value);
	refreshColors();
	clickInterior(document.configForm.interiorCode.value, true);

	if(!preventView) {
		$.musa_config.viewExterior()
	}
}

var clickedIntID=0;

function clickInterior( intCode, preventView ) {

	if(clickedIntID!=0)
	{
		var oldIdx=getInteriorIndex(clickedIntID);
		$('#int' + oldIdx).removeClass('swatch_on');
		var clothfound=false;
		var leatherfound=false;
	
	
		for(ind in intClothArray)
		{
			if (intClothArray[ind].idCode==clickedIntID && intClothArray[ind].showthis=="show") clothfound=true;
		}
		for(ind in intLeatherArray)
		{
			if (intLeatherArray[ind].idCode==clickedIntID && intLeatherArray[ind].showthis=="show") leatherfound=true;
		}
		
		if (clothfound||leatherfound) chngImg(clickedIntID, intColors[oldIdx].swatchImg);
		
	}
	
	var indx=getInteriorIndex(intCode);
	var onImg = intColors[indx].swatchImg; 
	onImg=onImg.substring(0, (onImg.length-4)) + "_on" + onImg.substring((onImg.length-4), onImg.length);
	
	$('#int' + indx).addClass('swatch_on');
	//alert(onImg);
	//alert(intCode);
	chngImg(intCode, onImg);
	clickedIntID=intCode;
	
	var extCode = document.configForm.exteriorCode.value;
	//alert(document.configForm.exteriorCode.value);
	var validFlag = false;

	for( var nI = 0; nI < validCombinations.length; nI++) {
		if ( validCombinations[nI].extIdCode == extCode
				&& validCombinations[nI].intIdCode == intCode ) {
			validFlag = true;
			break;
		}
	}

	document.configForm.interiorCode.value = intCode;
	if ( validFlag == false ) {
		//
		// Switch to the first valid exterior with this interior
		//
		for( var nI = 0; nI < validCombinations.length; nI++) {
			if ( validCombinations[nI].intIdCode == intCode ) {
				document.configForm.exteriorCode.value = validCombinations[nI].extIdCode;
				break;
			}
		}
	}
	refreshColors('int');
	
	if(!preventView) {
		$.musa_config.viewInterior();
	}
}

function refreshColors(intflag) {
	//alert("refreshing colors");
	var extCode = document.configForm.exteriorCode.value;
	var intCode = document.configForm.interiorCode.value;
	//alert("---" + extCode + ":" + intCode);
	var extIdx = getExteriorIndex( extCode );
	var intIdx = getInteriorIndex( intCode );
	var extDefault=extIdx;
	var intDefault=intIdx;
	chngImg('interiorglory', intColors[intIdx].gloryImg);
	chngImg('exteriorglory', extColors[extIdx].gloryImg);
	
	//chngImg('interiordetail', intColors[intIdx].detailImg);
	//chngImg('yourint', intColors[intIdx].yourImg);
	//chngImg('yourext', extColors[extIdx].yourImg);
	//onDisplayItemChange( INTERIOR_LABEL, intColors[intIdx].description );
	//onDisplayItemChange( EXTERIOR_LABEL, extColors[extIdx].description );
	

	if ( createdOriginalSnapshot == false ) {
		createOriginalSnapshot( extCode + intCode );
		createdOriginalSnapshot = true;
	}

	//onDisplayItemChange( PACKAGES_LABEL, '' );
	//onDisplayItemChange( OPTIONS_LABEL, '' );

	//
	// reflect the availability of colors with current interior exterior selections.
	//
	var	extArray = new Array();
	var extCount = 0;
	var	intArray = new Array();
	var intCount = 0;
	for ( var nI = 0; nI < validCombinations.length; nI++ ) {
		extIdx = getExteriorIndex( validCombinations[ nI ].extIdCode );
		intIdx = getInteriorIndex( validCombinations[ nI ].intIdCode );

		if ( validCombinations[ nI ].extIdCode == extCode ) 
		{		
			if(!intflag)
			{ 
				for (ind in intClothArray)
				{
					if (intClothArray[ind].idCode==validCombinations[ nI ].intIdCode)
					{
						intClothArray[ind].showthis="show";
					}
				}
				
				for (ind in intLeatherArray)
				{
					if (intLeatherArray[ind].idCode==validCombinations[ nI ].intIdCode)
					{
						intLeatherArray[ind].showthis="show";
					}
				}
							
			}
			intArray[intCount] = validCombinations[ nI ].intIdCode;
			intCount++;
		}
		if ( validCombinations[ nI ].intIdCode == intCode ) {
			//chngImg( validCombinations[ nI ].extIdCode, extColors[ extIdx ].swatchImg );
			//ABOVE ITEM REMOVED FOR 1-WAY DEPENDENCY
			extArray[extCount] = validCombinations[ nI ].extIdCode;
			extCount++;
		}
	}
	for ( var nIx = 0; nIx < extColors.length; nIx++ ) {
		var xfound = false;
		for ( var nJx = 0; nJx < extArray.length; nJx++ ) {
			if ( extColors[nIx].idCode == extArray[nJx] ) {
				xfound = true;
				break;
			}
		}
		if ( xfound == false ) {
			//chngImg( extColors[ nIx ].idCode, extColors[ nIx ].notAvailableSwatchImg );
			//ABOVE ITEM REMOVED FOR 1-WAY DEPENDENCY
		}
	}
	if(!intflag)
	{		
		htmlwrite="";
		for (ind in intClothArray)
		{	
			var tfound = false;
					
			for ( var nJt = 0; nJt < intArray.length; nJt++ ) 
			{
				if ( ind == intArray[nJt] ) 
				{
					tfound = true;
					break;
				}
			}
			if ( tfound == false ) 
			{
				intClothArray[ind].showthis="hide";
			}
			if(intClothArray[ind].showthis=="show") htmlwrite+=intClothArray[ind].contents;
		}				
		//alert("re-writing cloth");
		reWriteCloth(htmlwrite);
		
		htmlwrite="";
		for (ind in intLeatherArray)
		{	
			var tfound = false;
					
			for ( var nJt = 0; nJt < intArray.length; nJt++ ) 
			{
				if ( ind == intArray[nJt] ) 
				{
					tfound = true;
					break;
				}
			}
			if ( tfound == false ) 
			{
				intLeatherArray[ind].showthis="hide";
			}
			if(intLeatherArray[ind].showthis=="show") htmlwrite+=intLeatherArray[ind].contents;
		}				
		reWriteLeather(htmlwrite);
	}
	var clothfound=false;
	var leatherfound=false;
	
	if (clickedIntID != 0)
	{
		var indx=getInteriorIndex(clickedIntID);
		var onImg = intColors[indx].swatchImg;
		onImg=onImg.substring(0, (onImg.length-4)) + "_on" + onImg.substring((onImg.length-4), onImg.length);	
		
		for(ind in intClothArray)
		{
			if (intClothArray[ind].idCode==clickedIntID && intClothArray[ind].showthis=="show") clothfound=true;
		}
		for(ind in intLeatherArray)
		{
			if (intLeatherArray[ind].idCode==clickedIntID && intLeatherArray[ind].showthis=="show") leatherfound=true;
		}
		
		if (clothfound||leatherfound) chngImg(clickedIntID, onImg);
	}
	//
	// If the user goes back to his original selection
	// Restore the original snapshot.
	//
	if ( createdOriginalSnapshot == true ) {
		restoreOriginalSnapshot( extCode + intCode );
	}
	//onDisplayItemChange( 'msrp', extColors[extIdx].msrp );

var onExtImg = extColors[extDefault].swatchImg; 
	onExtImg=onExtImg.substring(0, (onExtImg.length-4)) + "_on" + onExtImg.substring((onExtImg.length-4), onExtImg.length);
	
	//alert(extCode);
	$('#ext' + extDefault).addClass('swatch_on');
	
	chngImg(extCode, onExtImg);
	showExtColorLabel (extCode);
	clickedExtID=extCode;
	
	if(loadIntDefaultFlag==true)
	{
		
		var onIntImg = intColors[intDefault].swatchImg; 
		onIntImg=onIntImg.substring(0, (onIntImg.length-4)) + "_on" + onIntImg.substring((onIntImg.length-4), onIntImg.length);
		$('#int' + intDefault).addClass('swatch_on');
		
		chngImg(intCode, onIntImg);
		showIntColorLabel (intCode);
		clickedIntID=intCode;
		loadIntDefaultFlag=false;
	}
	
	$.musa_config.writeInteriorLabels();
	$("#intColorText").html($("div.int_swatch div.swatch_on").html());
	//alert("end");
	
	//alert('exterior code after refresh: ' + document.configForm.exteriorCode.value);
	//alert('interior code after refresh: ' + document.configForm.interiorCode.value);
}

function showExtColorLabel (code) {
	var num = getExteriorIndex(code);
	colortxt = new makeLayer('extColorText');
	if (extColors[num].isAvailable==true) {
		if( extColors[num].additionalLabel != null)
		{
			colortxt.el.innerHTML = extColors[num].description + extColors[num].additionalLabel;
		} else {
			colortxt.el.innerHTML = extColors[num].description;
		}
	} else {
		msgtxt=new makeLayer('extMessageText');
		colortxt.el.innerHTML = extColors[num].description + ':';
		msgtxt.el.innerHTML = ' ' + extColors[num].whyNotAvailable;
		//chngImg('extAlertIcon', ALERT_ICON);
		//ABOVE ITEM REMOVED FOR 1-WAY SCHEME
	}
}

function showIntClothColorLabel(code) {
	//alert('writing to cloth color table with ' + code);
	var num = getInteriorIndex(code);
	colortxt = new makeLayer('intClothColorText');
	if (intColors[num].isAvailable==true) {
	
		//if(willShow[intColors[ num ].idCode] !=0)
		//{
			if( intColors[num].additionalLabel != null)
			{
				colortxt.el.innerHTML = intColors[num].description + intColors[num].additionalLabel;
			} else {
				colortxt.el.innerHTML = intColors[num].description;
			}
		//}
		
	} else {
		//msgtxt=new makeLayer('intClothMessageText');
		//colortxt.el.innerHTML = intColors[num].description + ':';
		//msgtxt.el.innerHTML = ' ' + intColors[num].whyNotAvailable;
		//chngImg('intClothAlertIcon', ALERT_ICON);
		//chngImg('intClothAlertIcon', "images/global/spacer.gif");
	}
}


function showIntColorLabel(code) {
	//alert('writing to color table with ' + code);
	var num = getInteriorIndex(code);
	colortxt = new makeLayer('intColorText');
	if (intColors[num].isAvailable==true) {
	
		//if(willShow[intColors[ num ].idCode] !=0)
		//{
			if( intColors[num].additionalLabel != null)
			{
				colortxt.el.innerHTML = intColors[num].description + intColors[num].additionalLabel;
			} else {
				colortxt.el.innerHTML = intColors[num].description;
			}
		//}
		
	} else {
		//msgtxt=new makeLayer('intClothMessageText');
		//colortxt.el.innerHTML = intColors[num].description + ':';
		//msgtxt.el.innerHTML = ' ' + intColors[num].whyNotAvailable;
		//chngImg('intClothAlertIcon', ALERT_ICON);
		//chngImg('intClothAlertIcon', "images/global/spacer.gif");
	}
}



function showLeatherMsg() {
   
    leathertxt = new makeLayer('leathermsg');
    leathertxt.el.innerHTML = 'The Leather Seat Surfaces are part of the loaded Grand Touring Package';
}

function hideLeatherMsg() {
    leathertxt = new makeLayer('leathermsg');
    leathertxt.el.innerHTML = '&nbsp;';
}


function showIntLeatherColorLabel(code) {
	var num = getInteriorIndex(code);
	colortxt = new makeLayer('intColorText');
	if (intColors[num].isAvailable==true) {
	
		if(willShow[intColors[ num ].idCode] !=0)
		{
			if( intColors[num].additionalLabel != null)
			{
				colortxt.el.innerHTML = intColors[num].description + intColors[num].additionalLabel;
			} else {
				colortxt.el.innerHTML = intColors[num].description;
			}
		}
	} else {
		msgtxt=new makeLayer('intLeatherMessageText');
		//colortxt.el.innerHTML = intColors[num].description + ':';
		//msgtxt.el.innerHTML = '&nbsp;' + intColors[num].whyNotAvailable;
		//chngImg('intLeatherAlertIcon', ALERT_ICON);
		chngImg('intLeatherAlertIcon', "images/global/spacer.gif");
	}
}

function eraseExtColorLabel(code) {
	colortxt = new makeLayer('extColorText');
	colortxt.el.innerHTML = "";
	msttxt = new makeLayer('extMessageText');
	msttxt.el.innerHTML = "";
	chngImg('extAlertIcon', SPACER_ICON);
}

function eraseIntClothColorLabel(code) {
	colortxt = new makeLayer('intClothColorText');
	colortxt.el.innerHTML = "";
	msttxt = new makeLayer('intClothMessageText');
	msttxt.el.innerHTML = "";
	chngImg('intClothAlertIcon', SPACER_ICON);
}

function eraseIntLeatherColorLabel(code) {
	colortxt = new makeLayer('intLeatherColorText');
	colortxt.el.innerHTML = "";
	msttxt = new makeLayer('intLeatherMessageText');
	msttxt.el.innerHTML = "";
	chngImg('intLeatherAlertIcon', SPACER_ICON);
}

function getExteriorIndex( code ) {
	if ( extColors != null ) {
		for ( var nI = 0; nI < extColors.length; nI++ ) {
			if ( extColors[nI].idCode == code ) {
				return nI;
			}
		}
	}
	return -1;
}

function getInteriorIndex( code ) {
	if ( intColors != null ) {
		for ( var nI = 0; nI < intColors.length; nI++ ) {
			if ( intColors[ nI ].idCode == code ) {
				return nI;
			}
		}
	}
	return -1;
}

/*** Added by Noelle - Begin 299 - scr04510 - OLD ***/
/*** scr04646 - hard coded special $289 lease ***/
/*** scr04765 - REMOVED hard coded special $289 lease ***/
function refreshSShot()
{
	//alert("in refreshSS");
	//alert("MODEL CODE " ); 
	//alert("modelcode" + document.getElementById("hiddenModelCode").value); 
	//alert("msrp " + document.getElementById("hiddenUnformattedmsrp").value);
	
	updateEstimatedLease(document.getElementById("hiddenModelCode").value, document.getElementById("hiddenUnformattedmsrp").value); 
}

function updateEstimatedLease( modelCode, msrp ) {
	//alert("CALLING ON TRIM SELECTED from COLORS PAGE");
	
	//scr04646 - hard coded special $289 lease
	//scr04765 - commented out
	//if(modelCode =='CX7  SP 2A  ' && (msrp == 24310 || msrp == 24345)) 
	//{
	//	tempLeaseAmt = 289 ;
	//	if(document.getElementById(LEASE_LABEL)) 
	//	{
	//	   document.getElementById(LEASE_LABEL).innerHTML = 
	//	   (tempLeaseAmt > 0 && isFinite(tempLeaseAmt))?"$"+commaNum(tempLeaseAmt)+"/mo":"Calculate";
//
	//	}					
			
	//   if (document.getElementById("cx7_disclaimer")) document.getElementById("cx7_disclaimer").style.display = "block";
	//} else 
	if (document.getElementById("cx7_disclaimer"))
		document.getElementById("cx7_disclaimer").style.display = "none";


}
/*** Added by Noelle - End  289 ***/

/**
*** The above code is static and does not change per request basis.
*** It will be maintained in 'config_colors.js' file and will be included in
*** the generated HTML.
*** The data below will be generated by the JSP on a per request basis.
*** The data will be populated in each HTML generated for 'Select Interior & Exterior' page
**/

