/**
	Carbuilder JS
	ben@dealer.com
**/


var cookies = new Cookies( );


function overTrim(obj, imgUrl){
	
	//change the class to highlight trim object	
	obj.className='trimOver';
	
	//change the image source to reflect that trim
	document.trimImage.src = imgUrl;

}

  	var aiOne = new Image();
  	    aiOne.src = "/carbuilder/lite/images/actionIcon_media_over.jpg";
	var aiTwo = new Image();
  	    aiTwo.src = "/carbuilder/lite/images/actionIcon_quote_over.jpg";	   	
	var aiThree = new Image();
  	     aiThree.src = "/carbuilder/lite/images/actionIcon_mail_over.jpg";
	var aiFour = new Image();
  	     aiFour.src = "/carbuilder/lite/images/actionIcon_info_over.jpg";	   	
	var aiFive = new Image();
		 aiFive.src = "/carbuilder/lite/images/actionItem_offer_over.jpg";

  function overActionItem(which){
     
  		document.getElementById("aImg_" + which).src = eval("h" + which + ".src");
  		document.getElementById("aItem_" + which).className = 'actionItemh';
		
  }

 function offActionItem(which){
     
  		document.getElementById("aImg_" + which).src = eval("o" + which + ".src");
  		document.getElementById("aItem_" + which).className = 'actionItem';
		
  }

	
/* TABS */

var focusedTab = null;
var focusedLeft = null;

function selectTab( whichTab ){
	
	if(focusedTab!=null){
		focusedTab.className='navTab';
		focusedLeft.className = 'navTabLeft';
	}else{
		document.getElementById("opts_tab_left").className='navTabLeft';
		document.getElementById("opts_tab").className='navTab';
	}
	
	newTab =  document.getElementById(whichTab + "_tab");
	newLeft = document.getElementById(whichTab + "_tab_left");
	
	
	newTab.className = 'navTabs';
	newLeft.className = 'navTabLefts';
	
	focusedTab = newTab;
	focusedLeft = newLeft;
	
	if(whichTab=='opts'){
		//.. then options need to be unhidden
		document.getElementById("infoLoader").style.display = 'none';
		document.getElementById("optionsContainer").style.display = 'block';
		try {
			top.adjustFrameHeight(getWindowHeight());	 
		} catch( e ) {
			parent.adjustFrameHeight(getWindowHeight());	 
		}
	}
}


function writeToPage( str ){
	//hide options
	document.getElementById("optionsContainer").style.display = 'none';
	
	//write to info
	var infoDiv = document.getElementById("infoLoader");
	infoDiv.innerHTML = str;
	infoDiv.style.display = 'block';
	var height = infoDiv.offsetHeight;
	
	//adjust frame height
	try {
		if(top.adjustFrameHeight) top.adjustFrameHeight(height+300);
	} catch( e ) {
		if(top.adjustFrameHeight) parent.adjustFrameHeight(height+300);
	}
} 


var interiorColor = null;
var exteriorColor = null;
var interiorRadio = null;
var iColorName = "";
var eColorName = "";

function selectColor(obj,which,id,name,evt){
	
	storedClientY = evt.clientY;
	
	if(which=='i'){
		if(interiorColor!=null){
			interiorColor.className='colorSwatch';	
			//interiorRadio.checked = false;
		}
		interiorColor=obj;
		iColorName = name;
		cookies.setCookie("CarbuilderIntColor", id);
	}else{
		if(exteriorColor!=null) exteriorColor.className='colorSwatch';			
		exteriorColor=obj;
		eColorName = name;
		cookies.setCookie("CarbuilderExtColor", id);
	}		
		obj.className='selectedSwatch';
	
	//alert(id);
	//select form field for logic.. 
	var  radioEl = document.getElementById(id);
		  radioEl.click();
	
	doLogic(radioEl,'optionsForm');
}

function overColor( colorName, which, colorPhoto ){	
	if(which=='e'){
		document.getElementById("ext_color").innerHTML = "<span class='barText'>Exterior Colors ("+ colorName +")</span>";
		//TODO: color photo
	}else{
		document.getElementById("int_color").innerHTML = "<span class='barText'>Interior Colors ("+ colorName +")</span>";
	}
}


