﻿var WeatherLeft = 0;
var WeatherTop = 0;
var WeatherWidth = 0;
var WeatherHeight = 0;
var WeatherShown = false;
var WeatherActionAllowed = true;

// FT : Code to track mouse movements

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all ? true : false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = TrackMouse;

var mouseX = 0
var mouseY = 0

function TrackMouse(e) {
    if (document.body != null) {
        if (IE) { // grab the x-y pos.s if browser is IE
            mouseX = event.clientX + document.body.scrollLeft
            mouseY = event.clientY + document.body.scrollTop
        } else {  //
            mouseX = e.pageX
            mouseY = e.pageY
        }

        if (mouseX < 0) {
            mouseX = 0
        }
        if (mouseY < 0) {
            mouseY = 0
        }
    }

    return true
}


var locCount = 0;
var weatherIndex = 0;
function LoadWeather() {
    weatherIndex = 0;
    LoadWeatherByIndex(weatherIndex);

    setTimeout('LoadNewLocation()', 5000);
}


function OpenHourByHourWindow(url) {
    window.open(url, "hbh", "status=0;width=640;height=480", true);
    return false;
}

function LoadWeatherByIndex(index) {
    var cwLines, cwValues;
    var hf = document.getElementById("ctl00_Toolbar_WeatherControl_hfCurrentWeather");
    var destination = document.getElementById("CurrentWeather");
    if ((hf != null) && (destination != null)) {
        var destinations = hf.value.split("|");

        if (locCount == 0) {
            locCount = destinations.length;
        }

        if (destinations.length > index) {
            cwLines = destinations[index].split("|");
            if (cwLines.length > 0) {
                cwValues = cwLines[0].split(";");
                SetCurrentWeather(destination, cwValues[0], cwValues[1], cwValues[2]);
            }
        }
    }
    hf = document.getElementById("ctl00_Toolbar_WeatherControl_hfForecasts");
    if (hf != null) {
        destinations = hf.value.split("@");
        if (destinations.length > 0) {
            var vals = destinations[index].split("¤");
            var hbh = document.getElementById("hbh");
            if ((vals.length > 1) && (vals[1] != "")) {
                hbh.childNodes[0].nodeValue = "Time for time";
                //hbh.attributes["onclick"].value = "window.open(\"" + vals[1] + "\",\"hbh\",\"status=0,width=740,height=280\");return false;";
                hbh.onclick = function () {
                    window.open(vals[1], "hbh", "status=0,width=740,height=280", true);
                    return false;
                }
            }
            else {
                hbh.href = "#";
                hbh.innerText = "";
            }
            cwLines = vals[0].split("|");
            for (var x = 0; x < cwLines.length; x++) {
                var line = cwLines[x];
                if (line != "") {
                    cwValues = line.split(";");
                    var firstSpan = document.getElementById("forecast" + (x + 1) + "1");
                    firstSpan.childNodes[0].nodeValue = cwValues[0];
                    var secondSpan = document.getElementById("forecast" + (x + 1) + "2");
                    secondSpan.className = "degrees " + cwValues[1];
                    //secondSpan.innerText = cwValues[2];
                    secondSpan.childNodes[0].nodeValue = cwValues[2];
                    var thirdSpan = document.getElementById("forecast" + (x + 1) + "3");
                    thirdSpan.style.background = "transparent url(" + cwValues[3] + ") left top no-repeat";
                    //thirdSpan.style.backgroundRepeat = "no-repeat";
                    thirdSpan.innerHTML = cwValues[4] + " m/s";
                }
            }
        }
    }
}


function LoadNewLocation() {
    if (WeatherShown == false) {
        weatherIndex++;
        if (weatherIndex >= locCount) {
            weatherIndex = 0;
        }

        LoadWeatherByIndex(weatherIndex);
    }

    setTimeout('LoadNewLocation()', 5000);
}

