
var sizelim = 40;
var sizer = 0;




var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



/* generate ourselves a nice li'l AJAX object */
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function updatePublic(div, id, val)
{
  v = (val?1:0);
  xmlhttp.open("POST", "ajax.php?action=switchpublic&method=updatePublic&val="+v+"&pl="+id,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4)
    {
       if(xmlhttp.status ==200)
	   {
		    msg = xmlhttp.responseText;
			var obj = getId(div);
			obj.innerHTML = msg;
	   }
	   else
	   {
		   	obj.innerHTML = "Failed";
	   }
	}
  }

  xmlhttp.send(null)
}

//delete a playlist using ajax, then display a response message
function deleteList(div, id)
{
 
  xmlhttp.open("POST", "ajax.php?action=deletelist&method=deleteList&pl="+id,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4)
    {
       if(xmlhttp.status ==200)
	   {
		    
		    msg = xmlhttp.responseText;
		    
		    var msg2 = msg.split("@");
	        var obj = getId(div);
		    if(msg2[0]=="valid")
	   		{
			    obj.innerHTML = msg2[1];
	    	}
	    	
		    
	   }
	}
  }

  xmlhttp.send(null)
}

//add songs to a playlist, via ajax, when done display a message
function addtoplaylistajax(pl, mp3, div)
{
 
  xmlhttp.open("POST", "ajax.php?action=addtoplaylist&method=addtoplaylist&pl="+pl+"&mp3="+mp3,true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4)
    {
       if(xmlhttp.status ==200)
	   {
		    
		    msg = xmlhttp.responseText;
			obj = getId(div);
			div.innerHTML = msg;
	   }
	}
  }

  xmlhttp.send(null)
}








//cust functions
//our main array which holds all of the playlist info while we're editing/creating
var playlist = Array();
//??
var listsize = 5;
//misc useful variables
var str = "";
var htmlcode = "";
var flashstring = "";
var flashurls = "";
var flashnames = "";
//the maximum number of songs a playlist can contain. 
var maxURLS = 50;

//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function deleteItem()
{
  
}

//move an item up the playlist, redundant, now dragging's been implemented
function moveUp(i)
{
  if(i>0)
  {
	setsave();
    var tmp = playlist[i-1][0];
    var tmp2 = playlist[i-1][1];
    var tmp3 = playlist[i-1][2];
    var tmp4 = playlist[i][0];
    var tmp5 = playlist[i][1];
    var tmp6 = playlist[i][2];
    playlist[i][0]=tmp;
    playlist[i][1]=tmp2;
    playlist[i][2]=tmp3;
    playlist[i-1][0]=tmp4;
    playlist[i-1][1]=tmp5;
    playlist[i-1][2]=tmp6;
  	updateList();
  }
	
}

//ditto...
function moveDown(i)
{
  if(i<playlist.length)
  {	
	setsave();
	if(i<playlist.length-1)
	{
    	var tmp = playlist[i+1][0];
    	var tmp2 = playlist[i+1][1];
    	var tmp3 = playlist[i+1][2];
	}
    var tmp4 = playlist[i][0];
    var tmp5 = playlist[i][1];
    var tmp6 = playlist[i][2];
    if(i<playlist.length-1)
	{
    	playlist[i][0]=tmp;
    	playlist[i][1]=tmp2;
    	playlist[i][2]=tmp3;
	}
    playlist[i+1][0]=tmp4;
    playlist[i+1][1]=tmp5;
    playlist[i+1][2]=tmp6;
    updateList();
  }
	
}

//unused. document cookies were destroying session variables, and were found to be unneccesary anyway. May still be called at points for certain purposes
function remCookie(i)
{


  //document.cookie="mp3listingTEMPURL"+i+"=";	
 // document.cookie="mp3listingTEMPTITLE"+i+"=";	
  //document.cookie="mp3listingTEMPID"+i+"=";	

  //document.cookie.replace("mp3listingTEMPURL"+i+";", "");	
}

