dojo.require("dojo.fx");
dojo.require("dojo.dnd.Source");
var anim = dojo.fx.animation;

function makeDraggable(saToggle) {
	if(typeof(saToggle) == "undefined") { saToggle = 0 };
	
	if(dojo.byId("listReturn")) {_makeDraggable(saToggle)}
}

function _makeDraggable(saToggle) {
	
	var listReturnDSource = new dojo.dnd.Source("listReturn", {copyOnly: true, selfAccept: false});
	if (saToggle > 0) { 
		listReturnDSource.selfAccept = true;
				
		dojo.connect(listReturnDSource, "onDndDrop", function(source, nodes, copy, target){
			if(dojo.dnd.manager().target !== this){//set so this function only fires for this object - lame but necessary
				return;
			}
			if(target.parent.id == "listReturnBody") {
				collection.store.updateCurrentPlaylist();
			}
		});
	}
	
	listReturnDSource.forInItems(function(item, id, map){
		//item.type[0] = "myDrag";
		var fullClassName = dojo.byId(id).className;
		if (fullClassName.indexOf("dragLibrary") > 0) {
			item.type[0] = "library";
		} else if (fullClassName.indexOf("drag") > 0) {
			item.type[0] = "both";
		} else {
			item.type[0] = "other";
		}
	});
}

function displayUMessage(UMessageText, UMessageType, UMessageGraphic) {
	
	var myBox = document.createElement("div");
	myBox.className= "umessageBox";
	
	//myBox.style.left = (ciWidth - 200) + "px";
	//myBox.style.top = (ciHeight - 69) + "px";
	myBox.style.right = 40 + "px";
	myBox.style.bottom = 20 + "px";
	myBox.style.opacity = 0;
	
	var mbShadow = document.createElement("div");
	mbShadow.className = "shadowHold";
	
	var mbMain = document.createElement("div");
	mbMain.className = "umMain";
	mbMain.innerHTML = (UMessageGraphic != "add") ? "<span class=\"check\">&#252;<\/span>" : "<span>+<\/span>";
	//mbMain.innerHTML = (UMessageType == 1) ? "<span class=\"check\">&#252;<\/span>" : "<span>+<\/span>";
	//mbMain.innerHTML = "<span class=\"check\">&#252;<\/span>";
	
	//UMessageText = "(X) tracks deleted from your Library";
	mbMain.innerHTML +=  html_escape(UMessageText);
	
	mbShadow.appendChild(mbMain);
	myBox.appendChild(mbShadow);
	
	document.body.appendChild(myBox);

	var fadeArgs = new Object;
	fadeArgs.node = myBox;
	fadeArgs.duration = 700;
	dojo.fadeIn(fadeArgs).play();

	
	var ntLeft = myBox.style.left;
	ntLeft = ntLeft.replace(/px/, "");
	var ntTop = myBox.style.top;
	ntTop = ntTop.replace(/px/, "");
	ntTop-=40;
	
	function whenDone1(nodes, anim) {
		document.body.removeChild(myBox);
	}
		
	//var anim = dojo.lfx.fadeOut(myBox, 2000, 0, whenDone1);
	fadeArgs = new Object;
	fadeArgs.node = myBox;
	fadeArgs.delay = 3000;
	fadeArgs.duration = 2000;
	fadeArgs.onEnd = function() {
		document.body.removeChild(myBox);
	}
	
	dojo.fadeOut(fadeArgs).play();
	
	// setTimeout(function() { anim.play(); }, 3000); //wrapped function here for local scope junk
	/**/
	//if(typeof(console) != "undefined") { console.log(UMessageText);}
	//window.status = UMessageText;
	/* var umwImage = "";
	switch(UMessageGraphic) {
		case "play": 	umwImage = "<div class=\"umwPlay\"></div>";
						break;
		case "add": 	umwImage = "<div class=\"umwAdd\"></div>";
						break;
	}
	dojo.byId("umessageContents").innerHTML = umwImage + html_escape(UMessageText);
	if(UMessageText != "") {
		try { anim.stop(); } catch(e) { } // anim.stop() has no properties until we have run the first animation
		dojo.byId('umessageContents').style.opacity = 100;
		dojo.byId('umessageContents').style.filter = "Alpha(Opacity=100)";
		if(UMessageType == 1) {
			anim = dojo.lfx.html.fadeOut('umessageContents', 7000);
			anim.play(0, true);
		} else {
			// YFT
			// anim = dojo.lfx.html.highlight('umessageContents',"#fbff9a",2000);
			// anim.play(0,true);
		}
	} */
}

var libNode;
function makeLibraryDroppable() {
	if (libNode == null) { 
	
	//console.log("yo null2");
	libNode = dojo.query("#artistTarget")[0];
	
	//create place for our drops to live
	var mDiv = document.createElement("div");
	mDiv.id = "libDropTargetHolder";
	//mDiv.style.border = "1px solid #0000FF"; //show it for now
	
	libNode.parentNode.appendChild(mDiv);
	
	//var dropTargetLogo = new dojo.dnd.Target(logoNode, {accept: ["myDrag"]});
	var dropTargetArtist = new dojo.dnd.Target(libNode, {accept: ["both"]});
	//cart.parent = dojo.query("#cartNode tbody")[0];
	dropTargetArtist.parent = mDiv;
	
	dojo.connect(dropTargetArtist, "onDndDrop", function(source, nodes, copy, target){
		if(dojo.dnd.manager().target !== this){//set so this function only fires for this object - lame but necessary
			return;
		}
		var elementIDs = "";
		var listType;
		if(target.parent.id == "libDropTargetHolder") {
			for(var j=0; j<nodes.length; j++){
				if (j) {
					elementIDs += ",";
				} else {
					switch(nodes[j].id.substring(0, 1)) {
						case "t":	listType = 0;
									break;
						case "a":	listType = 1;
									break;
						case "p":	listType = 2;
									break;
					}
				}
				elementIDs += nodes[j].id.substring(1);
			}
			AddToLibrary(elementIDs, listType);
		}
		
		//clean up dropped nodes
		dropTargetArtist.selectAll();
		dropTargetArtist.deleteSelectedNodes();
	});
	}

}

