var EXPLORER; //define si es explorer o firefox
var WMA11; //define si tiene plugin wma 11
var PLAYERLIST = new Array(); //almacena los players q tendra q pintar
var WMAOBJ;
var SAFARI=false; // dtecta si el navegador es safari
var URLPLUGIN;
var NAMEPLUGIN;
var INSTAGE;
var idAudio;

var agt=navigator.userAgent.toLowerCase(); 
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1)); 
var mac = (agt.indexOf("mac")!=-1); 

if(win){
	EXPLORER = false;
	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) EXPLORER=true;
	else if(navigator.userAgent.indexOf("Chrome") != -1) EXPLORER=true;

	if(EXPLORER==true) WMA11 = true;
	else {
		URLPLUGIN = "http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx";
		NAMEPLUGIN = "Windows media 11";
		WMA11= false;
		for(var a=0; a<navigator.plugins.length; a++){
			if(navigator.plugins[a].name=="Microsoft® Windows Media Player Firefox Plugin"){
				WMA11 = true;
				break;
			}
		}
	}
} else {
	URLPLUGIN = "http://www.apple.com/downloads/macosx/video/windowsmediaplayerformacosx.html";
	NAMEPLUGIN = "Windows media 9";
	SAFARI = false;
	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) SAFARI=true;	
	}
}

//pinta el objeto windowsa media con su codec correspondiente
function printWMAObject() {
	if (!document.getElementById("div_wma")) return;
	var temp = '<OBJECT type="application/x-ms-wmp" id="playerWMA" width="0" height="0">';
	if(EXPLORER) temp='<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" TYPE="application/x-oleobject" id="playerWMA" width="0" height="0">';
	temp += '<param name="URL" value="" />';
	temp += '<param name="autoStart" value="false" />';
	temp += '<param name="uiMode" value="invisible" />';
	temp+='</object>';
	document.getElementById("div_wma").innerHTML = temp;
	WMAOBJ = document.getElementById("playerWMA");
}

function makePlayer(temp){
	if (!WMAOBJ) printWMAObject();
	if(INSTAGE==temp.id){
		var layer = document.getElementById("div_"+temp.id);
		if(layer.style.visibility=="visible"){
			if ( WMAOBJ && WMAOBJ.controls) WMAOBJ.controls.stop();
			INSTAGE="";
			layer.innerHTML = "";
		} 
		else printFlashObject(temp);
	} else {
		if (INSTAGE) document.getElementById("div_"+INSTAGE).innerHTML = "";
		INSTAGE=temp.id;
		document.getElementById("div_"+temp.id).style.visibility="visible";
		document.getElementById("div_"+temp.id).style.position="absolute";
		document.getElementById("div_"+temp.id).style.zIndex="8";
		document.getElementById("div_"+temp.id).style.padding="0px";
		document.getElementById("div_"+temp.id).style.margin="0px";
		printFlashObject(temp);
	}
}

//pinta los bloques de flash o la salida fallida si no tiene plugin
function printFlashObject(temp){
	var nameMovie = "audio_wma11.swf";
	if(SAFARI) nameMovie = "audio_wma11_safari.swf";

	if((WMA11) || (SAFARI)){
		var strAux = '';
		strAux += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="190" height="19" id="audioPlayer" align="top">';
		strAux += '<param name="allowScriptAccess" value="sameDomain" />';
		strAux += '<param name="allowFullScreen" value="true" />';
		strAux += '<param name="movie" value="/flash/audioPlayer/' + nameMovie + '" />';
		strAux += '<param name="quality" value="best" />';
		strAux += '<param name="bgcolor" value="#000000" />';
		strAux += '<param name="menu" value="false" />';
		strAux += '<param name="wmode" value="window" />';
		strAux += '<param name="FlashVars" value="tipoAudio_fv=' + temp.tipo + '&urlAudio_fv=' + temp.href + '&idAudio_fv=audioPlayer&autostart_fv=' + temp.autostart + '&ancho_fv=' + temp.ancho + '&corto_fv=' + temp.corto + '&totalTime=' + temp.totalTime + '" />';
		strAux += '<embed src="/flash/audioPlayer/' + nameMovie + '" FlashVars="tipoAudio_fv=' + temp.tipo + '&urlAudio_fv=' + temp.href + '&idAudio_fv=audioPlayer&autostart_fv=' + temp.autostart + '&ancho_fv=' + temp.ancho + '&corto_fv=' + temp.corto + '&totalTime=' + temp.totalTime + '"';
		strAux += ' quality="best" bgcolor="#000000" width="190" height="19" name="audioPlayer" allowFullScreen="true" align="top" ';
		strAux += 'swLiveConnect="true" allowScriptAccess="sameDomain" wmode="window"';
		strAux += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" menu="false" />';
		strAux += '</object>';
	} else {
		var strAux = '<div style="background:#ddd;color:#000;text-transform:none;font-size:10px">Para poder escuchar correctamente el audio necesita descargarse el plugin <a href="' + URLPLUGIN + '" target="_blank" style="background:none">Windows Media 11</a></div>';
	}
	document.getElementById("div_"+temp.id).innerHTML = strAux;
}

//control de volumen
function changeVolumen(volumen) {
	if(WMAOBJ) WMAOBJ.settings.volume = volumen;
}

//ajuste de url de wma a reproducir
function setUrlPlayer(args){
	if (!WMAOBJ) printWMAObject();
	WMAOBJ.URL=args;
}

function controlPlayer(args, id){
	switch (args) {
		case "Play":
			idAudio=id;
			timeId=setInterval("checkTotalTime()",1000);
			WMAOBJ.controls.play();
			break;
		case "Pause":
			WMAOBJ.controls.pause();
			break;
		case "Stop":
			WMAOBJ.controls.stop();
			break;
	}
}

//controles del player play, pasuse etc MAC
function controlPlayerEmbed(args, id, url){
    switch (args) {
        case "Play":
           	var temp='<EMBED type="application/x-mplayer2" width="1" height="1" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" SRC="' + url + '" name="playerWMA" autostart="1" showstatusbar="0" showcontrols="0"></EMBED>';
           	document.getElementById("div_wma").innerHTML = temp;
			idAudio=id;
			timeId=setInterval("getTotalTime()",1000);
           	break;
        case "Pause":
           	document.getElementById("div_wma").innerHTML = "";
			break;
       	case "Stop":
           	break;
    }
}

// ******************** COMUNICACION CON FLASH ************************* //

//comprueba que el player e windows media esta listo
function checkTotalTime(){

	if((WMAOBJ.currentMedia.durationString!="00:00")&&(WMAOBJ.currentMedia.durationString!="")){
		getTotalTime();
		clearInterval(timeId);
	}


}





//devuelve la pelicula flash
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) return window[movieName];
    else return document[movieName];
}

//DEVUELVE EL TIEMPO TOTAL DEL AUDIO Q SE ESTA REPRODUCIENDO
function getTotalTime(){
	if (!WMAOBJ) return;
	setTimeout("clearInterval("+timeId+")",10);
	thisMovie(idAudio).setTotalTimeJS(WMAOBJ.currentMedia.durationString);
}

//DEVUELVE EL TIEMPO ACTUAL DEL AUDIO Q SE ESTA REPRODUCIENDO
function getCurrentTime(){
	thisMovie(idAudio).setCurrentTime(WMAOBJ.controls.currentPositionString);
}