function SetCurrentWeather(destination, location, style, temperature) {
    if (location == null) {
        location = "";
    }

    if (style == null) {
        style = "";
    }

    if (temperature == null) {
        temperature = "";
    }

    destination.innerHTML = location + " <SPAN class=\"degrees " + style + "\">" + temperature + "</SPAN>";
}

function ShowForecast() {
    if (WeatherShown == false) {
        var fdiv = document.getElementById("forecast");
        if (fdiv.style.display == "") {
            return;
        }

        // Check if there are any values to show
        var hf = document.getElementById("ctl00_Toolbar_WeatherControl_hfCurrentWeather");
        if (hf.value == "") {
            return;
        }

        //window.status = 'ShowForecast() 1';

        // Block show/hide until effect is finished
        WeatherActionAllowed = false;

        ShowWeatherForecast(true);
        var current = document.getElementById('CurrentWeatherSpan');
        var currentPos = findPos(current);
        var forecast = document.getElementById("forecast");
        var forecastPos = findPos(forecast);
        WeatherLeft = Math.min(currentPos[0], forecastPos[0]);
        WeatherTop = Math.min(currentPos[1], forecastPos[1]);
        WeatherWidth = forecast.scrollWidth;
        WeatherHeight = current.scrollHeight + forecast.scrollHeight;

        //window.status = 'ShowForecast() 2';

        setTimeout('CheckIfForecastShouldBeClosed()', 100);

        //window.status = 'ShowForecast() 3';

        WeatherShown = true;
        WeatherActionAllowed = true;

        //window.status = 'ShowForecast() 4';
    }
}

function ShowWeatherForecast(show) {
    var div = document.getElementById("forecast");
    if (show) {
        div.style.display = "";
    }
    else {
        div.style.display = "none";
    }
}

function CheckIfForecastShouldBeClosed() {
    if ((mouseX >= WeatherLeft) && (mouseY >= (WeatherTop - 10)) && (mouseX <= (WeatherLeft + WeatherWidth)) && (mouseY <= (WeatherTop + WeatherHeight))) {
        WeatherShown = true;
        setTimeout('CheckIfForecastShouldBeClosed()', 100);
        //window.status = "WS : " + WeatherShown;
        //window.status = "Innenfor 1 (" + mouseX + " >= " + WeatherLeft + ") && (" + mouseY + " >= " + WeatherTop + ") && (" + mouseX + " <= (" + WeatherLeft + " + " + WeatherWidth + ")) && (" + mouseY + " <= (" + WeatherTop + " + " + WeatherHeight + "))";
    }
    else if (WeatherActionAllowed) {
        // Block show/hide until effect is finished
        WeatherActionAllowed = false;

        ShowWeatherForecast(false);
        WeatherShown = false;
        WeatherActionAllowed = true;
        //window.status = " Lukker (" + mouseX + " >= " + WeatherLeft + ") && (" + mouseY + " >= " + WeatherTop + ") && (" + mouseX + " <= (" + WeatherLeft + " + " + WeatherWidth + ")) && (" + mouseY + " <= (" + WeatherTop + " + " + WeatherHeight + "))";
    }
    else {
        //window.status = 'Innenfor 2 WeatherActionAllowed = ' + WeatherActionAllowed;
        setTimeout('CheckIfForecastShouldBeClosed()', 100);
    }
}

function findPos(obj) {
    var curleft = curtop = 0;

    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }

    return [curleft, curtop];
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function showModalPopupViaClient(ev) {
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    var iframe = document.getElementById("ctl00_ifBookingView");
    if (iframe == null) {
        iframe = document.getElementById("ctl00_ctl00_ifBookingView");
    }
    if (StartWidth(ev.toLowerCase(), "http://")) {
        iframe.src = ev;
    }
    else {
        iframe.src = "http://" + ev;
    }
    modalPopupBehavior.show();
}

function getBrowserWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    return { width: myWidth, height: myHeight };
}


