var timerID = 0;
var tStart  = null;
var tSeconds = 0;
var currentTrack;

// Esto es lo que tienes que modificar...
//*********************************************************************************************

//Esto es el listado de archivos a reproducir en la WEB.
var trackList = ["austral.mp3","inicial0.mp3","inicial.mp3","1.mp3","0.mp3","2.mp3","3.mp3","4.mp3","5.mp3","6.mp3","7.mp3","8.mp3","9.mp3","10.mp3","11.mp3","12.mp3","13.mp3","14.mp3","15.mp3","16.mp3","17.mp3","18.mp3","19.mp3","20.mp3","21.mp3","22.mp3","23.mp3","24.mp3","25.mp3","26.mp3","27.mp3","28.mp3","29.mp3","30.mp3","31.mp3","32.mp3","33.mp3","34.mp3","35.mp3","36.mp3","37.mp3","38.mp3","39.mp3","40.mp3","41.mp3","42.mp3"];

//Esto es la duracion correspondiente a cada uno de los archivos a reproducir -coincide la posicion-
var trackDurations = [209,152,123,150, 113, 175, 213, 209, 170, 156, 181, 169, 145, 197, 136, 182, 155, 176, 241, 137, 219, 184, 190, 195, 199, 154, 118, 159, 190, 187, 152, 150, 166, 122, 230, 177, 237, 166, 165, 201, 178, 185, 190, 56, 195, 138];

//Este valor es la cantidad de archivos - 1
//Fijar tambien este valor en el archivo /common/getcurrenttrack.php
var finalTrackIndex = 45;

// Hasta aca....
//*********************************************************************************************


var sep;

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();

   tSeconds++;
   if (tSeconds > trackDurations[currentTrack]){
		Stop();
		initTrack(1, 0, tSeparator);
		/*if (currentTrack == finalTrackIndex){
			document.movie1.loop = true;			
		}
		else{
			Start();	
		}
		PlayIt(sep);*/
   }
   else{
	   	tDate.setTime(tDiff);      
   		timerID = setTimeout("UpdateTimer()", 1000);   
   }         
}


function Start() {
   tStart   = new Date();   
   timerID  = setTimeout("UpdateTimer()", 1000);
}


function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
	  tSeconds = 0;
   }

   tStart = null;
}


/* a javascript function that takes a QT movie and calls its "Play" method */
function PlayIt(){
   sep = tSeparator;
   if (currentTrack == finalTrackIndex){ 
   		document.getElementById("player").innerHTML = "<embed src=\""+ tSeparator +"media/"+ trackList[currentTrack] +"\" width=\"0\" height=\"0\" hspace=\"0\" vspace=\"0\" name=\"movie1\" enablejavascript=\"true\" loop=\"True\"> </embed>";
		Stop();	
   }
   else{
   		document.getElementById("player").innerHTML = "<embed src=\""+ tSeparator +"media/" + trackList[currentTrack] + "\" width=\"0\" height=\"0\" hspace=\"0\" vspace=\"0\" name=\"movie1\" enablejavascript=\"true\" loop=\"False\"> </embed>";
		Start();
   }
   //document.movie1.Play();
}

/* a javascript function that takes a QT movie and calls its "Stop" method */
function StopIt(){
   document.getElementById("player").innerHTML = "";
   //document.movie1.Stop();   
}


function sw(tSeparator){
	sep = tSeparator;
	if (document.getElementById('mt').innerHTML == "DETENER MENSAJES"){
		document.getElementById('mt').innerHTML = "ESCUCHAR MENSAJES";
		StopIt();
		Stop();
	}			
	else
	{
		document.getElementById('mt').innerHTML = "DETENER MENSAJES";
		initTrack(0, 0, tSeparator);
	}
}


function getHTTPObj()
{
     var xmlhttp;

    /*@cc_on
    @if (@_jscript_version >= 5)
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try{
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            xmlhttp = false;
        }
    }
    return xmlhttp; 
}


function httpUser(){	
	if (http.readyState == 4) {
		if (http.status == 200) {			  	  
			results = http.responseText;//.split(",");				 
			enProceso = false;			
			currentTrack = parseInt(results);	
			PlayIt();
		}		
	}			
}


function initTrack(inc, res, separator){
	if (!enProceso && http) {					
		var url = separator + "common/getcurrenttrack.php?inc="+inc+"&fti="+finalTrackIndex + "&res=" + res; 
		tSeparator = separator;
		http.open("GET", url, true);		   
		http.onreadystatechange = httpUser;		  		
		enProceso = true;
		http.send(null);				
	}		
}


function resetMgs(sep){
	StopIt();
	Stop();
	initTrack(0, 1, sep);
}

var http = getHTTPObj();
var enProceso = false;