﻿var Gallery = new function()
{
    this.ParentObject = null;
    this.ImageArray = null;
    this.CurrentIndex = 0;
    
    this.DisplayIndex = function(index)
    {
        this.CurrentIndex = index;
        ModalBox.Show();
        ModalBox.SetContentBlockText(this.GetSlideText());
        $('#ModalBox_ContentBlock').height(0).width(0);
    };
    this.GetSlideText = function()
    {
        //var linkText = (this.ImageArray.length > 1) ? "<a id='Gallery_Previous' href='" + this.ImageArray[this.GetPreviousIndex(this.CurrentIndex)] + "' onclick='Gallery.Previous(); return false;' title='Previous'><span>Prev</span></a><a id='Gallery_Next' href='" + this.ImageArray[this.GetNextIndex(this.CurrentIndex)] + "' onclick='Gallery.Next(); return false;' title='Next'><span>Next</span></a>" : "";
        return "<img id='Gallery_Image' src='" + this.ImageArray[this.CurrentIndex] + "' style='visibility: hidden;' alt='' onload='Gallery.ResizeModalBox();' />";
    };
    this.GetIndexByHref = function(href)
    {
        for(var i = 0; i < this.ImageArray.length; i++) { if(this.ImageArray[i] == href) { return i; } }
        return 0;
    };
    this.GetNextIndex = function(index)
    {
        var tmp = index + 1;
        if(tmp == this.ImageArray.length) { return 0; }
        return tmp;
    };
    this.GetPreviousIndex = function(index)
    {
        var tmp = index - 1;
        if(tmp < 0) { return (this.ImageArray.length - 1); }
        return tmp;
    };
    this.Next = function()
    {
        this.CurrentIndex = this.GetNextIndex(this.CurrentIndex);
        var txt = this.GetSlideText();
        $('#Gallery_Image').fadeTo('slow', 0, function() {ModalBox.SetContentBlockText(txt); $('#Gallery_Image').fadeTo(0,0); });
    };
    this.Previous = function()
    {
        this.CurrentIndex = this.GetPreviousIndex(this.CurrentIndex);
        var txt = this.GetSlideText();
        $('#Gallery_Image').fadeOut('slow', function() { ModalBox.SetContentBlockText(txt); $('#Gallery_Image').fadeTo(0,0); });
    };
    this.ResizeModalBox = function()
    {
        $('#ModalBox_Container').css('background','#FFF url(/images/loading.gif) no-repeat center');
        $('#Gallery_Image').fadeTo(0,0).css('display','none');
        $('#ModalBox_ContentBlock').animate( { width: $('#Gallery_Image').width(), height: $('#Gallery_Image').height() }, 1000);
        $('#ModalBox_Container').animate( { width: $('#Gallery_Image').width() }, 1000, function() { $('#ModalBox_Container').animate({ top: ((General.GetWindowHeight() / 2) - ($('#ModalBox_Container').height() / 2)), left: ((General.GetWindowWidth() / 2) - ($('#ModalBox_Container').width() / 2)) }, 1000); $('#Gallery_Image').css('display','block').css('visibility','visible').fadeTo('slow', 1); });
        
        if(Gallery.ImageArray.length > 1) { $('#Gallery_Image').click(function(e) { if(e.button == 0) { Gallery.Next(); } }); }
    };
    this.Show = function(obj)
    {
        this.ParentObject = obj.parentNode;
        this.ImageArray = new Array();
        
        var imgs = this.ParentObject.getElementsByTagName('a');
        for(var i = 0; i < imgs.length; i++) { this.ImageArray.push(imgs[i].href); }
        
        this.DisplayIndex(this.GetIndexByHref(obj.href));
    };
};
