(function(){

    var _clipboard = {};
    var _key = 'italmarcoClipboard';

    /**
     *  Schowek na produkty
     */
    Italmarco.Clipboard = {

        init: function(){
            _clipboard = $.jStorage.get(_key);
            if(!_clipboard){
                $.jStorage.flush();
                _clipboard = {};
            }

            Italmarco.Clipboard.createToolbar();
            Italmarco.Clipboard.showItems();
        },

        createdShowClipboardButton: function(){
            $('.clipboard-toolbar', '#content').prepend('<a href="'+$('#clipboard-link').text()+'" class="clipboard-show">Przejdź do schowka</a>');
        },

        createdAddButton: function(){
            if( Italmarco.Clipboard.hasItem(location.href) ){
                Italmarco.Clipboard.createdDeleteButton();
                return false;
            }

            $('.product.info', '#content').before('<p class="clipboard-toolbar"><a href="'+location.href+'" class="clipboard-add">Dodaj do schowka</a></p>')
            $('.clipboard-add', '#content').click(Italmarco.Clipboard.addItem);

            return true;
        },

        createdDeleteButton: function(){
            $('.product.info', '#content').before('<p class="clipboard-toolbar"><a href="'+location.href+'" class="clipboard-delete">Usuń ze schowka</a></p>')
            $('.clipboard-delete', '#content').click(Italmarco.Clipboard.deleteItem);
        },

        createToolbar: function( buttonsSet ){
            switch(buttonsSet){
                case 'delete':
                    Italmarco.Clipboard.createdDeleteButton();
                    Italmarco.Clipboard.createdShowClipboardButton();
                break;
                default:
                case 'add':
                case 'start':
                    Italmarco.Clipboard.createdAddButton();
                    Italmarco.Clipboard.createdShowClipboardButton();
                break;
            }
        },

        addItem:function(){
            _clipboard[location.href] = {
                url: location.href,
                title: $('.product.info .title', '#content').text(),
                properties: $('.product.info .properties', '#content').html(),
                img: $('.product.info .attachment-220x165', '#content').attr('src'),
                className: $('.product.info', '#content').attr('class').replace(/ info /gi, ' ')
            };

            $.jStorage.set(_key, _clipboard);
            $('.clipboard-add', '#content').parent().remove();
            Italmarco.Clipboard.createToolbar('delete');

            return false;
        },

        deleteItem: function(){
            _clipboard = $.jStorage.get(_key);

            if( Italmarco.Clipboard.hasItem(location.href) ){
                delete _clipboard[location.href];

                $.jStorage.set(_key, _clipboard);
                $('.clipboard-delete', '#content').parent().remove();
                Italmarco.Clipboard.createToolbar('add');
            }

            return false;
        },

        showItems: function(){
            if( $('#clipboard-show').length > 0 ){
                var productList = '<ul class="productsList">';
                var isEmpty = true;

                _clipboard = $.jStorage.get(_key);

                for(var product in _clipboard){
                    if (_clipboard.hasOwnProperty(product)) {
                        isEmpty = false;
                        productList += '<li class="'+_clipboard[product].className+'">'+
                            '<h2><a href="'+_clipboard[product].url+'">'+_clipboard[product].title+'</a></h2>'+
                            '<ul>'+_clipboard[product].properties+'</ul>'+
                            '<img width="220" height="165" src="'+_clipboard[product].img+'" class="attachment-post-thumbnail wp-post-image">'+
                        '</li>';
                    }
                }
                productList += '</ul>';

                if(isEmpty){
                    $('#clipboard-show').append('<p>Schowek jest pusty.</p>');
                } else {
                    $('#clipboard-show').append(productList);
                }
            }
        },

        hasItem: function(key){
            for(var product in _clipboard){
                if( product === key ){
                    return true;
                }
            }

            return false;
        }
    }
})();

