var UP_ARROW = new Image(),DOWN_ARROW = new Image(),listimg = new Image(),removeempty = 1;
UP_ARROW.src = "/images/arrow_up.gif";
DOWN_ARROW.src = "/images/arrow_down.gif";
listimg.src = "/images/mlist.gif";
var debug1 = true;
var debug2 = true;
var escapeMap = '';
var CPstring = '';

var hexNum = { 0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1, 
				A:1, B:1, C:1, D:1, E:1, F:1, 
				a:1, b:1, c:1, d:1, e:1, f:1 };
var jEscape = { 0:1, b:1, t:1, n:1, v:1, f:1, r:1 };
var decDigit = { 0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1 };
var GMCookie = {
	set : function(name, value, expirationInDays){if(expirationInDays){var date = new Date();date.setTime(date.getTime() + (expirationInDays * 24 * 60 * 60 * 1000));var expires = "; expires=" + date.toGMTString();} else{var expires = "";}		document.cookie = name + "=" + value + expires + "; path=/";},
	get : function(name){var namePattern = name + "=";var cookies = document.cookie.split(';');	for(var i = 0, n = cookies.length; i < n; i++) {var c = cookies[i];	while (c.charAt(0) == ' ') c = c.substring(1, c.length);if (c.indexOf(namePattern) == 0)return c.substring(namePattern.length, c.length);}return null},
	del : function(name){this.set(name, "", -1)} ,
	length:	function(){return document.cookie.length;},
	
	printAllMusicLinks:function(){
	   var musicURL = [],musicTitle = [],musicString = "";
	   var cookies = document.cookie.split(';');
	   	for(i in cookies){
			var music_record = cookies[i].split('=');
			if(music_record[0].indexOf('http') > -1)
				addToPlaylist(unescape(music_record[1]),unescape(music_record[0]),0);
		}
  
	},	
	removeAll:function(){
		var cookies = document.cookie.split(';');
		if(cookies){
			var l = cookies.length;
		   	for(var i=0;i<l;i++){
				var music_record = cookies[i].split('=');
				if(music_record[0].indexOf('http') > -1)
					this.del(music_record[0]);
			}
		}
		
		$('#pllist').empty();
		try{$('#pl')[0].removeChild($('#el')[0]);}catch(e){}
		$('#pl').prepend('<span id="el"><font color="#CCCCCC" size="5">--- Empty ---</font></span>');
		$('#favcount').html('(0/25)');
		$('#mylist').css('height','0px');
		removeempty = 1;
		return;
	},

	emptyList: function(){
	  var cookies = document.cookie.split(';');
	  count = 0;
	   for(i in cookies) {
		var music_record = cookies[i].split('=');
		if(music_record[0].indexOf('http') > -1){
		count++;		
		}
		}
		if(count == 0){
			return true;
		}else{
			return false;
		}
	}
	
	
};
function  dec2hex2 ( textString ) {
  var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
  return hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}
function  dec2hex4 ( textString ) {
  var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
  return hexequiv[(textString >> 12) & 0xF] + hexequiv[(textString >> 8) & 0xF] + hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}
