var MovingBoxBehavior = Class.create();
MovingBoxBehavior.Load = function() {
    OS.RegisterBehaviour(MovingBoxBehavior.Rules);
}

//MovingBoxBehavior.Gallery = null;
MovingBoxBehavior.Rules = {
    '.movingView>.content': function(element) {
        Element.identify(element);
        var oGallery = new MovingViewModel(element, MovingViewModel.Type.Box);
        Element.setStyle(element, { float: 'none' });
        element.select('.item').each(function(item) {
            Element.setStyle(item, { float: 'none' });

            if (Element.hasClassName('Body', 'viewGallery')) {

                //The following is non-moving box behavior
                Application.SetRollover(item);
                Event.observe(item, 'click', function() {
                    if (!PhotoGalleryModel.AlbumsLoaded) return;
                    var eKey = item.down('.key');
                    if (!eKey) return;
                    var sKey = eKey.innerHTML;
                    var oAlbum = PhotoGalleryModel.PhotoAlbums.First(function(item) { return item.Key == sKey });
                    if (oAlbum != null) {
                        PhotoGalleryBehavior.ShowPhotoViewer();
                        PhotoGalleryBehavior.ActiveAlbum = oAlbum;
                        PhotoGalleryModel.Draw(PhotoGalleryBehavior.Carousel, oAlbum);
                    }
                });
                return;

                //The following allows moving box behavior                
                Event.observe(item, 'mouseover', function() {
                    if (oGallery.currentItemId == item.id) return;
                    MovingBoxBehavior.LoadGalleryItem(oGallery, item.id);
                });
                Event.observe(item, 'click', function() {
                    if (Element.hasClassName(item, 'active')) return;
                    MovingBoxBehavior.LoadGalleryItem(oGallery, item.id);
                });
                var eCancel = item.down('.cmdClose');
                if (eCancel) Event.observe(eCancel, 'click', function() {
                    setTimeout(function() {
                        MovingBoxBehavior.LoadGalleryItem(oGallery, item.id);
                    }, 50);
                });
            } else {
                Event.observe(item, 'click', function() {
                    oGallery.reset();
                    oGallery.update(item.id);
                });
            }
        });
    }
}
MovingBoxBehavior.LoadGalleryItem = function(gallery, itemId) {
    gallery.reset();
    gallery.update(itemId);
}
MovingBoxBehavior.Load();
