//
// Last Modified: 2010/06/02 19:22
//
$(document).ready(function() {
    
    // setup for TESTIMONY page
    $("div.ewp_testimony").bind("click", function(e){
		$(this).children("div.ewp_short-testimony").hide();
		$(this).children("div.ewp_long-testimony").show();
    });
    
    // setup for NEWS TICKER page
    $("#news").fadeIn().newsticker(); 
    
    // setup for DIRECTORY page
    var ewpcookie = $.cookie("ewp_favlist");
    var nameList = "/scripts/getNames.php";
	$("div#favDropArea").load(nameList);
	$("span.delArtist").live("click", function(){
			var success = favArtist($(this), "del", false);
			if (success) { 
				$(this).parent("div").fadeOut(); 
				if ($.cookie("ewp_favlist") == "" || $.cookie("ewp_favlist") == null) { 
					$('div#favDropArea').html("<p>Drag an artist here.</p>"); 
				}
			}
		});

    $artist = $("div.dis_artist"); $favorites = $("div#favDropArea");
    
    $artist.draggable({ 
    	revert: 'invalid', 
    	//containment: $("#found_artists").length ? "#found_artists" : "document",
    	opacity: 0.65,
    	cursor: "move",
    	zIndex: 2700 
    	});
    $favorites.droppable({
			drop: function(event, ui) {
				addFavImage(ui.draggable);
			}
		});

	$("#resetFavList").bind("click", function() {
		var rsp = confirm("Are you sure you wish to clear 'My Picks' list?");
		if (rsp) {
			$.cookie("ewp_favlist", "", { path: "/", expires: -1});
			$('div#favDropArea').html("<p>Drag an artist here.</p>");
		}	
	});
	
	$("#emailList").bind("click", function() {
		$('div#emailListForm').slideDown();
	});

	function addFavImage($item) {
		var ewpcookie = $.cookie("ewp_favlist");
		
		//var name = $item.find("div.dis_artistName").text();
		var ewpid = $item.find("span.ewpid").attr("id");
		//window.status = ewpid;
		//$item.find("div.dis_artistName").appendTo("div#favDropArea")
		$item.fadeOut();
	
		if (ewpcookie == null) { 
			$.cookie("ewp_favlist", ewpid, { path: "/", expires: 1} );
			//window.status = ewpcookie;
		} else {
			ewpcookie = ewpcookie + "|" + ewpid; 
			$.cookie("ewp_favlist", ewpcookie, { path: "/", expires: 1} );
			//window.status = ewpcookie;
		}
		
		var nameList = "/scripts/getNames.php";
		$('div#favDropArea').load(nameList);		
	}
	
	// ADD an artist to the My Picks list
	$("#ewp_favoriteAdd").bind("click", function() {
		favArtist($("#ewp_favoriteAdd"), "add", true);
		$("#ewp_favoriteAdd").remove();
	});
	
	// REMOVE an artist to the My Picks list
	$("#ewp_favoriteDel").bind("click", function() {
		favArtist($("#ewp_favoriteDel"), "del", true);
		$("#ewp_favoriteAdd").remove();
	});

	function favArtist($item, $action, $doAlert) { // $doAlert = true/false
    	var ewpcookie = $.cookie("ewp_favlist");
		var ewpid = $item.attr("rel");
		// Perform Removal of Artist
		if ($action == "del") {
			if (ewpcookie == "" || ewpcookie == null) { 
				alert("Your list is empty.");
			} else {
				var ewplist = ewpcookie.split("|");
				var indx = ewplist.indexOf(ewpid);
				if (indx==-1) { 
					window.status = "ewpid: "+ewpid;
					alert("Error - Artist not in your list"); return false;
				}
				
				var newIds = ewplist.splice(indx,1); // cut out the index
				
				if (ewplist.length >= 1) {
					ewpcookie = ewplist.join("|"); // replace the old cookie
					$.cookie("ewp_favlist", ewpcookie, { path: "/", expires: 1} );
				} else {
					ewpcookie = "";
					$.cookie("ewp_favlist", "", { path: "/", expires: -1} ); // erase the cookie
				}
				
				//$item.html("Removed");
				var msg = "This artist has been removed from your favorites list!";
			}
		} else if ($action == "add") {
		
		// Perform Addition of Artist
			if (ewpcookie == "" || ewpcookie == null) { 
				$.cookie("ewp_favlist", ewpid, { path: "/", expires: 1} );
			} else {
				ewpcookie = ewpcookie + "|" + ewpid; 
				$.cookie("ewp_favlist", ewpcookie, { path: "/", expires: 1} );
			}
			//$item.html("Added");
			var msg = "This artist has been added to your favorites list!"
		}
		if ($doAlert) { alert(msg); }
		return true;
	}

});

