/* Expandable module */
function expandCollapse(id)
{
    var elm = document.getElementById("expandable_" + id);

    if (elm)
    {
        /* Hack alert -- footer will be hidden temporarily
        to recalculate bottom position correctly.
        */

        var footer = document.getElementById("footer");
        footer.style.display = "none";

        if (elm.style.height != "auto")
        {
            collapseAllElements();
            showElement(id);        
        }
        else
        {
            collapseElement(id);
        }

        footer.style.display = "block";
    }
}

function showElement(id) {
    var elm = document.getElementById("expandable_" + id);
    elm.style.height = "auto";

    var h3 = document.getElementById("expandableHead_" + id);
    if (h3) {                
        h3.style.backgroundColor = "rgb(102,102,102)";                 
        h3.style.color = "rgb(255,255,255)";              
        h3.style.backgroundImage = "url(/images/arrow-lightgray-down.gif)";            
        h3.style.backgroundPosition = "9px 9px";
    }
}

function collapseElement(id) {
    var elm = document.getElementById("expandable_" + id);
    elm.style.height = "19px";

    var h3 = document.getElementById("expandableHead_" + id);
    if (h3) {
        h3.style.backgroundColor = "rgb(204,204,204)";                
        h3.style.color = "rgb(0,0,0)";
        h3.style.backgroundImage = "url(/images/arrow-gray.gif)";          
        h3.style.backgroundPosition = "11px 7px";
    }
}
function collapseAllElements() {
    for(i=1;i<document.getElementsByTagName('div').length;i++){
        if (document.getElementById("expandable_" + i)){
        	collapseElement(i);
        }
    }
}
function showAllElements() {
    for(i=1;i<document.getElementsByTagName('div').length;i++){
        if (document.getElementById("expandable_" + i)){
        	showElement(i);
        }
    }
}
/* Windows module */
function configureCar(marketId) {
	/* For new CC inner size is important, but gotoUrlNewWinSizeByName() calcs outer size */
	var sWidth = 980;
	var sHeight = 680;
	sWidth -= 20;
	sHeight -= 25;

	switch(marketId){
		case 'ca':
			sUrl = 'http://www.porsche.com/canada/en/modelstart/';
			break;
		case 'ca-fr':
			sUrl = 'http://www.porsche.com/canada/fr/modelstart/';
			break;
		case 'us':
			sUrl = 'http://www.porsche.com/usa/modelstart/';
			break;
		default:
			sUrl = 'http://www.porsche.com/usa/modelstart/';
			break;
	}
	
    gotoUrlNewWinSizeByName(sUrl, sWidth, sHeight, "PVA");
}

/*
Function compareModels
Description: 
	Wrapper for compareModelMulti with empty model parameters. Is mainly used to open the CM Popup from the shopping buttons.
Input: 
	marketId: name of the market to be loaded in compare models.
Output: void
*/
function compareModels(marketId) {
	compareModelMulti(marketId, '', '', '', true);
}

/*
Function compareModelWith
Description:
	Wrapper for compareModelMulti to Open compare models tool (select page) with the given market name and two models to be compared to each other. 
Input:
	marketId: market name
	model1: First model's id.
	model2: Second model's id.	
Output: void.	
*/
function compareModelWith(marketId, model1, model2) {
	compareModelMulti(marketId, model1, model2, '', false);
}


/*
Function compareModelMulti
Description:
	Opens a popup window of variable size. 
	to load the compare models page for a given market, and three model IDs.
	The size can be adjusted for each market (pool) by setting the oneSizeFitsAll to false 
	and defining windowSizeX and windowSizeY for each pool.
Input:
	marketId: market (pool) name to be opened in the compare models tool.
	model1: First model's id.
	model2: Second model's id.	
	model3: Third model's id.
	openSelect: true if Select page is to be opened, false if compare page.
	(If further models are to be added, make the necessary changes in the url "cmUrl" and the window sizes.)
Output: void.	
*/
function compareModelMulti(marketId, model1, model2, model3, openSelect) {
	windowSizeX = 1003;
	windowSizeY = 625;
	oneSizeFitsAll = true;

	if (openSelect) 
		cmPage = "Select"
	else
		cmPage = "Compare";
		
	// make the window sizes pool dependent, if desired.
	if (!oneSizeFitsAll)
		switch (marketId) {
			case "usa":
				windowSizeX = 1003;
				windowSizeY = 625;
				break;
			case "canada":
				windowSizeX = 975;
				windowSizeY = 625;
				break;
		}
	// create the window and load the url.
	cmUrl = "http://www.porsche.com/all/comparemodels/" +cmPage + ".aspx?pool=" + marketId + "&model1=" + model1 + "&model2=" + model2 + "&model3=" + model3 + "";
	gotoUrlNewWinSize(cmUrl, windowSizeX, windowSizeY);
}

