﻿/// <reference path="jquery.js" />
/// <reference path="http://maps.google.com/maps/api/js?sensor=false" />

// #Scrollframe + wSpace = .content -höhe (weißer Container)
var wSpace = 145;

// Min. Gesamtraum ober- und unterhalb .content (weißer Container)
var cSpace = 151;

// Unternavigationsseiten
var corrector = 103;
if (isIE6) {corrector = 130; }

// Padding bottom mit Scrollbar
var dynPaddingBottom = 28;

// Prüft, ob der angezeigte Inhalt der URL entspricht
function checkContentURL() {
    var baseURL = document.location.protocol + "//" + document.location.hostname + "/";
    if (document.location != baseURL) {
           
        $.cookie("href", document.location);
        if ($.cookie("href").indexOf("#") > 0) {
            var cUrl = $.cookie("href").substring($.cookie("href").indexOf("#") + 1, $.cookie("href").length);            
            if ($("#scrollframe").attr("rel") != cUrl) {
                loadContent(cUrl);                
            }
        } else {
             
            $("#MainNav").hide();
            $(".content").height($(".content").height() + $("#globalNav").height() + dynPaddingBottom);
            $("#globalNav").fadeIn(300);
            // Keine Content - 404
            if ($(".content").height() < 250) {
                clickBackLink();
            }
        }
    }
}

// Lädt Content dyn. nach
function loadContent(href) {
    var wHeight = parseInt($(window).height(), 10);
    $("#MainNav,#scrollframe").hide();
    var imageFolder = "";
    var contentHeight = 0;

    $.get(href + "?ajax=1", function (data) {
            

        $("#scrollframe").html("");
        var content = $(data).filter("#scrollframe").html();
        $("#scrollframe").html(content);
        var messDiv = $("<div></div>").hide().appendTo($("body"));
        messDiv.html(content);
        contentHeight = messDiv.height() + wSpace + $("#globalNav").height() + 4;
        if ($(content).find(".subnavItem").length > 0) {
            contentHeight = contentHeight + 12;
        }
        
        messDiv.remove();

        $(".content").animate({ height: contentHeight }, 300, function () {
            $("#scrollframe").fadeIn(300, function () {
                // Bei Anreise Karte laden, sonst Bilder
                if (href.indexOf("anreise.aspx") > 0) {
                    $("#supersize img").remove();
                    loadGoogleMaps();
                } else {
                    $("#supersize div").remove();
                    loadImages(getImages(href), false, "");
                }

                $("#globalNav").fadeIn(300);
                prepareForm();
                fitBody();
            });
        });

    }).error(function () { clickBackLink(); });
    
} 

// Navigation für Ajax-Requests vorbereiten
function prepareNavigation() {
    $("#MainNav a").each(function () {
        if ($(this).attr("href") != "/") {
            var nHref = $(this).attr("href");
            $(this).attr("href", "/#" + nHref);
            $(this).click(function () {
                var wHeight = parseInt($(window).height(), 10);
                // Current Page
                $("#scrollframe").attr("rel", nHref);
                $("#scrollframe").hide();
                $("#MainNav").hide();

                // Content laden
                $.get(nHref + "?ajax=1", function (data) {
                    $("#scrollframe").html("");
                    var content = $(data).filter("#scrollframe").html();
                    $("#scrollframe").html(content);

                    var messDiv = $("<div></div>").hide().appendTo($("body"));
                    messDiv.html(content);
                    var contentHeight = messDiv.height() + wSpace + $("#globalNav").height() + 5;
                    messDiv.remove();
                    
                    fitBody(contentHeight);

                    $(".content").animate({ height: contentHeight }, 300, function () {
                        $("#globalNav").fadeIn(300);
                        $("#scrollframe").fadeIn(300, function () {
                            // Bei Anreise Karte laden, sonst Bilder
                            if (nHref.indexOf("anreise.aspx") > 0) {
                                $("#supersize img").remove();
                                loadGoogleMaps();
                            } else {
                                $("#supersize div").remove();
                                loadImages(getImages(nHref), false, "");
                            }
                            prepareForm();
                        });
                    });
                });
            });
        }
    });    
 
           
    // Nächstes Bild  
    $(".lnkNext").click(function () {
        $("#supersize img").fadeOut(500, function () {
           // $("#pager img").removeClass("active");
        });
        $("#supersize img:eq(" + GetNextImageIndex() + ")").fadeIn(500, function () {
            // Pager
            var cImageSrc = $("#supersize img:visible:first").attr("src");
            var index = 0;
            for (var c = 0; c < $("#supersize img").length; c++) {
                if ($("#supersize img:eq(" + c + ")").attr("src") == cImageSrc) {
                    //$("#pager img:eq(" + c + ")").addClass("active");
                }
            }
        });
    });

    // Vorheriges Bild  
    $(".lnkPrev").click(function () {
        $("#supersize img").fadeOut(500, function () {
           // $("#pager img").removeClass("active");
        });
        $("#supersize img:eq(" + GetPrevImageIndex() + ")").fadeIn(500, function () {
            // Pager
            var cImageSrc = $("#supersize img:visible:first").attr("src");
            var index = 0;
            for (var c = 0; c < $("#supersize img").length; c++) {
                if ($("#supersize img:eq(" + c + ")").attr("src") == cImageSrc) {
                    //$("#pager img:eq(" + c + ")").addClass("active");
                }
            }
        });
    });
}