function addDate(dateObject, numDays) {
    dateObject.setDate(dateObject.getDate() + numDays);
    return dateObject;
}

function hovdenBookingFromDateChanged(s, e) {
    var dt = dtHovdenBookFrom.GetDate();
    //dt = addDate(dt, 1);
    dtHovdenBookTo.SetDate(dt);
}

function hovdenBookingToDateChanged(s, e) {

}

function CheckBookingAvail() {
    var height = 850;
    var width = 1020;

    var fromDate = dtHovdenBookFrom.GetDate();
    var toDate = dtHovdenBookTo.GetDate();
    if ((fromDate == null) || (toDate == null)) {
        alert("Dates must be supplied before search can be performed");
        return false;
    }

    var language = "17";
    if (document.getElementById("ctl00_ctl00_Content_leftMenu_travelClickLanguageID") != null) {
        language = document.getElementById("ctl00_ctl00_Content_leftMenu_travelClickLanguageID").value;
    }
    var fromStr = fromDate.format("M%2Fd%2Fyyyy");
    var toStr = toDate.format("M%2Fd%2Fyyyy");
    var rooms = document.getElementById("ddHovdenBookingRooms")[document.getElementById("ddHovdenBookingRooms").selectedIndex].text;
    var adults = document.getElementById("ddHovdenBookingAdults")[document.getElementById("ddHovdenBookingAdults").selectedIndex].text;
    var children = document.getElementById("ddHovdenBookingChildren")[document.getElementById("ddHovdenBookingChildren").selectedIndex].text;
    var roomType = document.getElementById("hovdenBookingCat")[document.getElementById("hovdenBookingCat").selectedIndex].value;

    var url = "https://reservations.ihotelier.com/areasearch/default.cfm?PortalSymbol=hvn&ViewQty=1&SortBy=1&CurrentPage=1&sType=1&sInput=GeoID_6&ChangeLevel=6";
    url = url + "&GeoID_6=" + roomType + "&btnLevel=0&languageid=" + language + "&HotelID=All&DateIn=" + fromStr + "&DateOut=" + toStr;
    url = url + "&Rooms=" + rooms + "&Adults=" + adults + "&Children=" + children + "&Channel=C_Room&RateType=&PromoCode=";
    //ev = ev.replace(/%3a/gi, ":").replace(/%2f/gi, "/").replace(/%3f/gi, "?").replace(/%3d/gi, "&");

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');

    var panel = document.getElementById("ctl00_Panel1");
    if (panel == null) {
        panel = document.getElementById("ctl00_ctl00_Panel1");
    }
    panel.style.height = height;
    panel.style.width = width;

    if (url.indexOf('ihotelier') != -1) {
        panel.style.backgroundcolor = '#3b393c';
    }
    else {
        panel.style.backgroundcolor = '#ffffff';
    }

    var iframe = document.getElementById("ctl00_ifBookingView");
    if (iframe == null) {
        iframe = document.getElementById("ctl00_ctl00_ifBookingView");
    }

    iframe.src = url;

    //modalPopupBehavior.show();
    window.open(url, "hbh", "scrollbars=1,status=1,toolbar=1,resizable=1,width=" + width + ",height=" + height, true);
}

