/* Production LIVE developer API KEY */
_iLikeDevKey = "dk01Vl7eQvFcLk1HlJlL02ecb5_drDZguOL1VI6-nW96JWY=";

/* TEST Staging developer API KEY */
//_iLikeDevKey = "dk01mOHEVZBZw0Mtc0S-8iauJETCue1ttGGe7pj4oHVueKE=";
//_iLikeDevKey = "dk01wVWeq682bqLplofH8z42AhqEsoa3AM8vZJABenkRYvA=";

//local FB key
//fbKey = "565e3fe5d0d718762de4ad8d0e93289b";

//staging FB key
//fbKey = "9ce1c5879ef54ff57294cf75fe7ecbbc";

//live key
fbKey= "ba774d5bc24f226645f6e45e63849d26";




var fbID = "";
var songIDs;
var removedSongIDs="";

function showPlaylist(mode, playlistID, playlistReload) {
	//console.log("playlistID is: " + playlistID);
	
	if (playlistReload == "iLikeOnDisplaySong") {
		iLikeDisplayPlaylist({elId:"p1", playlistName: playlistID, editMode: mode, onDisplay: iLikeOnDisplaySong, maxHeight: "150", maxSongs: "10"});
	}
	else {
		iLikeDisplayPlaylist({elId:"p1", playlistName: playlistID, editMode: mode, onDisplay: iLikeOnDisplaySongNum, maxHeight: "150", maxSongs: "10"});
	}
}

function mySongCallback(song_id, song_name, artist_name) {
	if(song_id - 0 > 0) {
		detailsDiv = document.getElementById("chooser_details");
		if(detailsDiv) {
			detailsDiv.innerHTML = "You chose:<br/>'" + song_name + "' (id: " + song_id + ")<br/> by '" + artist_name +"'";
		}
		iLikeDisplaySong({elId:"s1", trackIds: song_id});
	}
}

function myPlaylistCallback(data) {
	msg = "Last playlist callback:<br/>";
	for(prop in data) {
		msg += prop + ": " + data[prop] + "<br/>";
	}
	detailsDiv = document.getElementById("playlist_details");
	if(detailsDiv) {
		detailsDiv.innerHTML = msg;
	}
}

function iLikeOnDisplaySong(data) {
	msg = "Song callback:<br/>";
	for(prop in data) {
		msg += prop + ": " + data[prop] + "\r\n";
	}
}

function generateRandomID() {
	date_now = new Date();
	var date = date_now.toString();
	//alert("date is: " + date);
	var id = date.replace(/ /gi, "");
	//alert("id is: " + id);
	return id;
}

function resetSongIDs() {
	var songList = songIDs.split(",");
	var deleteList = removedSongIDs.split(",");
	
	var k;
	var l;
	
	for (k=0;k<songList.length;k++) {
		//alert(songList[k]);
		for (l=0;l<deleteList.length;l++) {
			if (songList[k] == deleteList[l]) {
				//alert("deleting");
				songList[k] = "";
			}
		}
	}
	songList = songList.toString();
	songList = songList.replace(",,",",");
	if (Left(songList,1) == ",") {
		songList = Right(songList, songList.length-1);
	}
	if (Right(songList,1) == ",") {
		songList = Left(songList, songList.length-1);
	}
	songIDs = songList;
	removedSongIDs = "";
	//set cookie value
	setCookie('playlistSongID',songList,3650);
}

function checkInit(embed) {
	cPlaylist = getCookie('playlistID');
	if (cPlaylist!=null && cPlaylist!="") {
		//existing
		fbID = cPlaylist;
	}
	else {
		//new
		fbID = generateRandomID();
		setCookie('playlistID',fbID,3650);
    }
	//setCookie('playlistSongID',"",3650);
	
	cSongIDs = getCookie('playlistSongID');
	if (cSongIDs!=null && cSongIDs!="") {
		//existing
		songIDs = cSongIDs;
	}
	else {
		//empty
		songIDs = "";
    }
	getStats();
	getTopSongs();
	getTopPlaylists();
}

