// Global Variables
var curstatus="";
var Plugin;
var radio
var mute
function init()
{
radio=document.td_radio;	
radio.source=playlisturl;
document.getElementById('tab1').style.display='Block';
document.getElementById('Layer1').style.display='Block';
document.getElementById('Layer2').style.display='Block';
document.getElementById('tab2').style.display='none';
radio.SetLoop(false);    
mute=radio.GetMute();
Play();
radio.SetEnableContextMenu(false);
radio.SetWantErrors(true);       
}
function setslidepos(xval)
{
	var pos=(parseInt(xval)-33)
	pos=(pos/150)*100
	pos=radio.GetLength()*(pos/100)
	radio.setPosition(pos)
}
function showalert()
{
	ExecStop()
	alert("Oh No !! you dont have Real Player installed, i need Real Player to play the Songs.\n\n Please download the Latest Real Player from http://www.real.com \n")		
}
function Play()
{
	if(Plugin==0){showalert();return}
	var clipstate=radio.GetPlayState();
	if(clipstate!=4)
	{
		setVolLayerPos(50)		
		radio.DoPlay()			
		ExecPlay()
		GetPlayerState()
	}
	else
	{
		radio.DoPlay();	
	}
		document.getElementById("tab_play").style.display="none"
		document.getElementById("tab_pause").style.display="block"

}

function Next()
{
	if(Plugin==0){showalert();return}	

	if((radio.GetCurrentEntry()==(radio.GetNumEntries()-1))&& radio.GetLoop()==true)	
	{		
		radio.SetPosition(radio.GetLength());
	}
	else
	{
		radio.DoNextItem();
	}
}
function Prev()
{
	if(Plugin==0){showalert();return}
	radio.DoPrevItem();
}

function GetCurClipTime()
{
	var tm_cur=radio.GetPosition()
	var tm_lth=radio.GetLength()
	var tme=(tm_cur/tm_lth)*100
	if(tm_cur>0)
	{		
		if(document.MM_curDrag!=document.getElementById('Layer2'))
		{
			document.getElementById('Layer2').style.left=33+(150*tme)/100+"px"			
		}
		//document.getElementById("spn_time").innerText=gettime(radio.GetPosition())+" / "+gettime(radio.GetLength())
	}
	else
	{
		document.getElementById('Layer2').style.left=33+"px"			
		//document.getElementById("spn_time").innerText="00:00"+" / "+"00:00"		
	}
}

