﻿/// <reference path="jquery.js" />

/**************/
/*** Helper ***/
/**************/
var isIE6 = false;
if ($.browser.msie && parseInt($.browser.version, 10) == 6) { isIE6 = true; }

var isIE7 = false;
if ($.browser.msie && parseInt($.browser.version, 10) == 7) { isIE7 = true; }

var isIE8 = false;
if ($.browser.msie && parseInt($.browser.version, 10) == 8) { isIE7 = true; }

var isSafari = false;
if ($.browser.safari) { isSafari = true; }

var isMozilla = false;
if ($.browser.mozilla) { isMozilla = true; }

var isMobile = false;
if (navigator.userAgent.match(/iPhone/i)) { isMobile = true; }
if (navigator.userAgent.match(/iPod/i)) { isMobile = true; }
if (navigator.userAgent.match(/iPad/i)) { isMobile = true; }
if (navigator.userAgent.match(/Android/i)) { isMobile = true; }

// Vorangestelle Null erzeugen
function addZero(zahl) {
    if (zahl < 10) { zahl = "0" + zahl.toString() }
    return zahl;
}


// Check, ob Browser "onhashchange"-Event unterstützt
function canHashChange() {
    return ("onhashchange" in window) && (!isIE7);
}


// Bilder der aktuellen Seite   
function getImages(siteurl) {
    var arrImages;
    for (var i = 0; i < Options.length; i++) {
        if (Options[i][siteurl] != null) {
            arrImages = Options[i][siteurl][0].split(";");
        }
    }
    if (arrImages == null) {
        arrImages = Options[0]['/default.aspx'][0].split(";");
    }
    return arrImages;
}


// Bilder Ordner
function getImageFolder(siteurl) {
    var cFolder;
    for (var i = 0; i < Options.length; i++) {
        if (Options[i][siteurl] != null) {
            cFolder = Options[i][siteurl][2];
        }
    }
    if (cFolder == undefined) {
        cFolder = Options[0]['/default.aspx'][2];
    }
    return cFolder;
}


// Farb-Theme der aktuellen Seite
function getColor(siteurl) {
    var cColor;
    for (var i = 0; i < Options.length; i++) {
        if (Options[i][siteurl] != null) {
            cColor = Options[i][siteurl][1];
        }
    }
    if (cColor == undefined) {
        cColor = Options[0]['/default.aspx'][1];
    }
    return cColor;
}


