/**
 * This may be a little messy, but I had to modify it to allow for multiple galleries per page
 * and so that those operated independently of one another
 * if you need more than 5, just define some more up here at the top.
 * -- Justin
 */


var currentPhoto1;
var currentPhoto2;
var currentPhoto3;
var currentPhoto4;
var currentPhoto5;

$(document).ready(function() {

    var galTemp;

    $("[id^='visualgallery-']").each(function(i){
        galTemp = this;

        $("#" + $(galTemp).attr("id") + " .photo-holder").removeClass("show");
        eval("currentPhoto" + $(galTemp).attr("id").replace(/visualgallery\-/g, "") + " = 0");

        $("#" + $(galTemp).attr("id") + " .choose-photo").click(function(){
            var id = $(this).attr("rel");

            showPhoto(id, $(this).parent().parent().parent().parent().attr("id"));

            return false;
        });
        rotatePhotos($(galTemp).attr("id"));
    });

    //showPhoto(currentPhoto);

});

function showPhoto(id, gallery_id)
{
    //console.log(id + gallery_id);
    $("#" + gallery_id + " .photo-holder").removeClass("show");
    $("#" + gallery_id + " .photo-button").removeClass("active");

    $("#" + gallery_id + " #photo-" + id).addClass("show");
    $("#" + gallery_id + " #button-" + id).addClass("active");

    //Set the opacity to 0 and height to 1px
    $('#' + gallery_id + ' #gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });

    //Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
    $('#' + gallery_id + ' #gallery .caption').animate({opacity: 0.7},100 ).animate({height: '40px'},500 );

    $("#" + gallery_id + " .caption .content").html($("#" + gallery_id + " #photo-" + id).children("img").attr("rel"));
}

function rotatePhotos(gallery_id) {
    showPhoto(eval("currentPhoto" + gallery_id.replace(/visualgallery\-/g, "")), gallery_id);

    if((eval("currentPhoto" + gallery_id.replace(/visualgallery\-/g, ""))) >= $("#" + gallery_id + " .choose-photo:last").attr("rel")) {
        //currentPhoto = 0;
        eval("currentPhoto" + gallery_id.replace(/visualgallery\-/g, "") + " = 0");
    } else {
        eval("currentPhoto" + gallery_id.replace(/visualgallery\-/g, "") + "++");
        //currentPhoto++;
    }

    window.setTimeout(function(){rotatePhotos(gallery_id)}, 5000);
}
