﻿var currentPosition = 0; 
var currentTtal = 0;
var player = null;
var user=getUser();
var idUsuario=user['id'];
var PLAYERLIST = new Array(); //almacena los players q tendra q pintar
var SAFARI=false; // dtecta si el navegador es safari
var EXPLORER; //define si es explorer o firefox
var WMA11; //define si tiene plugin wma 11
var currentState = "NONE"; 



//comprueba elnavegador y si tiene player windows media11 si es firefox
function initPlayerNew(){
	setPluginNew();
	fillPlayerNew();	
}

function playerReady(thePlayer) {
   player = window.document[thePlayer.id];
   addListeners();
}

function addListeners() {
   if (player) { 
      player.addModelListener("STATE", "stateListener");
      //if(idUsuario==undefined){ 
	      player.addModelListener("TIME", "positionListener");
	      currentTtal=30;
	   //}
   } else {
	   setTimeout("addListeners()",100);
   }
}
 function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	   currentState = obj.newstate; 
 	   if (currentState == "PLAYING") {
		   imgEcualizador("play");
		}else{
		   imgEcualizador("stop");
	   }
   }

//posicion en la que va el audio
function positionListener(obj) { 
   currentPosition = parseInt(obj.position); 
   
   if (currentPosition>currentTtal){
      player.sendEvent('STOP');
   }
}

//almacena los players q tiene q pintar
function setPlayernNew(temp){
	var encontrado = false
	
	//busca antes si ya se encontraba para reemplazarlo
	for (var i=0; i < PLAYERLIST.length; i++){
		if (PLAYERLIST[i].id == temp.id){
			PLAYERLIST[i] = temp;
			encontrado = true
			break;		
		}
	}
	//si no lo ha encontrado lo setea
	if (!encontrado)
		PLAYERLIST.push(temp);
	   initPlayerNew();
	
}

//pinta los players de la pagina
function fillPlayerNew(){
	for(var a=0; a<PLAYERLIST.length; a++){
		FlashObject(PLAYERLIST[a]);
	}
}

//pinta los bloques de flash o la salida fallida si no tiene plugin
function FlashObject(temp){
	var id = temp.id;
	var xref = temp.href;
	var autostart = temp.autostart;
	var ancho = temp.ancho;
	
	
	
	if(SAFARI)var nameMovie = "audioPlayer_mini_wma11_safari.swf";
		else var nameMovie = "player-radio.swf";
	
	if((WMA11) || (SAFARI)){
		
		var strAux = '';

      strAux += '<object id="'+ id +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9.0.115" width="' + ancho + '" height="40">';
      strAux += '<param name=bgcolor value="#FFFFFF">';
      strAux += '<param name=movie value="/flashp/audioplayer/' + nameMovie + '">';
      strAux += '<param name=allowfullscreen value="true">';
      strAux += '<param name=allowscriptaccess value="always">';
      strAux += '<param name="flashvars" value="height=40&width=250&skin=flashp/audioplayer/bekle.zip&file=' + xref + '&autostart='+autostart+'">';

      strAux += '<embed name="'+ id +'" ';
      strAux += 'type="application/x-shockwave-flash" ';
      strAux += 'width="' + ancho + '" height="40"'; 
      strAux += 'bgcolor="#FFFFFF" ';
      strAux += 'src="/flashp/audioplayer/' + nameMovie + '"';
      strAux += 'allowfullscreen="true"';
      strAux += 'allowscriptaccess="always"';
      strAux += 'flashvars="height=40&width=250&skin=flashp/audioplayer/bekle.zip&file=' + xref + '&autostart='+autostart+'">';
      strAux += '</embed>';
      strAux += '</object>';
      addMediaItem({id:id});
	} else {
		
		var strAux = 'Para poder escuchar correctamente el audio necesita descargarse el plugin <a href="' + URLPLUGIN + '" target="_blank">Windows Media 11</a>';
	}
	
	var DIVFLASH = document.getElementById("div_" + id);
	DIVFLASH.innerHTML = strAux;
	
}