function getPlaylistDetails() {
	new Ajax.Request('getbyplaylistid.aspx', {
		method: "post",
		parameters: {playlistid: getCookie("playlistID")},
		onSuccess: function(transport) {
			
			var str = transport.responseText.split("&")
			
			var playlistName = str[1].substring(str[1].indexOf('=')+1);
			var firstName = str[2].substring(str[2].indexOf('=')+1);
			var lastName = str[3].substring(str[3].indexOf('=')+1);
			
			setCookie('txtPlayListName',playlistName,3650);
			setCookie('txtFirstName',firstName,3650);
			setCookie('txtLastName',lastName,3650);
			
			$("txtFirstName").value=getCookie("txtFirstName").toString();
			$("txtLastName").value=getCookie("txtLastName").toString();
			$("txtPlayListName").value=getCookie("txtPlayListName").toString();
			
			
		},
		onFailure: function(transport) {
			if (getCookie("txtFirstName")!="") $("txtFirstName").value=getCookie("txtFirstName").toString();
			if (getCookie("txtLastName")!="") $("txtLastName").value=getCookie("txtLastName").toString();
			if (getCookie("txtPlayListName")!="") $("txtPlayListName").value=getCookie("txtPlayListName").toString();
		}
	});
}

function getSelectedPlaylistDetails(plid) {
		new Ajax.Request('getbyplaylistid.aspx', {
		method: "post",
		parameters: {playlistid: plid},
		onSuccess: function(transport) {
			var playlistName;
			var firstName;
			var lastName;
			
			var str = transport.responseText.split("&");
			
			if ((str[0].substring(str[0].indexOf('=')+1)) == "0") {
				playlistName = str[1].substring(str[1].indexOf('=')+1);
				firstName = str[2].substring(str[2].indexOf('=')+1);
				lastName = str[3].substring(str[3].indexOf('=')+1);
				
				if(playlistName == "undefined"){
					playlistName = getCookie("txtPlayListName").toString();
					firstName = getCookie("txtFirstName").toString();
					lastName = getCookie("txtLastName").toString();
				}else{
					if (getQueryStr("view") != "true") {
						setCookie('txtPlayListName',playlistName,3650);
						setCookie('txtFirstName',firstName,3650);
						setCookie('txtLastName',lastName,3650);	
					}
				}
			}
			else {
				// --- Hide the social sharing ---
				//document.getElementById("playListShareHolder").style.display="none";
				
				var response = transport.responseText.split("=");
				//if((response[1] != "ServerError")&&(response[1] != "-4")){
					playlistName = getCookie("txtPlayListName").toString();
					firstName = getCookie("txtFirstName").toString();
					lastName = getCookie("txtLastName").toString();
				/*}else{
					alert(getCookie("txtPlayListName").toString());
					playlistName = "undefined";
					firstName = "undefined";
					lastName = "undefined";
				}*/
			}
			
			if(playlistName != "undefined"){
				$('playListHolder').innerHTML = "<p style='font-size:14px;font-weight:bold'>" + firstName + " " + lastName + "'s<br /><span>" + playlistName + "</span></p><div id='playListDisplayInfoCount'></div>";
			}
			
			$('playListDisplayInfo').setStyle({display:'none'})
		},
		onFailure: function(transport) {

		}
	});
}

function getEmbedPlaylistDetails(plid) {
	new Ajax.Request('getbyplaylistid.aspx', {
		method: "post",
		parameters: {playlistid: plid},
		onSuccess: function(transport) {
			
			var str = transport.responseText.split("&")
			
			var playlistName = str[1].substring(str[1].indexOf('=')+1);
			var firstName = str[2].substring(str[2].indexOf('=')+1);
			var lastName = str[3].substring(str[3].indexOf('=')+1);
			
			setCookie('txtPlayListName',playlistName,3650);
			setCookie('txtFirstName',firstName,3650);
			setCookie('txtLastName',lastName,3650);
			
			$('playListDisplayInfo').innerHTML = "<p style='font-size:11px;color:#999;padding:0 0 5px 0'>" + getCookie("txtFirstName").toString() + " " + getCookie("txtLastName").toString() + "'s<br><span style='font-size:24px;color:#fff;font-weight:bold'>" + getCookie("txtPlayListName").toString() + "</span></p><div id='playListDisplayInfoCount'></div>";
		},
		onFailure: function(transport) {

		}
	});
}