function offColor(which){
	if(which=='e'){
		if(exteriorColor != null)
			document.getElementById("ext_color").innerHTML = "<span class='barText'>Exterior Colors ("+ eColorName +")</span>";
		else
			document.getElementById("ext_color").innerHTML = "<span class='barText'>Exterior Colors</span>";
		//TODO: color photo
	}else{
		if(interiorColor != null)
			document.getElementById("int_color").innerHTML = "<span class='barText'>Interior Colors ("+ iColorName +")</span>";
		else
			document.getElementById("int_color").innerHTML = "<span class='barText'>Interior Colors</span>";
			
	}

}


var selectedPackage = null;
var selectedPkgPrice = 0;
var optionsTotal = 0;

function doFade(formEl){
  
	if(formEl.checked)
		Fat.fade_element("opt_"+formEl.id, 24, 2000, "#FFFFFF", "#FFFBC2");
	else 
		Fat.fade_element("opt_"+formEl.id, 24, 2000, "#FFFBC2", "#FFFFFF" );
}

function optionLayerClick(id,evt){

	storedClientY = evt.clientY;
	
	//do that which must be done
	formObj = document.getElementById(id);
	
	if(formObj.checked)
		formObj.checked = false;
	else
		formObj.checked = true;
		
	doLogic(formObj,'optionsForm'); 
	doFade(formObj);
	
}

function unClickBox(formObj){
	if(formObj.checked)
		formObj.checked = false;
	else
		formObj.checked = true;
}
function updatePrices(){
	
	
	oPrice = document.getElementById("options_price");
	tPrice = document.getElementById("total_price");
	newTotal = optionsTotal + totalPrice;
	
	
	oPrice.innerHTML = "<b>Options Price:</b> " + dollarFormat(optionsTotal);
	tPrice.innerHTML = "<b>Total Price:</b> " + dollarFormat(newTotal);

	document.paymentcalcForm.price.value = newTotal;	
	populate(calculate());
	
}


function dollarFormat(num){
		
		num = num.toString()
		if(num.indexOf("$") == 0)
			num = num.substring(1, num.length);
		
		var num = parseInt(num).toString();
		
		var end = num.substring(num.length-3, num.length);
		var neg = false;
		if(num.charAt(0) == '-'){
			num = num.substring(1,num.length);
			neg = true
		}
		
		if(num.length > 3)
			ret =  num.substring(0, num.length-3) + "," + end;
		else
			ret = num;
		
		return  (neg) ? "-$" + ret : "$" + ret; 
	
}


function wrt(str){
	document.write(str);
}




var storedClientY = 0;

function openDialog(dialogObj, param, violation){
    var el = document.getElementById(dialogObj.id);
	if(!el) { //... ie sucks
		var el = document.getElementById(dialogObj.name); 
	}
		
   el.style.position = 'absolute';
    if(!violation){
	    
	    if(param){
		    var curUrl = el.src.toString();
			
			if(curUrl.indexOf("?") > 0)
			    curUrl = curUrl.substring(0, curUrl.indexOf("?"));

				dialogObj.getDoc().body.innerHTML = "<p style='padding:15px;font-weight:bold;'>Loading form, one moment please...</p>";
				el.src = curUrl + "?form=" + param;
			}
			
			if( document.all ){
				el.style.left = (document.body.offsetWidth/2) - (el.offsetWidth/2) + 'px';
				el.style.top = '60px';
			}else{
				el.style.left = (window.innerWidth/2) - (el.offsetWidth/2) + 'px';
				el.style.top = '60px';
			}
			
			if(param == 'deposit'){
				el.style.height = '680px';
			}
	}else{
		
		var yCoord = (window.event) ? event.clientY : storedClientY;
		el.style.visibility = 'visible';
		
		if(yCoord < 60) yCoord = 290;
		if( document.all ){
			el.style.left = (document.body.offsetWidth/2) - (el.offsetWidth/2);
			el.style.top = yCoord - 200;
		}else{
			el.style.left = (window.innerWidth/2) - (el.offsetWidth/2) + 'px';
			el.style.top = yCoord - 200 + 'px';
		}
		
	}

	dialogObj.showDialog( );

}

