﻿var currentImageIndex = 0;

function getURL() {
    return document.location.hash.replace(/#/g, "");
}

function lnkHide() {
    $('#lnkExpand').removeClass('active');
    $('#lnkExpand').html('Mehr Infos');
    $('#ctext h2, #ctext h3, #ctext p').fadeOut(800);
    $('#ctext').slideUp(500, function () {
        if (isIE6) {
            $('#supersize').animate({ opacity: 1.00 }, 500);
        } else {
            $('#supersize').animate({ opacity: 1.00 }, 500, function () {
                $('body').css({ "background-color": "#fff" });
            });
        }
    });
    $('#supersize').css("cursor", "default");
}

function lnkExpand() {
    if (isIE6) {
        $('#lnkExpand').addClass('active');
        $('#ctext h2, #ctext h3, #ctext p').fadeIn(600);
        $('#ctext').stop(true, true).slideDown(500);
        $('#supersize').css("cursor", "pointer");
    } else {
        $('body').css({ "background-color": "#000" });
        //$('#supersize').stop(true, true).animate({ opacity: 1.00 }, 500, function () {
        $('#lnkExpand').addClass('active');
        $('#ctext h2, #ctext h3, #ctext p').fadeIn(600);
        $('#ctext').stop(true, true).slideDown(500);
        $('#lnkExpand').text('Schliessen');
        $('#supersize').css("cursor", "pointer");
        //});
    }
}



// Contenthöhe anpassen
function fitContent() {
    if (isIE6) {
        if (location.pathname == "/de/alu-zaun-konfigurator/") {
            $('.genYellow').width('640px');
            $('#lnkExpand').width('571px');
            $('.headline').width('571px');
        }
    } else {
        if ($('.contentLarge').length > 0) {
            $('.genYellow').width('640px');
            $('#lnkExpand').width('571px');
            $('.headline').width('564px');
        }
    }

    var wHeight = $(window).height();
    var hdrHeight = ($("#header").height() + 40);
    var cntHeight = $("#ctext").height();
    //alert("wHeight:" + wHeight + " " + "hdrHeight:" + hdrHeight + " " + "cntHeight:" + cntHeight);
    if (wHeight < (hdrHeight + cntHeight)) {
        if (isIE6) {
            $("#header").css({ 'position': 'relative', 'width': ($(window).width() - 13), 'z-index': '500' });
        } else {
            $("#header").css({ 'position': 'fixed', 'width': ($(window).width() - 13) });
            $(".contentLeft, .contentLarge, .content").css({ 'margin-top': '145px' });
            $(".contentLeft #ctext, .contentLarge #ctext, .content #ctext").height("auto").css("margin-bottom", "0px");
        }
    } else {
        if (isIE6) {
            $("#header").css({ 'position': 'relative', 'width': 'auto', 'z-index': '500' });
        } else {
            $("#header").css({ 'position': 'relative', 'width': 'auto' });
            $(".contentLeft, .contentLarge, .content").css({ 'margin-top': '0px' });
            //$(".contentLeft #ctext").height(wHeight - hdrHeight - 18).css("margin-bottom", "0px");
            $(".content #ctext").css("margin-bottom", "0px");
        }
    }
}

// Iframe über gesamte Fläche
function iframe() {
    var frm = $("#panoDIV");
    if (frm) {
        if (isIE) {
            $(frm).width($(window).width());
            $(frm).height(($(window).height() - 2) - ($("#header").height() + 40));
        } else {
            $(frm).width($(window).width());
            $(frm).height($(window).height() - ($("#header").height() + 40));
        }
    }
    //    frm.width($(window).width());
    //    frm.height($(window).height() - ($("#header").height() + 40));

    //$("h1").html(frm.width() + "(" + $(window).width() + ")" + " " + frm.height() + "(" + $(window).height() + ")")

}


$(window).resize(function () {
    fitBackground();
    fitContent();
    iframe();
});

$(window).load(function () {
    fitBackground();
    fitContent();
    iframe();
});


$(document).ready(function () {
    fitContent();
    prepareNavigation();
    checkContentURL();
    iframe();
    // Nur Startseite
    $.cookie("href", document.location);
    if ($.cookie("href").indexOf("#") < 0) {
        loadImages("/");
    }

    // navigation hover/color effekt
    $('#MainNav li a').not("#MainNav li a.active").hover(function () {
        $(this).css({ color: '#c49e3f' }, 300);
    }, function () {
        $(this).stop(true, true).animate({ color: '#888888' }, 300)
    });
    $('#MainNav li a.active').hover(function () {
        $(this).css({ color: '#c49e3f' }, 400);
    }, function () {
        $(this).stop(true, true).animate({ color: '#c49e3f' }, 400)
    });

    // Hover Pfeile Bildnavigation
    $("#imgNav a").hover(function () {
        $(this).stop(true, true).animate({ "background-color": "#c49e3f" }, 200);
    }, function () {
        $(this).stop(true, true).animate({ "background-color": "#000000" }, 200);
    });

});

// Prüft, ob der angezeigte Inhalt der URL entspricht
function checkContentURL() {
    if (document.location.hash != "") {
        $.cookie("href", document.location);
        var cUrl = $.cookie("href").substring($.cookie("href").indexOf("#") + 1, $.cookie("href").length);
        if ($("#masterframe").attr("rel") != cUrl) {
            loadContent(cUrl);
        }
    }
}

// Lädt Content dyn. nach
function loadContent(href) {
    if (href.indexOf("/produkte/") < 0) {
        $('#lnkExpand').addClass('active');
        $('body').css({ "background-color": "#000" });
        $('#supersize').css("opacity", "1.00");
    } else {
        $('body').css({ "background-color": "#efefef" });
        $('#supersize').stop(true, true).animate({ opacity: 1.00 }, 500);
    }


    $.get(href + "?ajax=1", function (data) {
        //$('.result').html(data);
        var nContent = $(data).filter("#masterframe").html();
        var nHeader = $(data).filter("#header").html();
        $("#masterframe").html(nContent);
        $("#header").html(nHeader);
        prepareNavigation();
        fitContent();

        $("#masterframe").attr("rel", href);
        var imgPath = href.replace(/\/de\//, "/");
        loadImages(href);
        //$("#MainNav ul ul .active ul").scroller({ keepScrollPos: true });

        // Zustand Content
        if (href.indexOf("/produkte/") < 0) {
            $('#lnkExpand').addClass('active');
            $('#lnkExpand').text("Schliessen");
            $('body').css({ "background-color": "#000" });
            $('#supersize').css("opacity", "1.00");
            $('#ctext,#ctext h2, #ctext h3, #ctext p').fadeIn(600);
        } else {
            if (href == "/de/produkte/") {
                $('#lnkExpand').addClass('active');
                $('#lnkExpand').text("Schliessen");
                $('body').css({ "background-color": "#000" });
                $('#supersize').css("opacity", "1.00");
                $('#ctext,#ctext h2, #ctext h3, #ctext p').fadeIn(600);
            } else {
                $('body').css({ "background-color": "#efefef" });
                $('#lnkExpand').removeClass('active');
                $('#supersize').stop(true, true).animate({ opacity: 1.00 }, 500);
                $('#ctext,#ctext h2, #ctext h3, #ctext p').hide();
            };
        }

        // navigation hover/color effekt
        $('#MainNav li a').not("#MainNav li a.active").hover(function () {
            $(this).css({ color: '#c49e3f' }, 300);
        }, function () {
            $(this).stop(true, true).animate({ color: '#888888' }, 300)
        });
        $('#MainNav li a.active').hover(function () {
            $(this).css({ color: '#c49e3f' }, 400);
        }, function () {
            $(this).stop(true, true).animate({ color: '#c49e3f' }, 400)
        });

        // Hover Pfeile Bildnavigation
        $("#imgNav a").hover(function () {
            $(this).stop(true, true).animate({ "background-color": "#c49e3f" }, 200);
        }, function () {
            $(this).stop(true, true).animate({ "background-color": "#000000" }, 200);
        });

        // Pager
        $("#pager").html(currentImageIndex + 1 + "/" + $("#supersize img").length);
        //        loadImages(href);
    });
}


function prepareNavigation() {

    $("#MainNav a, .lnkNews, .lnkCnt").not("#imgNav a").each(function () {
        if ($(this).attr("useajax") != "false") {
            var nHref = $(this).attr("href");
            $(this).attr("href", "/#" + nHref);
            $(this).click(function () {
                var cLink = $(this);

                $(window).unbind('hashchange');

                $.get(nHref + "?ajax=1", function (data) {
                    var nContent = $(data).filter("#masterframe").html();
                    var nHeader = $(data).filter("#header").html();
                    $("#masterframe").html(nContent);
                    $("#header").html(nHeader);
                    prepareNavigation();
                    fitContent();

                    $("#masterframe").attr("rel", nHref);

                    // Zustand Content
                    if (nHref.indexOf("/produkte/") < 0) {
                        $('#lnkExpand').addClass('active');
                        $('#lnkExpand').text("Schliessen");
                        $('body').css({ "background-color": "#000" });
                        $('#supersize').css("opacity", "1.00");
                        $('#ctext,#ctext h2, #ctext h3, #ctext p').fadeIn(600);
                    } else {
                        if (nHref == "/de/produkte/") {
                            $('#lnkExpand').addClass('active');
                            $('#lnkExpand').text("Schliessen");
                            $('body').css({ "background-color": "#000" });
                            $('#supersize').css("opacity", "1.00");
                            $('#ctext,#ctext h2, #ctext h3, #ctext p').fadeIn(600);
                        } else {
                            $('body').css({ "background-color": "#efefef" });
                            $('#lnkExpand').removeClass('active');
                            $('#supersize').stop(true, true).animate({ opacity: 1.00 }, 500);
                            $('#ctext,#ctext h2, #ctext h3, #ctext p').hide();
                        };
                    }

                    // navigation hover/color effekt
                    $('#MainNav li a').not("#MainNav li a.active").hover(function () {
                        $(this).css({ color: '#c49e3f' }, 300);
                    }, function () {
                        $(this).stop(true, true).animate({ color: '#888888' }, 300)
                    });
                    $('#MainNav li a.active').hover(function () {
                        $(this).css({ color: '#c49e3f' }, 400);
                    }, function () {
                        $(this).stop(true, true).animate({ color: '#c49e3f' }, 400)
                    });

                    // Hover Pfeile Bildnavigation
                    $("#imgNav a").hover(function () {
                        $(this).stop(true, true).animate({ "background-color": "#c49e3f" }, 200);
                    }, function () {
                        $(this).stop(true, true).animate({ "background-color": "#000000" }, 200);
                    });


                    // Index aktuelles Bild zurücksetzen 
                    currentImageIndex = 0;
                    // Bilder laden
                    loadImages(nHref);

                    // Zurück-Button
                    $(window).bind('hashchange', function () {
                        checkContentURL();
                        currentImageIndex = 0;
                    });
                });
            });
        }

    });



    // Nächstes Bild
    $(".lnkNext").click(function () {
        var imgCount = $("#supersize img").length;
        if (imgCount > 1) {
            var nextIndex;
            (currentImageIndex == imgCount - 1) ? nextIndex = 0 : nextIndex = currentImageIndex + 1;
            $("#supersize img").fadeOut(600);
            $("#supersize img:eq(" + nextIndex + ")").fadeIn(400, function () {
                currentImageIndex = nextIndex;
                $("#pager").html(currentImageIndex + 1 + "/" + imgCount);
            });
        }
    });


    // Vorheriges Bild
    $(".lnkPrev").click(function () {
        var imgCount = $("#supersize img").length;
        var prevIndex;
        (currentImageIndex == 0) ? prevIndex = imgCount - 1 : prevIndex = currentImageIndex - 1;
        $("#supersize img").fadeOut(600);
        $("#supersize img:eq(" + prevIndex + ")").fadeIn(400, function () {
            currentImageIndex = prevIndex;
            $("#pager").html(currentImageIndex + 1 + "/" + imgCount);
        });
    });


}


var isIE = ($.browser.msie);
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;



// 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, "")]);
    }
});