function makePlaylistsDroppable() {
	var droppablePlaylists = dojo.query(".dropLibrary");
	
	for(var i=0; i<droppablePlaylists.length; i++){
		//console.log("makePlaylistsDroppable iteration: " + i + ", pn: " + droppablePlaylists[i].parentNode.id);
		var currentPlaylistDrop = new dojo.dnd.Target(droppablePlaylists[i], {accept:["both", "library"]});
		
		//quick dirty tmp
		var holdnode = dojo.query("#libDropTargetHolder")[0];
		currentPlaylistDrop.parent = holdnode;
	
		dojo.connect(currentPlaylistDrop, "onDndDrop", function(source, nodes, copy, target){
			if(dojo.dnd.manager().target !== this){//set so this function only fires for this object - lame but necessary
				return;
			}
			var elementIDs = "";
			var listType;
			var playlistID;
			
			if(target.parent.id == "libDropTargetHolder") {
				for(var j=0; j<nodes.length; j++){
					if (j) {
						elementIDs += ",";
					} else {
						playlistID = target.node.id.substring(8);
						switch(nodes[j].id.substring(0, 1)) {
							case "t":	listType = 0;
										break;
							case "a":	listType = 1;
										break;
							case "p":	listType = 2;
										break;
						}
					}
					elementIDs += nodes[j].id.substring(1);
					
					//
					/*
					var elementID = nodes[j].id;
					switch(elementID.substring(0, 1)) {
						case "t":	// universalMessage += "track ";
									if(target.node.id.substring(8) != 0) {
										libraryDisplay.addTrackToPlaylist(elementID.substring(1), "Track Name", target.node.id.substring(8));
									} else {
										showNewPlaylistPop(elementID.substring(1), 0);
									}
									break;
						case "a":	// universalMessage += "album " + elementID.substring(1);
									libraryDisplay.addContentToPlaylist(elementID.substring(1), 1, target.node.id.substring(8));
									// collection.store.addItem(elementID.substring(1), 1);
									break;
						case "p":	// universalMessage += "playlist " + elementID.substring(1);
									libraryDisplay.addPlaylistToPlaylist(elementID.substring(1), 2, target.node.id.substring(8));
									break;
					}
					*/
				}
			}
			switch(listType) {
				case 0:	if(playlistID != 0) {
							if (elementIDs.indexOf(",") == -1) {
								libraryDisplay.addTrackToPlaylist(elementIDs, "Track Name", playlistID);
							} else {
								libraryDisplay.addTracksToPlaylist(elementIDs, playlistID);
							}
						} else {
							showNewPlaylistPop(elementIDs, listType);
						}
						break;
				case 1:	libraryDisplay.addContentToPlaylist(elementIDs, listType, playlistID);
						break;
				case 2:	libraryDisplay.addPlaylistToPlaylist(elementIDs, listType, playlistID);
						break;
				
			}
			
			//clean up dropped nodes
			target.selectAll();
			target.deleteSelectedNodes();
			//currentPlaylistDrop.selectAll();
			//currentPlaylistDrop.deleteSelectedNodes();
		});
	}
}

function makeListReturnDroppable() {
	return false;
	var drop = new dojo.dnd.Target(dojo.byId("listReturnBody"), ["playlist"]);
	drop.onDropEnd = function(event) {
		collection.store.updateCurrentPlaylist();
	}
}

var logoNode;
function makeLogoTarget() {
	if (logoNode == null) { 
	
	//console.log("yo null2");
	logoNode = dojo.query(".logoDrop")[0];
	logoNode.id = "primaryLogoDrop";
	
	
	//var dropTargetLogo = new dojo.dnd.Target(logoNode, {accept: ["xtable"]});
	var dropTargetLogo = new dojo.dnd.Target(logoNode, {accept:["library","both"]});
	
	dojo.connect(dropTargetLogo, "onDndDrop", function(source, nodes, copy, target){
		if(dojo.dnd.manager().target !== this){//set so this function only fires for this object - lame but necessary
			return;
		}
		//console.log("drop");
		if(target.parent.id == "primaryLogoDrop") {
			//console.log("nodes id", nodes[0].id);
			
			for(var j=0; j<nodes.length; j++){
				var elementID = nodes[j].id;
				//console.log(elementID);
				switch(elementID.substring(0, 1)) {
					case "t":	Queue(elementID.substring(1), 0);
								break;
					case "a":	Queue(elementID.substring(1), 1);
								break;
					case "p":	Queue(elementID.substring(1), 2);
								allPlaylists = "";
								break;
					//MATTNOTE: what about works - ugh
				}
			}
		}
		
		dropTargetLogo.selectAll();
		dropTargetLogo.deleteSelectedNodes();
		//console.log("unsuccessful logo drop2");
	});
	}
	//console.log("makeLogoTarget complete");
}

function makeDroppable() {
	makeLibraryDroppable();
	makePlaylistsDroppable();
}

function initDrag() {
	//makeDraggable();
	//makeDroppable();
	//makeLogoDroppable();
	
	makeLogoTarget();
	makeLibraryDroppable();
	//console.log("initDrag complete");
}



