﻿// Top Ratings: Customized

var currentSongId;
var activeSongId;
var nOffSet = 0;
var rowNum = 0;
var DJIDlocal=0;
var thisStr = "";

//var txtPreviewSp = "&nbsp&nbsp&nbsp Escuchar Canción <span style=\"font-size:10px;\">(30s)</span>";
//var txtPreviewEn = "&nbsp&nbsp&nbsp Preview Song <span style=\"font-size:10px;\">(30s)</span>&nbsp&nbsp";
var txtPreviewSp = "&nbsp&nbsp&nbsp Escuchar Canción &nbsp&nbsp&nbsp";
var txtPreviewEn = "&nbsp&nbsp&nbsp Preview Song &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";

currentSongId = -1;
activeSongId=-1;




//FF - Flash Player
    function StartStream(StreamURL, DisplayText ,Position, nLength)
    {
    activeSongId=Position;
    document.getElementById('timer' + activeSongId).innerHTML = "";
    
    if (nLength > 120)
    {
        nOffSet = 90;
    }
    else
    {    
        nOffSet = Math.round(nLength * .25);
    }    
    if ((nOffSet + 30) > nLength)
    {
        nOffSet = 0;
    }  	 

    bNewSong=true;
    StreamURL = StreamURL.replace(/___/g,"_00");
    StreamURL = StreamURL.replace(/---/g,"/00");
	StreamURL = GetMediaLimeLightURL(StreamURL,'mp3',StartStream2CallBack);
    }


