var thumbs      // Thumbs (Links a)
var navPrev     // Navigationsschattfläche zurück
var navNext     // Navigationsschattfläche weiter
var opacity = 0.95; // Opacity

$(document).ready(function() {

    thumbs = $("#ctl00_content_thumbs a");
    navPrev = $("#ctl00_content_arrowLeft");
    navNext = $("#ctl00_content_arrowRight");

    // Loading-Indikator für normale Galerien sichtbar machen und verstecken
    $("#loading").removeClass("hidden");
    hideLoading();

    $("a.selected img").fadeTo(200, opacity);
    
    // --- Klick auf Thumbnail: Bild anzeigen
    thumbs.click(function() {
        var currThumb = $(this);
        // Wenn das Bild bereits aktiv ist, abbrechen
        if (currThumb.hasClass("selected")) {
            return false;
        }
        else {
            // Css-Klasse bei allen Thumbs zurücksetzen und neuen Thumb auswählen
            thumbs.removeClass("selected");
            currThumb.addClass("selected");

            // Anzahl der Thumbs und Index des aktuellen Thumbs ermitteln
            var imgCount = thumbs.length;
            var currIndex = getCurrIndex();

            // Index des Bildes ermitteln dem der Href für den Link entnommen werden soll
            var linkIndex = currIndex + 1;
            if (linkIndex >= imgCount) linkIndex = 0;

            // Fallunterscheidung "Referenz"
            if ($("#ctl00_content_thumbs").hasClass("referenz")) {
                return showDirect(currIndex, linkIndex);
            }
            else {
                // Normale Galerien - Bild anzeigen
                return fadeImgageIn(currIndex, linkIndex);
            }
        }
    });


    // --- Klick auf Hauptbild: Nächstes Bild anzeigen
    $("#ctl00_content_mainImage").click(function() {
        // Fallunterscheidung "Referenz"
        if ($("#ctl00_content_thumbs").hasClass("referenz")) {
            // Referenzen
            return false;
        }
        else {
            // Normale Galerien - Bild anzeigen
            return nextImg();
        }
    });


    // --- Klick auf den Link des Hauptbildes: Nächstes Bild anzeigen
    $("#mainContent a").click(function() {
        return nextImg();
    });


    // --- Klick auf den Pfeil nach links: Vorheriges Bild anzeigen
    navNext.click(function() {
        return nextImg();
    });


    // --- Klick auf den Pfeil nach rechts: Nächstes Bild anzeigen
    navPrev.click(function() {
        return prevImg()
    });


    // --- Tooltip mit Alternativtext zu Bildern unterdrücken
    $("img").hover(function() {
        // Alternativtext  merken
        this.alt1 = this.alt;
        this.alt = "";
    },
	function() {
        // Alternativtext wieder herstellen
	    this.alt = this.alt1;
	});

	$("a").hover(function() {
	    // Alternativtext  merken
	    this.aalt1 = this.title;
	    this.title = "";
	},
	function() {
	    // Alternativtext wieder herstellen
	    this.title = this.aalt1;
	});


    // --- Hover preview
    $(".referenz > a").hover(function() {
        // Ursprünglichen Titel  merken
        this.t = this.title;
        this.title = "";
       
        $("#ctl00_content_mainImage").hide();

        // Neuen Pfad ermitteln und zuweisen
        var imgUrl = getImageUrl(this.href);
        // Insert preloaded image after it finishes loading
        $('<img />')
            .attr('src', imgUrl)
            .load(function() {
            });
        $("#mainContent").append("<p id='previewImg'><img src='" + imgUrl + "' </p>");
    },
	function() {
	    // Ursprünglichen Titel wieder herstellen
	    this.title = this.t;
	    $("#previewImg").remove()
	    $("#ctl00_content_mainImage").show();
	});
});