//pinta el objeto windowsa media con su codec correspondiente
function WMAObject(){
	
	var temp = '';
	if(EXPLORER){
		temp+='<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" TYPE="application/x-oleobject" id="playerWMA" width="0" height="0">';
	} else {
		temp+='<OBJECT type="application/x-ms-wmp" id="playerWMA" width="0" height="0">';		
	}
	//temp += '<param name="URL" value="' + URLAUDIO + '" />';
	temp += '<param name="URL" value="" />';
	temp += '<param name="autoStart" value="False" />';
	temp += '<param name="uiMode" value="invisible" />';
	temp+='</object>';
	
	
	if(EXPLORER){
		if(WMA11)temp+='<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" TYPE="application/x-oleobject" id="playerWMABACK" width="0" height="0">';
			else temp+='<OBJECT ID="playerWMABACK" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" TYPE="application/x-oleobject" width="0" height="0">';
	} else {
		temp+='<OBJECT type="application/x-ms-wmp" id="playerWMABACK" width="0" height="0">';		
	}
	
	//temp += '<param name="URL" value="' + URLAUDIO + '" />';
	temp += '<param name="URL" value="" />';
	temp += '<param name="autoStart" value="False" />';
	temp += '<param name="uiMode" value="invisible" />';
	temp+='</object>';
	
	
	var DIVAUDIO = document.getElementById("div_wma");
	if(DIVAUDIO)
	DIVAUDIO.innerHTML = temp;
	WMAOBJ = document.getElementById("playerWMA");
	WMAOBJBACK = document.getElementById("playerWMABACK");
	if(WMAOBJBACK)
	if(WMAOBJBACK.settings)WMAOBJBACK.settings.volume = 0;

}


function setPluginNew(){
	
	var agt=navigator.userAgent.toLowerCase(); 
	var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1)); 
	var mac = (agt.indexOf("mac")!=-1); 
	
	if(win){
		EXPLORER = set_navegadorNew();
		if(EXPLORER==true){
			//WMA11 = checkPluginExplorer();
			WMA11 = true;
		} else {
			URLPLUGIN = "http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx";
			NAMEPLUGIN = "Windows media 11";
			WMA11 = checkPluginNew();
		}
		
		if(WMA11)WMAObject();
		
	} else {
		
		URLPLUGIN = "http://www.apple.com/downloads/macosx/video/windowsmediaplayerformacosx.html";
		NAMEPLUGIN = "Windows media 9";
		SAFARI = checkPluginMac();
	}
}

//comprobacion de navegador
function set_navegadorNew(){
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1 && navigator.userAgent.indexOf("Opera") < 0){
		nav = true;
	} else if(navigator.userAgent.indexOf("Chrome") != -1){
		nav = true;
	}else if(navigator.userAgent.indexOf("Opera") != -1){
		nav = false;
	}else if(navigator.appName.indexOf("Netscape") != -1 || navigator.userAgent.indexOf("Mozilla") != -1){
		nav = false;
	}else{
		nav = false;
	}
	return nav;
}


//comprobacion plugin wma 11
function checkPluginNew(){
	
	var vuelta = false;
	for(var a=0; a<navigator.plugins.length; a++){
		//if(navigator.plugins[a].name.indexOf("Windows Media Player Firefox Plugin")!=-1){
		if(navigator.plugins[a].name=="Microsoft® Windows Media Player Firefox Plugin"){
			vuelta = true;
			break;
		}
	}
	return vuelta;
}
function checkPluginExplorer(){
	var vuelta = false;
	if(new ActiveXObject('WMPlayer.OCX')) {
		vuelta=true;
	} else {
		vuelta=false;
	}
	return vuelta;
}

function checkPluginMac(){
	
	var nse = "";
	for (var i=0;i<navigator.mimeTypes.length;i++) {
		nse += navigator.mimeTypes[i].type.toLowerCase(); 
	}
	if (nse.indexOf("application/x-mplayer2") != -1){
		if (navigator.mimeTypes["application/x-mplayer2"].enabledPlugin != null){
			return true;	
		} else {
			return false;
		}
	}
}

function imgEcualizador(accion){
	if(accion=="play"){
		var classSelect = "ecu-anim";
	} else {
		classSelect = "ecu";
	}
	var ecualizadorImg = document.getElementById('ecualizador');
	if(ecualizadorImg)ecualizadorImg.className = classSelect;
}

