var jukebox = jukebox || {};



jukebox.removeItem = function(itemId){

	var url = '/api/jukebox.php';
	var itemTitle = "";
	var waitingDialog = document.createElement("div");
	
	waitingDialog.className = 'flora';
	waitingDialog.id = 'waitingDialog';
	$(waitingDialog).html("<div style='text-align: center;'><img src='/facebox/loading.gif' style='width:32px; height: 32px;'></div>");

	
	$("#bodyRight").append(waitingDialog);
	
	$(waitingDialog).dialog({});
	
	var params = {"itemId": itemId, "c":"getItemInfo"};
	$.getJSON(url,params,function(data){
			itemTitle = data.itemTitle
			var confirmDialog = document.createElement("div");
			confirmDialog.className='flora';
			confirmDialog.id='confirmDialog';
			
			
			$(confirmDialog).html("<div style='text-align: center;'>Are you sure that you would like to remove <span style='font-weight: bold;'>"+itemTitle+" </span>  from the front page?</div>");
			
			$("#bodyRight").append(confirmDialog);
		
			$(confirmDialog).dialog({
				"open" : function() {
					$(waitingDialog).dialog("close");
				},
				"buttons": {
				
					"Ok" : function(){
						
						$(waitingDialog).dialog("open");
						$(confirmDialog).dialog("close");
						var params = {"c":"remove","entity":itemId};
						
						$.getJSON(url,params,function(data){
							if(data.result ==1){
							 var successDialog = document.createElement("div");
							 	successDialog.className='flora';
								successDialog.id='successDialog';
								
								$(successDialog).html("<div style='text-align: center;'>"+itemTitle+" has been removed from front page PlayList!</div>");
								
								$("#bodyRight").append(successDialog);
								
								$(successDialog).dialog({
								"open": function(){
									$(waitingDialog).dialog("close");
									$(waitingDialog).remove();
								},
								"buttons":{
									"Ok": function(){
										$(successDialog).dialog("close");
										$(successDialog).remove();
										
									}
								}
								
								});							
	
								try {
									theMotherhood.audioPlayer.removeRow(itemId);
								} catch(e) {}
								
							}else{
							
							
							}
						
						});
						
					
					},
					"Cancel" : function(){
					
						$(waitingDialog).remove();
						$(confirmDialog).dialog("close");
						$(confirmDialog).remove();
						
					}


		

	}

});

});

}

jukebox.addItem = function(itemId){
	
	var url = '/api/jukebox.php';
	var itemTitle = "";
	var waitingDialog = document.createElement("div");
	
	waitingDialog.className = 'flora';
	waitingDialog.id = 'waitingDialog';
	$(waitingDialog).html("<div style='text-align: center;'><img src='/facebox/loading.gif' style='width:32px; height: 32px;'></div>");

	
	$("#bodyRight").append(waitingDialog);
	
	$(waitingDialog).dialog({});
	
	var params = {"itemId": itemId, "c":"getItemInfo"};
	$.getJSON(url,params,function(data){
		itemTitle = data.itemTitle;
		var confirmDialog = document.createElement("div");
			confirmDialog.className='flora';
			confirmDialog.id='confirmDialog';
			
			
			$(confirmDialog).html("<div style='text-align: center;'>Are you sure that you would like to promote <span style='font-weight: bold;'>"+itemTitle+" </span>  to the front page?</div>");
			
			$("#bodyRight").append(confirmDialog);
		
			$(confirmDialog).dialog({
				"open" : function() {
					$(waitingDialog).dialog("close");
				},
				"buttons": {
					"Ok": function(){
						$(confirmDialog).dialog("close");
						$(confirmDialog).remove();
						$(waitingDialog).dialog("open");
						
						var params = {"entity": itemId, "c": "add"};
						$.getJSON(url,params,function(json){
						
							if(json.result!=-1){
								
							 var successDialog = document.createElement("div");
							 	successDialog.className='flora';
								successDialog.id='successDialog';
								
								$(successDialog).html("<div style='text-align: center;'>"+itemTitle+" has been added to the front page PlayList!</div>");
								
								$("#bodyRight").append(successDialog);
								
								$(successDialog).dialog({
								"open": function(){
									$(waitingDialog).dialog("close");
									$(waitingDialog).remove();
								},
								"buttons":{
									"Ok": function(){
										$(successDialog).dialog("close");
										$(successDialog).remove();
									}
								}
								
								});							
							}else{
							
								 var errorDialog = document.createElement("div");
							 	errorDialog.className='flora';
								errorDialog.id='errorDialog';
								
								$(successDialog).html("<div style='text-align: center;'>Adding "+itemTitle+" to the front page could not be completed at this time<br><Br>"+data.error+"</div>");
								
								$("#bodyRight").append(errorDialog);
								
								$(errorDialog).dialog({
									"open": function(){
									$(waitingDialog).dialog("close");
									$(waitingDialog).remove();
									},
									"buttons" : {
										"Ok":function(){
											$(errorDialog).dialog("close");
											$(errorDialog).remove();
										
										}
									
									}
								
								});
							}
							
							
						});
						
					},
					"Cancel": function(){
						$(confirmDialog).dialog("close");
						$(confirmDialog).remove();
						$(waitingDialog).remove();
					}
				}});
	
	});

}