// Aktuelle URL ohne Hash
function getURL() {
    var tUrl = document.location.hash.replace(/#/g, "");
    // Query-String eliminieren
    if (tUrl.indexOf("?") > -1) { tUrl = tUrl.substring(0, tUrl.indexOf("?")) }
    // DEfault
    if (tUrl == "") { tUrl = "/"; }
    return tUrl;
    fitScrollingContent();
}

function getIe6URL() {
    var tUrl = document.location.pathname.replace("/" + langKey, "");
    if (tUrl == "") { tUrl = "/"; }
    return tUrl;
}

function getREL() {
    return getURL().replace(/\//g, "").replace(/.aspx/g, "");
}

// Sidebar
function sidebar() {
    $("#lnkOnlineBuchen").click(function () {
        $("#sidebar").fadeIn(150);
        addLayer();
    });

    // iFrame laden
    $("#lnkSubmitSuchenBuchen").click(function () {
        suchenUndBuchen();
        // Loading GIF anzeigen
        $(this).addClass("isLoading");

    });

    // Aktuelles Datum setzen
    var cDate = new Date();
    $("select.drpJahr:first").val(cDate.getFullYear());
    $("#drpMonat").val(addZero(cDate.getMonth() + 1));
    $("#drpTag").val(addZero(cDate.getDate()));
}

// Suchen und Buchen
function suchenUndBuchen() {
    $("#lnkOnlineBuchen").css("display", "none");

    var from = $("#txtAnreise").val();
    var till = $("#txtAbreise").val();

    var dateFrom = new Date(parseInt(from.substring(6, 10),10), parseInt(from.substring(3, 5),10) - 1, parseInt(from.substring(0, 2),10));
    var dateTill = new Date(parseInt(till.substring(6, 10), 10), parseInt(till.substring(3, 5), 10) - 1, parseInt(till.substring(0, 2), 10));

    var nSrc = "http://web.deskline.net/hunsrueck_h/" + langKey + "/qfind_accommodation?" +
               "qfdArrivalDate=" + dateFrom.getFullYear() +
                                   addZero(dateFrom.getMonth() + 1) +
                                   addZero(dateFrom.getDate()) + "&" +
                "tempDay=" + dateFrom.getDate() + "&" +
                "tempMonth=" + parseInt(dateFrom.getMonth() + 1,10) + "&" +
                "tempYear=" + dateFrom.getFullYear() + "&" +
                "qfdNights=" + (dateTill - dateFrom) / 86400000 + "&" +
                "qfdNumberOfUnits=" + $("#drpZimmer").val() + "&" +
                "qfdAdults=" + $("#drpPersonen").val() + "&" +
                "qfdAccommodationType=" + $("#drpType").val() + "&" +
                "qfdOnlyBookable=1"

    addIframe(nSrc, null, null);
}

// Aktuelle Scrollposition
function getScroll() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant   
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant   
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode   
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

// Prepariert alle externen Links
//function iFrameLinks() {
//    $("#nav a[href^='http://web.deskline']").each(function () {
//        $(this).unbind("click");
//        $(this).attr("nHref", $(this).attr("href"));
//        $(this).removeAttr("href");

//        $(this).click(function () {
//            window.scrollTo(0, 0);
//            var cLink = $(this);
//            addIframe(cLink.attr("nHref"), null, null);
//            $("#lnkOnlineBuchen").css("display", "none");
//        });
//    });
//}

function makeItIFrame(link) {
    link.unbind("click");
    link.attr("nHref", link.attr("href"));
    link.removeAttr("href");

    link.click(function () {
        window.scrollTo(0, 0);
        var cLink = $(this);
        addIframe(cLink.attr("nHref"), null, null);
        $("#lnkOnlineBuchen").css("display", "none");
    });
}


// Interne Links in AJAX-Links konvertieren
function makeItAjax(link) {
    if (!link.hasClass("lnkLanguage")) {
        if (link.attr("href") != null) {
            var nHref = link.attr("href").replace("http://" + location.hostname, "").replace("/#", "");
            link.attr("href", nHref.substring(0, 4) + "#" + nHref.substring(3, nHref.length));
        }        
    }    
}

// Alte Bilder Löschen (DOM bereinigen)
function cleanImages(rel) {
    $("#images img").each(function () {
        if ($(this).attr("rel") != rel) {
            $(this).remove();
        }
    });
}

// Aktive Knoten setzen
function setActiveNodes() {
    // Aktiver Link
    $("#nav ul ul").hide();   
    $("#nav a").removeClass("active");
    $("#nav li").removeClass("active")
    link = $("#nav a[href='/" + langKey + "/#" + getURL() + "']");
  
    // Aktives Listenelement
    var cActiveLI = link.parent("li");
    cActiveLI.addClass("active");
    cActiveLI.find(">ul").show();
  
    var ccActiveLI = cActiveLI.parent().parent("li").first();
    ccActiveLI.addClass("active");
    ccActiveLI.find(">ul").show();  

    var cccActiveLI = ccActiveLI.parent().parent("li").first();
    cccActiveLI.addClass("active");
    cccActiveLI.find(">ul").show();  

    var ccccActiveLI = cccActiveLI.parent().parent("li").first();
    ccccActiveLI.addClass("active");
    ccccActiveLI.find(">ul").show(); 
}




// iPad
function fitMobile() {
    if (isMobile) {
        $("#header,#images").css("position", "absolute");
        $("#content,#footer").css("position", "relative");
        $("#footer").css("display", "none");
        $("#nav ul").width("690px");
    }
}

// iFrame resize
function fitContent() {
    $("#iFrame").height($(window).height() - 175);
    $("#iFrame").width($(window).width());
}

// Layer einblenden
function addLayer() {
    $("#layer").remove();
    $("body").prepend($("<div>")
            .css("display", "none")
            .attr("id", "layer")
            .height($(document).height())
            .width($(document).width())
            .fadeTo(150, 0.5).click(function () {
                removeLayer();
            })
     );
}

// Layer ausblenden
function removeLayer() { 
    $("#layer,#sidebar").fadeOut(150, function () {
        $("#layer").remove();
    });
}

// iFrame erstellen
function addIframe(path, iWidth, iHeight) {
    removeLayer();

    if (iWidth == null) { iWidth = $(window).width(); }
    if (iHeight == null) { iHeight = $(window).height() - 175; }
    var cContainer = $("body");

    // iFrame laden
    $("#iframeHeader").click(function () {
        removeIframe();
        $("#lnkOnlineBuchen").css("display", "block");

    }).fadeIn();

    $("<iframe>")
        .attr("src", path)
        .attr("id", "iFrame")
        .attr("frameborder", "0")
        .css("position", "absolute")
        .css("background-color", "#ffffff")
        .css("top", "150px")
        .css("z-index", "10000")
        .css("display", "none")
        .css("overflow-y", "scroll")
        .height(iHeight)
        .width(iWidth)
        .appendTo(cContainer)
        .delay(500)
        .fadeIn(500, function () {
            $(".iContent").fadeOut();
        });    
}



// iFrame entfernen
function removeIframe() {
    $("#ajax-loader-content").remove();
    $(".iContent").fadeIn();
    $("iframe,#iframeHeader,#imagesHeader").fadeOut(300, function () {
        $("iframe").remove();
        $("#lnkOnlineBuchen").css("display", "block");
    });    
    $(".isLoading").removeClass("isLoading");
}

// Routenplaner
function calculateRoute() {
    // Standard-Text
    $("#txtStart").val(textStart);
    $("#txtZiel").val(textEnde);

    // Blur
    $("#txtStart").blur(function () {
        if ($(this).val() == "") { $(this).val(textStart) }
    });
    
    $("#txtZiel").blur(function () {
        if ($(this).val() == "") { $(this).val(textEnde) }
    });

    // Focus
    $("#txtStart").focus(function () {
        if ($(this).val() == textStart) { $(this).val("") }
    });

    $("#txtZiel").focus(function () {
        if ($(this).val() == textEnde) { $(this).val("") }
    });

    // Route berechnen
    $("#lnkCalculateRoute").click(function () {
        if ($("#txtStart").val() == textStart || $("#txtStart").val() == "") {
            $("#txtStart").addClass("active");
        } else {
            $("#txtStart").removeClass("active");
        }


        if ($("#txtZiel").val() == textEnde || $("#txtZiel").val() == "") {
            $("#txtZiel").addClass("active");
        } else {
            $("#txtZiel").removeClass("active");
        }

        if ((document.getElementById("txtZiel"))) {
            if ($("#txtStart").val() != textStart && $("#txtZiel").val() != textEnde) {
                if ($("#txtStart").val() != "" && $("#txtZiel").val() != "") {
                    window.open("http://maps.google.de/maps?f=d&hl=de&saddr=" + $("#txtStart").val() + "&daddr=" + $("#txtZiel").val());
                }
            }
        } else {
            if ($("#txtStart").val() != textStart) {
                if ($("#txtStart").val() != "" && $("#txtZiel").val() != "") {
                    window.open("http://maps.google.de/maps?f=d&hl=de&saddr=" + $("#txtStart").val() + "&daddr=Gebäude 663, 55483 Hahn-Flughafen (Hunsrück-Touristik GmbH)");
                }
            }
        }
    });
}

// Schriftgröße anpassen
function fontSize() {
    $("#slider").slider({ min: 0,
        max: 11,
        step: 0.1,
        orientation: 'vertical',
        stop: function (event, ui) {  }
    }).bind("slide", function (event, ui) {
        $("body,input[type=text],select").not("input#at16filt").not("span.welcome").css("font-size", 13 + ui.value + "px");
        $("body,input[type=text],select").not("input#at16filt").not("span.welcome").css("line-height", 19 + ui.value + "px");
        $.cookie("allfs", parseInt(13 + ui.value));
        $.cookie("alllh", parseInt(19 + ui.value));
        $("input[type=text]").not("input#at16filt").height(16 + ui.value + "px");
        $.cookie("inpH", parseInt(16 + ui.value));
        $("select").height(25 + ui.value + "px");
        $("select").width(140 + (ui.value * 2) + "px");
        $.cookie("selectW", parseInt(140 + (ui.value * 2)));
        $.cookie("selectH", parseInt(25 + ui.value));

        $("p.small, p.teaser").css("font-size", 14 + ui.value + "px");
        $("p.small, p.teaser").css("line-height", 22 + ui.value + "px");
        $.cookie("pfs", parseInt(14 + ui.value));
        $.cookie("plh", parseInt(22 + ui.value));
        $("h1").css("font-size", 24 + ui.value + "px");
        $("h1").css("line-height", 28 + ui.value + "px");
        $.cookie("h1fs", parseInt(24 + ui.value));
        $.cookie("h1lh", parseInt(28 + ui.value));
        $("h2").css("font-size", 16 + ui.value + "px");
        $("h2").css("line-height", 21 + ui.value + "px");
        $.cookie("h2fs", parseInt(16 + ui.value));
        $.cookie("h2lh", parseInt(21 + ui.value));
        $("h3").css("font-size", 16 + ui.value + "px");
        $("h3").css("line-height", 21 + ui.value + "px");
        $.cookie("h3fs", parseInt(16 + ui.value));
        $.cookie("h3lh", parseInt(21 + ui.value));

//        $("#nav ul").css("font-size", 15 + ui.value + "px");
//        $("#nav ul").css("line-height", 30 + ui.value + "px");
        fitNav();
    });

    if (langKey == "de") {
        $("#slider a").attr("title", "Schriftgröße");
    } else if (langKey == "nl") {
        $("#slider a").attr("title", "Letterkorps");
    } else if (langKey == "fr") {
        $("#slider a").attr("title", "Taille de charactères");
    } else if (langKey == "es") {
        $("#slider a").attr("title", "Tamaño de lettra");
    } else {
        $("#slider a").attr("title", "Font Size");
    } 
}


