// Script to handle all the functions around dealing with favorites

$(document).ready(function() {
    var $fav_obj = $("#my_favorites");
    var tsTimeStamp = new Date().getTime();

//    alert($fav_obj.length);
    if ($fav_obj.length) {
        // we have some favorites div present, now we can work on it.

	var no_favorites = "<span id='no_favorites'></span>";
	// fetch the list of favorites from the server and populate the div.
	$.ajax({
		dataType: "html",
		url: '/ajax/favorites/fav_list/' + tsTimeStamp,
		success: function (data, textStatus) {
        	    tsTimeStamp = new Date().getTime();
	    	    $fav_obj.html(data);

		    var text = data.replace(" ", "");
		    if (!text) {
			// no favorites
			$fav_obj.html(no_favorites);
		    }
		},
		error: function(data) {
		    alert ('error fetching of the page');
		}
        });

	$(".fav-add").live('click', function() {
	    var $mid = $(this).attr("mid");
            $("#mid"+$mid).hide();
            $("#mid-added"+$mid).show();

            $.ajax({
                url: '/ajax/favorites/fav_add/mid' + $mid + '/' + tsTimeStamp,
 	        success: function (data) {
		    var count = $("#my_favorites_count").html();
		    if (count == undefined) {
			count = 0;
		    }
		    count = parseInt(count) + 1;
		    $("#my_favorites_count").html(count);

	            $.ajax({
			dataType: "html",
	                url: '/ajax/favorites/fav_list/' + tsTimeStamp,
		        success: function (data) {
	    	            $("#my_favorites").html(data);
        	    	    tsTimeStamp = new Date().getTime();
	                }
                    });
	        }
            });
	    return false;
        });

    	$(".fav-delete").live('click', function() {
	    var $mid = $(this).attr("mid");
	    if ($mid == undefined) {
                $mid = "";
            }
	    $("#mid"+$mid).show();
            $("#mid-added"+$mid).hide();

	    $.ajax({
	        url: '/ajax/favorites/fav_delete/mid' + $mid + '/' + tsTimeStamp,
	        success: function (data) {
		    var count = $("#my_favorites_count").html();
		    count = parseInt(count) - 1;
		    $("#my_favorites_count").html(count);
	    	    tsTimeStamp = new Date().getTime();

	            $.ajax({
			dataType: "html",
	    	        url: '/ajax/favorites/fav_list/' + tsTimeStamp,
		        success: function (data) {
        	    	    tsTimeStamp = new Date().getTime();
	    	            $("#my_favorites").html(data);
			    var text = data.replace(" ", "");
			    if (!text) {
				// no favorites
				$fav_obj.html(no_favorites);
			    }
	   	        }
                    });
	        }
            });
	    return false;
        });

//	$fav_obj.sortable({
//	    placeholder: 'ui-state-highlight'
//	});
//	$fav_obj.disableSelection();

	$fav_obj.live('sortstop', function() {
	    var $list = "";
	    $("li", $fav_obj).each(function() {
	        var id = $(this).attr('id');
		$list += "|" + id;
	    });
	    $.ajax({
	        url: '/ajax/favorites/fav_reorder/list' + $list + '/' + tsTimeStamp
            });
	});

	// now search for all add favorites divs hide all the ones that we currently
	$.getJSON("/ajax/favorites/fav_json/" + tsTimeStamp, function(data) {
	    var count = 0;
	    if (data && data.favs) {
		$.each(data.favs, function(i, list){
	            var $text = list.id;
	            count = count + 1;
		    $("#mid" + $text).hide();
		    $("#mid-added" + $text).show();
    		});
		$("#my_favorites_count").html(count);
		tsTimeStamp = new Date().getTime();
	    }
	});

    }

});

