﻿// Javascript for Range page specific functionality
var initialState = true;
var contentDivResized = true;

// returns value of currently selected model
function getCurrentModel()
{    return currentModel; }

function isMSBrowser()
{    try 
    {   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp = null;
    } 
    catch(ex) 
    {	return false; }
    return true;
}

function ieVersion()
{	if (navigator.appVersion.indexOf("MSIE")!=-1)
    {	temp=navigator.appVersion.split("MSIE");
		return parseFloat(temp[1]);
    }
    return 0;
}

function expandContentDiv()
{	// ALL body tag ids MUST be "LEXUS".
	var bodytag = document.getElementById("LEXUS");

	if (bodytag != null)
	{	var contentDiv = getRefToDiv("content");
		var primaryContentDiv = getRefToDiv("primary-content");
		var secondaryContentDiv = getRefToDiv("secondary-content");
		var additionalSpacer = (ieVersion() == 6) ? -170 : 30; // IE 6 needs to have 170 SUBTRACTED, whereas every other browser needs 30 ADDING.
	}


	if(contentDiv != null && primaryContentDiv != null && secondaryContentDiv != null)
	{	var contentHeight = primaryContentDiv.offsetHeight + secondaryContentDiv.offsetHeight + additionalSpacer;
		if(contentHeight < 500){contentHeight = 500;}

		expandDiv("content", "open", 0, contentHeight);


		//set the zIndex for FF in Mac - otherwise, swf layers over text
		if(document.getElementById('main_movie') != null){var objMovie=document.getElementById('main_movie');objMovie.style.zIndex='5';primaryContentDiv.style.zIndex='6';}


	} 
}

// Lower values case a slower initial move speed. Must be > 1
var toggleDivSpeedMultiplier = 2;
// The delay between each move. 
var toggleDivDelay = 10;

var strTitle = document.title;

// The function that is called repeatedly until the expansion is done
function expandDiv(divId, openClose, increment, toHeight) 
{ 	var d = getRefToDiv(divId);
    var height;
    var intinc = parseInt(increment);
    if (d==null || d.tagName!="DIV") { return; }
    if(d.style.height==null || d.style.height=="")
    {	height = 0;
    }
    else
    {	height = parseInt(d.style.height.substring(0,d.style.height.length - 2));
    }

    d.style.height = toHeight+"px";
    return;
	
    if (openClose=="open") 
    {	if ((height + intinc >= parseInt(toHeight)) || parseInt(intinc) == 0 ) 
	    {	d.style.height = toHeight+"px";
	    }
	    else 
	    {	if(d.style.height==null)
	        {	d.style.height = d.style.height+increment+"px";
	        }
	        else
	        {	var newHeight = height + intinc;
	            d.style.height = newHeight+"px";
	        }
		    setTimeout("expandDiv('"+divId+"','"+openClose+"','"+increment+"','"+toHeight+"')",toggleDivDelay);
	    }
    }
    else 
    {	if (d.style.height - increment <= toHeight) 
	    {	//alert("3");
			//d.style.height = toHeight+"px";
	    }
	    else 
	    {	//alert("4");
			//d.style.height = d.style.height-increment+"px";
			//setTimeout("expandDiv('"+divId+"','"+openClose+"','"+increment+"','"+toHeight+"')",toggleDivDelay);
	    }
    }
}

// method called by javascriptInterface within global.js
function carSelection(jObj)
{	var performAjax = true;
	currentModel = jObj.params[0];
	if(performAjax)
	{	document.title = strTitle + ' - ' + currentModel + '...';

		var model = findAspnetControl("hdnModelName");
		if(model != null)
		model.value = currentModel;

		triggerOnClick("btnHiddenHeader");
		triggerOnClick("btnHiddenMain");

		document.title = strTitle + ' - ' + currentModel;
	}
	else
	{	window.location = './' + currentModel + '/Index.aspx';
	}
}

