// import handler

/*
----------------------------------------------------------------------------------------------------
SPOTIFY
--------------------------------------------------------------------------------------------------*/
var suname;
var spw;
var numrows;
var evenorodd = "even";
function importSpotifyPlaylists($f) {
	resetList();
	showLoader("#spotifyImport");
	suname = document.getElementById("spotifyLoginForm").su.value;
	spw = document.getElementById("spotifyLoginForm").sp.value;
	
	$.post("/import/getSpotifyPlaylists/", { su: suname, sp: spw },
		function(result) {
			if (result == "loginerror") {
				hideLoader();
				showPrompt("spotifyloginerror");
			} else {
				handlePlaylistURIs(result);
			}
		}
	);
}

function handlePlaylistURIs(result) {
	var uris = result.split(",");
	var numrows = uris.length - 1;
	for (i = 0; i < numrows; i++) {
		if (uris[i].length > 0) {
			getPlaylistFromURI(uris[i], i);
		}
	}
	hideLoader();
}

function getPlaylistFromURI(uri, i) {
	$.post("/import/getOneSpotifyPlaylist/", { uri:uri, su: suname, sp: spw },
		function(result) {
			var row;
			if (i == 0) {
				$("#playlistimportbuttons").show();
				
				$(".importselected").click(function(e) {
					importSelectedPlaylists();
					e.preventDefault();
				});
				$(".importall").click(function(e) {
					importAllPlaylists();
					e.preventDefault();
				});
				
				$("#plitable").show();
				$("#spotifylogin").removeClass("spotBox tabBox");
				$("#spotifylogin").addClass("spotBox shortTop nobb");
			}
			if (evenorodd == "even") {
				row = "<tr class=\"odd\">";
				evenorodd = "odd";
			} else {
				row = "<tr class=\"even\">";
				evenorodd = "even";
			}
			row += result;
			row += "</tr>";
			$("#plitable").children("tbody").append(row);
		}
	);
}

var spotifyimp = 0;
function importSelectedPlaylists() {
	spotifyimp = 0;
	var cb = document.getElementById("importPlaylist").checkbox;
	if (cb.length == undefined) {
		cb = new Array(cb);
	}
	for (i = 0; i < cb.length; i++) {
		if (cb[i].checked == true) {
			var cbox = cb[i];
			spotifyimp++;
			requestPlaylist(cbox.value,cbox);			
		}
	}
	if (spotifyimp == 0) {
		showPrompt("selectplaylist");
	} else {
		showPrompt("spotifyimported", 0, spotifyimp);
	}
}

function importAllPlaylists() {
	spotifyimp = 0;
	var cb = document.getElementById("importPlaylist").checkbox;
	if (cb.length == undefined) {
		cb = new Array(cb);
	}
	for (i = 0; i < cb.length; i++) {
		var cbox = cb[i];
		spotifyimp++;
		requestPlaylist(cbox.value,cbox);	
	}
	showPrompt("spotifyimported", 0, spotifyimp);
}

function requestPlaylist(uri, cb) {
	$(cb).hide();
	var progress = $(cb).parent().parent().find("td.progress");
	var m1 = $(cb).parent().parent().find("td > #mood1").val();
	var m2 = $(cb).parent().parent().find("td > #mood2").val();
	$(progress).html("loading");
	$.post("/import/importSpotifyPlaylist/", { uri:uri, su: suname, sp: spw, mood1:m1, mood2:m2 },
		function(result) {
			$(progress).html("done");
		}
	);
	
}

function addSpotifyPlaylist() {
	resetList();
	showLoader("#spotifyPlaylist");
	var uri = document.getElementById("addSpotify").spotifyuri.value;
	if (uri.length > 16) {
		$.post("/import/addOneSpotifyPlaylist/", { uri:uri },
			function(result) {
				hideLoader();
				if (result == "error") {
					showPrompt('invaliduri');
				} else {
					var row;
					$("#playlistimportbuttons").show();
					
					$(".importselected").click(function(e) {
						importAllPlaylists();
						e.preventDefault();
					});
					$(".importall").click(function(e) {
						importAllPlaylists();
						e.preventDefault();
					});
					
					$("#plitable").show();
					$("#spotifylogin").removeClass("spotBox tabBox");
					$("#spotifylogin").addClass("spotBox shortTop nobb");
					if (evenorodd == "even") {
						row = "<tr class=\"odd\">";
						evenorodd = "odd";
					} else {
						row = "<tr class=\"even\">";
						evenorodd = "even";
					}
					row += result;
					row += "</tr>";
					$("#plitable").children("tbody").append(row);
				}
			}
		);
	} else {
		showPrompt("invaliduri");
	}
}

/*
----------------------------------------------------------------------------------------------------
SHARED
--------------------------------------------------------------------------------------------------*/

function showLoader(anchor) {
	$(function() {
		$("#loader").overlay({
			top: ($(window).height() / 2) - 33,
			expose: { 
				color: '#fff', 
				loadSpeed: 0, 
				opacity: 0.8
			},
			closeOnClick: false,
			api: true
		}).load();
		
	});
}