function mouseover()
{
	if(!mute)
	{
		MM_swapImage('Image73','','images/audio_images/audio-d-buttons-over_15.gif',1)
	}
}
function mouseout()
{
	if(!mute)
	{
		MM_swapImgRestore()
	}	
}
function setmute()
{
	if(Plugin==0){showalert();return}
	if(!radio.GetMute())
	{
			radio.SetMute(true);			
			mute=true
			document.getElementById('Image48').src="images/audio_images/volume-mute.gif"
			document.getElementById('Image48').title="Un Mute"
	}
	else
	{
			radio.SetMute(false);
			mute=false;
			document.getElementById('Image48').title="Mute"
			document.getElementById('Image48').src="images/audio_images/audio-down_12.gif"
	}
}
function setrepeat(cval)
{
	if(Plugin==0){showalert();return}
	if(!radio.GetLoop())
	{
		radio.SetLoop(true);
		document.getElementById(cval).title="Repeat Off"
		document.getElementById(cval).src="images/audio_images/audio-down-over_20.gif"		

	}
	else
	{
		radio.SetLoop(false);		
		document.getElementById(cval).title="Repeat On"
		document.getElementById(cval).src="images/audio_images/audio-down_20.gif"		
	}
}
function showtime()
{
	var pos,stime	
	pos=MM_findObj('Layer2').MM_LEFTRIGHT
	pos=(pos/150)
	stime=parseInt(pos*100)
	pos=radio.GetLength()*pos
	document.getElementById("Layer3").innerHTML="<span class='style110'>"+gettime(parseInt(pos))+"("+stime+"%)</span>"
	var slidepos_left=removepx(document.getElementById("Layer2").style.left)
	var slidepos_top=removepx(document.getElementById("Layer2").style.top)
	var slidepos_height=removepx(document.getElementById("Layer2").style.height)
	document.getElementById("Layer3").style.left=parseInt(slidepos_left)-parseInt(16)+"px"
	document.getElementById("Layer3").style.top=parseInt(slidepos_top)-(parseInt(slidepos_height)+parseInt(16))+"px"	
	document.getElementById("Layer3").style.display="block"	

}
function showvolume()
{
	var pos
	pos=MM_findObj('Layer1').MM_LEFTRIGHT
	pos=Math.round(pos*1.11)+"%"
	document.getElementById("Layer3").innerHTML="<span class='style110'>Vol:"+pos+"</span>"
	var slidepos_left=removepx(document.getElementById("Layer1").style.left)
	var slidepos_top=removepx(document.getElementById("Layer1").style.top)
	var slidepos_height=removepx(document.getElementById("Layer1").style.height)
	document.getElementById("Layer3").style.left=parseInt(slidepos_left)-parseInt(25)+"px"
	document.getElementById("Layer3").style.top=parseInt(slidepos_top)-(parseInt(slidepos_height)+parseInt(16))+"px"	
	document.getElementById("Layer3").style.display="block"	
}
function hideshowvolume()
{
		document.getElementById("Layer3").style.display="none"	
}
function removepx(cval)
{
	if(cval=="")
	{
		return 0
	}
		return cval.substr(0,cval.length-2)

}
function seek()
{
	document.getElementById("Layer3").style.display="none"	

	if(Plugin==0){return}
	var pos
	pos=MM_findObj('Layer2').MM_LEFTRIGHT
	pos=(pos/150)*100
	pos=radio.GetLength()*(pos/100)
	radio.setPosition(pos)
}
function gettime(cval)
{
	cval=cval/1000
	var sec,mins,hr
	sec=Math.floor(cval % 60)
	mins=Math.floor(cval/60)
	if(mins>=60)
	{
		hr=Math.floor(mins/60)
		mins=mins % 60
	}	
	else
	{
		hr=0		
	}	
	return formatStr(mins)+":"+formatStr(sec)
}
function formatStr(cval)
{
	if(cval<10)
	{
		return "0"+cval
	}
	else
	{
		return cval
	}
}
function Pause()
{
	radio.DoPause();
	document.getElementById("tab_play").style.display="block"
	document.getElementById("tab_pause").style.display="none"

}
function Stop()
{
	if(Plugin==0){showalert();return}
	for(var k=0;k<4;k++)
	{
		document.getElementById("spn"+k).innerHTML=''
	}
	radio.DoStop();
}
function setVolume()
{
	if(Plugin==0){return}
	showvolume()
	radio.SetVolume(parseInt(MM_findObj('Layer1').MM_LEFTRIGHT*1.1))	
	if(radio.GetMute())
	{
		radio.SetMute(false);			
		mute=false
		document.getElementById('Image73').src="images/audio_images/audio-d-buttons_15.gif"
	}

}
function setVolLayerPos(cval)
{	

	var lft
	var lft1=0
	lft=document.getElementById('Layer1').style.left
	if(lft.substr(lft.length-2,2)=="px")
	{
		lft1=lft.substr(0,lft.length-2)
	}
	if(lft1<=322)
	{
		document.getElementById('Layer1').style.left=(parseInt(lft1)+parseInt(cval))+"px"		
		radio.SetVolume(cval*1.1)	
	}

}
function getTitle()
{	
	for(var i=0;i<=4;i++)
	{
		document.getElementById("spn"+i).innerHTML=""
	}
	var str,spl
	str=radio.GetTitle();
	spl=str.split("+")
	if(spl[0]!="")
	{
		for(var k=0;k<spl.length-2;k++)
		{
			var tmpstr=spl[k]
			if(tmpstr.length>30)
			{
				tmpstr=tmpstr.substr(1,30)+"..."	
			}			
			//
			
			//document.getElementById("spn"+k).innerHTML=tmpstr
			//document.getElementById("spn"+k).title=spl[k]
			tmpstr=""
		}
		var stars=""
		//var fullstars=Math.ceil(spl[4])
		var halfstars=0
		var halfstarcount=0
		//halfstars=Math.floor(spl[4])
		var totstars = spl[4];
		
	   halfstars=spl[4]%2;
	   fullstars = Math.floor(spl[4]/2);
	   
		for(var i=0;i<totstars;i++)
		{
			if(i <= fullstars-1)
				stars=stars+"<img src='images/star-new-1.gif' align='absmiddle'>"
		
			
		}
		for(var i=0;i<totstars;i++)
		{
			
		
			if( i <= halfstars-1)
			{
				stars=stars+"<img src='images/star-new-2.gif' align='absmiddle'>"
			}
		}
		for(var j=1;j<=(5-(fullstars+halfstars));j++)
		{
			stars=stars+"<img src='images/star-new-3.gif' align='absmiddle'>"
		}
		
	//	document.getElementById("spn4").innerHTML=stars
//		document.title= " :: TeluguDreams Audio Player" + (radio.GetCurrentEntry()+1)+". "+spl[2]+"-"+spl[0]+" :: TeluguDreams Audio Player"
		document.title = "TeluguDreams Radio :: " + spl[0];
	}			
	currentclipno=-1
}
function ExecStop()
{
	document.getElementById("tab_stop").style.display="block"
	document.getElementById("tab_det").style.display="none"
	document.getElementById("Layer2").style.display="none"
	document.getElementById("img_slide").style.visibility="hidden"
	document.title="Presented By TeluguDreams"
	document.getElementById("tab_play").style.display="block"
	document.getElementById("tab_pause").style.display="none"

}
function ExecPlay()
{
	document.getElementById("tab_stop").style.display="none"
	document.getElementById("tab_det").style.display="block"
	for(var i=0;i<=4;i++)
	{
		document.getElementById("spn"+i).innerHTML=""
	}	
	document.getElementById("Layer2").style.display="block"
	document.getElementById("img_slide").style.visibility="visible"
}

