Powered.UserGalleryControl = Class.create({
    viewId: null,
    navId: null,
    userId: null,
    selectedMediaId: null,
    selectedGalleryId: null,
    defaultGalleryId:null,
    currentView:'Gallery',
    readOnly:false,

    initialize: function(viewId, navId, selectedGalleryId, defaultGalleryId, disabled, userId) {
        if (!viewId || !navId) {
            alert("You must have an ID for your view and the nav divs!");
            return;
        }
        this.viewId = viewId;
        this.navId = navId;
        this.userId= userId;
        this.selectedGalleryId = selectedGalleryId;
        this.readOnly=disabled;
        this.defaultGalleryId =  defaultGalleryId;
    },

    refreshGalleryNav: function(userId) {
        new Ajax.Updater(this.navId, Powered.contextPath + "/profile/fragments/galleryNav.jsp",
        {  parameters: "userId=" + userId,
            asynchronous:true,
            evalScripts:true
        });
    },

    updateGalleryNav: function(galleryId) {
        new Ajax.Updater('Gallery_' + galleryId, Powered.contextPath + "/profile/fragments/galleryNavEntry.jsp",
        {  parameters: "id=" + galleryId,
            asynchronous:true,
            evalScripts:true
        });
    },

    updateGalleryView: function(galleryId, pageNum) {
        this.currentView='Gallery';
        new Ajax.Updater(this.viewId, Powered.contextPath + "/profile/fragments/galleryContent.jsp",
        {
            parameters: "galleryId=" + galleryId + "&page=" + pageNum +"&userId="+this.userId,
            asynchronous:true,
            evalScripts:true
        });
        $('Gallery_' + this.selectedGalleryId).removeClassName("Selected");
        this.selectedGalleryId = galleryId;
        $('Gallery_' + this.selectedGalleryId).addClassName("Selected");
        this.selectedMediaId = null;
    },

    deleteGallery: function(control, galleryId) {
        new Ajax.Updater(this.navId, Powered.contextPath + "/userGallery/delete.jsp",
        {
            parameters: "id=" + galleryId,
            asynchronous:true,
            evalScripts:true,
            onSuccess:function() {
                 control.refreshGalleryNav(control.userId);
                 control.updateGalleryView(control.defaultGalleryId, 1);
            }
        });

    },

    onRemove: function () {
          Powered.pageModal = Powered.Modal.show(Powered.contextPath + "/files/fragments/fileRemoveMenu.jsp",
          {id: 'RemovePhoto', title: 'Remove Photo',  params: 'id=' + this.selectedMediaId+ '&galleryId='+this.selectedGalleryId});
    },

    removePhoto:function (control, galleryId, mediaId) {
        new Ajax.Updater('Modal_content', Powered.contextPath + "/files/fragments/fileRemove.jsp",
        {
            parameters: "id=" + mediaId +  "&galleryId=" + galleryId+"&currentGalleryId="+this.selectedGalleryId,
            asynchronous:true,
            evalScripts:true,
            onSuccess: function() {
                Powered.pageModal.resizeToContent();
                Powered.pageModal._findFocusableElements();
                if (galleryId == control.selectedGalleryId) {
                    control.updateGalleryView(galleryId, 1);
                }
            }
        });
    },

    deletePhoto: function(control, galleryId, mediaId) {
        Powered.pageModal.hide();
        var confirm = new Powered.Confirm($('DeletePhotoButton'), {
            msg: 'Do you wish to delete this photo from the entire site?',
            onSuccess:function() {
                new Ajax.Updater('Notification', Powered.contextPath + "/files/fragments/fileDelete.jsp",
                {
                    parameters: "mode=" + control.currentView + "&id=" +mediaId + "&galleryId=" + galleryId,
                    asynchronous:true,
                    evalScripts:true,
                    onSuccess: function() {
                        if (galleryId != control.defaultGalleryId) {
                            control.updateGalleryNav(galleryId);
                        }
                        control.updateGalleryView(galleryId, 1);
                    }
                });
                 window.setTimeout(function() {control.clearNotification();}, 3000);
            },
            okButtonText: 'Delete'
        });
    },
    
    setAlbumCover:function (control, mediaId) {
         new Ajax.Updater('Notification', Powered.contextPath + "/userGallery/setAlbumCover.jsp",
        {
            parameters: "id=" + mediaId + "&galleryId=" + this.selectedGalleryId,
            asynchronous:true,
            evalScripts:true
        });
         window.setTimeout(function() {control.clearNotification()}, 3000);
    },

    clearNotification:function() {
          $('Notification').innerHTML ='';
    }
});