// --- Nächstes Bild anzeigen
function nextImg() {
    // Anzahl der Thumbs und Index des aktuellen Bildes ermitteln
    var imgCount = thumbs.length;
    var currIndex = getCurrIndex();

    // Neuen Index festlegen
    currIndex++;
    if (currIndex >= imgCount) currIndex = 0;

    // Index des Bildes ermitteln dem der Href für den Link entnommen werden soll
    var linkIndex = currIndex + 1;
    if (linkIndex >= imgCount) linkIndex = 0;

    // Bild anzeigen
    return fadeImgageIn(currIndex, linkIndex)
}


// --- Vorheriges Bild anzeigen
function prevImg() {
    // Anzahl der Thumbs und Index des aktuellen Bildes ermitteln
    var imgCount = thumbs.length;
    var currIndex = getCurrIndex();

    // Neuen Index festlegen
    currIndex--;
    if (currIndex < 0) currIndex = imgCount - 1;

    // Index des Bildes ermitteln dem der Href für den Link entnommen werden soll
    var linkIndex = currIndex - 1;
    if (linkIndex < 0) linkIndex = imgCount - 1;

    // Bild anzeigen
    return fadeImgageIn(currIndex, linkIndex)
}


// Zum Bild mit dem übergebenen Index blenden 
function fadeImgageIn(index, linkIndex) {
    // Css-Klasse bei allen Thumbs zurücksetzen und neuen Thumb auswählen
    thumbs.removeClass("selected");
    thumbs.eq(index).addClass("selected");
    $("a img").fadeTo(200, 1);
    $("a.selected img").fadeTo(200, opacity); 

    // Pfad zum Bild aus dem aktuellen Thumb ermitteln
    var imgUrl = getImageUrl(thumbs.eq(index).attr("href"));

    // Link für das neue Bild aus dem nächsten Thumb ermitteln
    var imgLink = thumbs.eq(linkIndex).attr("href");

    // Bild anzeigen
    if (imgUrl != "") {
      showLoading();
        $("#ctl00_content_mainImage")
        .fadeOut(300, function() {
        var newImg = $("<img />").attr('src', imgUrl)
        .load(function() {
            newImg.attr('src', "");
            $("#ctl00_content_mainImage")
            .attr("src", "")
            .attr("src", imgUrl)
            $("#ctl00_content_mainImage").fadeIn(300);
            hideLoading();
        });
      })
        return false;
    }
    else {
        // Bei einem Fehler, den normalen Link ausführen
        return true
    }
}



// Zum Bild mit dem übergebenen Index blenden
function showDirect(index, linkIndex) {
    // Css-Klasse bei allen Thumbs zurücksetzen und neuen Thumb auswählen
    thumbs.removeClass("selected");
    thumbs.eq(index).addClass("selected");
    $("a img").fadeTo(200, 1);
    $("a.selected img").fadeTo(200, opacity); 

    // Pfad zum Bild aus dem aktuellen Thumb ermitteln
    var imgUrl = getImageUrl(thumbs.eq(index).attr("href"));

    // Link für das neue Bild aus dem nächsten Thumb ermitteln
    var imgLink = thumbs.eq(linkIndex).attr("href");

    // Bild anzeigen
    if (imgUrl != "") {
        $("#ctl00_content_mainImage")
        .attr('src', imgUrl)
        .attr('href', imgLink);
        return false;
    }
    else {
        // Bei einem Fehler, den normalen Link ausführen
        return true
    }
}

/// Hilfsfunktionen ///

// Ladeanzeige einblenden
function showLoading() {
    $("#loading").show(10); // 1 wg. IE
}

// Ladeanzeige ausblenden
function hideLoading() {
   $("#loading").hide(10);
}

// Index des aktuellen Bildes ermitteln und zurückgeben
function getCurrIndex() {
    var imgCount = thumbs.length;
    for (var i = 0; i <= imgCount - 1; i++)
        if (thumbs.eq(i).hasClass("selected")) {
        return i;
        break;
    }
}

// Pfad zum Bild aus einem Pad mit folgendem Muster extrahieren:
// Still.aspx?img=images/01_Still/01.jpg
function getImageUrl(url) {
    try {
        var a = url.split("=");
        var imgPath = a[1];
        return imgPath
    }
    catch (e) {
        return "";
    }
}