function getURLParam(strParamName)
{	var strReturn = "";
    var strHref = window.location.href;

    if ( strHref.indexOf("?") > -1 )
    {	var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
        {	if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 )
            {	var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

var timer;
var timerCount;
var timerCountLimit=20;
function setToRange(movieName)
{	debug('[range.js]setToRange:start function, movieName='+movieName);
	timerCount=0;
	clearTimeout(timer);
	debug('[range.js]setToRange:setting timer');
	//timer=setTimeout(function() { waitForPageReady.call(this, movieName, "Range"); }, "500");
	var checkAvailable = setInterval(function() {
                   if(pageAvailable())
                   {    
                       clearInterval(checkAvailable);
                       waitForPageReady.call(this, movieName, "Range");
                   }
                 }, 100);
	debug('[range.js]timer set');
	//var objects=document.getElementsByTagName('embed');for(var i=objects.length-1;i>=0;i--){objects[i].style.zIndex='5';)
	//var objects=document.getElementById('primary-content');for(var i=objects.length-1;i>=0;i--){objects[i].style.zIndex='6';)
	debug('[embed and primary-content zindex set for mac ff bug on ajax layering:end function');
}

function setToCarChapter(movieName, modelName)
{	debugFunctionTitle='setToCarChapter:';debug('entered function movieName='+movieName+', modelName='+modelName);
	timerCount=0;
	clearTimeout(timer);
	debug('setting timer');
	//timer=setTimeout(function() { initCarChapter.call(this, movieName, "Chapter"); }, "500");
	//timer=setTimeout(function() { waitForPageReady.call(this, movieName, "Chapter", modelName); }, "500");
                var checkAvailable = setInterval(function() {
                    if(pageAvailable())
                    {    
                        clearInterval(checkAvailable);
                        waitForPageReady.call(this, movieName, "Chapter", modelName);
                    }
                }, 100);
	debug('[setToCarChapter]end function');
}

function waitForPageReady(movieName, stage, modelName)
{	// First, check if the pageAvailable() function - held in Global.js - returns true or false.

	debug('[range.js]waitForPageReady(): entered function');
	if(!pageAvailable())
	{	debug('[range.js]waitForPageReady(): page not available');
		return;
	}
    
	debug('[range.js]waitForPageReady(): page is available');
	// One way, or the other, we need to cancel the timer here to allow the remaining
	// logic to flow through.
	clearTimeout(timer);
    
	// Next, check if we have exceeded our timeout.
	if(++timerCount > timerCountLimit)
	{	debug('[range.js]waitForPageReady(): timer exceeded retry count');
		return;
	}
    
    // The page is available, and we haven't exceeded our timerCountLimit.
    // Call the relevant initialisation function.
    if(stage == "Range")
    {	try
        {
	debug('[range.js]waitForPageReady(): calling initRange('+movieName+');');
	initRange(movieName);
        }
        catch(ex)
        {	// initRange failed, so set the timerCountLimit to allow a few more tries.
	debug('[range.js]waitForPageReady(): range.js:timerCountLimit = 25;');
            timerCountLimit = 25;
	debug('[range.js]waitForPageReady(): calling setToRange('+movieName+');');
            setToRange(movieName);
        }
    }
    else if(stage == "Chapter")
    {	try
        {	initCarChapter(movieName, modelName);
                SWFReplace();

        }
        catch(ex)
        {	// initCarChapter failed, so set the timerCountLimit to allow a few more tries.
            timerCountLimit = 25;
            setToCarChapter(movieName, modelName);
        }
    }
}

function initRange(movieName)
{	//expandDiv("flashwrapper", "open", "0", "500");

//alert('initRange('+movieName+')');

    // Changed to accommodate RURU range movie
    expandDiv("content", "open", "0", "920");

//alert('calling setFlashSWFState(init_range, , moviename');

    setFlashSWFState("init_range", "", movieName)
}

function initCarChapter(movieName, modelName)
{	var currentModel = "";
    if(modelName == null || modelName == "")
    {	var model = findAspnetControl("hdnModelName");
        if(model != null)
        {	currentModel = model.value;
        }
    }
    else
    {	currentModel = modelName;
    }
    
    if(currentModel != null)
    {	setFlashSWFState("init_car_chapter", currentModel, movieName);
    }
}

function setFlashSWFState(state, model, movieName)
{	var msg = "setFlashSWFState(" + state + ", " + model + ", " + movieName + ")";
    //alert(msg);

    try
    {	var jObj = {"command": state, "return": false, "params": [model]};  
		flashInterface(jObj, movieName);
	}
	catch(ex) 
	{	//alert(msg + " : exception thrown!\n" + ex.description);
	    throw(ex);
	}
}
