﻿var images = new Array();
var imageId = undefined;

$(document).ready(function() {
    $(".ImageGallery").each(function() {
        var src;
        var alt;

        $(this).find(".Image").each(function() {
            alt = this.alt;
            src = this.src;
        });
        
        var galleryid = images.length;

        images[galleryid] = new Object();
        images[galleryid].alt = alt;

        var lastSlash = src.lastIndexOf("/");
        var secondLastSlash = src.lastIndexOf("/", lastSlash - 1);

        images[galleryid].src = src.substr(0, secondLastSlash) + "/Full" + src.substr(lastSlash);

        (new Image()).src = images[galleryid].src;

        $(this).find(".Link").click(function() {
            ShowImagePopup(galleryid);
        });
    });

    $("#PopupImage .Close").each(function() {
        this.onclick = function() {
            $('#PopupImage').animate({ height: 'hide', opacity: 'hide' }, 'slow');
        }
    });

    $("#PopupImage .Next").each(function() {
        this.onclick = function() {
            ShowImage(imageId + 1);
        }
    });

    $("#PopupImage .Back").each(function() {
        this.onclick = function() {
            ShowImage(imageId - 1);
        }
    });
})

function ShowImagePopup(id) {
    /* out of range */
    if (id < 0 || id >= images.length) return;

    if($('#PopupImage').css("position") != "fixed")
        scroll(0, 0);
        
    if (id <= 0)
    {
        $("#PopupImage .Back").hide();
    }
    else
    {
        $("#PopupImage .Back").show();
    }

    
    if(id == images.length - 1)
    {
        $("#PopupImage .Next").hide();
    }
    else
    {
        $("#PopupImage .Next").show();
    }

    imageId = id;

    UpdateImage(imageId);

    $('#PopupImage').animate({ height: 'show', opacity: 'show' }, 'slow');
}

function ShowImagePopupByUrl(src) {
    var length = src.length;
    
    for (var i = 0; i < images.length; i++) {
        if (src == images[i].src.substr(images[i].src.length - length, length)) {
            ShowImagePopup(i);
            
            return;
        }
    }
}

function UpdateImage(id) {
    $('#PopupImageImage').each(function() {
        this.alt = images[id].alt;
        this.src = images[id].src;
    });

    $('#PopupImage .Text').each(function() {
        this.firstChild.data = images[id].alt;
    });
}

function ShowImage(id) {
    /* out of range */
    if (id < 0) return;
    
    if (id <= 0)
    {
        $("#PopupImage .Back").hide();
    }
    else
    {
        $("#PopupImage .Back").show();
    }

    
    if(id == images.length - 1)
    {
        $("#PopupImage .Next").hide();
    }
    else
    {
        $("#PopupImage .Next").show();
    }
    
    imageId = id;
    $('#PopupImageImage').animate({ opacity: 'hide' }, 'slow', function() {
        UpdateImage(imageId);

        $('#PopupImageImage').animate({ opacity: 'show' }, 'slow');
    });
}