function gotoUrlNewWinSimple(s)
{
    newWin = window.open(s, "newWin" + getRandomInt(10000));
}

function gotoUrlNewWin(s)
{
    sWidth = 826;
    sHeight = 610;

    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=no,status=no");
}

function gotoUrlNewWinSize(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=no,status=no");
}

	
function gotoUrlNewWinSizeCloseOnBlur(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 25;
    g_newWinFocus = window.open(s, "newWinCloseOnBlur", "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,location=no,resizable=no,scrollbars=no,status=no");	   	   	
    g_newWinFocus.focus();	    
    
    window.onfocus = function()
    {
    	if (g_newWinFocus)
    	{
    		g_newWinFocus.close()
    		g_newWinFocus = null;	    	
    	}
    }
}		

function gotoUrlNewWinSizeScrollable(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=yes,status=no");
}


function gotoUrlNewWinSizeScrollableWithMenu(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 50;

    if (screen.height < 768) {
        if (sHeight > 450) sHeight = 430;
        if (sWidth > 783) sWidth = 790;
        sOffset = 0;
    }
    else if (screen.height < 1024) {
        if (sHeight > 550) sHeight = 550;
        sOffset = 10;
    }
    else {
        sOffset = 40;
    }

    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=" + sOffset + ",top=" + sOffset + ",dependent=yes,location=yes,menubar=yes,toolbar=yes,resizable=no,scrollbars=yes,status=yes");
}

function gotoUrlNewWinSizeScrollableWithMenuR(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 50;

    if (screen.height < 768) {
        if (sHeight > 450) sHeight = 430;
        if (sWidth > 783) sWidth = 790;
        sOffset = 0;
    }
    else if (screen.height < 1024) {
        if (sHeight > 550) sHeight = 550;
        sOffset = 10;
    }
    else {
        sOffset = 40;
    }

    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=" + sOffset + ",top=" + sOffset + ",dependent=yes,location=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,status=yes");
}


function gotoUrlNewWinDefaultSize(s)
{
    sWidth = 570;
    sHeight = 610;

    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=no,status=no");
}

function gotoUrlNewWinSizeCentered(s, iWidth, iHeight)
{
    iWidth += 20;
    iHeight += 25;    
    iLeft = Math.round((screen.width - iWidth) / 2);
    iTop = Math.round((screen.height - iHeight) / 2);
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + iWidth + ",height=" + iHeight + ",left=" + iLeft + ",top=" + iTop + ",dependent=yes,location=no,resizable=no,scrollbars=no,status=no");
}

function gotoUrlNewWinSizeScrollableResizeable(s, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=yes,scrollbars=yes,status=no");
}

function gotoUrlNewWinSizeByName(s, sWidth, sHeight, sName)
{
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, sName, "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=no,status=no");
}

function openLeanWin(sUrl, sWidth, sHeight)
{
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(sUrl, "newWin" + getRandomInt(10000), "width=" + sWidth + ",height=" + sHeight + ",left=140,top=140,dependent=yes,location=no,resizable=no,scrollbars=no,status=no,menubar=no,toolbar=no");
}

function gotoUrlFullscreen(sUrl, bScroll) {
	var width = screen.width;
	var height = screen.height;
	var left = 0;
	var top = 0;
	
	if (screen.width > 1920)
	{
		width = 1920;
		height = 1200;
		left = Math.round((screen.width - width) / 2);
		top = Math.round((screen.height - height) / 2);
	}
	
    var scrollbars = 0;
    if (bScroll) scrollbars = 1;
    
	var sProps = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scrollbars + ",resizable=0,left=" + left + ",top=" + top + ",width=" + (width - 10) + ",height=" + (height - 55);	
	var site = window.open(sUrl,"porschemicrosite", sProps);
	site.focus();		
}

function gotoUrlFullscreenResizable(sUrl) {
	var width = screen.width;
	var height = screen.height;
	var left = 0;
	var top = 0;
	
	if (screen.width > 1920)
	{
		width = 1920;
		height = 1200;
		left = Math.round((screen.width - width) / 2);
		top = Math.round((screen.height - height) / 2);
	}
	
	var sProps = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;	
	var site = window.open(sUrl,"porschemicrosite_rs", sProps);
	site.focus();
}

function getRandomInt(max)
{
    return Math.round( Math.random() * (max-1) );
}
