﻿
function GalleryScrollerModule(listID, extendedDisplay)
{
    this.ListID = listID;
    this.ExtendedDisplay = extendedDisplay
    this.Index = 0;
    this.Items = new Array();
    
    this.AddItem = function(galleryScrollerModuleItem) { this.Items[this.Items.length] = galleryScrollerModuleItem; }
    this.ScrollPrevious = function() { 
    
        var index = this.Index;
        
        index--;
        
        if (index < 0)
            index = this.Items.length - 1;
            
        this.Index = index;
        
        this.Draw();
    };
    this.ScrollNext = function() { 
    
        var index = this.Index;
        
        index++;
        
        if (index > (this.Items.length - 1))
            index = 0;
            
        this.Index = index;
        
        this.Draw();
    };
    this.Draw = function() {
        
        var ul = document.getElementById(this.ListID);
        var li, a, img, span;
        var previousIndex, nextIndex;
        
        // previous index
        if (this.Index == 0)
            previousIndex = this.Items.length - 1;
        else
            previousIndex = this.Index - 1;
            
        // next index
        if (this.Index == (this.Items.length - 1))
            nextIndex = 0;
        else
            nextIndex = this.Index + 1;
        
        if (!ul) 
            return;

        // clear the previous items
        while (ul.hasChildNodes())
        {
            ul.removeChild(ul.firstChild);
        }
        
        // previous link
        li = document.createElement("li");
        li.className = "prev";
        
        // only 1 gallery
        if (previousIndex != this.Index)
        {
            a = document.createElement("a");
            a.setAttribute("href", "javascript:" + this.ListID + ".ScrollPrevious()");
            
            a.appendChild(document.createTextNode("Previous"));
            li.appendChild(a);
        }
        ul.appendChild(li);
        
        // previous gallery image
        if (this.ExtendedDisplay)
        {
            li = document.createElement("li");
            li.className = "prev_gallery";
            
            a = document.createElement("a");
            a.setAttribute("href", this.Items[previousIndex].LinkURL);
            
            img = document.createElement("img");
            img.setAttribute("src", this.Items[previousIndex].Thumbnail.src);
            img.setAttribute("alt", this.Items[previousIndex].Title);
            
            a.appendChild(img);            
            li.appendChild(a);
            ul.appendChild(li);
        }
        
        // current gallery image
        li = document.createElement("li");
        li.className = "current";
        
        a = document.createElement("a");
        a.setAttribute("href", this.Items[this.Index].LinkURL);
        
        img = document.createElement("img");
        img.setAttribute("src", this.Items[this.Index].Thumbnail.src);
        img.setAttribute("alt", this.Items[this.Index].Title);
        
        a.appendChild(img);
        
        span = document.createElement("span");
        span.appendChild(document.createTextNode(this.Items[this.Index].Title));
        
        li.appendChild(a);
        li.appendChild(span);
        ul.appendChild(li);
        
        // next gallery image
        if (this.ExtendedDisplay)
        {
            li = document.createElement("li");
            li.className = "next_gallery";
            
            a = document.createElement("a");
            a.setAttribute("href", this.Items[nextIndex].LinkURL);
            
            img = document.createElement("img");
            img.setAttribute("src", this.Items[nextIndex].Thumbnail.src);
            img.setAttribute("alt", this.Items[nextIndex].Title);
            
            a.appendChild(img);            
            li.appendChild(a);
            ul.appendChild(li);
        }
        
        // next link
        li = document.createElement("li");
        li.className = "next";
        
        // only 1 gallery
        if (nextIndex != this.Index)
        {
            a = document.createElement("a");
            a.setAttribute("href", "javascript:" + this.ListID + ".ScrollNext()");
            
            a.appendChild(document.createTextNode("Next"));
            li.appendChild(a);
        }
        
        ul.appendChild(li);
    };
}

function GalleryScrollerModuleItem(linkURL, imageURL, title)
{
    this.LinkURL = linkURL;
    this.Thumbnail = new Image(); this.Thumbnail.src = imageURL;
    this.Title = title;
}