//remove an item from the list - due to the reshuffling of the list via dragging, this involves a small amount of complexity, which becomes
//apparent in updateList(), what with all the Sortable things
function removeItem(i)
{
  setsave();
  //var arr = Sortable.sequence("url_list");
  //var j = arr[i];
  //remCookie(j);
  //alert(i);
  playlist.splice(i, 1);
  //alert(playlist.length);
  updateList();
}

//unused
function resetEdit()
{
	
}

//unused
function doEdit(obj)
{

	
	//var txt = obj.innerHTML;
	//obj.innerHTML = "<input type='text' value='"+txt+"'/><span onClick='this.parent.parent.innerHTML=\""+txt+"\"'>RESET</span>";
		
}

//generic input validation function
function addslashes(str) {
str=str.replace(/\'/g,'`');
str=str.replace(/\"/g,'`');
str=str.replace(/\0/g,'\\0');
str=str.replace(/&amp;/g,'&');
return str;
}

//an array that stores all the URL/Title combos modified by this user
var modified = Array();


//update all details in the playlist array, then...
function updatelisttext(obj, id, i)
{
  if(modified.indexOf(playlist[i][2])==-1)
	modified.push(playlist[i][2]);
  var tmp = "";
  for(var t=0; t<modified.length; t++)
  {
		
 	tmp = tmp+","+modified[t];
  }
  document.submitform.modified.value = tmp;
  
	
	var txt = addslashes(obj.value);
	var obj2 = getId(id+""+i);
	if(id=="name")
		playlist[i][0] = txt;
	if(id=="url")
		playlist[i][1] = txt;
	obj2.value = txt;
	setsave();
	updateList();
}
var screated=false;
//update the list on screen, to reflect any changes, and create an associated "Sortable" object.
function updateList()
{
	
  str = "";
  htmlcode = "";
  flashstring = "";
  flashurls = "";
  flashnames = "";
  str+="<ul id='url_list' class='sortablelist'>";
  var j=0;
  var arr;
  var blank=true;
  /*if((getId("url_list")!=undefined))
  {
    Sortable.destroy('url_list');
    
    

  }*/
  
  
  
  for(var i=0; i<playlist.length; i++)
  {
    	
	j=i;
	if(playlist[j]!=undefined)
	{
		blank=false;
		//this should be a short term solution...
		//for some reason there seems to be difficulty implementing the drag-drop stuff
		//in IE, I may well figure out why soon enough, but for the moment I've just put the
		//old system in place, arrows to sort the list up and down
		
		if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<=7))
		{
		str+="<li class='draglistitem' id='url_"+j+"' style='cursor:pointer;' ><span><img alt='[down]' border=0 src='images/downarrow.jpg' onclick='moveDown("+j+");'></span><span><img  alt='[up]' border=0 src='images/uparrow.jpg' onclick='moveUp("+j+");'></span><span><input type='text' onChange='updatelisttext(this, \"url\", "+j+");' size=50 value='"+addslashes(playlist[j][1])+"'></span><span><input type='text' onChange='updatelisttext(this, \"name\", "+j+");' size=40 value='"+addslashes(playlist[j][0])+"'></span><span onClick='removeItem("+j+")' class='	deletelink'><img  alt='[delete]' border=0 src='images/cross.jpg'></span></li>";
		}
		else
		str+="<li class='draglistitem' id='url_"+j+"'><span><img  alt='[drag]' border=0 src='images/tabdrag.jpg'></span><span><input type='text' onChange='updatelisttext(this, \"url\", "+j+");' size=50 value='"+addslashes(playlist[j][1])+"'></span><span><input type='text' onChange='updatelisttext(this, \"name\", "+j+");' size=40 value='"+addslashes(playlist[j][0])+"'></span><span onClick='removeItem("+j+")' class='	deletelink'><img  alt='[delete]' border=0 src='images/cross.jpg'></span></li>";
	 	htmlcode+="<input type='hidden' id='name"+j+"' name='name"+j+"' value='"+addslashes(playlist[j][0])+"'><input type='hidden' id='url"+j+"' name='url"+j+"' value='"+addslashes(playlist[j][1])+"'><input type='hidden' id='id"+j+"' name='id"+j+"' value='"+playlist[j][2]+"'>";
 	}
		
  } 
 

  
  
  str+="</ul>";
  obj = getId("numadded");
  obj.innerHTML = "("+playlist.length+" added)";
  obj = getId("currentp");
  var obj2 = getId("savebutton");
  
  if(blank==true)
  { //disable saving
    
    
    obj2.disabled=true;
  	obj.innerHTML = "<center><h3>There's currently nothing in your playlist.<br/><br/> Enter some mp3 urls and titles in the table above</h3></center>";
	}
  else{
	
	 obj2.disabled=false;
  	 //obj.innerHTML = "<table class='playlist' cellpadding=0 cellspacing=0 border=0>"+str+"</table>";
  	 
 	 
  	 obj.innerHTML = str;
  	 if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<=7))
  	 {}
  	 else
  	 	Sortable.create('url_list', {onUpdate: reshuffle});
  
  	 
	 	 
	 
	 
	}
  obj = getId("htmlformcode");
  obj.innerHTML = htmlcode;
  
  //this possibley breaks things in IE and Opera
 
  
  //Sortable.create('url_list');
  
}

