if (!window.ImageGallery)
	var ImageGallery = new Object();

Object.extend(ImageGallery, {
    newPopupImage: null,
    imageGalleryImages: [],
    galleryImageDisplay: [],
    afterImageChange: [],
    clickItems: [],
    
    resetLinks: function()
    {
        if(ImageGallery.galleryImageDisplay.length > 0)
        {
            var ImageLinks = $$('a.ImageGalleryThumbnail');
            ImageLinks = ImageLinks.concat($$('a.GalleryImageLink'));
            ImageLinks = ImageLinks.concat($$('a.GalleryBack'));
            ImageLinks = ImageLinks.concat($$('a.GalleryNext'));
            ImageLinks = ImageLinks.concat($$('a.ControlButton'));

            for(var i=0;i<ImageLinks.length;i++)
            {
                Event.stopObserving(ImageLinks[i].id, 'click');
            
                for(var j=0; j<ImageGallery.galleryImageDisplay[0].images.length;j++)
                    if (ImageGallery.galleryImageDisplay[0].images[j].href.toLowerCase() == ImageLinks[i].href.toLowerCase())
                    {
                        Event.observe(ImageLinks[i].id, 'click', ImageGallery.imageGalleryClickEvent);
                        break;
                    }
            }
        }
    },
    
    getImageIndex: function(href)
    {
        var index = -99;
        if(ImageGallery.galleryImageDisplay.length > 0)
            for(var i=0;i<ImageGallery.galleryImageDisplay[0].images.length;i++)
                if (ImageGallery.galleryImageDisplay[0].images[i].href.toLowerCase() == href.toLowerCase())
                {
                    index = i;
                    break;
                }
            
        return index;
    },
    
    displayImage: function(href)
    {
        var current = ImageGallery.getImageIndex(href);

        if(current > -99)
        {
            for(var i=0; i<ImageGallery.galleryImageDisplay.length; i++)
            {
                var next = current == ImageGallery.galleryImageDisplay[i].images.length - 1 ? 0 : current + 1;
                var previous = current == 0 ? ImageGallery.galleryImageDisplay[i].images.length - 1 : current - 1;
                var next2 = (current + 2 + ImageGallery.galleryImageDisplay[i].images.length) % ImageGallery.galleryImageDisplay[i].images.length;
                var previous2 = (current - 2 + ImageGallery.galleryImageDisplay[i].images.length) % ImageGallery.galleryImageDisplay[i].images.length;

                if($(ImageGallery.galleryImageDisplay[i].image) != null) 
                {
                    if($(ImageGallery.galleryImageDisplay[i].next) != null)
                        $(ImageGallery.galleryImageDisplay[i].next).href = ImageGallery.galleryImageDisplay[i].images[next].href;

                    if($(ImageGallery.galleryImageDisplay[i].back) != null)
                        $(ImageGallery.galleryImageDisplay[i].back).href = ImageGallery.galleryImageDisplay[i].images[previous].href;

                    if($(ImageGallery.galleryImageDisplay[i].hires) != null)
                    {
                        if(ImageGallery.galleryImageDisplay[i].images[current].displayOrginalImage)
                        {
                            $(ImageGallery.galleryImageDisplay[i].hires).href = ImageGallery.galleryImageDisplay[i].hirespath + '?f=' + ImageGallery.galleryImageDisplay[i].images[current].filename + '&n=' + ImageGallery.galleryImageDisplay[i].images[current].encodedtitle;
                            $(ImageGallery.galleryImageDisplay[i].hires).up().show();
                        }
                        else
                        {
                            $(ImageGallery.galleryImageDisplay[i].hires).up().hide();
                        }
                    }
                    
                        
                    if($(ImageGallery.galleryImageDisplay[i].controlsHiddenId) != null)     
                        $(ImageGallery.galleryImageDisplay[i].controlsHiddenId).value = ImageGallery.galleryImageDisplay[i].images[current].id;

                    if($("MB_" + ImageGallery.galleryImageDisplay[i].controlsHiddenId) != null)     
                        $("MB_" + ImageGallery.galleryImageDisplay[i].controlsHiddenId).value = ImageGallery.galleryImageDisplay[i].images[current].id;
                        
                    this.setValue(ImageGallery.galleryImageDisplay[i].title, ImageGallery.galleryImageDisplay[i].images[current].title);
                    this.setValue(ImageGallery.galleryImageDisplay[i].description, ImageGallery.galleryImageDisplay[i].images[current].description);
                    this.setValue(ImageGallery.galleryImageDisplay[i].price, ImageGallery.galleryImageDisplay[i].images[current].price);
                    this.setValue(ImageGallery.galleryImageDisplay[i].keywords, ImageGallery.galleryImageDisplay[i].images[current].keywords);
                    this.setValue(ImageGallery.galleryImageDisplay[i].code, ImageGallery.galleryImageDisplay[i].images[current].code);
                    this.setValue(ImageGallery.galleryImageDisplay[i].summary, ImageGallery.galleryImageDisplay[i].images[current].summary);
                    
                    if($(ImageGallery.galleryImageDisplay[i].isnew) != null)
                    {
                        if(ImageGallery.galleryImageDisplay[i].images[current].isnew)
                            $(ImageGallery.galleryImageDisplay[i].isnew).show();
                        else
                            $(ImageGallery.galleryImageDisplay[i].isnew).hide();
                    }
                    
                    if($(ImageGallery.galleryImageDisplay[i].showMultipleId) != null)
                    {
                        if(ImageGallery.galleryImageDisplay[i].images[current].showMultiple)
                            $(ImageGallery.galleryImageDisplay[i].showMultipleId).show();
                        else
                            $(ImageGallery.galleryImageDisplay[i].showMultipleId).hide();
                    }
                    
                    for(var j=0; j<ImageGallery.galleryImageDisplay[i].customElements.length; j++)
                    {
                        var customElement = ImageGallery.galleryImageDisplay[i].customElements[j];
                        this.setValue(customElement.elementId,  ImageGallery.galleryImageDisplay[i].images[current].customValues.get(customElement.name));
                    }

                    if(ImageGallery.galleryImageDisplay[i].popup.length > 0)
                    {
                        this.popupDisplay = ImageGallery.galleryImageDisplay[i];
                        this.oldPopupImage = $(ImageGallery.galleryImageDisplay[i].image);
                        
                        this.newPopupImage = new Image();
                        this.newPopupImage.src = ImageGallery.galleryImageDisplay[i].images[current].file;
                        
                        Modalbox.options.title = ImageGallery.galleryImageDisplay[i].images[current].title;
                        ImageGallery.setImageObserver(current);
                    }
                    else
                    {
                        $(ImageGallery.galleryImageDisplay[i].image).src = ImageGallery.galleryImageDisplay[i].images[current].file;                   
                        $(ImageGallery.galleryImageDisplay[i].image).title = ImageGallery.galleryImageDisplay[i].images[current].title;
                    }
    
                    for(var j=0;j<this.afterImageChange.length; j++)
                        this.afterImageChange[j]($(ImageGallery.galleryImageDisplay[i].imageWrapper), ImageGallery.galleryImageDisplay[i].images[current]);
                        
                    if($(ImageGallery.galleryImageDisplay[i].previewImages.next1) != null)
                        $(ImageGallery.galleryImageDisplay[i].previewImages.next1).src = ImageGallery.galleryImageDisplay[i].images[next].file;

                    if($(ImageGallery.galleryImageDisplay[i].previewImages.previous1) != null)
                        $(ImageGallery.galleryImageDisplay[i].previewImages.previous1).src = ImageGallery.galleryImageDisplay[i].images[previous].file;
                        
                    if($(ImageGallery.galleryImageDisplay[i].previewImages.next2) != null)
                        $(ImageGallery.galleryImageDisplay[i].previewImages.next2).src = ImageGallery.galleryImageDisplay[i].images[next2].file;

                    if($(ImageGallery.galleryImageDisplay[i].previewImages.previous2) != null)
                        $(ImageGallery.galleryImageDisplay[i].previewImages.previous2).src = ImageGallery.galleryImageDisplay[i].images[previous2].file;
                        
                    for(var j=0;j<ImageGallery.galleryImageDisplay[i].printButtons.length; j++)
                    {
                        var printButton = $(ImageGallery.galleryImageDisplay[i].printButtons[j]);
                        if(printButton != null)
                        {
                            var node = printButton.parentNode;
                            node.innerHTML = node.innerHTML.replace(/giid=[0-9]+/, 'giid=' + ImageGallery.galleryImageDisplay[i].images[current].id);
                            this.resetLinks();
                        }
                    }
                }
            }
        }
    },
    
    setValue: function(elementId, value)
    {
        var element = $(elementId);
        
        if(element != null)
        {
            var inner = element.select('div.GalleryImageValue');
            if(inner.length > 0)
                inner[0].innerHTML = value;
            else
                element.innerHTML = value;
            
            if(value == '')
                element.hide();
            else 
                element.show();
        }
    },
    
    setImageObserver: function (observerCurrent)
    {
        this.observerCurrent = observerCurrent;
        
        setTimeout(function() {
                if(this.newPopupImage.readyState == 'complete' || this.newPopupImage.complete || !(typeof this.newPopupImage.naturalWidth != 'undefined' && this.newPopupImage.naturalWidth == 0))
                {
                    this.oldPopupImage.src = this.newPopupImage.src;
                    var imgTags = $(this.popupDisplay.popup).select('img');
  		    for (var i = 0; i < imgTags.length; i++) 
  		    {
			if (imgTags[i].hasClassName('GalleryImage') == true)
				imgTags[i].title = this.popupDisplay.images[this.observerCurrent].title;
		    }                    
                    if(this.popupDisplay.popupheight == 0)
                    {
                        if(!Modalbox.initialized)
                            Modalbox.show($(this.popupDisplay.popup), 
                                     {title: this.popupDisplay.images[this.observerCurrent].title, 
                                        width: this.popupDisplay.popupwidth, 
                                        autoFocusing: false,
                                        afterLoad: function(event) { ImageGallery.resetLinks(); },
                                        afterHide: function(event) { ImageGallery.resetLinks(); }
                                        });
                        else
                        {
                            $(Modalbox.MBcaption).update(Modalbox.options.title);
                            Modalbox.resizeToContent();
                        }
                    }
                    else
                    {
                        if(!Modalbox.initialized)
                        {
                            var viewPortHeight = document.viewport.getHeight() - 20;
                            var popupHeight = this.popupDisplay.popupheight;
                            if(popupHeight> viewPortHeight)
                            	popupHeight = viewPortHeight;
                            	
                            Modalbox.show($(this.popupDisplay.popup), 
                                     {title: this.popupDisplay.images[this.observerCurrent].title, 
                                        width: this.popupDisplay.popupwidth, 
                                        height: popupHeight,
                                        autoFocusing: false,
                                        afterLoad: function(event) { ImageGallery.resetLinks(); },
                                        afterHide: function(event) { ImageGallery.resetLinks(); }
                                        });
			}
                        else
                            $(Modalbox.MBcaption).update(Modalbox.options.title);
                    }
                }
                else
                    this.setImageObserver(this.observerCurrent);   
            }.bind(this), 50);
    },
                   
    imageGalleryClick: function (event) 
    {
        var element = Event.findElement(event, 'a');
        ImageGallery.displayImage(element.href.toLowerCase());
        Event.stop(event);
    },
    
    getImageSize: function(width, height)
    {
        var widthRatio = width / ImageGallery._popupWidth;
        var heightRatio = height / ImageGallery._popupWidth;
        var ratio = (widthRatio > heightRatio) ? widthRatio : heightRatio;

        return {width: width / ratio, height: height / ratio};
    },
    
    showMouseOverPopup: function(event)
    {
        if(ImageGallery._popupContainer != null && ImageGallery._popupImage != null)
        {
            var element = Event.findElement(event, 'a');
            
            var images  = element.select('img');
            if(images.length > 0)
            {
                ImageGallery._popupImage.src = images[0].src.replace(/w=[0-9]+/, 'w=' +  ImageGallery._popupWidth).replace(/h=[0-9]+/, 'h=' +  ImageGallery._popupHeight);


                var imagesObserver = new ImagesObserver(ImageGallery._popupContainer, 
                    { onImagesLoaded: function() { 
                    
                        ImageGallery._popupContainer.style.width = (ImageGallery._popupImage.width) + 'px';
                        ImageGallery._popupContainer.style.height = (ImageGallery._popupImage.height) + 'px';
                    
                    }});
                    
                ImageGallery._popupContainer.show();
                element.observe('mouseout', ImageGallery.hideMouseOverPopup);
                element.observe('mousemove', ImageGallery.moveMouseOverPopup);
            }
        }
    },
    
    hideMouseOverPopup: function(event)
    {
        var element = Event.element(event);
        ImageGallery._popupContainer.hide();
        element.stopObserving('mouseout');
        element.stopObserving('mousemove');
    },
    
    moveMouseOverPopup: function(event)
    {
        var xcoord = 50;
        var ycoord= -25;

        var scrollOffsets = document.viewport.getScrollOffsets();

	    var viewportWidth = document.viewport.getWidth();
	    var viewportHeight = document.viewport.getHeight();

        var pointerX = Event.pointerX(event) - scrollOffsets.left;
        var pointerY = Event.pointerY(event) - scrollOffsets.top;

        var containerWidth = ImageGallery._popupContainer.getWidth();
        var containerHeight = ImageGallery._popupContainer.getHeight();

        if((pointerX + xcoord + containerWidth) > viewportWidth) 
            xcoord = 0 - (xcoord + containerWidth);
            
        if((pointerY + ycoord + containerHeight) > viewportHeight) 
            ycoord = 0 - (ycoord + containerHeight);

        ImageGallery._popupContainer.style.left = (Event.pointerX(event) + xcoord) + "px";
        ImageGallery._popupContainer.style.top = (Event.pointerY(event) + ycoord) + "px";
    },
    
    _popupContainer: null,
    _popupImage: null,
    _popupWidth: 150,
    _popupHeight: 150,
    
    setMouseOverLinks: function(popupContainer, popupImage, popupWidth, popupHeight, linkClass)
    {
        ImageGallery._popupContainer = $(popupContainer);
        
        if(ImageGallery._popupContainer !=null)
        {
            var container = ImageGallery._popupContainer.cloneNode(true);
            container.id = popupContainer;
            ImageGallery._popupContainer.remove();
            document.body.appendChild(container);
            ImageGallery._popupContainer = $(popupContainer);
        }

        ImageGallery._popupContainer.absolutize();
        ImageGallery._popupImage = $(popupImage);
        ImageGallery._popupWidth = popupWidth;
        ImageGallery._popupHeight = popupHeight;
        
        if(ImageGallery._popupContainer != null && ImageGallery._popupImage != null)
        {
            ImageGallery._popupContainer.hide();
            
            var ImageLinks = $$('a.' + linkClass);
            
            for(var i=0;i<ImageLinks.length;i++)
                ImageLinks[i].observe('mouseover', ImageGallery.showMouseOverPopup);
        }
    },
    
    showSeriesPopup: function(popupId, title)
    {
        Modalbox.options.title = title;
        Modalbox.show($(popupId), {width: 800, height: 600, autoFocusing: false});
    }
    
});

ImageGallery.imageGalleryClickEvent =  ImageGallery.imageGalleryClick.bindAsEventListener(ImageGallery);
