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 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 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 = "#D9E9F6";
			$(container).style.backgroundColor = "#D9E9F6";
			$(container).style.border = "1px solid #D9E3D9";
			$(container).style.borderBottom = "1px solid #D9E9F6";
			//$(container).style.fontWeight = 'bold';
		}else{
			$(tabs[i]).style.display ="none";
			$(container).style.backgroundColor = "#FFFFFF";
			$(container).style.border = "1px solid #FFFFFF";
            $(container).style.borderBottom = "1px solid #D9E3D9";
			//$(container).style.fontWeight = 'normal';
		}
	}
}

function createTopStuff(songs,albums,singers){
	
	createTabList(songs,"index.php?q=[TITLE]&client=bollyclips.com&cfeed=tw%3BTB%3A0%3BFS%3A9pt%3B&cat=e0,Song","topsongs");
	createTabList(singers,"index.php?q=[TITLE]&client=bollyclips.com&cfeed=tw%3BTB%3A0%3BFS%3A9pt%3B&cat=e0,Singers","topsingers");
	createTabList(albums,"index.php?q=[TITLE]&client=bollyclips.com&cfeed=tw%3BTB%3A0%3BFS%3A9pt%3B&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);
