var WishList = {
    init: function() {
 //       if ($('#js_clear_wishlist').length) {
            $('#js_clear_wishlist').live('click', function() {
                $('#js_whishlist_wrapper .column_header')
                    .after(
                        '<div class="bgr">' +
                            '<div class="top">' +
                                '<div class="content align_center">' +
                                    '<i>No Entries</i>' +
                                '</div>' +
                            '</div>' +
                        '</div>'
                    );

                $('#js_whishlist_wrapper')
                    .find('.scroller_top, .scroller_area, .scroller_bottom')
                    .add($(this))
                    .remove();

                $.get('/wishlist.html?do=clear', function(){
                     if ($('.js_remove_from_wishlist').length) {
                         $('.js_remove_from_wishlist').attr('class', 'js_add_to_wishlist').text('Add to Cart');
                     }
                });
               
                return false;
            });

            $('.js_wishlist_pager a').live('click', function() {
                WishList.getPage($(this));
                return false;
            });

  //      }

        $('.js_add_to_wishlist, .js_remove_from_wishlist').live('click', function(){
            if ($(this).hasClass('gray')) {
                return false;
            }

            var oClicked = $(this);
            var sAction = $(this).hasClass('js_add_to_wishlist') ? 'add' : 'remove';
            var oScrollerWrapper = $('#js_whishlist_wrapper'); 
            
            $.ajax({
                url: '/wishlist.html',
                type: 'POST',
                dataType: 'json',
                data: {
                    'do': sAction,
                    'username': oClicked.attr('rel')
                },
                /**
                 * Disable button
                 */
                beforeSend: function() {
                    oClicked.addClass('gray');
                                    
                    var ScrollerArea = oScrollerWrapper.find('.scroller_area');
                    if (ScrollerArea.length) { 
                        oScrollerWrapper.find('.scroller_loading')
                            .height(ScrollerArea.height())
                            .css({
                                'top': ScrollerArea.position().top,
                                'left': ScrollerArea.position().left
                            }).show();
                    } 
                },

                success: function(data) {
                    if ('ok' == data.type) {
                        oClicked.toggleClass('js_add_to_wishlist')
                            .toggleClass('js_remove_from_wishlist');

                        if (sAction == 'add') {
                            oClicked.text('Remove from Cart');
                        } else {
                            oClicked.text('Add to Cart');
                        }
                    }
          
                    $.post('/wishlist.html', 
                           {'username': oClicked.attr('rel')},
                           function(data) {
                               oScrollerWrapper.replaceWith(data.content);
                               oScrollerWrapper.find('.scroller_loading').hide();
                           }, "json");
                },
                /**
                 * Redirect browser to login page if 403 status code received
                 */
                error: function(xhr) {
                    if (403 == xhr.status) {
                        location.replace('/login');
                    }
                },
                /**
                 * Enable button
                 */
                complete: function() {
                    oClicked.removeClass('gray');
                }
            });
            return false;
        });
    },

    /**
     * Get specific page via ajax request
     */
    getPage: function(link) {
        var oScrollerWrapper = $('#js_whishlist_wrapper');

        $.ajax({
            url: '/wishlist.html',
            data: { 'page': link.attr('rel') },
            dataType: 'json',
            type: 'POST',

            beforeSend: function () {
                var ScrollerArea = oScrollerWrapper.find('.scroller_area');
                oScrollerWrapper.find('.scroller_loading')
                    .height(ScrollerArea.height())
                    .css({
                        'top': ScrollerArea.position().top,
                        'left': ScrollerArea.position().left
                    })
                    .show();
            },

            success: function (data) {
                if (data.type == 'ok') {
                    oScrollerWrapper.replaceWith(data.content);
                }
            },

            error: function(xhr) {
                if (403 == xhr.status) {
                    location.replace('/login');
                }
            },

            complete: function () {
                 oScrollerWrapper.find('.scroller_loading').hide();
            }

        });
    }
};

$(document).ready(function(){
    WishList.init();
});