function reshuffle()
{
 setsave();
 
 var arr = Array();
 arr = Sortable.sequence("url_list");
 var tmp = Array();
 for(var i=0; i<playlist.length; i++)
 {
	tmp[i] = Array();
	tmp[i][0] = playlist[i][0];	 
	tmp[i][1] = playlist[i][1];
	tmp[i][2] = playlist[i][2];
 }
 for(var i=0; i<arr.length; i++)
 {
	var t = arr[i];
	playlist[i][0] = tmp[t][0];
	playlist[i][1] = tmp[t][1];
	playlist[i][2] = tmp[t][2];
 }
 
 updateList();

}

function previewChanges()
{

  flashnames="";
  flashurls="";
  //for(i=0; i<playlist.length; i++)
  var arr=Array();
  var j;
  if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<=7))
  {arr=playlist;}
  else
   	arr = Sortable.sequence("url_list");

  	for(i=0; i<arr.length; i++)
  	{
	  	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<=7))
	  		j = i;
	  	else
	  		j=arr[i];
	  	flashnames+= playlist[j][0]+"|";
	  	flashurls+= playlist[j][1]+"|";
  	} 
  
  	flashstring="mp3names="+flashnames.replace(/&amp;/g, "%26")+"&mp3urls="+flashurls.replace(/&amp;/g, "%26");	
  	//alert(flashstring+"String"+j);
  	updatePlayer(flashstring);	
  
  
}

function addToPlaylist(errors)
{
  
  setsave();
  var tmp;
  var url = "";
  var title = "";
  
  sizer = playlist.length;
  var blank = true;
  for(i=0; i<8; i++)
  {
	if(errors[i]==false)
	{
		url = document.urllist.elements[(i*2)].value;  
		title = document.urllist.elements[(i*2)+1].value;  
		if(title!="")
		{
			blank=false;
			sizer++;
			if(sizer>sizelim)
			{
				alert("You can have a maximum of "+sizelim+"\nsongs in a playlist");	
				i=999;	
			}
			else
			{
				playlist.push(Array(title, url, -1));		
				document.urllist.elements[(i*2)].value = "";
    			document.urllist.elements[(i*2)+1].value = "";
			}
    	}
    	
	}
		


  }
  
  if(blank==true)
  	alert("Add some MP3 Urls and Titles into the list above, then hit this button again!\nOnce that's done, hit 'Preview changes...' to see your new songs in action");
  
  updateList();
}