function getStats() {
	new Ajax.Request('getstat.aspx', {
		onSuccess: function(transport) {
			var str = transport.responseText.split("&")
			
			var songs = addCommas(str[1].substring(str[1].indexOf('=')+1));
			var playlists = addCommas(str[2].substring(str[2].indexOf('=')+1));

			$("playListCounterText").innerHTML = "<span class=songs>" + songs + " songs</span> created in <span class=playlists>" + playlists + " playlists</span> around the world";
			
		},
		onFailure: function(transport) {
			$("playListCounterText").innerHTML = "Unable to retrieve data.";
		}
	});
}

function getTopSongs() {
	new Ajax.Request('getpopular.aspx', {
		method: "post",
		parameters: {type: "songs"},
		onSuccess: function(transport) {
			var str = transport.responseText.split("&");
			
			trackIdsArr = str[1].substring(str[1].indexOf('=')+1) + "," + str[2].substring(str[2].indexOf('=')+1) + "," + str[3].substring(str[3].indexOf('=')+1) + "," + str[4].substring(str[4].indexOf('=')+1) + "," + str[5].substring(str[5].indexOf('=')+1)
			
			iLikeDisplaySong({elId:"topSong", trackIds: trackIdsArr});
		},
		onFailure: function(transport) {
			trackIdsArr = "20366,20368,20372"
			
			iLikeDisplaySong({elId:"topSong", trackIds: trackIdsArr});
		}
	});
}

function getTopPlaylists() {
	new Ajax.Request('getpopular.aspx', {
		method: "post",
		parameters: {type: "playlists"},
		onSuccess: function(transport) {
			
			var str = transport.responseText.split("&");
				
			playListIdsArr = str[1].substring(str[1].indexOf('=')+1) + "," + str[2].substring(str[2].indexOf('=')+1) + "," + str[3].substring(str[3].indexOf('=')+1) + "," + str[4].substring(str[4].indexOf('=')+1) + "," + str[5].substring(str[5].indexOf('=')+1);
			
			playListNamesArr = str[6].substring(str[6].indexOf('=')+1) + "," + str[7].substring(str[7].indexOf('=')+1) + "," + str[8].substring(str[8].indexOf('=')+1) + "," + str[9].substring(str[9].indexOf('=')+1) + "," + str[10].substring(str[10].indexOf('=')+1);
			
			playListIdsArr = playListIdsArr.split(",");
			playListNamesArr = playListNamesArr.split(",");
			
			var html = "";
			for (i=0;i<=4;i++) {
				if (playListIdsArr[i] != "" && playListIdsArr[i] != null) {
					html = html + "<div class=topListRow><div class=topListRowPlay><a href=view-playlist.shtml?plid="+playListIdsArr[i]+"&amp;view=true><img src=../images/playlist/icon-play.png alt=Play width=21 height=22 /></a></div><div class=topListRowTrack><a href=view-playlist.shtml?plid="+playListIdsArr[i]+"&amp;view=true>"+playListNamesArr[i]+"</a></div></div>"	
				}
			}
			$('topList').innerHTML = html
			//alert(playListIdsArr);
			
			
		},
		onFailure: function(transport) {
			$('topList').innerHTML = "Unable to retrive"
		}
	});
}

