var XMLurl = "";
var curr = 1;
var total = 2;
var nextCall = 0;
var visitList = [];
var innerList = new Array(4);
var windowWidth = 0;
var widthFit = true;
var descriptionShow = true;
$(document).ready(function(){
    $("#navImgGal03 a").hide();
    $("#navImgGal03 span").show();
    $("#bPrev").click(function(){requestImg("p");});
    $("#bNext").click(function(){requestImg("n");});;
    $("#abPrev").click(function(){requestImg("p");});
    $("#abNext").click(function(){requestImg("n");});;
    if (nextCall == 0) {
        tmp1 = $("#abPrev").attr("href");
        if (typeof tmp1 != 'undefined') {
            tmp2 = $("#abNext").attr("href");
            tmpstart = tmp1.search(/img.+/) +4;
            prev = parseInt(tmp1.substring(tmpstart,tmp1.search(/&.+/)));
            next = parseInt(tmp2.substring(tmpstart,tmp2.search(/&.+/)));
            if(next == 2){nextCall = 1;}
            else{nextCall = prev +1;}
            XMLurl = $("#slideshow img").attr("src");
            XMLurl = XMLurl.substring(0,XMLurl.search(/mediafiles.+/)) + "showdetailviewAJAX";
            requestImg("s");
        }
    }
});
function requestImg(pchoise){
    if (pchoise != "s") {
        if (curr == total && pchoise == "n") {nextCall = 1;}
        else if (curr == 1 && pchoise == "p") {nextCall = total;}
            else if (pchoise == "n") {nextCall = curr + 1;}
                else if (pchoise == "p") {nextCall = curr - 1;}
    }
    if ((visitList.length > 0) && (visitList[nextCall - 1][0] != undefined)) {
        heightTmp = parseInt(visitList[nextCall - 1][2]);
        $("#slideshowBox").height(heightTmp);
        $("#imgDatNam").html(visitList[nextCall - 1][0]);
        $("#slideshow").fadeOut(1, function(){$(this).height(heightTmp);$(this).html(visitList[nextCall - 1][1])}).fadeIn(800,function(){
            if (visitList[nextCall - 1][3]  != undefined) {$(this).append("<p>" + visitList[nextCall - 1][3] + "</p>");}$(this)
        });
        
        curr = nextCall;
    }
    else {
        $.ajax({
            type: "POST",
            url: XMLurl,
            async: false,
            dataType: "xml",
            data: {img: nextCall},
            success: function(xml){
                $(xml).find('Img').each(function(){
                    numImgs_text = parseInt($(this).attr('numImgs'));
                    imgName_text = $(this).attr('imgName');
                    imgSrc_text = $(this).attr('imgSrc');
                    imgNum_text = parseInt($(this).attr('imgNum'));
                    imgDescr_text = $(this).attr('imgDescr');
                    imgWidth_text = parseInt($(this).attr('imgWidth'));
                    imgHeight_text = parseInt($(this).attr('imgHeight'));
                    total = parseInt(numImgs_text);
                    if(widthFit == true && windowWidth < imgWidth_text){
                        windowWidth = $("#slideshowBox").width();
                        faktor = windowWidth/imgWidth_text;
                        imgWidth_text = windowWidth;
                        imgHeight_text = parseInt(imgHeight_text * faktor);
                    }
                    descriptionHight = (descriptionShow == true && imgDescr_text != undefined)?25:0;
                    linktmp = '<img src=\"' + imgSrc_text + '\" alt=\"' + imgDescr_text + '\" width=\"' + imgWidth_text + '\" height=\"' + imgHeight_text + '\" />';
                    if (pchoise != "s") {
                        $("#imgDatNam").html(imgName_text);
                        $("#slideshow").fadeOut(1, function(){
                            $(this).height(parseInt(imgHeight_text));
                            $("#slideshowBox").height(parseInt(imgHeight_text + descriptionHight));
                            $(this).html(linktmp).fadeIn(800);
                            if (descriptionHight > 0) {$(this).append("<p>" + imgDescr_text + "</p>");}
                        });
                            
                    }
                    else {for (var iTmp = 0; iTmp < total; iTmp++) {visitList[iTmp] = innerList.slice();}}
                    visitList[nextCall - 1][0] = imgName_text;
                    visitList[nextCall - 1][1] = linktmp;
                    visitList[nextCall - 1][2] = imgHeight_text + descriptionHight;
                    visitList[nextCall - 1][3] = imgDescr_text;
                    curr = parseInt(imgNum_text);
                });
            }
        });
    }
}