$().ready(function() {

    /*
     * shows the 'add favorite icon' on mouseover 
     * and removes it on mouseout
     */
    $('li.fav_sel').hover(
        function(e){
            $(this).prepend('<div class="fav_sel_icon" style=""></div>');
        },
        function(e){
            $(this).children('div.fav_sel_icon').remove();
        }
    );
    
    /*
     * adds an item to the favorites via ajax call, if successfull
     * the item is added to the favorites widget view
     */
    $('div.fav_sel_icon').live('click', function(e){
       var self = $(this);
       var id =  self.closest('li').attr('id')
	   var src = self.next('a').children('img:first').attr('src');
	   var href = self.next('a').attr('href');
	   self.removeClass('fav_sel_icon').addClass('fav_del_icon');
	   
	   $('#fav_widget_favmenu').children('li').fadeIn('slow');
	   $('#fav_widget_trash').fadeIn('slow');
        $.ajax({
            type: "POST",
            url: base_url + "/favorites/add_favorite",
            data: "proId=" + id + "&proImg=" + src + "&proLink=" + href,
            dataType: "text",
            success: function(result) {
				if (result == 1) {
					$('#fav_widget_favlist').append('<li class="fav_item" id="favitem_' + id + '"><a href="' + href + '"><img src="' + src + '" /></a></li>');
				} else if(result == -1) {
					self.removeClass('fav_del_icon').addClass('fav_sel_icon');
					dialog(target(base_url + '/favorites/exceeded'), 380, 200);
				}
            },
            error: function(result){
                self.removeClass('fav_del_icon').addClass('fav_sel_icon');
            }
        });
    });
    
    /*
     * deletes an item from the favorites list via ajax call
     */
    $('div.fav_del_icon').live('click', function(e){
        var self = $(this);
		var id = self.closest('li').attr('id');
		
        $('#fav_widget_favlist').children('#favitem_' + id).hide('slow', function() { 
            $(this).remove(); 
		});
        self.removeClass('fav_del_icon').addClass('fav_sel_icon');
		
		deleteFavorite(id, self);
    }); 
    
    /*
     * makes the list within the favorites widget sortable
     */
    $('#fav_widget_favlist').sortable( {
        opacity : 0.9,
        placeholder: 'placeholder',
        stop : function (element, ui) {
            if (drag == false) {
                var serial = $('#fav_widget_favlist').sortable('serialize');
                $.ajax({
                    url: base_url + "/favorites/sort_favorites",
                    type: "POST",
                    data: serial,
                    dataType: "text",
                    success: function(msg){},
                    error: function(msg){}
                });
            }
        
            drag = false;
        }
    });
    
    /*
     * disables the selection property (needed for sortable)
     */
    $('#fav_widget_favlist').disableSelection();
    

    /*
     * makes the trash droppable, accepting the favorites items
     */
    $('#fav_widget_trash').droppable({
        accept: '#fav_widget_favlist > li',
        activeClass: 'trash_highlight',
        drop: function(ev, ui) {
            drag = true; //set true to prevent sorting if item is dropped 
            id =  ui.draggable.attr('id').substr(8) ;
            ui.draggable.hide('slow', function() { $(this).remove(); } );
            $('#'+id).children('div.fav_del_icon').remove();
            deleteFavorite(id, ui.draggable);
        }
    });

    /*
     * deletes a favorite item with the specified id with an ajax call
     */
    function deleteFavorite(id, caller) {

		$.ajax( {
            type: "POST",
            url: base_url + "/favorites/delete_favorite",
            data: "proId=" + id,
            dataType: "text",
            success: function(result) {
                deleteCleaner(true, caller);
            },
            error: function(result) {
                deleteCleaner(false, caller);
            }
        });
    }
	
	function deleteCleaner(status, caller) {
        if ($('#fav_widget_favlist').children('li'). length == 1) {
            $('#fav_widget_favmenu').children('li:not(.show_always)').fadeOut('slow');
            $('#fav_widget_trash').fadeOut('slow');
        }
		return status;
	}
    
    /*
     * Save
     */
    $('a.ajaxFavSave').live('click', function(event){  
        dialog($(this), 600, 390);
        return false;
    });
    
    /*
     * Load
     */
    $('a.ajaxFavLoad').live('click', function(event){   
        dialog($(this), 600, 290);
        return false;
    });
    
    /*
     * Clear
     */
    $('a.ajaxFavClear').live('click', function(event){
        dialog($(this), 380, 130);
        $('input.cancelBtn').live('click', dialogBlur);
        return false;
    });
    
    /*
     * Send
     */
    $('a.ajaxFavSend').live('click', function(event){ 
        dialog($(this), 600, 390);
        $('input.cancelBtn').live('click', dialogBlur);
        return false;
    });
	
    
    /*
     * Import Eport
     */
    $('a.ajaxFavImpExp').live('click', function(event){ 
        dialog($(this), 600, 500);
        return false;
    });


});