/////////// Script para el reproductor de video /////////////////////////////
/////////// Lee XML y arma el menu debajo del Reproductor ///////////////////

		function importXML(file, ancho, largo, flag) {
			xmlDoc = null; 
			xmlHttp = null; 	
			theFile = file;
			
			initPlayer(theFile,ancho,largo); // llamada al reproductor desde acá.
		
			if ((flag)
			||(theFile.substring(0,4).toLowerCase() == "http")
			||(location.protocol.substring(0,4).toLowerCase() == "http")) { 
				httpFlag = true;
				httpXMLDoc(theFile); 
			} else { 
				httpFlag = false;
				loadXMLDoc(theFile); 
			}
		}
		
		
		function loadXMLDoc(file) { if (debug()) { alert("xmlDoc.load: "+file); }
			if ((document.implementation)&&(document.implementation.createDocument)) { 
				xmlDoc = document.implementation.createDocument("", "", null);  
				xmlDoc.onload = loaded;	
			} else if (window.ActiveXObject) { 
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
				xmlDoc.onreadystatechange = loadStat; 
			} 
		
			if (xmlDoc != null) {xmlDoc.load(file);} 
			else if (! stealth) { alert(msg1); return; } 
		}
		
		
		function loadStat() { if (xmlDoc.readyState == 4) loaded(); }
		
		
		function httpXMLDoc(file) {
			if (window.XMLHttpRequest) { 
				xmlHttp = new XMLHttpRequest(); 
			} else if (window.ActiveXObject) { 
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
		
			if (xmlHttp != null) {
				xmlHttp.onreadystatechange = getStat;
				xmlHttp.open("GET", file, true);
				xmlHttp.send(null);
			} else if (! stealth) { alert(msg1); return; } 
		}
		
		
		function getStat() { if(checkReadyState(xmlHttp)) { xmlDoc = xmlHttp.responseXML.documentElement; loaded(); } }
		
		
		function checkReadyState(obj) { 
			if(obj.readyState == 4) {
				if(obj.status == 200) { return true; } 
				else { if (! stealth) { alert(msg2); } return; }
			} 
		}


		function loaded() {	
		  showTags('track','writeroot');
		};


		var Urls = new Array();
		var Imgs = new Array();
		
		function showTags(theTag,thePlace) {
	
		  function getTag(tag) { 
			var tmp='';
			xx=x[i].getElementsByTagName(tag);     
			try { tmp=xx[0].firstChild.data; } 
			catch(er) { tmp=''; }    
			return(tmp); 
		  };

		  var xx; var x; var txt;
		  x = xmlDoc.getElementsByTagName(theTag);
		  txt='<table border="0" cellPadding="0" cellspacing="1" width="100%" align="center">'; 
		  txt+='<tr>';
		  
		  for (i=x.length-1; i>=0; i--) {   // el loop lo hace en forma decreciente	
			  
			Urls[i]=getTag("location"); 
			Imgs[i]=getTag("image");
			
			txt+='<td width="33%" align="center"><a href="javascript:loadNplay('+i+')"><img src="'+getTag("image")+'" width="80" height="61" border="0" title="'+getTag("title")+'" /></a></td>';

		  }
		  txt+='</tr>';
		  txt+="</table>";

		  document.getElementById(thePlace).innerHTML=txt;
		};


// específico para API  V.4 del reproductor-----

var currentState = "NONE"; 
var previousState = "NONE"; 
var player = null;
var playerMP = null;

// ------------------------------------
		function playerReady(obj)
		{
		player = gid(obj.id);
		//shufflePlaylist();
		displayFirstItem();
		playerMP = document.getElementById('MP'); // Captura del id del reproductor de audio, se asigna a una variable.
		addListeners();
		};
// ------------------------------------		
		function addListeners() 
		{
			if (player) {
			player.addModelListener("STATE", "stateListener");
			} else {
			setTimeout("addListeners()",100);
			}

		};
// ------------------------------------
		function stateListener(obj) 
		{ // estados a evaluar IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
			currentState = obj.newstate; 
			previousState = obj.oldstate; 
		 
			if ((currentState == "PLAYING")&&(previousState == "BUFFERING")) {
						
				if((playerMP !== null))
				{
				playerMP.sendEvent('VOLUME', '0'); // Mutea el reproductor del banner si está inicializado con playerReady.
				player.sendEvent('VOLUME', '100'); // Aseguramos que los videos salgan con 100 de volumen, siempre.
				}

			}
		};

// ------------------------------------
		function itemMonitor(obj)
		{
		gid('titulete').innerHTML = '<span>' + player.getPlaylist()[obj.index].title + '</span>';
		//gid('theAutor').innerHTML = '<span>(' + player.getPlaylist()[obj.index].author + ')</span>';
		gid('theCaption').innerHTML = '<span>' + player.getPlaylist()[obj.index].description + '</span>';
		//gid('duration').innerHTML = '<span>' + player.getPlaylist()[obj.index].duration + ' segs.</span>';
		};
// ------------------------------------
		function displayFirstItem()
		{
		if(player.getPlaylist())
		{
		  //itemMonitor({index:0});
		  itemMonitor({index:player.getConfig()['item']}); // por si está el shuffle en true o false, 					            												 funciona igual
		  player.addControllerListener('ITEM', 'itemMonitor');
		}
		else
		{
		  setTimeout("displayFirstItem()",100);
		}
		};
// ------------------------------------
		function gid(name)
		{
		return document.getElementById(name);
		};
// ------------------------------------
		function loadNplay(idx)
		{
		player.sendEvent('ITEM', idx);
		//setTimeout("player.sendEvent('PLAY', 'true')", 100);
		};
// ------------------------------------
      var playIndex =  null

      //carga una nueva playlist
      function loadPlaylist(playlist, idx)
      {
        playIndex = idx;
        player.sendEvent('LOAD', playlist);
        // opcional - para empezar en un item en particular del playlist
        setTimeout("player.sendEvent('ITEM', playIndex)", 200);
      };
	  



// fin update para V.4 -----------------------------

          ///////////////////////// reproductor /////////////////////////
          //////////// funciona con /swfobject.js (v2.1) ///////////////
 
function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
        swfobject.removeSWF(thePlayerId);
        var tmp=document.getElementById(theWrapper);
        if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
}
 
 
function createPlayer(theFile, ancho, largo, theImage) {
	
		// si theFile es una playlist lo dejamos así (PORTADA), 
		// SINO agregamos extensión al video y a la imagen (que viene como parámetro en theImage
		
		var theImage
		if (theFile.indexOf('xml')== -1){
			theImage = theImage + ".jpg"
			theFile = theFile + ".flv"
			}
			else{
			theFile = theFile
		}
		
        var flashvars = {
                file:theFile,
				image:theImage,
                autostart:"false",
				skin: "/js/mediaplayer/stylish_slim.swf", 
				displayclick:"none",  //era true
				controlbar:"bottom",
				shuffle:"true",
				stretching:"fill",
				icons:"true",
				volume:"100"
        }
        var params = {
                allowfullscreen:"true",
                allowscriptaccess:"always",
				wmode:"opaque"
        }
        var attributes = {
                id:'mpl',
                name:'mpl'
        }
        swfobject.embedSWF("/js/mediaplayer/player.swf", "player", ancho, largo, "9.0.115", false, flashvars, params, attributes);
}
  
function initPlayer(theFile, ancho, largo) { 
        deletePlayer('wrapper', 'player', 'mpl'); 
        createPlayer(theFile, ancho, largo);
}


// fin update para V.4 -----------------------------