function showModalPopupViaClientHW(ev, height, width, openInPopup) {
    ev = ev.replace(/%3a/gi, ":").replace(/%2f/gi, "/").replace(/%3f/gi, "?").replace(/%3d/gi, "&");
    var size = getBrowserWindowSize();
    if ((size.height < height) || (size.width < width)) {
        openInPopup = true;
    }

    if (openInPopup == false) {
        var modalPopupBehavior = $find('programmaticModalPopupBehavior');

        var panel = document.getElementById("ctl00_Panel1");
        if (panel == null) {
            panel = document.getElementById("ctl00_ctl00_Panel1");
        }
        panel.style.height = height;
        panel.style.width = width;

        if (ev.indexOf('ihotelier') != -1) {
            panel.style.backgroundcolor = '#3b393c';
        }
        else {
            panel.style.backgroundcolor = '#ffffff';
        }

        var iframe = document.getElementById("ctl00_ifBookingView");
        if (iframe == null) {
            iframe = document.getElementById("ctl00_ctl00_ifBookingView");
        }
        if (StartWidth(ev.toLowerCase(), "http://")) {
            iframe.src = ev;
        }
        else {
            iframe.src = "http://" + ev;
        }
        modalPopupBehavior.show();
    }
    else {
        var url = ev;
        if (StartWidth(url.toLowerCase(), "http://") == false) {
            url = "http://" + url;
        }
        window.open(url, "hbh", "scrollbars=1,status=1,toolbar=1,resizable=1,width=" + width + ",height=" + height, true);
    }
}

function StartWidth(str, expression) {
    return str.match("^" + expression) == expression;

}

function hideModalPopupViaClient() {
    //ev.preventDefault();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
}



// Tellus Image toggling
function initTellusToggle() {
    ShowHideTellusNavigators();
}

function ShowHideTellusNavigators() {
    var current = document.getElementById("tellusCurrImg");
    var currImg = current.src;
    var i1 = document.getElementById("hidTellusImg1");
    var i2 = document.getElementById("hidTellusImg2");
    if ((i1 != null) && (i2 != null)) {
        var currIndex = -1;
        var cnt = -1;
        var i;
        for (i = 1; i < 100; i = i + 1) {
            var c = document.getElementById("hidTellusImg" + i);
            if (c == null) {
                cnt = i - 1;
                break;
            }

            if (c.value == currImg) {
                currIndex = i;
            }
        }

        var prev = document.getElementById("imgTellus_previous");
        var next = document.getElementById("imgTellus_next");
        if (currIndex > 1) {
            prev.style.visibility = "";

            prev.onclick = function () {
                imgTellus_prev();
            }
        }
        else {
            //prev.style.display = "none";
            prev.style.visibility = "hidden";
        }

        if (currIndex < cnt) {
            next.style.display = "";

            next.onclick = function () {
                imgTellus_next();
            }
        }
        else {
            next.style.display = "none";
        }
    }
    else {
        var prev = document.getElementById("imgTellus_previous");
        var next = document.getElementById("imgTellus_next");

        prev.style.visibility = "hidden";
        next.style.visibility = "hidden";
    }
}

function imgTellus_prev() {
    var ci = GetTellusCurrentImageIndex();
    var pi = ci - 1;

    var sp = document.getElementById("hidTellusImg" + pi);

    var current = document.getElementById("tellusCurrImg");


    current.src = sp.value;

    ShowHideTellusNavigators();
}

function imgTellus_next() {
    var ci = GetTellusCurrentImageIndex();
    var ni = ci + 1;

    var sn = document.getElementById("hidTellusImg" + ni);

    var current = document.getElementById("tellusCurrImg");

    current.src = sn.value;

    ShowHideTellusNavigators();
}

function GetTellusImageCount() {
    var cnt = -1;
    for (i = 1; i < 100; i = i + 1) {
        var c = document.getElementById("hidTellusImg" + i);
        if (c == null) {
            cnt = i - 1;
            break;
        }
    }

    return cnt;
}

function GetTellusCurrentImageIndex() {
    var current = document.getElementById("tellusCurrImg");
    var currImg = current.src;

    var currIndex = -1;
    for (var i = 1; i < 100; i = i + 1) {
        var c = document.getElementById("hidTellusImg" + i);
        if (c == null) {
            break;
        }

        if (c.value == currImg) {
            currIndex = i;
        }
    }

    return currIndex;
}

function hideModalBookingPopupViaClient() {
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
}

function CheckIfBookingSubTypeSelected() {
    var x = 1;
}



