﻿
//Cycled advertising images
var myImageArray = new Array("bilen.jpg", "lyset.jpg", "penge.jpg", "spurvene.jpg", "vandet.jpg");
var preloaded = new Array();
var scrollTop;
var needsCal = new Array();
var xmlHttp = null;
var LastUpdate = '';

function BeginRequestHandler(sender, args) {

    var elem = document.getElementById('MainContentDiv');

    scrollTop = elem.scrollTop;
    document.body.style.cursor = 'wait';

    changecss('.handCursor', 'cursor', 'wait');
}



function EndRequestHandler(sender, args) {

    var elem = document.getElementById('MainContentDiv');

    elem.scrollTop = scrollTop;
    document.body.style.cursor = 'default';
    changecss('.handCursor', 'cursor', 'pointer');
} 



function preload_images() {
    for (var i = 0; i < arguments.length; i++) {
        preloaded[i] = document.createElement('img');
        preloaded[i].setAttribute('src', arguments[i]);
    }
}
//Return a random image from myImageArray
function getImageFile() {

    var randomnumber = Math.floor(Math.random() * 5);

    return myImageArray[randomnumber];
}

//Show a (Possibly the same?) new random image every 15 seconds
function rotateImage() {
    var myImage = getImageFile();
    myImageSrc = document.getElementById("topLogo");
    myImageSrc.src = "files/designimages/" + myImage;
    setTimeout("rotateImage()", 15000);
}


//On main window resize... is not a standard supported event, but it works in IE and Firefox
//Make sure all our main elements fit in the window, and look "nice"
function OnResized() {

    var newHeight1 = getHeight() - 50;
    newHeight1 -= findPosY(document.getElementById("MainContentDiv"));    
    newHeight1 -= document.getElementById("FooterDiv").offsetHeight;
    var newHeight2 = newHeight1 - pxToInt(getStyle(document.getElementById("MainContentDiv"), "padding-top"));
    newHeight2 -= pxToInt(getStyle(document.getElementById("MainContentDiv"), "padding-bottom"));
    newHeight1 -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "padding-top"));
    newHeight1 -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "padding-bottom"));
    if ((newHeight1 > 150) && (newHeight2 > 150)) {
        newHeight1 += "px";
        newHeight2 += "px";
        //alert('New1=' + newHeight1 + ' New2=' + newHeight2);
        document.getElementById("MainContentDiv").style.height = newHeight2;
        document.getElementById("LeftContentDiv").style.height = newHeight1;
    }
    /*
    var newWidth = document.getElementById("ContentBoxDiv").offsetWidth;
    
    newWidth -= document.getElementById("LeftContentDiv").offsetWidth;
    
    newWidth -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "padding-right"));
    newWidth -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "padding-left"));
    newWidth -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "margin-left"));
    newWidth -= pxToInt(getStyle(document.getElementById("LeftContentDiv"), "margin-right"));
    
    newWidth -= pxToInt(getStyle(document.getElementById("MainContentDiv"), "padding-right"));
    newWidth -= pxToInt(getStyle(document.getElementById("MainContentDiv"), "padding-left"));
    newWidth -= pxToInt(getStyle(document.getElementById("MainContentDiv"), "margin-left"));
    newWidth -= pxToInt(getStyle(document.getElementById("MainContentDiv"), "margin-right"));
    
    var border;
    try{
         newWidth -= pxToInt(getComputedStyle(document.getElementById("MainContentDiv"), '').getPropertyValue('border-right-width'));
         newWidth -= pxToInt(getComputedStyle(document.getElementById("MainContentDiv"), '').getPropertyValue('border-left-width'));
         newWidth -= pxToInt(getComputedStyle(document.getElementById("LeftContentDiv"), '').getPropertyValue('border-right-width'));
         newWidth -= pxToInt(getComputedStyle(document.getElementById("LeftContentDiv"), '').getPropertyValue('border-left-width'));
    } catch(e){
        newWidth -= document.getElementById("MainContentDiv").currentStyle.borderLeftWidth;
        newWidth -= document.getElementById("MainContentDiv").currentStyle.borderRightWidth;
        newWidth -= document.getElementById("LeftContentDiv").currentStyle.borderLeftWidth;
        newWidth -= document.getElementById("LeftContentDiv").currentStyle.borderRightWidth;
    }
    
   
    document.getElementById("MainContentDiv").style.width = newWidth + "px";
    */
    
}


//Main body onload event
function OnLoaded() {
    //fixMinWidthForIE();
    OnResized();
    CollapsersSkipLastState = false;
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    //setTimeout('GetLastUpdate()', 5000);
}


function validateTime(Elm) {
    var tmpStr = Elm.value;
    var RegStr = /^(\d{1,2}):(\d{1,2})((:|\.)\d*)?$/;
    var re = new RegExp(RegStr);
    var m = re.exec(tmpStr);
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    if (m != null) {
        while (m[1].charAt(0) == '0') {
            m[1] = m[1].substr(1);
        }
        while (m[2].charAt(0) == '0') {
            m[2] = m[2].substr(1);
        }        
        hours = parseInt(m[1]);
        minutes = parseInt(m[2]);
    }
    if (hours > 24) {
        hours = 23;
    }
    if (minutes > 59) {
        minutes = 59;
    }
    if (hours < 10) {
        hours = '0' + hours;
    }
    if (minutes < 10) {
        minutes = '0' + minutes;
    } 
    Elm.value = hours + ':' + minutes;
}










function GetLastUpdate() {
    if ((LastUpdate != '') && (LastUpdate != null)) {
        //document.getElementById("UpdatesAvailable").innerHTML = "Getting status";
        var Url = "GetLastUpdate.ashx?lastupdate=" + encodeURI(document.getElementById(LastUpdate).value);

        xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = ProcessRequest;
        xmlHttp.open("GET", Url, true);
        xmlHttp.send(null);
    } else {
        setTimeout('GetLastUpdate()', 5000);    
    }
}

function ProcessRequest() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        if (xmlHttp.responseText == "Not found") {
        }
        else {
            var info = xmlHttp.responseText;
            if (info != '' && info != '-1' && info != document.getElementById("UpdatesAvailable").innerHTML) {
                document.getElementById("UpdatesAvailable").innerHTML = info;
                document.getElementById("UpdatesAvailable").style.display = 'block';
            } else if (info == '' || info == '-1') {
                document.getElementById("UpdatesAvailable").style.display = 'none';                        
            }
            setTimeout('GetLastUpdate()', 5000);
        }
    } else if (xmlHttp.readyState == 4 && xmlHttp.status != 200) {
        setTimeout('GetLastUpdate()', 5000);
    }
}

function updateBeskrivelse() {
    if (typeof editor1 != 'undefined') { editor1.updateElement() }
}
