var WildColoursEyes = function() {
    var PhotoElement = Class.create({
        initialize: function(category, thumb, large, id) {
            this.category = category;
            this.thumb = thumb;
            this.large = large;
            this.id = id;
        },
        toElement: function() {
            var container = new Element('li', {
                id: this.id
            });
            var anchor = new Element('a', {
                href: '/photo-gallery/' + this.category + '/' + this.large,
                className: 'lightwindow',
                title: this.category, //,
                //                rel: 'gallery[' + this.category + ']'
                rel: 'shadowbox[' + this.category + ']'
            });
            var img = new Element('img', {
                src: '/photo-gallery/' + this.category + '/' + this.thumb,
                alt: ''
            });
            anchor.insert(img);
            container.insert(anchor);

            return container;
        }
    });
    var SlideshowElement = Class.create({
        initialize: function(title, image, id) {
            this.title = title;
            this.image = image;
            this.id = id;
            this.visible = false;
        },
        toElement: function() {
            var style = '';
            if (!this.visible)
                style = 'display:none;'
            var container = new Element('li', {
                id: this.id,
                style: style
            });
            var img = new Element('img', {
                src: '/slideshow/' + this.title + '/' + this.image,
                alt: ''
            });
            container.insert(img);

            return container;
        },
        setVisible: function(visible) {
            this.visible = visible;
        }
    });
    var clearList = function(control) {
        var list = $(control);

        while (list.childNodes[0]) {
            list.removeChild(list.childNodes[0]);
        }
    };
    var showSlideshow = function(title, photos) {
        for (var i = 0; i < photos.length; i++) {
            var newSlide = new SlideshowElement(
				    title,
				    photos[i],
				    'photo' + i.toString())
            if (i == 0)
                newSlide.setVisible(true);
            $('slideshow').insert(newSlide);
        }
        Effect.toggle('slideshow', 'Appear', { duration: 0.3 });
    };
    var showPhotos = function(category, photos) {
        for (var i = 0; i < photos.length; i++) {
            var newPhoto = new PhotoElement(
				category,
				photos[i].Thumbnail,
				photos[i].Large,
				'photo' + i.toString())
            $('photos').insert(newPhoto);
        }
        Effect.toggle('photos', 'Appear', { duration: 0.3 });

        //        myLightWindow._setupLinks();
        Shadowbox.setup();
    };
    var updatePhotoCategoryTitle = function(title) {
        Effect.Fade('photo-category', { duration: 0.1, from: 1.0, to: 0.1,
            afterFinish: function() {
                $('photo-category').update(title);
                Effect.Appear('photo-category', { duration: 0.1 });
            }
        });
    };
    return {
        preAnimation: function() {
            // Hide panels
            $('left-side').hide();
            $('right-side').hide();
            $('footer').hide();
        },
        animation: function() {
            if (Prototype.Browser.IE) {
                try {
                    document.execCommand('BackgroundImageCache', false, true);
                } catch (e) { }
            }

            // Remove loading
            $('page-wrapper').style.background = "none";

            if ($('slideshow-wrapper') != null) {
                // Show down the panels
                Effect.BlindDown('left-side', { duration: 0.5,
                    afterFinish: function() {
                        Effect.Appear('right-side-wrapper', { duration: 0.3 });
                    }
                });
                Effect.SlideDown('footer', { duration: 0.5 });
            } else {
                Effect.Appear('left-side', { duration: 0.2 });
                Effect.Appear('right-side-wrapper', { duration: 0.2 });
                Effect.Appear('footer', { duration: 0.2 });
            }
        },
        loadPhotos: function(category) {
            Effect.Fade('photos', { duration: 0.3,
                afterFinish: function() {
                    clearList('photos');

                    new Ajax.Request("/photo-gallery.ashx", {
                        parameters: { cat: category },
                        onSuccess: function(transport) {
                            var photos = transport.responseJSON;
                            updatePhotoCategoryTitle(category);
                            showPhotos(category, photos);
                        },
                        method: "get"
                    });
                }
            });
        },
        loadSlideshow: function(title) {
            SlideShow.stop();
            Effect.Fade('slideshow', { duration: 0.3,
                afterFinish: function() {
                    clearList('slideshow');

                    new Ajax.Request("/slideshow.ashx", {
                        parameters: { title: title },
                        onSuccess: function(transport) {
                            var slides = transport.responseJSON;
                            updatePhotoCategoryTitle(title);
                            showSlideshow(title, slides);
                            SlideShow.start();
                        },
                        method: "get"
                    });
                }
            });
        }
    };
} ();
Event.observe(window,"load", WildColoursEyes.animation);
