$().ready(function() {

    /*
     * shows the 'add favorite icon' on mouseover 
     * and removes it on mouseout
     */
    $('.fav_sel').hover(
        function(e) {
            $(this).prepend('<div class="fav_sel_icon fav_sel_icon_'+lang+'" style=""></div>');
        },
        function(e) {
            $(this).children('div.fav_sel_icon').remove();
        }
    );
	
	$('div.fav_sel_icon, div.fav_del_icon').live('mouseover', function(e){
        $(this).next('a.fav_add_border').toggleClass('fav_pmborder');
	});
	
	$('div.fav_sel_icon, div.fav_del_icon').live('mouseout', function(e){
        $(this).next('a.fav_add_border').toggleClass('fav_pmborder')
    });
    
    /*
     * 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){
		/*
		   if($(this).closest('ol').hasClass('fav_login_msg')) {
		   	   $('#dialog').dialog('destroy'); 
		   	   dialog(target(base_url + '/account/login_error_favorites'), 400, 250);
			   return 0;
		   }
		*/
	   var self = $(this);
       var id =  self.closest('.fav_sel').attr('id').substr(3);

	   self.removeClass('fav_sel_icon').addClass('fav_del_icon').addClass('fav_del_icon_' + lang);
	   
	   /*show hidden elements */

       $('#fav_widget_message').fadeOut('slow');
	   $('#fav_widget_favmenu').children('li').fadeIn('slow');
	   $('#fav_widget_favlist').fadeIn('slow');

	   $('#fav_widget_trash').fadeIn('slow');
	   
        $.ajax({
            type: "POST",
            url: base_url + "/favorites/add_favorite",
            data: "proId=" + id,
            dataType: "json",
            success: function(result) {
				    
				if (1 == result.status && $('#favitem_' + result.id).length == 0 ) {
					$('#fav_widget_favlist').append('<li class="fav_item" id="favitem_' + result.id + '"><a href="' + base_url + 'pmsht/' + result.href + '"><img src="' + result.src + '" /></a></li>');
					
				    var rand = Math.random().toString().substr(2);
					
				   /* show bubble */
				    $('.tooltip_left').fadeOut(150);
			        $('body').prepend('<div id="tooltip_'+rand+'" class="trans_dark tooltip_left" style="top: '+(e.pageY)+'px; left: '+(e.pageX - 215)+'px;"><div class="head round_topleft"></div><div class="head_img"></div><div class="body round_bottom"></div></div>');
			        $('#tooltip_'+rand+' > div.body').html(result.msg);
			        $('#tooltip_'+rand).fadeIn(300);
					
					window.setTimeout(function(){
						$('#tooltip_'+rand).fadeOut(300, function(){
							$(this).remove();
						});
					}, 4000);
					
				} else if(-1 == result.status) {
					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('.fav_sel').attr('id').toString().substring(3);
		
        $('#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_'+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);
            }
        });
    }
	
	/*
	 * cleans after the deletion process
	 */
	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');
            $('#fav_widget_message').fadeIn('slow');
        }
		return status;
	}
	
	/* show tooltip */
	$('ul#fav_widget_favmenu > li > a > img').mouseover(function(e){
		var self = this;
		var rand = Math.random().toString().substr(2);
		$('body').prepend('<div id="tooltip_'+rand+'" class="trans_dark tooltip_left" style="top: '+(e.pageY)+'px; left: '+(e.pageX - 215)+'px;"><div class="head round_topleft"></div><div class="head_img"></div><div class="body round_bottom"></div></div>');
		
		$('#tooltip_'+rand+' > div.body').html($(self).attr('alt'));
		$('#tooltip_'+rand).fadeIn(300);
	});
	
	/* hide tooltip */
	$('ul#fav_widget_favmenu > li > a > img').mouseout(function(e){
        $('.tooltip_left').fadeOut(300, function() {
			$(this).remove();
		});
    });
    
    /*
     * 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;
    });


});