/**********/
/* HELPER */
/**********/
function getImages(siteurl) {    
    for (var i = 0; i < Bilder.length; i++) {
        if (Bilder[i][siteurl] != null) {            
            return Bilder[i][siteurl].split(";");
        }
    }
}

function getHeight(siteurl) {
    for (var i = 0; i < Bilder.length; i++) {
        if (Hoehen[i][siteurl] != null) {
            return Hoehen[i][siteurl];
        }
    }
}

// Anreise berechnen
var text = "Strasse, PLZ, Ort";
function send() {
    var feld = document.getElementById("rsrc0")
    if (feld.value == text) {
        feld.style.borderColor = "#be9f57";
        feld.style.backgroundColor = "#fff";
    }
    else {
        window.open('http://maps.google.de/maps?f=d&hl=de&saddr=' + document.getElementById('rsrc0').value + '&daddr=Auf´m+Eichelfeld+1,+54518+Dreis+(Waldhotel+Sonnora)');
    }
}

function fitBody(contentHeight) {
    if (contentHeight == null) { 
        contentHeight = $(".content").height();
    }
    
    if ($(window).height() < contentHeight + cSpace) {
        $("html,body,#supersize").css("min-height", contentHeight + cSpace);
        $("#vivre_logo").css("top", contentHeight + 24);
        $("#footer").css("top", contentHeight + cSpace - 56);
    } else {
        $("html,body,#supersize").css("min-height", $(window).height());
        $("#vivre_logo").css("top", $(window).height() - 127);
        $("#footer").css("top", $(window).height() - 54);
    }
    fitBackground();    
}


function prepareContent() {
    // $(".content").draggable();
    $("p, h3, #scrollframe ul").each(function () {
        $(this).click(function () {
            clickBackLink();
        });
    });
}


// Link zurück zur Navigation
function clickBackLink() {
    var messDiv = $("<div></div>").hide().appendTo($("body"));
    messDiv.html($("#MainNav").html());
    var contentHeight = messDiv.height() + 246;
    if (isMac) {contentHeight = contentHeight + 20; }
    messDiv.remove();
    $("#supersize div").remove();
    $("#globalNav").hide();
    $("#scrollframe").fadeOut(300, function () {
        $(".content").animate({ height: contentHeight }, 300, function () {
            // -- checkScrollbar(true);
            $("#MainNav").fadeIn(300);
            loadImages(getImages("/"), false,"");
            $("#scrollframe").html("");
            fitBody();
        });
    });
}

// Elemente der Subnavgation einblenden
function loadSubnavItem(item) {
    var messDiv = $("<div></div>").hide().width(230).appendTo($("body"));
    messDiv.html($("#gen" + item).html());
    if ($("#gen" + item).hasClass("genLast")) {
        corrector = 89;
        } else {
        corrector = 103;     
    }
    var nHeight = parseInt(messDiv.height()) + cSpace + ($(".subnavItem").length * 19) + corrector;
    messDiv.remove();

    $(".subnavItem div").slideUp(300);
    $(".content").animate({ height: nHeight }, 300, function () {
        $(".subnavItem a").removeClass("active");
        $("#gen" + item).slideDown(300, function () {
            $("#lnk" + item).addClass("active");
            loadImages($("#lnk" + item).attr("rel").split(";"), false, item);         
        });
    });

}

var isIE6 = ($.browser.msie && parseInt($.browser.version) == 6);
var isIE7 = ($.browser.msie && parseInt($.browser.version) == 7);
var isIE8 = ($.browser.msie && parseInt($.browser.version) == 8);
var isMozilla = $.browser.mozilla;

var isSafari = false;
if ($.browser.safari) { isSafari = true; }

var isMac = false;
if (navigator.userAgent.match(/Mac/i)) { isMac = 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; }


/**********/
/* EVENTS */
/**********/

$(window).resize(function () {
    fitBackground();
    fitBody();
});

$(window).load(function () {
    fitBackground();
    fitBody();
});

$(window).bind('hashchange', function () {
    checkContentURL();
    // prepareNavigation();
    prepareForm();
});

$(document).ready(function () {
    checkContentURL();
    prepareNavigation();
    prepareForm();
});

// Log all jQuery AJAX requests to Google Analytics
$(document).ajaxSend(function (event, xhr, settings) {
    if (typeof _gaq !== "undefined" && _gaq !== null) {
        _gaq.push(['_trackPageview', settings.url.replace(/\?ajax=1/g, "")]);
    }
});
