/*
    name : historyRecoverOptions
    file : jquery.historyRecoverOptions.js
    author : gregory tomlinson
    copyright: (c) 2010 bit.ly
    Dual licensed under the MIT and GPL licenses.
    ///////////////////////////
    ///////////////////////////        
    dependencies : jQuery 1.4.2, jquery.cookie.js
    ///////////////////////////
    ///////////////////////////
    
*/

(function($) {
    
    $.fn.historyRecoverOptions = function( options ) {
        // extend the defaults settings
        var el = this, o = $.extend(true, defaults, options), options;
        
        el.bind('hashList', attachOptions);
        
        return this;
        
        // function success(jo) {
        //     console.log(jo)
        // }
        
        function error(jo) {
            console.log('error un deleting', jo)
        }
        
        function attachOptions(e,data) {
            options = el.find('.optionsDropDownContainer');
            
            options.each(function(idx,el){                
                $el = $(el);
                $el.html('<div class="unDeleteButtonBox"><a class="unDeleteButton" href="">Unarchive</a></div>')
            });
            
            el.find('.unDeleteButton').click(function(e) {
                e.preventDefault();
                var $this = $(this), $p = $this.parents('.optionsDropDownContainer'), hash = $p.attr('type');
                
                console.log(hash)
                
                o.params.user_hash = hash;
                connector(o.url, o.params, function(jo) {
                    console.log('success UN deleting', $this)
                    $this.trigger('recoverHistoryItem')
                    $p.find('.unDeleteButtonBox a').replaceWith( $('<span/>').html('Recovered') )
                }, error);
            })
        }

    }
    
    
    var defaults = {
    
        url : '/data/resurrect/',
        params : {
            user_hash : ''
        }
        
    }, $bod;
    

    
    function connector(url, params, callback, error) {
        var str = $.param( params );
        $.ajax({
            dataType: 'json',
            data : str,
            traditional : true,
            jsonp: 'callback',
            'url' : url,
            success: callback,
            'error' : error           
        });
    }

})(jQuery);