function StopStream()
{
  var Position = -1;
  
  sendEvent('stop');
  
 if (currentSongId != -1) {
   var loadingText = txtPreviewSp;
   var language = getCookie("idioma");    
   if(language == "EN")
       loadingText = txtPreviewEn;
                    
    document.getElementById('statbar'+currentSongId).style.display = '';
    document.getElementById('statbar' + currentSongId).innerHTML=loadingText
    document.getElementById('timer' + currentSongId).innerHTML = "";
	document.getElementById('stopButton'+currentSongId).style.display = 'none';
	document.getElementById('playButton'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.width = '0px';
	
	currentSongId = Position;
  }
}


function StartStream2CallBack(StreamURL)
{
    StopStream();
    loadFile(StreamURL,'',null,null,123);
    sendEvent('playpause');
    movefrw(nOffSet);
	sendEvent('volume',70);

	currentSongId = activeSongId;
    document.getElementById('stopButton'+currentSongId).style.display = '';
    document.getElementById('playButton'+currentSongId).style.display = 'none';
    document.getElementById('songid'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.width = '0px';
    
    var loadingText = "&nbsp&nbsp&nbsp Cargando... &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    var language = getCookie("idioma");    
    if(language == "EN")
        loadingText = "&nbsp&nbsp&nbsp Loading... &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
   
    document.getElementById('statbar'+ currentSongId).style.display = '';
    document.getElementById('statbar' + currentSongId).innerHTML=loadingText; 
}

function thisMovie(movieName) 
{
	    if(navigator.appName.indexOf("Microsoft") != -1) {
	    	return window[movieName];
		} else {
			return document[movieName];
		}
}

function movefrw(timeIn)
{
    sendEvent('scrub',timeIn);
}

function sendEvent(typ,prm) 
{
    thisMovie("mediaplayer").sendEvent(typ,prm);
}
	
function loadFile(fil,tit,lnk,img,fid) 
{
	thisMovie("mediaplayer").loadFile(fil,tit,lnk,img,fid);
}






//WINDOWS MEDIA PLAYER (LOAD AND TIMER)
function StopStreamW()
{
    //thisStr = thisStr + "StopStreamW" + ":\n";
   
	var Position = -1;
	
	StopW();
	
	if (currentSongId != -1) 
	{
	document.getElementById('timer' + currentSongId).innerHTML = "";
	document.getElementById('stopButton'+currentSongId).style.display = 'none';
	document.getElementById('playButton'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.width = '0px';
	currentSongId = Position;
	}

}

function StartStreamW(StreamURL, DisplayText, Position, nLength)
{
    //thisStr = thisStr + "StartStreamW" + ":\n";
    
	activeSongId=Position;
	document.getElementById('timer' + activeSongId).innerHTML = "";	
	if (nLength > 120)
	{
    		nOffSet = 90;
	}
	else
	{    
		nOffSet = Math.round(nLength * .25);
	}    
	if ((nOffSet + 30) > nLength)
	{
        	nOffSet = 0;
	}
	
	bNewSong=true;
	StreamURL = StreamURL.replace(/___/g,"_00");
	StreamURL = StreamURL.replace(/---/g,"/00");
	StreamURL = GetMediaLimeLightURL(StreamURL,'wma',StartStreamWCallBack);
	
}

function StartStreamWCallBack(StreamURL)
{   
    //thisStr = thisStr + "StartStreamWCallBack" + ":\n";
    
    var loadingText = "&nbsp&nbsp&nbsp Cargando... &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    var language = getCookie("idioma");    
    if(language == "EN")
        loadingText = "&nbsp&nbsp&nbsp Loading... &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
   

	StopStreamW();
	loadFileW(StreamURL);
	PauseW();
	FfwdW(nOffSet);
	setVolumeW(70);
	currentSongId = activeSongId;
	
	var cmd='WaitForLoad();';
	setTimeout(cmd,10);
	
	document.getElementById('statbar' + currentSongId).innerHTML=loadingText;
	document.getElementById('statbar'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.display = '';
	document.getElementById('songid'+currentSongId).style.width = '0px';
	document.getElementById('stopButton'+currentSongId).style.display = '';
	document.getElementById('playButton'+currentSongId).style.display = 'none';
}

function WaitForLoad()
{

//thisStr = thisStr + "WaitForLoad" + ":\n";

   if (thisMovieW("WMAPlayer").openState<13)
    {
        document.getElementById('playButton' + activeSongId).style.display='none';
        document.getElementById('stopButton' + activeSongId).style.display='';
        document.getElementById('songid'+activeSongId).style.display = '';
	    document.getElementById('songid'+activeSongId).style.width = '0px';
    	var cmd='WaitForLoad();';
    	setTimeout(cmd,10);
    }
    else
    {
    
        var loadingText = txtPreviewSp;
        var language = getCookie("idioma");    
        if(language == "EN")
           loadingText = txtPreviewEn;
                    
        document.getElementById('statbar' + activeSongId).innerHTML=loadingText;
	    document.getElementById('statbar'+activeSongId).style.display = '';
        document.getElementById('songid'+activeSongId).style.display = '';
	    document.getElementById('songid'+activeSongId).style.width = '0px';
        document.getElementById('playButton' + activeSongId).style.display='none';
        document.getElementById('stopButton' + activeSongId).style.display='';
     	PlayW();
     	
     	var randomnumber=Math.floor(Math.random()*99999)
     	thisStr = thisStr + "rannum:" + randomnumber + ":\n";
     	rowNum = randomnumber;
     	RefreshPreview(randomnumber);
 	}
}

function setVolumeW(iPct)
{
	thisMovieW("WMAPlayer").settings.volume=iPct;
}


function PlayW()
{
	thisMovieW("WMAPlayer").controls.play();
}


function PauseW()
{
	thisMovieW("WMAPlayer").controls.pause();
}

function StopW()
{
	thisMovieW("WMAPlayer").controls.stop();
}


function FfwdW(iSecs)
{
	thisMovieW("WMAPlayer").controls.currentPosition=iSecs;
}
	

function loadFileW(sStreamURL) 
{
	thisMovieW("WMAPlayer").URL=sStreamURL;
}

function thisMovieW(movieName) 
{
    return document.WMAPlayer;
}







///COMMON
function GetMediaLimeLightURL(media,mediaType,callBack)
{
//alert("GetMediaLimelight");
     var xmlhttp=false;
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    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;
	    }
    }
    var URL;
    switch (mediaType)
    {
    }        
    xmlhttp.open("GET", 'GetEncodedMediaURL.aspx?media='+media+'&mediaType='+mediaType,true);
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
       callBack(xmlhttp.responseText);
      }
     }
     xmlhttp.send(null)
}

function IncrementTracking(incLength)
{

              var len = 0;
              var currentWidth = 0;
                
                
              if (currentSongId > 0)
              {
		        len = (document.getElementById('songid' + currentSongId).style.width).length;
		        
		        if (len == 2)
		        {
		        currentWidth = (document.getElementById('songid' + currentSongId).style.width).substring(0,2);
		        }
		        else
		        {
		        currentWidth = (document.getElementById('songid' + currentSongId).style.width).substring(0,3);
		        }
		        currentWidth = parseFloat(currentWidth) + parseFloat(incLength);
		        document.getElementById('songid'+currentSongId).style.display = '';
	            document.getElementById('songid'+currentSongId).style.width = currentWidth + 'px';
	          }

}
///

