
function hiLiteChatButton(){
    //
    // hilight the 3rd party chat button we can't get an ID for
    //
    var image = getChatButton();
    image.src = image.src.replace('Off','On');  
}
function loLiteChatButton(){
    //
    // lolight the 3rd party chat button we can't get an ID for
    //
    var image = getChatButton();
    image.src = image.src.replace('On','Off');  
}
function getChatButton(){
    // get all elements that are in ChatButtonContainer
    var nodes = $A($('ChatButtonContainer').childElements());
    // the second node [1] is the A node, return it's first child, the image node
    return $A(nodes[1].childElements())[0];
}
function gen_changeImage(strImageID, strTargetURL) {
	var imageObject;
	imageObject = document.images;
	imageObject[strImageID].src = strTargetURL;
}

//
// support site menu handler
//
function startNavList(){
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("navmenu");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

//really simple function to roll an image
function roll(id, from_state, to_state){
	var obj =document.getElementById(id);
	obj.src = obj.src.replace(from_state,to_state);
}

function AddClass(obj, classname) {
    if(!(obj==null)) {
		if (obj.className.length == 0) {
			obj.className = classname;
		}
		else {
			if (!(obj.className.match(new RegExp(classname, "g")))) {
				obj.className += ' ' + classname;	
			}
		}
    }
}
function RemoveClass(obj, classname) {
    /*October 3, 2005 Note_RC:
      I came across a bug when using this function that if you called the function to remove a class
      that wasn't there, and the object using the said class had a transparent background, IE would
      not "repaint" the screen.*/
    if(!(obj==null)) {
	    if (obj.className.match(new RegExp(classname, "g"))) {
		    obj.className = obj.className.replace(new RegExp(classname, "g"), "");
	    }
    }
}

//need the empty implementation so that other browsers will not throw an error
function HoverOn()	{
}
function HoverOff(){
}

/*
var MyMainMenuEvent = 0;
function ShowMainMenu(menucontainerid) {
	if (MyMainMenuEvent == 0)
	{
		//No event has been set yet, the menu was not on
		AddClass(document.getElementById(menucontainerid),'menuon');
	}
	else {
		//The event has been set but hasn't fired yet. Cancel the event
		window.clearTimeout(MyMainMenuEvent);
		MyMainMenuEvent = 0;
	}
}
*/
function ShowMainMenu(menucontainerid,flashmenuid) {
	//if (MyMainMenuEvent == 0) {
		//No event has been set yet, the menu was not on
		var objFlash;
		objFlash = document.getElementById(flashmenuid);
		if (movieIsLoaded(objFlash)) {
			objFlash.SetVariable("active",'1');
		}
		AddClass(document.getElementById(menucontainerid),'menuon');
	//}
}

/*
function HideMainMenu(menucontainerid,delay) {
	MyMainMenuEvent = setTimeout(function(){HideMainMenuDelayed(menucontainerid);}, delay);
}
*/
function HideMainMenu(flashmenuid) {
	var objFlash;
	objFlash = document.getElementById(flashmenuid);
	if (movieIsLoaded(objFlash)) {
		objFlash.SetVariable("active",'0');
	}
}
/*
function HideMainMenuDelayed(menucontainerid) {
	RemoveClass(document.getElementById(menucontainerid),'menuon');	
	window.clearTimeout(MyMainMenuEvent);
	MyMainMenuEvent = 0;
}
*/
/*
function HideMenu() {
	alert('called');
	menucontainerid = 'MenuContainer';
	RemoveClass(document.getElementById(menucontainerid),'menuon');	
	window.clearTimeout(MyMainMenuEvent);
	MyMainMenuEvent = 0;
}
*/
function HideMenu() {
	alert('called');
	menucontainerid = 'MenuContainer';
	RemoveClass(document.getElementById(menucontainerid),'menuon');
}

function movieIsLoaded (theMovie) {
	if (typeof(theMovie) != "undefined") {
		return theMovie.PercentLoaded() == 100;
	}
	else {
		return false;
	}
}

function findPosX(obj, parentlimit) {
	var curleft = 0;
	if(obj) {
		if (obj.offsetParent) {			
			while ((obj.offsetParent) && (obj != parentlimit)) {
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x) {
			curleft += obj.x;
		}
	}
	else {
		curleft = -99;
	}
	return curleft;
}
function findPosY(obj, parentlimit) {
	var curTop = 0;
	if(obj)	{
		if (obj.offsetParent) {
			while (obj.offsetParent) {				
				curTop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y) {
			offsetTop += obj.y;
		}
	}
	else {
		offsetTop = -99;
	}
	return curTop;
}

function AnimatorObject(id, style_function, initial_state) {
	var me			  = new Object();
	me.id			  = id;
	me.timer_id		  = -1;
	me.d			  = 0;
	me.state		  = initial_state;
	me.style_function = style_function
	
	//Set the initial state
	var style_function_arg = me.style_function + "('" + me.id + "', " + me.state + ")";
	eval(style_function_arg);
	
	//So we can call it from object.
	me.animate = function(target_state, number_of_steps, when_done_callback) {
		me.target_state		  = target_state;
		me.number_of_steps	  = number_of_steps;
		me.when_done_callback = when_done_callback;
		me.step_animation();
	}
	
	//The actual animation machinery.
	me.step_animation = function() {
		//Defaults
		if(!me.number_of_steps)
			me.number_of_steps = 1;
		if(!me.when_done_callback)
			me.when_done_callback = "";
		
		//Get the desired direction dd
		var dd = 0;
		if(me.target_state - me.state < 0)
			dd = -1;
		else if(0 < me.target_state - me.state)
			dd = 1;
			
		//If the desired direction dd is not the same as the current direction d,
		//then kill the other animation process and use the desired direction.
		if (me.d!=dd) {
			if(me.timer_id!=-1)
				clearTimeout(me.timer_id);
			me.timer_id = -1;
			me.d = dd;
		}
		
		//step size
		var ss = 0;
		if(me.number_of_steps)
			ss = Math.abs(me.target_state - me.state)/me.number_of_steps;
		
		//step towards the new state
		me.state+=me.d*ss;
		
		//If we are at the end point, terminate.
		if(0 < me.d) {
			if(me.target_state < me.state) {
				me.state = me.target_state;
				me.d=0;
			}
		}
		else if(me.d < 0){
			if(me.state < me.target_state) {
				me.state = me.target_state;
				me.d=0;
			}
		}
		
		//Call the function that changes the style that is being animated.
		if(me.style_function) {
			style_function_arg = me.style_function + "('" + me.id + "', " + me.state + ")";
			eval(style_function_arg);
		}
		
		me.number_of_steps--;
		if(me.d && 0<me.number_of_steps) {
			me.timer_id = setTimeout(me.step_animation,33);
		}
		else {
			me.timer_id = -1;
			
			if(me.when_done_callback) {
				eval(me.when_done_callback);
			}
		}
	}
		
	return me;
}

//Define a function that specifies what to animate.
function set_height(id, nv) {
	var obj = document.getElementById(id);
	if(obj) {
		if(obj.getAttribute("open_height")==null)
			obj.setAttribute("open_height", 100);
		var oh = parseFloat(obj.getAttribute("open_height"));
		
		//nv = Math.exp(nv*nv)/2.71828;
		obj.style.height = nv*oh + "px";
	}
}
//Construct the animator (passing in the above function name)	
function HeightAnimator(id, initial_state) {
	return AnimatorObject(id, 'set_height', initial_state)
}

function OpenDropDown(menuID) {

/*
	var frameCount, AnimationMilliSecPeriod
	var startingHeight, endingHeight;
	var curHeight;
	
	startingHeight = 0;
	frameCount = 30;
	AnimationMilliSecPeriod = 500;
	
	if (document.getElementById('MenuGroup_ID' + menuID) == null) return;
	endingHeight =  document.getElementById('MenuGroup_ID' + menuID).offsetHeight;
	
	var i;
	var curEventObj, prevEventObj, firstEventObj;
	firstEventObj = null;
	curEventObj = null;
	prevEventObj = null;
	
	for (i=0; i<frameCount; i++) {
		curEventObj = new EventChain();
		curHeight = (i+1)/frameCount * endingHeight + 'px';
		var func = "AdjustHeight('MenuGroupContainer_ID" + menuID + "','" + curHeight + "')"
		curEventObj.AddEventTask("eval(" + func + ");");
		
		if (prevEventObj != null) {
			prevEventObj.SetNextEvent(curEventObj);
			prevEventObj.SetNextEventTimeout(AnimationMilliSecPeriod/frameCount);
		}
		if (firstEventObj == null) {
			firstEventObj = curEventObj;
		}
		prevEventObj = curEventObj;
	}
	if (firstEventObj) firstEventObj.Fire();
*/
}
function CloseDropDown(menuID) {

//alert('CloseDropDown, '+menuID);

/*
	var frameCount, AnimationMilliSecPeriod
	var startingHeight, endingHeight;
	var curHeight;
	
	endingHeight = 0;
	frameCount = 30;
	AnimationMilliSecPeriod = 500;
	
	if (document.getElementById('MenuGroup_ID' + menuID) == null) return;
	startingHeight =  document.getElementById('MenuGroup_ID' + menuID).offsetHeight;
	
	var i;
	var curEventObj, prevEventObj, firstEventObj;
	firstEventObj = null;
	curEventObj = null;
	prevEventObj = null;
	
	for (i=frameCount; i>0; i--) {
		curEventObj = new EventChain();
		curHeight = (i-1)/frameCount * startingHeight + 'px';
		var func = "AdjustHeight('MenuGroupContainer_ID" + menuID + "','" + curHeight + "')"
		curEventObj.AddEventTask("eval(" + func + ");");
		
		if (prevEventObj != null) {
			prevEventObj.SetNextEvent(curEventObj);
			prevEventObj.SetNextEventTimeout(AnimationMilliSecPeriod/frameCount);
		}
		if (firstEventObj == null) {
			firstEventObj = curEventObj;
		}
		prevEventObj = curEventObj;
	}
	if (firstEventObj) firstEventObj.Fire();
*/
}
function AdjustHeight(objID, height) {
	document.getElementById(objID).style.height = height;
}
function IsMouseOutInternal(e, obj)
{
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	//Make sure we have a relatedTarget/toElement
	if (typeof(reltg) == 'undefined') {
		return true;
	}

	if (typeof(obj.contains) != 'undefined'){
		if (obj.contains(tg) && obj.contains(reltg)) {
			return true;
		}
	}
	else {
		if (contains(obj,tg) && contains(obj,reltg)) {
			return true;
		}
	}
	return false;
}
function contains (container, containee)
{
	//taken from: http://www.faqts.com/knowledge_base/view.phtml/aid/32247
	while (containee) {
		if (container == containee) {
		return true;
		}
		containee = containee.parentNode;
	}
	return false;
}

function showIframe(){
	alert('hi')
}

function showConfiguratorPopUp(langid){

	//Dailog dimensions
	var width         = 910;
	var height        = 700;
	
	//Configurator external domain and with internal/external port numbers
	var ext_domain = "configurator.mitsubishi-motors.ca";
	var test_port = 20475;
	var demo_port = 30475;
	
	//Construct the appropriate url for the given environment.
	var url = "http://";
	var domain = document.domain;
	switch(domain) {
		case 'localhost':
		case 'windshield':
		case 'build':
		case 'staging':
			url += domain + ':' + test_port + '/';
			break;
		case "demo1.dmt.ca":
			url += domain + ':' + demo_port + '/';
			break;
		case "alphademo2.dmt.ca":
			url += "alphademo1.dmt.ca" + '/';
			break;
		default:
			url += ext_domain + '/';
	}
	
	url += "?lng=" + langid
	
	//
	//open the dialog
	//
	var win1 = window.open(url,'mywindow','width=' + width + ',height=' + height + ',top=0,left=0,scrollbars=yes')
	//
	// this brings the window back into focus when it's already opened, but behind another window
	//
	win1.focus();
}
