// INIT
var bookmarks = new BookmarkList();
jQuery('#profile_thumbnails_loader').show();
jQuery('#profile_thumbnails').hide();
jQuery(function() {
    var $ptl;
    if (($ptl = jQuery('#profile_thumbnails_loader')) && $ptl.length) {
        $ptl.hide();
        jQuery('#profile_thumbnails').show();
    }

    if (BookmarkList.config.bookmarked_ids)
        bookmarks.addToList(BookmarkList.config.bookmarked_ids);

    // Photo titles (add/remove links)
    jQuery("a[rel=profile_gallery]").fancybox(
        { titlePosition : 'inside'
        , titleFormat: function(title, currentArray, currentIndex, currentOptions) {
            var media_id = currentArray[currentIndex].getAttribute('data-media-id')
              , action = bookmarks.hasBookmark(media_id) ? 'remove' : 'add';
            return jQuery('<a href="#" />')
                .addClass(action + '_bookmark')
                .attr('data-media-id', media_id)
                .html(action.replace(/^(.)/, function(f) { return f.toUpperCase(); }) + ' Bookmark');
          }
        ,'onStart'     : function() {
            jQuery('#right_ad_box').hide();
          }
        ,'onClosed'     : function() {
            jQuery('#right_ad_box').show();
          }
        }
    );

    // Add/Remove Bookmarks
    jQuery('.add_bookmark').live('click', function() {
        var $this = jQuery(this);
        $this.removeClass('add_bookmark').addClass('remove_bookmark').html('Remove Bookmark');
        bookmarks.addBookmark($this.attr('data-media-id'));
        return false;
    });
    jQuery('.remove_bookmark').live('click', function() {
        var $this = jQuery(this);
        $this.removeClass('remove_bookmark').addClass('add_bookmark').html('Add Bookmark');
        bookmarks.removeBookmark(this.getAttribute('data-media-id'));
        return false;
    });
});

// Functions & Data
function BookmarkList(bookmarks) {
    // Methods
    this.addToList = function(bookmarks) {
        var obj = this;
        jQuery.each(bookmarks, function(idx, n) {
            obj.bookmarks[n] = true;
        });
    };

    this.addBookmark = function(id) {
        var successHandler = jQuery.proxy(this.successForAddBookmark, this);
        jQuery.ajax({
            type: 'post',
            url: '/index.php?c=bookmarks&a=add',
            dataType: 'json',
            data: {m_id: id, ajax: 1, ts: (new Date()).getTime()},
            success: successHandler
        });
    };

    this.successForAddBookmark = function(data) {
        if (data.content) {
            jQuery('#top_err_not_message').html(data.content);
        }
    };

    this.removeBookmark = function(id) {
        jQuery.ajax({
            type: 'post',
            url: '/index.php?c=bookmarks&a=remove',
            data: {m_id: id, ajax: 1, ts: (new Date()).getTime()},
            dataType: 'json', 
            success: jQuery.proxy(this.successForRemoveBookmark, this)
        });
    };

    this.successForRemoveBookmark = function(data) {
        if (data.content) {
            jQuery('#top_err_not_message').html(data.content);
        }
    };

    this.hasBookmark = function(id) {
        return (id in this.bookmarks);
    };
    
    // Initialise
    this.bookmarks = {};
    bookmarks && this.addToList(bookmarks);
};

BookmarkList.config = {};