function validateForm() {
	var txtFirstName = document.getElementById("txtFirstName");
	var txtLastName = document.getElementById("txtLastName");
	var txtPlayListName = document.getElementById("txtPlayListName");
	
	setCookie("txtFirstName",txtFirstName.value,3650);
	setCookie("txtLastName",txtLastName.value,3650);
	setCookie("txtPlayListName",txtPlayListName.value,3650);
	
	if (txtFirstName.value == "First Name" || txtFirstName.value == "" || txtFirstName.value == null) {
		alert("Oops, you haven't entered your first name");
		txtFirstName.value="";
		txtFirstName.focus();
		//document.getElementById("errFirstName").style.display="block";
		return false;
	}
	
	if (txtLastName.value == "Last Name" || txtLastName.value == "" || txtLastName.value == null) {
		alert("Oops, you haven't entered your last name");
		txtLastName.value="";
		txtLastName.focus();
		//document.getElementById("errFirstName").style.display="block";
		return false;
	}
	
	
	if (txtPlayListName.value == "Enter your Playlist Name" || txtPlayListName.value == "" || txtPlayListName.value == null) {
		alert("Oops, you haven't entered your Playlist name");
		txtPlayListName.value="";
		txtPlayListName.focus();
		//document.getElementById("errPlaylistName").style.display="block";
		return false;
	}
	
	submitForm();
	getStats();
	var url = "view-playlist.shtml?plid=" + getCookie("playlistID").toString();
	
	window.location = url;
}

function submitForm() {
	var checkFBIDExist = "";
	var txtFirstName = document.getElementById("txtFirstName");
	var txtLastName = document.getElementById("txtLastName");
	var txtPlayListName = document.getElementById("txtPlayListName");
	
	checkFBIDExist = FB.Connect.get_loggedInUser();
	
	if (checkFBIDExist != null && checkFBIDExist != "") {
		//api call to add name/playlsit to db.
		var form = document.createElement("form");
		form.setAttribute("method", "post");
		form.setAttribute("action", "save.aspx");
		
		
		//fbid
		var formFbid= document.createElement("input");
		formFbid.setAttribute("type", "hidden"); 
		formFbid.setAttribute("name", "fbid"); 
		formFbid.setAttribute("value", FB.Connect.get_loggedInUser()); 
		
		//playlistname
		var formPlaylistName = document.createElement("input");
		formPlaylistName.setAttribute("type", "hidden"); 
		formPlaylistName.setAttribute("name", "playlistname"); 
		formPlaylistName.setAttribute("value", txtPlayListName.value); 
		
		//songid
		var formSongid = document.createElement("input");
		formSongid.setAttribute("type", "hidden"); 
		formSongid.setAttribute("name", "songid"); 
		formSongid.setAttribute("value", getCookie('playlistSongID').replace(/,/g,"|")); 
		
		//playlistid
		var formPlaylistId= document.createElement("input");
		formPlaylistId.setAttribute("type", "hidden"); 
		formPlaylistId.setAttribute("name", "playlistid"); 
		formPlaylistId.setAttribute("value", getCookie('playlistID')); 
		
		//first name
		var formFirstName= document.createElement("input");
		formFirstName.setAttribute("type", "hidden"); 
		formFirstName.setAttribute("name", "firstname"); 
		formFirstName.setAttribute("value", txtFirstName.value); 
		
		//last name
		var formLastName= document.createElement("input");
		formLastName.setAttribute("type", "hidden"); 
		formLastName.setAttribute("name", "lastname"); 
		formLastName.setAttribute("value", txtLastName.value); 
		
		
		form.appendChild(formFbid);
		form.appendChild(formPlaylistName);
		form.appendChild(formSongid);
		form.appendChild(formPlaylistId);
		form.appendChild(formFirstName);
		form.appendChild(formLastName);
		
		document.body.appendChild(form);
		
		//alert(FB.Connect.get_loggedInUser() + "-" + txtPlayListName.value + "-" + getCookie('playlistSongID').replace(/,/g,"|") + "-" + getCookie('playlistID'));
		form.submit();	
	}
}
