var extplaylistdebug = false;

function debugMessage(str) {
	if (extplaylistdebug)
		alert(str);
}

//**************************************************
//  Set cookie "playlist2"
//**************************************************
function setCookiePlaylist(cookieValue) {
document.cookie = "playlist2" + "=" + escape(cookieValue) + 
		 ";path=/;domain=batanga.com";
}


//**************************************************
//  Get playlist (from cookie playlist2)
//**************************************************
function getPlaylist() {
	return getCookie('playlist2');
}

function isEmptyPlaylist() {
  currentCookiePlaylist = getPlaylist();   
  return (currentCookiePlaylist == null || currentCookiePlaylist == "");
}

function nothingIsBeingPlayed() {
  nowPlaying = getCookie('nowPlaying');   
  return (nowPlaying == null || nowPlaying == "");
}


//**************************************************
//   Add entity to Playlist from an Externale page
//	 AddLinkId is the addlink
//**************************************************
function addToPlaylistFromExtPage(guid){
  guid = guid.replace("{","");
  guid = guid.replace("}","");
  debugMessage("going to add "+guid+" to playlist");
  addToCookiesPlaylist(guid,'1');
  debugMessage("added "+guid+" to playlist");

  debugMessage("going to set display for AddToPlaylist-"+guid+" and AddedToPlaylist-"+guid);
  try
  {
   document.getElementById('AddToPlaylist-'+guid).style.display='none';
   debugMessage("Good for AddToPlaylist-"+guid);
  }catch(err){
	   debugMessage("Failed on AddToPlaylist-"+guid);
  }

  try
  {
   document.getElementById('AddedToPlaylist-'+guid).style.display='';
   debugMessage("Good for AddedToPlaylist-"+guid);
  }catch(err){
	   debugMessage("Failed on AddedToPlaylist-"+guid);
  }


}

//**************************************************
//  Put Add entities link in external pages
//**************************************************
function PutExternalAddLink() {
}


//**************************************************
//  Check Add entities link in external pages
//**************************************************
function checkExternalAddLink(retry)
{
	var cookiePlaylist = getCookie('playlist2');
	var cookieNowPlaying = getCookie('nowPlaying');

	debugMessage("To check add links");


		debugMessage("Playlist is not empty");
		var alldivs = document.getElementsByTagName("div"); 

		for (var i = 0; i < alldivs.length; i++)
		{ 
			if (alldivs[i].getAttribute('id') != null && alldivs[i].getAttribute('id').match('AddToPlaylist-'))
			{ 
				debugMessage("Find one div id="+alldivs[i].getAttribute('id'));
				guid = alldivs[i].getAttribute('id').substr(14);
				if (cookiePlaylist!= null && cookiePlaylist!='' && cookiePlaylist.match(guid)) {
					debugMessage("this guid="+guid+" is in the playlist, let's update add links");
					alldivs[i].style.display = "none";
					try {document.getElementById('AddedToPlaylist-'+guid).style.display = "";}catch(e){}
					try {document.getElementById('NowPlaying-'+guid).style.display = "none";}catch(e){}
				} else if (cookieNowPlaying != null && cookieNowPlaying != "" &&
							cookieNowPlaying.match(guid)) {
					try {document.getElementById('NowPlaying-'+guid).style.display = "";}catch(e){}
					try {document.getElementById('AddedToPlaylist-'+guid).style.display = "none";}catch(e){}
					alldivs[i].style.display = "none";
				} else {
					debugMessage("this guid="+guid+" is NOT not the playlist, let's update add links");
					alldivs[i].style.display = "";
					try {document.getElementById('AddedToPlaylist-'+guid).style.display = "none";}catch(e){}
					try {document.getElementById('NowPlaying-'+guid).style.display = "none";}catch(e){}
				}
				
			}
		}
   debugMessage("Done checking!");

	if (retry) 
		setTimeout("checkExternalAddLink(true);",3000);
}


//**************************************************
//  Add entity to Cookie "playlist2" 
//**************************************************
function addToCookiesPlaylist(guid,position){
 var cookiePlaylist = getPlaylist();
 if(cookiePlaylist=='' || cookiePlaylist==null)
    {
		setCookiePlaylist(guid+"*");}
 else
    {if (position=='1')
      {		
		setCookiePlaylist(guid+"*"+cookiePlaylist);}
      else
      {
		setCookiePlaylist(cookiePlaylist+guid+"*");}
    }   
}
