﻿/// <reference path="jquery.js" />
/// <reference path="jquery.background.js" />

function fitBackground() {
    var defHeight = 750;
    var defWidth = 1260;
    var cQuot = defWidth / defHeight;
    var nHeight = $(window).height() - $("#header").height() - $("#footer").height();
    if (isMobile) {
        if ($(".iContent").height() > $(window).height()) {
            nHeight = $(".iContent").height();
        }
        $("#images").width("1035px");
    }
    var nWidth = parseInt(nHeight * cQuot, 10);

    // Container
    $("#images").height(nHeight);
    

    // Bilder
    if (nHeight * cQuot < $(window).width()) {
        nWidth = $(window).width();
        nHeight = nWidth / cQuot;
    }
    
    $("#images img").each(function () {
        $(this).height(nHeight);
        $(this).width(nWidth);
        $(this).css("margin-top", nHeight * -0.5);
        $(this).css("margin-left", nWidth * -0.5);
    });

}

var cImageFolder = "";

// Aktuelle Bilder laden
function loadImages(url) {
    // Neuer Bilderordner?
    if (cImageFolder != getImageFolder(url) || cImageFolder == "") {
        // Neue Bilder laden
        var arr = getImages(url);
        var rel = getREL();

        $("#imgNav").html("");

        $("#cCount").html(arr.length);
        $("#cIndex").html("1");

        var c = arr.length - 1;

        // setTimeout("cleanImages()", 2000);

        for (var i = c; i > -1; i--) {
            var img = $("<img>")
                .attr("src", arr[i])
                .attr("rel", rel)
                .css("display", "none")
                .css("position", "absolute");
                if (i == c) {
                    img.load(function () {
                        $(this).fadeIn(350, function () {
                            cleanImages();
                        }).addClass("isVisible");
                    })
                    
                }
                img.appendTo($("#images"));
                addPagerItem(img, "imgPager" + (i == 0 ? 'Active' : ''), i);
            }
        
        fitBackground();
        // Aktuellen Ordner setzen
        cImageFolder = getImageFolder(url);               
    } else {
        // Nächstes Bild im gleichen Ordner wählen
        setTimeout('nextImage(null)', 500);
    }    
}

// Nicht-aktuelle Bilder löschen
function cleanImages() {    
    var rel = getREL();
    if (rel != "") { 
       $("#images img").each(function () {
            if ($(this).attr("rel") != rel) {
                $(this).remove();
            }
        }); 
    }    
}

// Pager (Bilder)
function addPagerItem(img, cssClass, id) {
    $("<a>")
    .attr("id", "lnkImg" + id)
    .addClass(cssClass)
    .click(function () {
        // Bild einblenden      
        img.fadeIn(300, function () {
            $("#images img").not(img).fadeOut(300).removeClass("isVisible");
        }).addClass("isVisible");
        // (re-)set active CSS-class
        $("#imgNav a").each(function () {
            $(this).removeClass("imgPagerActive");
            $(this).addClass("imgPager");
            $("#cIndex").html(id + 1);
        });
        var cLink = $(this);
        cLink.removeClass("imgPager");
        cLink.addClass("imgPagerActive");
            
    }).prependTo("#imgNav");
}

/* Content ein-/ausblenden */
function switchContent() {
    if (parseInt($(".iContent").css("top")) < 0) {
        showContent();
    } else {
        hideContent();
    }
}

function showContent() { 
    $(".iContent").animate({ top: "0px" }, 1000);
    $("#imagesHeader").fadeOut();
}

function hideContent() { 
    $(".iContent").animate({ top: "-" + parseInt($(".iContent").height() + $("#header").height()) }, 1000);
    // Header laden / Content ausblenden
    $("#imagesHeader").click(function () {
        $(this).fadeOut();
        $(".iContent").animate({ top: "0px" }, 1000);
    }).fadeIn();
}

// Aktuelle Bild-ID
function currentImageID() {
    var cID = 0;
    for (var i = 0; i < $("#imgNav a").length; i++) {
        if ($("#imgNav a:eq(" + i + ")").hasClass("imgPagerActive")) {
            cID = i;
        }
    }
    return cID;
}

function nextImageID() {
    var nID = 0;
    if (currentImageID() < $("#imgNav a").length - 1) {
        nID = currentImageID() + 1;
    }
    return nID;
}

function prevImageID() {
    var nID = $("#imgNav a").length - 1;
    if (currentImageID() > 0) {
        nID = currentImageID() - 1;
    }
    return nID;
}

function nextImage(hideC) {
    $("#imgNav a:eq(" + nextImageID() + ")").trigger("click");
    if (hideC) { hideContent() }
}

function prevImage(hideC) {
    $("#imgNav a:eq(" + prevImageID() + ")").trigger("click");
    if (hideC) { hideContent() }
}