function hideLoader() {
	$(function() {
		var l = $("#loader").overlay();
		l.close();
	});
}

function resetList() {
	$("#plitable").children("tbody").children("tr").remove();
}

/*
----------------------------------------------------------------------------------------------------
ITUNES
--------------------------------------------------------------------------------------------------*/

function xmlLoaded() {
	var str = "";
	resetList();
	hideLoader();
	$("#import_ifr").ready(function() {
		$("#import_ifr").contents().find("tr").each(function () {
			if (evenorodd == "even") {
				row = "<tr class=\"odd\">";
				evenorodd = "odd";
			} else {
				row = "<tr class=\"even\">";
				evenorodd = "even";
			}
			row += $(this).html();
			row += "</tr>";
			$("#plitable").children("tbody").append(row);
		});
	});
	
	$(".importselected").click(function(e) {
		importItunesSelected();
		e.preventDefault();
	});
	$(".importall").click(function(e) {
		importItunesAll();
		e.preventDefault();
	});
	
	
	$("#playlistimportbuttons").show();
	$("#plitable").show();
	$("#spotifylogin").removeClass("spotBox tabBox");
	$("#spotifylogin").addClass("spotBox nobb");
	
	$.scrollTo( $('#plitable'), 800 );
}

function xmlFailed() {
	showPrompt('xmlfailed');
}	

function importItunesSelected() {
	var cb = document.getElementById("importPlaylist").checkbox;
	var post = new Array();
	if (cb.length == undefined) {
		cb = new Array(cb);
	}
	var c = 0;
	for (i = 0; i < cb.length; i++) {
		if (cb[i].checked == true) {
			post[c] = new Array();
			post[c][0] = cb[i];
			post[c][1] = cb[i].value;
			c++;
		}
	}
	if (c == 0) {
		showPrompt('selectplaylist');
	} else {
		sendItunesRequest(post);
	}
}

function importItunesAll() {
	var cb = document.getElementById("importPlaylist").checkbox;
	var post = new Array();
	
	if (cb.length == undefined) {
		cb = new Array(cb);
	}
	for (i = 0; i < cb.length; i++) {
		post[i] = new Array();
		post[i][0] = cb[i];
		post[i][1] = cb[i].value;
	}
	sendItunesRequest(post);
}

function sendItunesRequest(arr) {
	jqarray = new Array();
	for (i = 0; i < arr.length; i++) {
		var cb = arr[i][0];
		$(cb).hide();
		var m1 = $(cb).parent().parent().find("td > #mood1").val();
		var m2 = $(cb).parent().parent().find("td > #mood2").val();
		jqarray.push(new Array(arr[i][1], m1, m2));
		var progress = $(cb).parent().parent().find("td.progress");
		$(progress).html("loading");
	}
	showLoader("#importPlaylist");
	$.post("/lib/importItunes.php", { 'arr':jqarray },
		function(result) {
			hideLoader();
			showPrompt("itunesimported",0,arr.length);
		}
	);
	
}

function showItunesProgress() {
	showLoader("#iTunesImport");
	
}

/*
----------------------------------------------------------------------------------------------------
MISSING TRACKS
--------------------------------------------------------------------------------------------------*/

function searchMissing(id) {
	$('#btn_' + id).html("");
	$('#btn_' + id).loading(true, { img:'/images/indicator.white.gif', align:'center' });
	$.post("/lib/searchUnknown.php", { 'id':id, 'artist':document.getElementById('artist_' + id).value, 'track':document.getElementById('track_' + id).value },
		function(result) {
			$('#btn_' + id).html(result);
		}
	);	
}

var skipped = 0;
function removeMissing(id) {
	skipped++;
	$.post("/import/removeMissingTrack/", { 'id':id },
		function(result) {
//			if ($('#mt_' + id).parent().find('div:hidden:eq(0)')) {
//				$('#mt_' + id).parent().find('div:hidden:eq(0)').slideDown();
//			}
//			$('#mt_' + id).slideUp("normal", function() { $('#mt_' + id).remove(); });
			
			$('#btn_' + id).remove();
			$('#mt_' + id).fadeTo("fast", .2);
			if (skipped == trackcount) {
				history.go(0);
			}
		}
	);
}

function hideMissing(id) {
	if ($('#mt_' + id).parent().find('div:hidden:eq(0)')) {
		$('#mt_' + id).parent().find('div:hidden:eq(0)').slideDown();
	}
	$('#mt_' + id).slideUp("normal", function() { $('#mt_' + id).remove(); });
}

function setTrackCount(tc) {
	trackcount = tc;
}

/*
----------------------------------------------------------------------------------------------------
GENERAL
--------------------------------------------------------------------------------------------------*/

function updateMoods(playlistid) {
	$.post("/import/updateMoods/", { 'id':playlistid, 'mood1':$("#mood1").val(), 'mood2':$("#mood2").val() }, 
		function(result) {
			document.location.href = "/playlist/" + playlistid + "/";	
		}
	);
}