
function hidePrevOrNext(jq_pageLinkLevelElement) {
    //console.dir(jq_pageLinkLevelElement);
    if(jq_pageLinkLevelElement.parent().prev().hasClass("photogallery-page-li-Prev")) {
        jq_pageLinkLevelElement.closest(".section").find(".photogallery-page-li-Prev").hide();
    }
    else {
        jq_pageLinkLevelElement.closest(".section").find(".photogallery-page-li-Prev").show();
    }
    
    //control hiding and showing of next links
    if(jq_pageLinkLevelElement.parent().next().hasClass("photogallery-page-li-Next")) {
        jq_pageLinkLevelElement.closest(".section").find(".photogallery-page-li-Next").hide();
    }
    else {
        jq_pageLinkLevelElement.closest(".section").find(".photogallery-page-li-Next").show();
    }
}

$(document).ready(function() {

    //setup the images to work with prettyphoto
    $(".photogallerylist a").prettyPhoto({
        allowresize: false,
        opacity: .4,
        default_width: 640,
        default_height: 385
    });

    if(typeof String.prototype.trim !== 'function') {
          String.prototype.trim = function() {
            return this.replace(/^\s+|\s+$/g, ''); 
          }
        }
    //$("#photogallery-1").show().removeClass('photogallery-hidden').addClass('photogallery-shown');
    
    $(".photogallery-page-link").click(function() {
        //console.dir($(this).text());
        var jq_cmpHolder = $(this).closest(".photogallery"); //component holder, make sure we're only looking underneath this
        var pgNum = $.trim($(this).text());
        //alert("-"+pgNum+"-");
        
        //var lastPgNum = $.trim($(".photogallery-page-link-fake:visible").text());
        var lastPgNum = $.trim(jq_cmpHolder.find(".photogallery-page-link-fake:visible").text());
        var nextPgNum = parseInt(lastPgNum) + 1;
        var prevPgNum = parseInt(lastPgNum) - 1;
        
        if(pgNum == "Next") {
            jq_cmpHolder.find(".photogallery-page-link-"+nextPgNum+":first").click();
        }
        else if(pgNum == "Prev") {
            jq_cmpHolder.find(".photogallery-page-link-"+prevPgNum+":first").click();
        }
        else {
            //load the images
            jq_cmpHolder.find("#photogallery-"+pgNum+" img").each(function() {
                var imgSrc = $(this).next('input').attr('value');
                $(this).attr('src',imgSrc);
            });
            
            //control hiding and showing of previous links
            hidePrevOrNext($(this));

            var directionOut = "right";
            var directionIn = "left";
            if(lastPgNum < pgNum) {
                directionOut = "left";
                directionIn = "right";
            }
            
            //hide the current gallery  
            //$(".photogallery-shown").fadeOut().hide().removeClass('photogallery-shown').addClass('photogallery-hidden');
            jq_cmpHolder.find(".photogallery-shown").hide("slide",{direction:directionOut},300, function() {
                jq_cmpHolder.find("#photogallery-"+pgNum).show("slide",{direction:directionIn},400).removeClass('photogallery-hidden').addClass('photogallery-shown');
            }).removeClass('photogallery-shown').addClass('photogallery-hidden');
            //$(".photogallery-shown").hide("fade",{}, 1000).removeClass('photogallery-shown').addClass('photogallery-hidden');
            
            jq_cmpHolder.find(".photogallery-page-link").show();
            jq_cmpHolder.find(".photogallery-page-link-fake").hide();
            jq_cmpHolder.find(".photogallery-page-link-"+pgNum).hide();
            jq_cmpHolder.find(".photogallery-page-link-fake-"+pgNum).show();
        }
        
        return false;
    });

    //for each photo gallery section
    $(".section:has(.photogallerylist)").each(function(index) {
        //console.dir($(this).find(".photogallery-page-link-fake:visible:first"));
        //console.dir($(this).find(".photogallery-page-link-fake:visible:first").parent());
        hidePrevOrNext($(this).find(".photogallery-page-link-fake:visible:first"));
        //console.dir($(this).find("a"));
        $(this).find(".photogallerylist-item a").attr("rel","prettyPhoto["+index+"]");
    });
    /*
    $(".photogallerylist").each(function(index) {
        $(this).find("li.photogallerylist-item a").attr("rel","prettyPhoto["+index+"]");
    });*/
    
});