var clipno
var currentclipno=-1
function GetPlayerState()
{
	var timer;
	var clipstate=radio.GetPlayState();
	clipno=radio.GetCurrentEntry();
	if(currentclipno!=clipno && clipstate==3)
	{
		currentclipno=clipno
		getTitle()		
	}
	var clipstr=""
	GetCurClipTime()
	if(curstatus!=clipstate)
	{		
		curstatus=clipstate
		switch(clipstate)
		{
			case 0:
			clipstr="Stopped";						
			clearInterval(timer)
			ExecStop()
			break;
			case 1:
			clipstr="Contacting...";
			break;
			case 2:			
			clipstr="Buffering..."
			break;
			case 3:						
			clipstr="Playing";
			break;
			case 4:
			clipstr="Paused";
			break;
			case 5:
			clipstr="Seeking...";
			break;							
		}
		document.getElementById("spn_status").innerText=clipstr
	}
	timer=setTimeout('GetPlayerState()',500)
}

function doWin(x,winName,scrollable){

	isScroll=0;
	
		if(winH < 1)
			winH=1;
		if(winW < 1)
			winW=1;
	if (screen.availWidth && screen.availHeight)
		    var xMax = screen.availWidth, yMax = screen.availHeight;
		else
		if (document.layers)
		    var xMax = window.outerWidth, yMax = window.outerHeight;
		else
		    var xMax = 640, yMax=480;

		if (winW > xMax-20){
			isScroll = 1;
			winW = xMax-20;
		}
		if (winH > yMax-20){
			isScroll = 1;
			winH = yMax-30;
		}
		    
		var xOffset = (xMax/2)-(winW/2), yOffset = (yMax/2)-(winH/2);
		
		if ( scrollable )
			isScroll = 1;
		
		thisWin = window.open (x,winName,'width='+winW+',height='+winH+',directories=no,toolbar=no,menubar=no,location=no,scrollbars='+isScroll+',minimize=yes,resizable=no,copyhistory=no,status=no,titlebar=no,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
        return thisWin;
}