var num = 0;
var w_total =0;
var w_box = 0;

function getXMLHTTPRequest() {
    try {
        req = new XMLHttpRequest();
    } catch (err1) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (err2) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3) {
                req = false;
            }
        }
    }
    return req;
}

var http = getXMLHTTPRequest();

function getImage(num) {
    var myurl = 'getimage.php?num='+num;
    myRand = parseInt(Math.random() * 999999999999999);
    var modurl = myurl + "&rand=" + myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHttpResponse;
    http.send(null);
}

function useHttpResponse() {
    if (http.readyState == 4) {
        if (http.status == 200) {
            var timeValue = http.responseXML.getElementsByTagName("main_image")[0];
            document.myimage.src = timeValue.childNodes[0].nodeValue;

            var floatValue = http.responseXML.getElementsByTagName("floating")[0];
            document.getElementById('floating').innerHTML = floatValue.childNodes[0].nodeValue;

            var menuColorValue = http.responseXML.getElementsByTagName("menuColor")[0];
            //var menuColor = =menuColorValue.childNodes[0].nodeValue;
            //document.getElementById("nav").a.link.style.color = '"'+floatValue.childNodes[0].nodeValue+'"';
            document.getElementById('nav').style.backgroundColor = menuColorValue.childNodes[0].nodeValue;

            //for( var i = 0; i < document.getElementById('nav').getElementsByTagName('a').length; i++ ) {
                //document.getElementById('nav').getElementsByTagName('span')[i].style.color=menuColorValue.childNodes[0].nodeValue;
            //}

            var featureValue = http.responseXML.getElementsByTagName("feature")[0];
            document.tab.src = "images/tab"+featureValue.childNodes[0].nodeValue+".png";
            document.tab_b.src = "images/tab"+featureValue.childNodes[0].nodeValue+"_b.png";
            document.tab_c.src = "images/tab"+featureValue.childNodes[0].nodeValue+"_c.png";
            document.tab_m.src = "images/tab"+featureValue.childNodes[0].nodeValue+"_m.png";

            //document.getElementById('nav').style.backgroundImage = "images/tab"+featureValue.childNodes[0].nodeValue+"_a.png";




            var bgValue = http.responseXML.getElementsByTagName("background")[0];
            document.body.style.backgroundColor = bgValue.childNodes[0].nodeValue;

            var numValue = http.responseXML.getElementsByTagName("num")[0];
            num = numValue.childNodes[0].nodeValue;

            var div = document.getElementById("floating");
            //div.style.backgroundColor = menuColorValue.childNodes[0].nodeValue;

            var topFloat = http.responseXML.getElementsByTagName("top")[0];
            var leftFloat = http.responseXML.getElementsByTagName("left")[0];
            var topFloatValue =topFloat.childNodes[0].nodeValue;
            var leftFloatValue =leftFloat.childNodes[0].nodeValue;
            var div = document.getElementById("floating");
            div.style.top = topFloatValue + "px";
            var div = document.getElementById("floating");
            div.style.left = leftFloatValue + "px";
   }
    } else {
        //  document.getElementById('showtime').innerHTML = timeValue;
    }
}

function resizeImage() {

    var origWidth = document.myimage.width;
    var origHeight = document.myimage.height;

    //define image ratio
    //var ratio = origHeight / origWidth;
    var ratio = .4638671875;
    //get browser dimensions
    var winW = 630,
        winH = 460;
    if (document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    if (document.compatMode == 'CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
        winW = document.documentElement.offsetWidth;
        winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }


    var winWidth = winW;
    var winHeight = winH;
    var winRatio = winHeight / winWidth;
    //resize the image minus 20 for verticle scrollbar
    document.myimage.width = (winW - 20);
    document.myimage.height = ((winW - 20) * ratio);

    document.pfloat.width = ((winW - 20) * .50);
    document.pfloat.height = (((winW - 20) * .546875) * .50);

}

function getWindowSize() {
    var winW = 630,
        winH = 460;
    var ratio = .4638671875;
    if (document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    if (document.compatMode == 'CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
        winW = document.documentElement.offsetWidth;
        winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }
    if (winW < 1200) {
      var ttop = 650;
    } else {
    var ttop = (winW * ratio);
    }
    var div = document.getElementById("lowerContent");
    div.style.top = (ttop - 125) + "px";
    var div = document.getElementById("footer");
    div.style.top = (ttop + 325) + "px";

}


function centerIt() {
    if (self.innerHeight) {
        var x = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        var x = document.documentElement.clientWidth;
    } else if (document.body) {
        var x = document.body.clientWidth;
    }
    w_box = x;
    w_total = (w_box - 960) / 2;
    var div = document.getElementById("container");
    div.style.left = w_total + "px";
    var div = document.getElementById("lowerContent");
    div.style.left = w_total + "px";
    var div = document.getElementById("footer");
    div.style.left = w_total + "px";
}



function resizeStuff() {
    getWindowSize();
    resizeImage();
    centerIt();
}
var TO = false;
$(window).resize(function () {
    if (TO !== false) clearTimeout(TO);
    TO = setTimeout(resizeStuff, 200); //200 is time in miliseconds
});
