﻿function updateResolution()
{
    getResolution();
    setResolution();
}

var lastLoadedCss = "";

function setCss(width)
{
    // If window width smaller than 1000, remove extension column but loading additional CSS file.
    if (width > 1200 && lastLoadedCss != "LayoutWide")
    {
        loadCssFile(getFullCssPath()+'LayoutWide.css', 'rightColumnStyle');
        lastLoadedCss = "LayoutWide";
    }
    else if (width >= 1000 && width <= 1200 && lastLoadedCss != "LayoutNarrow")
    {
        loadCssFile(getFullCssPath()+'LayoutNarrow.css', 'rightColumnStyle');
        lastLoadedCss = "LayoutNarrow";
    }
    else if (width < 1000 && lastLoadedCss != "LayoutLeftOnly")
    {
        if ((navigator.appVersion.indexOf("MSIE 7") == -1 && navigator.appVersion.indexOf("MSIE 6") == -1) || lastLoadedCss == "")
        {            
            loadCssFile(getFullCssPath()+'LayoutLeftOnly.css', 'rightColumnStyle');
        }
        lastLoadedCss = "LayoutLeftOnly";
    }
}

function getResolution()
{
    // Determine screen resolution to add results to the query string as well.
    var width = 0, height = 0;
    
    //Non-IE
    if (typeof(window.innerWidth) == 'number')
    {
        width = window.innerWidth;
        height = window.innerHeight;
    }
    
    //IE 6+ in 'standards compliant mode'
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    }
    
    //IE 4 compatible
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }

    // Save the values to cookies.
    createCookie('screen', screen.width + 'x' + screen.height, 365);
    createCookie('window', width + 'x' + height, 1000);
    createCookie('depth', screen.colorDepth, 365);

    //document.cookie = 'screen=' + screen.width + 'x' + screen.height;
    //document.cookie = 'window=' + width + 'x' + height;
    //document.cookie = 'depth=' + screen.colorDepth;

    setCss(width);

    // Create and return resolution detection script.
    return '&screen=' + screen.width + 'x' + screen.height + '&window=' + width + 'x' + height + '&depth=' + screen.colorDepth;
}

function setResolution()
{
    var controlScreen = document.getElementById(getScreenResolutionId());
    var controlWindow = document.getElementById(getWindowResolutionId());
    var controlDepth = document.getElementById(getColourDepthId());
    
    if (document.all)
    { 
        if (controlScreen != null)
        {
            controlScreen.innerText = "Screen resolution: " + readCookie('screen');     
        }
        if (controlWindow != null)
        {
            controlWindow.innerText = "Window resolution: " + readCookie('window');
        }
        if (controlDepth != null)
        {
            controlDepth.innerText = "Colour depth: " + readCookie('depth');
        }
    }
    else    // In Firefox innerText does not work
    {
        if (controlScreen != null)
        {
            controlScreen.textContent = "Screen resolution: " + readCookie('screen');         
        }
        if (controlWindow != null)
        {
            controlWindow.textContent = "Window resolution: " + readCookie('window');         
        }
        if (controlDepth != null)
        {
            controlDepth.textContent = "Colour depth: " + readCookie('depth');                
        }
    }
}

function loadCssFile(filename, id)
{
    removeCssFile(id);
    
    var fileref = document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filename);
    fileref.setAttribute("id", id);

    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}

function removeCssFile(id)
{
    var fileref = document.getElementById(id);
    if (fileref != null)
        fileref.parentNode.removeChild(fileref);
}

function createCookie(name, value, days) 
{
    var expires = "";
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i=0;i < ca.length;i++) 
    {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) 
{
    createCookie(name,"",-1);
}

function addLoadEvent(func, param) 
{   
    var oldonload = window.onload;   
    if (typeof window.onload != 'function') 
    {   
        window.onload = func;   
    } 
    else 
    {   
        window.onload = function() 
        {   
            if (oldonload) 
            {   
                oldonload();   
            }   
            if (param == null)
            {
                func();
            }
            else
            {
                func(param);   
            }
        }
    }   
} 

if(window.top != window.self) 
{
    window.top.location.href = window.self.location;
}
window.status = 'Racing Sports Cars';
window.onresize = updateResolution;    
