/* RLT Javascript
   Copyright 2010 AR

   Version:
   1.0 - 1 March 2010
   
   Dependencies: 
   none   
*/

// Globals
var stageExpire = 0;

var specsImages = new Array();
var specsImagesExt = 'jpg';
var specsImagesPath = ""; 
var specsButton = 0;

// this is a function that attaches (or hacks) DOMContentLoaded event for all browsers as of 2007
//(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st = 
//setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
//if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
//else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
//document.addEventListener("DOMContentLoaded",i,false); } else if(e){     (
//function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
//i();t=null;}catch(e){st(arguments.callee,0);}})();}else{window.onload=i;}})(loadStage);




/*
// this is yet another function that does tha hack for all browsers as of 2007
function onContent(f){//(C)webreflection.blogspot.com
var a=onContent,b=navigator.userAgent,d=document,w=window,c="onContent",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,".",c,"()}'></scr","ipt>");
a[c]=(function(o){return function(){a[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(a[c]);
if(d[e])d[e]("DOMContentLoaded",a[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))(function(){/loaded|complete/.test(d[r])?a[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
};
onContent(loadStage);
*/


// with faster machines this seems to help with the flickering in case of IE
//if (clientBrowser == "Internet Explorer") 
scrollToPosition();

//alert(clientBrowser);
//if (clientBrowser == "Opera") scrollToPosition();


// chose this method for readability, it only deals with Mozilla and IE (checking for objects), the rest all go to window.onload
document.write('<script id="__init_script" defer="true" src="//[]"></script>');
function registerInit(callback) {

    //for Mozilla
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", callback, false);
    }

    // for Internet Explorer 
    if (document.getElementById) {
        var deferScript = document.getElementById('__init_script');
        if (deferScript) {
            deferScript.onreadystatechange = function() {
                if (this.readyState == 'complete') {
                    callback();
                }
            };

            // check whether script has already completed 
            deferScript.onreadystatechange();

            // clear reference to prevent leaks in IE 
            deferScript = null;
        }
    }

    // for other browsers 
    window.onload = callback;
}

// Stage object events
//window.onload = loadStage;
window.onresize = scrollToPosition;


// Stage onload
function loadStage() {

    // quit if this function has already been called
    if (arguments.callee.done) return;
    
    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    //alert(clientBrowser);

    scrollToPosition();
    //scrollToPositionByChunks;

    // Run page load if required
    if (typeof(pageLoad) == 'function') pageLoad();

    // Run secondary page load if required
    if (typeof(pageLoad2) == 'function') pageLoad2();

    // one more page load if required
    if (typeof(pageLoad3) == 'function') pageLoad3();
    
}

registerInit(loadStage);


function clientWidth() {
    return filterResults (
        window.innerWidth ? window.innerWidth : 0,
        document.documentElement ? document.documentElement.clientWidth : 0,
        document.body ? document.body.clientWidth : 0
    );
}
function clientHeight() {
    return filterResults (
        window.innerHeight ? window.innerHeight : 0,
        document.documentElement ? document.documentElement.clientHeight : 0,
        document.body ? document.body.clientHeight : 0
    );
}
function scrollLeft() {
    return filterResults (
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0
    );
}
function scrollTop() {
    return filterResults (
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0
    );
}
function filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function scrollToPosition()
{

    var newScrollX = 0;

    winWidth = clientWidth();
    winHeight = clientHeight();
    scrollX = scrollLeft();
    scrollY = scrollTop();
    
    //alert(winWidth);

    // calculate where to scroll...
    newScrollX = Math.floor((1920 - (Math.max(winWidth, 1024)))/2);

    //alert(newScrollX);
    if (scrollX != newScrollX)
    {
        window.scroll(newScrollX, scrollY);   
    }
}

function scrollToPositionByChunks()
{

    var newScrollX = 0;
    var i = 10;
    
    winWidth = clientWidth();
    winHeight = clientHeight();
    scrollX = scrollLeft();
    scrollY = scrollTop();
    
    // calculate where to scroll...
    if (winWidth < 1024)
    {
        newScrollX = scrollX;
    }
    else
    {
        newScrollX = Math.floor((1920 - winWidth)/2);
    }

    j = Math.min(i,newScrollX);
    
    //alert(j);
    
    while (j < newScrollX)
    {
        window.scroll(j, scrollY); 
        j = Math.min(i + j, newScrollX);
        //alert(j);
    }
    
}

function pauseload(millis)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate - date < millis);
} 

function loopLoad(){
    
     if (window.onload)
     {
          loadStage();
          
     } else {
          setTimeout('loopLoad();', 1000);
     }
         
}

// Preload specs images
function preloadSpecsImage(obj, img, mode) {
    
    // Init
    var index = obj.length;
    obj[index] = new Array();
    
    // Off
    obj[index][0] = new Image();
    obj[index][0].src = specsImagesPath + img + '_off.' + specsImagesExt;
    
    // On
    obj[index][1] = new Image();
    if (mode == 0) {
        obj[index][1].src = specsImagesPath + img + '_on.' + specsImagesExt;
    }
    
    // Over
    obj[index][2] = new Image();
    obj[index][2].src = specsImagesPath + img + '_over.' + specsImagesExt;
}

// Change specs images
function changeSpecsImage(element, but, index) {
    
    var img = document.getElementById(element);    
    if (specsButton == but && index == 0) index = 1;
    img.src = specsImages[but][index].src
}

// Change Link colour
function changeColor(idObj,colorObj)
{
    document.getElementById(idObj).style.color = colorObj;
}

