﻿function PhotoGallery(popUpExtenderID, imageID, nextLinkID, prevLinkID, containerID) {
    this.popUpExtenderID = popUpExtenderID;
    this.nextLinkID = nextLinkID;
    this.prevLinkID = prevLinkID;
    this.image = $get(imageID);
    this.imagesArray = new Array();
    this.altTextArray = new Array();
    this.currentIndex = -1;
    this.containerID = containerID;
    var galleryObj = this;

    addEvent($get(nextLinkID), 'click',
            function(evt) {
                var imageIndex = galleryObj.currentIndex;
                if (imageIndex < galleryObj.imagesArray.length - 1) {
                    imageIndex = imageIndex + 1;
                }
                else {
                    imageIndex = 0;
                }

                var returnVal = galleryObj.showImage(imageIndex);

                if (evt.returnValue) {
                    evt.returnValue = returnVal;
                }
                if (evt.preventDefault && !returnVal) {
                    evt.preventDefault();
                }

                return returnVal;
            }
        , true);

    addEvent($get(prevLinkID), 'click',
            function(evt) {
                var imageIndex = galleryObj.currentIndex;
                if (imageIndex > 0) {
                    imageIndex = imageIndex - 1;
                }
                else {
                    imageIndex = galleryObj.imagesArray.length - 1;
                }

                var returnVal = galleryObj.showImage(imageIndex);

                if (evt.returnValue) {
                    evt.returnValue = returnVal;
                }
                if (evt.preventDefault && !returnVal) {
                    evt.preventDefault();
                }

                return returnVal;
            }
        , true);

    this.showImage = function(imageIndex) {

        var popUpWidth = getWindowWidth() - 150;
        var popUpHeight = getWindowHeight() - 150;

        var container = $get(this.containerID);
        container.parentNode.style.width = (20 + popUpWidth) + 'px';
        container.parentNode.style.height = (80 + popUpHeight) + 'px';
        container.style.width = popUpWidth + 'px';
        container.style.height = popUpHeight + 'px';

        this.image.src = this.imagesArray[imageIndex];
        this.image.alt = this.altTextArray[imageIndex];
        this.image.title = this.altTextArray[imageIndex];
        this.currentIndex = imageIndex;

        if (popUpEnabled()) {
            var popUpExtender = $find(this.popUpExtenderID);
            if (popUpExtender) {
                popUpExtender.show();
                return false;
            }
        }
        return true;
    }

    this.addImage = function(imageUrl, altText, linkID) {
        this.imagesArray[this.imagesArray.length] = imageUrl;
        this.altTextArray[this.altTextArray.length] = altText;

        var link = $get(linkID);
        var galleryObj = this;
        var imageIndex = this.imagesArray.length - 1;

        if (this.imagesArray.length > 1) {
            $get(this.nextLinkID).style.display = "";
            $get(this.prevLinkID).style.display = "";
        }

        addEvent(link, 'click',
            function(evt) {
                var returnVal = galleryObj.showImage(imageIndex);

                if (evt.returnValue) {
                    evt.returnValue = returnVal;
                }
                if (evt.preventDefault && !returnVal) {
                    evt.preventDefault();
                }

                return returnVal;
            }
        , true);
    }
}

function repositionPanel(formID, panelID)
{
    var form = $get(formID);
    var panel = $get(panelID);

    form.appendChild(panel);
}

function popUpEnabled()
{
    if (getWindowWidth() <= 250 || getWindowHeight() <= 250) 
    {
        return false;
    }

    return true;
}
