﻿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) {
        //ev.preventDefault();
        var modalPopupBehavior = $find('programmaticModalPopupBehavior');
        var iframe = document.getElementById("ctl00_ifBookingView");
        iframe.src = ev;
        modalPopupBehavior.show();
    }

    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;
    }