var XMLurl = "";
var curr = 1;
var total = 2;
var nextCall = 0;
var visitList = [];
var innerList = new Array(3);
$(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");
            prev = parseInt(tmp1.substring(5,tmp1.search(/&.+/)));
            next = parseInt(tmp2.substring(5,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);
        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 = $(this).attr('numImgs');
                    imgName_text = $(this).attr('imgName');
                    imgSrc_text = $(this).attr('imgSrc');
                    imgNum_text = $(this).attr('imgNum');
                    imgDescr_text = $(this).attr('imgDescr');
                    imgWidth_text = $(this).attr('imgWidth');
                    imgHeight_text = $(this).attr('imgHeight');
                    total = parseInt(numImgs_text);
                    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));
                            $(this).html(linktmp).fadeIn(800);});
                    }
                    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;
                    curr = parseInt(imgNum_text);
                });
            }
        });
    }
}