//FLASH MEDIA PLAYER FOR Firefox (LOAD AND TIMER)
function getUpdate(typ,pr1,pr2)
 {
    
    
        if(typ == "time")
		{ 
		    currentPosition = pr1 - nOffSet;
		    remainingTime = Math.round(pr2);
		    
		    
		    if (currentPosition<0)
		    {
		        //document.getElementById('PreviewTiming').innerHTML = '00:00';   
		    }
		    else if (currentPosition<60)
		    {
		        if ((currentPosition >= 25) && (currentPosition <=30))
		        {
		           document.getElementById('timer' + activeSongId).innerHTML = ":0" + (30 - currentPosition); 
		        }
		        else
		        {
		            document.getElementById('timer' + activeSongId).innerHTML = "";
		        }
		        
		        //var language = getCookie("idioma"); 
		     
		        //increment tracking strip
//		        if(language == "EN")
//		        {
//		            IncrementTracking(2);
//		        }
//		        else
//		        {
//		            IncrementTracking(2);
//		        }
                IncrementTracking(2);
	            
		        
		        if (currentPosition>=31)
		        {
		        StopStream();
		        }
		    }
		   else if (currentPosition>=60)
    	   {
		        min=Math.floor(currentPosition/60);
		        sec= currentPosition - (min*60);
		        if (sec<10)
		        {
		            //document.getElementById('PreviewTiming').innerHTML = '0'+min+":0"+sec;
		        }
		        else if  (sec>=10)
		        {
		           //document.getElementById('PreviewTiming').innerHTML = '0'+min+":"+sec;
		        }
		        
		    }
		    
		}
		else if(typ == "load")
            { 
                iLoad = pr1;
    		    
					var loadingText = txtPreviewSp;
                    var language = getCookie("idioma");    
                    if(language == "EN")
                    loadingText = txtPreviewEn;
    
				if (iLoad<100)
				{
                    document.getElementById('playButton' + activeSongId).style.display='none';
                    document.getElementById('stopButton' + activeSongId).style.display='';
				}
				else
				{
                    document.getElementById('statbar' + activeSongId).innerHTML=loadingText;
                    document.getElementById('statbar' + activeSongId).style.display='';
                    document.getElementById('playButton' + activeSongId).style.display='none';
                    document.getElementById('stopButton' + activeSongId).style.display='';
    		        sendEvent('playpause');
				}
            }
            
    }
///// 
   
//// WINDOWS MEDIA TIMER/PREVIEW

//sets global var (rowNum) based on grid row id
function SetPreviewMarker(aNum)
{
    //rowNum = aNum;
}


function RefreshPreview(locRowNum)
{

    //thisStr = thisStr + "RefreshPreview" + ":\n";
    //thisStr = thisStr + "RP" + locRowNum + ":\n";
    
    //var locRowNum is local version of rowNum -- allows previous iterations of 
    //RefreshPreview to be exited, so RefreshPreview does not run concurrently with itself
    
    if (rowNum == locRowNum)
    {
      
	var currentPosition = parseInt(document.WMAPlayer.controls.currentPosition) - nOffSet;
	
	if ( currentPosition < 0 ) currentPosition = 0;
	
	if (currentPosition<0)
		    {
		      document.getElementById('stopButton'+currentSongId).style.display = 'none';
		      document.getElementById('playButton'+currentSongId).style.display = '';
		      document.getElementById('songid'+currentSongId).style.display = '';
	          document.getElementById('songid'+currentSongId).style.width = '0px';  
		    }
		    else if (currentPosition <=30)
		    {   
		        var language = getCookie("idioma"); 
                if(language == "EN")
		        {
		            IncrementTracking(3);
		        }
		        else
		        {
		            IncrementTracking(3.5);
		        }
		         
		         
		         if (currentSongId > 0)
		         {
		           if ((currentPosition >= 24) && (currentPosition <=29))
		            {
		           document.getElementById('timer'+currentSongId).innerHTML = ":0" + (29 - currentPosition); 
		            }
		            else
		            {
		            document.getElementById('timer'+currentSongId).innerHTML = "";
		            }
		          }   
		                setTimeout("RefreshPreview(" + locRowNum + ")", 1000);   
		    }
		    else
		    { 
		        document.getElementById('stopButton'+currentSongId).style.display = 'none';
		        document.getElementById('playButton'+currentSongId).style.display = '';

                document.getElementById('songid'+currentSongId).style.display = '';
	            document.getElementById('songid'+currentSongId).style.width = '0px';
		        document.WMAPlayer.controls.Stop();
		     }

		        
	    }
	    
}