var PhotoGalleryBehavior = Class.create();
PhotoGalleryBehavior.Load = function() {
    OS.RegisterBehaviour(PhotoGalleryBehavior.Rules);
}
PhotoGalleryBehavior.ActiveAlbum = null;
PhotoGalleryBehavior.Carousel = null;
PhotoGalleryBehavior.Rules = {
    '#Gallery_Albums': function(element) {
        PhotoGalleryModel.LoadPhotoAlbums();
    },
    '#Cnt_Albums .item': function(element) {
        var eView = Element.down(element, '.cmdView');
        var eKey = Element.down(element, '.key');
        if (!eView || !eKey) return;
        var sKey = eKey.innerHTML;
        Event.observe(eView, 'click', function() {
            if (!PhotoGalleryModel.AlbumsLoaded) return;
            var oAlbum = PhotoGalleryModel.PhotoAlbums.First(function(item) { return item.Key == sKey });
            if (oAlbum != null) {
                PhotoGalleryBehavior.ShowPhotoViewer();
                PhotoGalleryBehavior.ActiveAlbum = oAlbum;
                PhotoGalleryModel.Draw(PhotoGalleryBehavior.Carousel, oAlbum);
            }
        });
    },
    '#Cmd_ReturnToAlbums': function(element) {
        Event.observe(element, 'click', function() {
            PhotoGalleryBehavior.ShowAlbums();
        });
    },
    '#Cmd_NextPhoto': function(element) {
        Event.observe(element, 'click', function() {
            PhotoGalleryBehavior.Carousel.next(true);
        });
    },
    '#Cmd_PreviousPhoto': function(element) {
        Event.observe(element, 'click', function() {
            PhotoGalleryBehavior.Carousel.previous(true);
        });
    },
    '#Cnt_PhotoViewer .carousel': function(element) {
        PhotoGalleryBehavior.Carousel = new Carousel('PhotoViewer', element, 85, 44, PhotoGalleryBehavior, {
            setSize: 6,
            duration: .75,
            moveOpacity: .8,
            direction: 'horizontal',
            itemParser: function(item) {
                var eCaption = item.down('.caption');
                var sCaption = null;
                if (eCaption) sCaption = eCaption.innerHTML;
                var eKey = item.down('.key');
                var sKey = eKey.innerHTML;
                return { caption: sCaption, key: sKey };
            },
            setItemEvents: function(carousel, itemElement, carouselItem, observer) {
                Event.observe(itemElement, 'click', function() {
                    carousel.activate(carouselItem);
                });
                Event.observe(itemElement, 'mouseover', function() {
                    Element.addClassName(itemElement, 'hover');
                });
                Event.observe(itemElement, 'mouseout', function() {
                    Element.removeClassName(itemElement, 'hover');
                });
            },
            unsetItemEvents: function(carousel, itemElement, carouselItem, observer) {
                Event.stopObserving(itemElement, 'click');
                Event.stopObserving(itemElement, 'mouseover');
                Event.stopObserving(itemElement, 'mouseout');
            }
        });
    }
}
PhotoGalleryBehavior.fireActiveCarouselLoaded = function(carousel) {
}
PhotoGalleryBehavior.fireDeactiveCarouselItem = function(carousel, element, item) {
    Element.removeClassName(element, 'selected');
}
PhotoGalleryBehavior._hidePhotos = function(targetId) {
    var ePhotoContainer = $('Media_Photo');
    ePhotoContainer.select('img').each(function(img) {
        if (img.id != targetId) Element.hide(img);
    });
}
PhotoGalleryBehavior.fireActiveCarouselItem = function(carousel, element, item) {
    Element.addClassName(element, 'selected');

    //Load the photo
    var ePhotoContainer = $('Media_Photo');
    var sId = 'Img_' + item.key;
    var eImg = $(sId);
    if (!eImg) {
        var oImg = PhotoGalleryBehavior.ActiveAlbum.Photos.First(function(photo) { return photo.Key == item.key });
        if (!oImg) return;
        eImg = OS.AppendChild(ePhotoContainer, 'img');
        Element.hide(eImg);
        eImg.id = sId;
        eImg.src = 'thumbnail.ashx?scope=4&ref=' + oImg.ImagePath + '&width=600&height=401&forceQuality=true&cacheSize=true';
        eImg.onload = function() {
            Element.show(eImg);
            Element.absolutize(eImg);
            Element.hide(eImg);
            PhotoGalleryBehavior._hidePhotos(eImg.id);
            var ef = new Effect.Appear(eImg, { duration: .25 });
        }
    } else {
        PhotoGalleryBehavior._hidePhotos(eImg.id);
        var ef2 = new Effect.Appear(eImg, { duration: .25 });
    }
}
PhotoGalleryBehavior.ShowAlbums = function() {
    Element.hide('Mv_PhotoViewer');
    Element.show('Gallery_Albums');
}
PhotoGalleryBehavior.ShowPhotoViewer = function() {
    Element.hide('Gallery_Albums');
    Element.show('Mv_PhotoViewer');
}

PhotoGalleryBehavior.Load();