// Tellus Image toggling
function initHovdenTellusToggle() {
    ShowHideHovdenTellusNavigators();
}

function ShowHideHovdenTellusNavigators() {
    var current = document.getElementById("tellusCurrImg");
    var currImg = current.src;
    var i1 = document.getElementById("hidTellusImg1");
    var i2 = document.getElementById("hidTellusImg2");
    if ((i1 != null) && (i2 != null)) {
        var currIndex = -1;
        var cnt = -1;
        var i;
        for (i = 1; i < 100; i = i + 1) {
            var c = document.getElementById("hidTellusImg" + i);
            if (c == null) {
                cnt = i - 1;
                break;
            }

            if (c.value.split('¤')[0] == currImg) {
                currIndex = i;
            }
        }

        var prev = document.getElementById("imgTellus_previous");
        var next = document.getElementById("imgTellus_next");
        if (currIndex > 1) {
            prev.style.visibility = "";

            prev.onclick = function () {
                imgHovdenTellus_prev();
            }
        }
        else {
            //prev.style.display = "none";
            prev.style.visibility = "hidden";
        }

        if (currIndex < cnt) {
            next.style.display = "";

            next.onclick = function () {
                imgHovdenTellus_next();
            }
        }
        else {
            next.style.display = "none";
        }
    }
    else {
        var prev = document.getElementById("imgTellus_previous");
        var next = document.getElementById("imgTellus_next");

        prev.style.visibility = "hidden";
        next.style.visibility = "hidden";
    }
}

function imgHovdenTellus_prev() {
    var ci = GetHovdenTellusCurrentImageIndex();
    var pi = ci - 1;

    var sp = document.getElementById("hidTellusImg" + pi);
    var vals = sp.value.split("¤");

    try {
        document.getElementById('tellusImgName').innerText = vals[1];
        document.getElementById('tellusImgCredit').innerText = vals[2];
    } catch (err) {

    }

    var current = document.getElementById("tellusCurrImg");


    current.src = vals[0];

    ShowHideHovdenTellusNavigators();
}

function imgHovdenTellus_next() {
    var ci = GetHovdenTellusCurrentImageIndex();
    var ni = ci + 1;

    var sn = document.getElementById("hidTellusImg" + ni);
    var vals = sn.value.split("¤");

    try {
        document.getElementById('tellusImgName').innerText = vals[1];
        document.getElementById('tellusImgCredit').innerText = vals[2];
    } catch (err) {

    }

    var current = document.getElementById("tellusCurrImg");

    current.src = vals[0];

    ShowHideHovdenTellusNavigators();
}

function GetHovdenTellusImageCount() {
    var cnt = -1;
    for (i = 1; i < 100; i = i + 1) {
        var c = document.getElementById("hidTellusImg" + i);
        if (c == null) {
            cnt = i - 1;
            break;
        }
    }

    return cnt;
}

function GetHovdenTellusCurrentImageIndex() {
    var current = document.getElementById("tellusCurrImg");
    var currImg = current.src;

    var currIndex = -1;
    for (var i = 1; i < 100; i = i + 1) {
        var c = document.getElementById("hidTellusImg" + i);
        if (c == null) {
            break;
        }

        var url = c.value.split("¤")[0];
        if (url == currImg) {
            currIndex = i;
        }
    }

    return currIndex;
}

function openUrl(gotoUrl) {
    var baseUrl = window.location.href.substring(0, window.location.href.indexOf(window.location.pathname) + 1);
    var langTag = "";
    var url = window.location.pathname;
    if (url.indexOf("/") > -1) {
        url = url.substring(url.indexOf("/") + 1);

        if (url.indexOf("/") > -1) {
            url = url.substring(0, url.indexOf("/"));

            if ((url == "en") || (url == "no") || (url == "de")) {
                langTag = url + "/";
            }
        }
    }

    window.location = baseUrl + langTag + gotoUrl;
}
