var zeHeight = window.screen.height-120;
var numz = 0;
var doit1 = "";
var doit2 = "";
var popupWin;
var	myPlayList = new Array();


	function openPlayer(reload) {
  //alert(zeHeight);
		if(!reload){
			if(!popupWin || popupWin.closed){
				popupWin = window.open("inc/player.html", "RLR Music Player", "status=0, location=0, scrollbars=1, resizable=no, height="+zeHeight+", width=492");
				popupWin.focus();
			}else{
				popupWin.focus();
				popupWin.update(numz-1 , myPlayList[numz-1]["name"], myPlayList[numz-1]["mp3"]);
			}
		}else{
			popupWin = window.open("", "RLR Music Player", "status=0, location=0, scrollbars=1, resizable=no, height="+zeHeight+", width=475");
			popupWin.focus();	
		}
		doit2="";
	}

	function shufflePlaylist(){
		myPlayList.sort(function() {return 0.5 - Math.random()})
	}
	
	function remove(from) {
		for( var i in myPlayList){
			if(i == from){
				myPlayList[i]="remove";
			}
		}
	}
	
	function song_in_array(song){
		for( var i in myPlayList){
			if (myPlayList[i]["mp3"] == song) {
				return true;          
			}
		}
		return false;
	}
	
	function doSomething(name,media,artist) {
		if(!song_in_array(media)){
			var newArr = [];
			newArr["name"] =  artist + " - " + name;
			newArr["mp3"] = media;
			myPlayList[numz] =newArr;
			numz++;
			openPlayer();
		}else{
			if(doit2=="closed"){
				popupWin = window.open("inc/player.html", "RLR Music Player", "status=0, location=0, scrollbars=1, resizable=no, height="+zeHeight+", width=492");
				doit2="";
			}
		}
		popupWin.focus();	
	}
	function playALL(ID){
		var target = $(ID).attr("href") + " a";
		$(target).each(function(){
			var name = $(this).attr("title");
			var media = $(this).attr("href");
			var artist = $(this).attr("rel");
			if(!song_in_array(media)){
				var newArr = [];
				newArr["name"] =  artist + " - " + name;
				newArr["mp3"] = media;
				myPlayList[numz] =newArr;
				numz++;
			}else{
				if(doit2=="closed"){
					popupWin = window.open("inc/player.html", "RLR Music Player", "status=0, location=0, scrollbars=1, resizable=no, height="+zeHeight+", width=492");
					doit2="";
				}
			}
		});
		openPlayer();
		popupWin.focus();
		window.setTimeout(popupWin.reloadPlayList,200);
	}
$(window).unload(function(){
	var PLstr ="";
	for( x in myPlayList){
		PLstr+= myPlayList[x].name + "!|?|!" + myPlayList[x].mp3 + "!#?#!";
	}
	PLstr += doit2;
	//document.cookie = escape(PLstr);
	escPL = escape(PLstr);
	$.cookies.set( 'playList', escPL );
});
$(document).ready(function(){
	if($.cookies.get('playList')){
		var cook =unescape($.cookies.get('playList'));
		var sng = cook.split("!#?#!");
		for( x in sng){
			if(sng[x] != "" && sng[x] != "undefined" && sng[x] != "closed"){
				var itm = sng[x].split("!|?|!");
				if(itm[0]!="" && itm[0] != "undefined"){
					var newArr = [];
					newArr["name"] =  itm[0];
					newArr["mp3"] = itm[1];
					myPlayList[numz] =newArr;
					numz++;
					doit1 = "hasCook";
				}	
			}
			if(sng[x] == "closed"){doit2=sng[x];}
		}
		if(doit1 == "hasCook" && doit2 != "closed" ){openPlayer(true);}
	}
	$(".tracklist a").each(function(){
		var name = $(this).attr("title");
		var media = $(this).attr("href");
		var artist = $(this).attr("rel");
		$(this).click(function(){
			doSomething(name,media,artist);
			return false;
		});
	});
	$(".latest_listen, .listen_all").click(function(){
		playALL(this);
		return false;
	});
});