function convertCP2pEsc ( textString ) {
	// textstring: sequence of Unicode code points, derived from convertChar2CP()
	var outputString = "";
	// remove initial spaces
	textString = textString.replace(/^\s+/, '');
	if (textString.length == 0) { return ""; }
	// make all multiple spaces a single space
	textString = textString.replace(/\s+/g, ' ');
	var listArray = textString.split(' ');
	// process each codepoint
	for ( var i = 0; i < listArray.length; i++ ) {
		var n = parseInt(listArray[i], 16);
		//if (i > 0) { outputString += ' ';}
		if (n == 0x20) { outputString += '%20'; }
		else if (n >= 0x41 && n <= 0x5A) { outputString += String.fromCharCode(n); } // alpha
		else if (n >= 0x61 && n <= 0x7A) { outputString += String.fromCharCode(n); } // alpha
		else if (n >= 0x30 && n <= 0x39) { outputString += String.fromCharCode(n); } // digits
		else if (n == 0x2D || n == 0x2E || n == 0x5F || n == 0x7E) { outputString += String.fromCharCode(n); } // - . _ ~
		else if (n <= 0x7F) { outputString += '%'+dec2hex2(n); }
		else if (n <= 0x7FF) { outputString += '%'+dec2hex2(0xC0 | ((n>>6) & 0x1F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else if (n <= 0xFFFF) { outputString += '%'+dec2hex2(0xE0 | ((n>>12) & 0x0F)) + '%' + dec2hex2(0x80 | ((n>>6) & 0x3F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else if (n <= 0x10FFFF) {outputString += '%'+dec2hex2(0xF0 | ((n>>18) & 0x07)) + '%' + dec2hex2(0x80 | ((n>>12) & 0x3F)) + '%' + dec2hex2(0x80 | ((n>>6) & 0x3F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else { outputString += '!Error ' + dec2hex(n) +'!'; }
		}
		return( outputString );
	}
function dec2hex ( textString ) {
 return (textString+0).toString(16).toUpperCase();
}
function convertChar2CP ( textString ) { 
	var haut = 0;
	var n = 0;
	CPstring = '';
	for (var i = 0; i < textString.length; i++) {
		var b = textString.charCodeAt(i); 
		if (b < 0 || b > 0xFFFF) {
			CPstring += 'Error ' + dec2hex(b) + '!';
			}
		if (haut != 0) {
			if (0xDC00 <= b && b <= 0xDFFF) {
				CPstring += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
				haut = 0;
				continue;
				}
			else {
				CPstring += '!erreur ' + dec2hex(haut) + '!';
				haut = 0;
				}
			}
		if (0xD800 <= b && b <= 0xDBFF) {
			haut = b;
			}
		else {
			CPstring += dec2hex(b) + ' ';
			}
		}
	CPstring = CPstring.substring(0, CPstring.length-1);
	return (convertCP2pEsc( CPstring ));
	}


	
function toggleSlide(obj,anc){
	objJQ = $("#"+ obj); // Converting to jQuery obj
	ancJQ = $(anc); // Converting to jQuery obj
	if(objJQ.css("display")=='none'){
		//objJQ.show();
		objJQ.fadeIn();
		ancJQ.empty().append('<img src="'+UP_ARROW.src+'" height="12" width="12" border="0">');
	}else{
		objJQ.hide();
		ancJQ.empty().append('<img src="'+DOWN_ARROW.src+'" height="12" width="12" border="0">');
	}
}

function showMusicHelp(){
	if(!GMCookie.get('dsmh')){
		document.write('<td valign="top" width="70%" style="border-top:1px solid #FFE1F1;border-bottom:1px solid #FFE1F1" bgcolor="#FFF4FB" id="dsmh_l" ><span style="float:right"><a href="JavaScript:void(0)" class="mhlp" alt="Don\'t show this help again." onclick="dsmh();return false;"><img src="/images/closem.gif" border="0"></a></span><div style="padding-top:10px;padding-left:5px"><b>What can I do on this page?</b><ol><li>Click on a song to <b>play</b> it.</li><li>Click <img src="/images/add.gif" alt="add to playlist" border="0"> to add to  <b>favorites</b>.Click <img src="/images/mbin.gif" border="0" height="12"> to empty favorites.</li><li> Click <img src="/images/arrow_down.gif" alt="add to playlist" border="0" width="12" height="12"> to see <b>more details</b>.</li></div></td></td><td align="left" valign="top" width="30%" style="background:#F7FAFD" style="border-bottom:1px solid #E8F0F9" id="dsmh_r">&nbsp;</td>');
	}
}
//+++++++++++++++++++++++
//// ADD TO PLAYLIST ////
//+++++++++++++++++++++++
function addToPlaylist(title,url,setcookie){
	if(typeof(setcookie) == "undefined" && GMCookie.get(escape(jQuery.trim(url)))){
		cordinates = $('#pllist table:contains('+title+')').filter('+escape(jQuery.trim(url))+').offset();
		relativeto_window = $('#pllist').offset();
		$('#mylist')[0].scrollTop = cordinates.top-relativeto_window.top;
		$('#pllist table:contains('+title+')').filter('+escape(jQuery.trim(url))+').vkfade('FC9E09');
		vsbl = parseInt($('#pllist table:contains('+title+')').filter('+escape(jQuery.trim(url))+').offset().top-vScrollTop());
	
		if(vsbl < -12){
			end = vInnerHeight()+vScrollTop();
			endTop = parseInt(end-35); 
			$('body').append('<div id="btip" style="display:none;position:absolute;background:#FC9E09;'
							+'padding-top:12px;text-align:center;z-index:100;top:'+endTop
							+'px;right:0px;width:130px;height:25px;font-size:12px">'
							+'Already in your favourite</div>');
			$('#btip').fadeIn('slow',function(){
				$('#btip').fadeOut(2000,function(){
					$('body')[0].removeChild($('#btip')[0]);
				});
			});
		}
		return;
	}
	
	if(removeempty == 1){$('#pl')[0].removeChild($('#el')[0]);removeempty = 0;}
	$('#mylist').css('height','180px');
	d = new Date().getTime().toString();
	paraid = Math.floor(Math.random()*d*10);	
	newnodeFreg	="<table border='0' id='"+ paraid +"' class='plpara' width='100%' "
				+"style='height:20px;border-bottom:1px solid #F1F1F1;padding:0px;margin:0px;'>"
				+"<td width='16' align='left'><img src='"+listimg.src+"'></td><td>"
				+"<a href='Javascript:void(0)' onclick=\"JavaScript:mw('"+url+"');\">"
				+"<font color=#02679a size='1' title='Click to play'><B>"+title
				+"</B></font></td><td width='30' align='right'><a href='JavaScript:void(0)'"
				+" onclick=\"removeIt('"+paraid+"','"+url+"')\" class='cb'><img border='0' "
				+"src='/images/delete1.gif' border='0'></a></td></table>";
	$('#pllist').append(newnodeFreg);
	$('#favcount').html('('+$('#pllist table').length+'/25)');
	if(typeof(setcookie) == "undefined"){
	try{$('#'+paraid).vkfade();}catch(e){alert(e);}
		vsbl = parseInt($('#'+paraid).offset().top-vScrollTop());
		if(vsbl < -12){
		    end = vInnerHeight()+vScrollTop();
			endTop = parseInt(end-35); 
			$('body').append('<div id="btip" style="display:none;position:absolute;background:#FFFF00;'
							+'padding-top:12px;text-align:center;z-index:100;top:'+endTop
							+'px;right:0px;width:130px;height:25px;font-size:12px">Added to your favourite'
							+'</div>');
			$('#btip').fadeIn('slow',function(){
				$('#btip').fadeOut(2000,function(){
					$('body')[0].removeChild($('#btip')[0]);
				});
			});
		}
	}

	$('#mylist')[0].scrollTop = $('#pllist')[0].scrollHeight;
	if(typeof(setcookie) != "undefined" && setcookie == 0){ 
	}else{
		GMCookie.set(escape(jQuery.trim(url)),title,30);
	}
}

function dropBeacon(title,album){
	try{(new Image()).src = 
			"http://dir.guruji.com/images/music_fav_add?title="
			+ title
			+ (album?"&album=" + album:"");}
	catch(e){}
}
function cr(){
	if(confirm('Are you sure, you want to empty your playlist?')){
	GMCookie.removeAll();return false;}else{return false;}
}

function removeIt(objId,url){
	$('#pllist')[0].removeChild($('#'+objId)[0]);GMCookie.del(escape(jQuery.trim(url)));
	$('#favcount').html('('+$('#pllist table').length+'/25)');
	if(GMCookie.emptyList()==true){
		$('#pl').prepend('<span id="el"><font color="#CCCCCC" size="5">--- Empty --</font></span>');
		removeempty = 1;$('#mylist').css('height','0px');}
}

function dsmh(){
 if(!GMCookie.get('dsmh')){
	 $('#dsmh_l').hide();
	 $('#dsmh_r').hide();
	 GMCookie.set('dsmh','1',365);
 }
}

function TogglePlayRow(id){
	  var objJQ = $("#"+ id);
	  if(objJQ.css("display")=='none'){
	  	//$(".plnum").hide()
	  	objJQ.fadeIn();
	  }else{
	  	objJQ.hide();
	  }
}
		
function setWhatVal(str){try{$('whatVal').innerHTML = str;$('q').focus();}catch(e){}}
function printPlayList(){
	if(!$('favlist') || $('favlist').length==0) return;
	var musicURL = [],musicTitle = [],musicString = "";
	var cookies = document.cookie.split(';');
	var str = "<ol class=\"topsongs\">";var count = 0;
	
	for(var i=0,l=cookies.length,count=0;i<l && count<10;i++){
		var music_record = cookies[i].split('=');
		if(music_record[0].indexOf('http') > -1){
			count++;
			str +=	"<li><a onclick='JavaScript:mw(\""+unescape(unescape(music_record[0]))+
					"\")' href='JavaScript:void(0)'>"+unescape(unescape(music_record[1]))
					+"</a><br/></li>";
		}
	}
	str += "</ol>";
	$('favlist').innerHTML = str;
}
function showTab(tn){
	tabs = ['top_songs','top_singers','top_albums','fav'];
	for(i=0;i<tabs.length;i++){
		container = tabs[i]+"_container";
		if(tn == tabs[i]){
			$(tn).style.display ="";
			$(tn).style.backgroundColor = document.getElementById('albm').value;
			$(container).style.backgroundColor = document.getElementById('albm').value; 
			$(container).style.border = "1px solid #D9E3D9";
			$(container).style.borderBottom = "1px solid "+document.getElementById('albm').value;;
		}else{
			$(tabs[i]).style.display ="none";
			$(container).style.backgroundColor = "#FFFFFF";
			$(container).style.border = "1px solid #FFFFFF";
            $(container).style.borderBottom = "1px solid #D9E3D9";
		}
	}
}

function createTopStuff(songs,albums,singers){
	var val = convertChar2CP(document.getElementById('cfeed').value);
	var cfeed = "music_search.html?q=[TITLE]&client="+document.getElementById('client').value+"&cfeed="+val;
	createTabList(songs,cfeed+"&cat=d0,Song","topsongs");
	createTabList(singers,cfeed+"&cat=d0,Singers","topsingers");
	createTabList(albums,cfeed+"&cat=d0,AlbumTitle","topalbums");
}

function createTabList(arr,url,ul){
	var l = arr.length;
	var uls = [$(ul+'_left'),$(ul+'_right')];
	for(var i=0;i<l;i++){
		var li = ce("li");var a = ce("a");
		a.setAttribute("href",url.replace("[TITLE]",arr[i].replace(/[ ]+/g,"+")));
		a.appendChild(tn(arr[i]));
		li.appendChild(a);
		uls[i%2].appendChild(li);
	}}

if(typeof(addEvent) != 'undefined') addEvent(window,"load",printPlayList);

