//Global vars
	try {
		document.domain = "batanga.com";
	} catch (e) {}

// ACCIONES DE LA VENTANA DEL PLAYER
var PLAY = 1;
var ADD = 2;


// Fecha de expiración de la cookie que indica si se abrió
// alguno de los players (video o radio)
// (como son 2 días después, realmente no importa mucho que la hora sea tomada
// al cargar la página y no justo antes de dar valor a la variable)
var expires=new Date();
expires.setTime(expires.getTime() + 48 * 60 * 60 * 1000); // expira dentro de 2 dias


function popUpWindow2(url, name, width, height) {

	var left 
	var top 
	left = getCenteredLeft('789') - 5;
	top = getCenteredTop('545') - 12;

	// Build the param string.
	var params = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0";
	params += ",width=" + width;
	params += ",height=" + height;
	params += ",left=" + left;
	params += ",top=" + top;
	params += ",screenX=" + left; // for Netscape.
	params += ",screenY=" + top;
	// Open the window. Set the opener property if it's missing. And return the window handle.
	var wh = window.open(url, name, params);
	return wh;

	//if (wh.opener == null) wh.opener = window;
}

function popUpWindow3(url, name, width, height) {

	var left;
	var top;
	left = getCenteredLeft(width) - 5;
	top = getCenteredTop(height) - 12;

	// Build the param string.
	var params = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0";
	params += ",width=" + width;
	params += ",height=" + height;
	params += ",left=" + left;
	params += ",top=" + top;
	params += ",screenX=" + left; // for Netscape.
	params += ",screenY=" + top;
	// Open the window. Set the opener property if it's missing. And return the window handle.
	var wh = window.open(url, name, params);
	return wh;

	//if (wh.opener == null) wh.opener = window;
}

function getCenteredLeft(width) {
	var sw = screen.availWidth;
	if (sw > 0) return (Math.floor((sw - width) / 2));
	else return (0);
}

function getCenteredTop(height) {
	var sh = screen.availHeight;
	if (sh > 0) return (Math.floor((sh - height) / 2));
	else return (0);
}


/****************************************************************************/
// FUNCIONES PARA EL PLAYER DE RADIO

var playerWindow = null;
var gGuid,gImage,gHeadline,gFrom;
var bShowBumper = false;
var strName,strWidth, strHeight, strPath;

strPath = '/player/new/default.asp';
strName = 'PTV3PlayerWindow'
strWidth = '691';
strHeight = '505';

function registerPlayer() {
	if(playerWindow!=null) {
		if(!playerWindow.closed) playerWindow.register();
	}
}

function openPlayer() {
	//ALWAYS try to get a reference to an already opened player window
	playerWindow =  popUpWindow2('', strName, strWidth, strHeight);
}	

// returns:
// true, if player window wasn't already opened and fully loaded
// false, if player window was already opened and loaded
function openPlayerWindow() {
	//just in case but shouldn't happen
//	alert('before check player window status');
	if (playerWindow == null) {
//		alert('playerWindow = null');
		playerWindow = popUpWindow2(strPath, strName, strWidth, strHeight);
		return true;
	}
	
	if(playerWindow.closed) {
//		alert('playerWindow closed');
		playerWindow.open();
		return true;
	}
	
	try {
		if(playerWindow.location.href.match(strPath)==null){ // player window wasn't already loaded, so it contains a blank page instead
//			alert('playerWindow in blank');
			//playerWindow.location=strPath;
			playerWindow = popUpWindow2(strPath, strName, strWidth, strHeight);
			return true;
		}
	} catch (e) {
//		alert('playerWindow in blank at catch');
		//playerWindow.location=strPath;
		playerWindow = popUpWindow2(strPath, strName, strWidth, strHeight);
		return true;
	}
	//alert('playerWindow already opened and loaded');
	return false;
}

function loadVideoPlayer()
{
	//alert("abriendo");
	setCookiePlayerOpened("videoPlayerOpened", "true",expires,"/")	
}
function unloadVideoPlayer()
{
	//alert("cerrando");
	setCookiePlayerOpened("videoPlayerOpened", "false",expires,"/")
}
function getCookiePlayerOpened(cookieName)
{
	cookieValue = getCookie(cookieName);
	if (cookieValue=="" || cookieValue==null)
	{
		playerCookies=getCookie("playersOpened");
		if (playerCookies!=null && playerCookies!="")
		{
			playerCookiesArray = playerCookies.split(";")
			if (cookieName == "videoPlayerOpened")
				cookieValue=playerCookiesArray[0];
			if (cookieName == "radioPlayerOpened")
				cookieValue=playerCookiesArray[1];
		}
	}
	return cookieValue;
}
function setCookiePlayerOpened(cookieName,newValue,expires,path)
{
	//alert("set cookie");
	playerCookies=getCookie("playersOpened");
	videoCookie=getCookie("videoPlayerOpened");
	radioCookie=getCookie("radioPlayerOpened");
	
	if (videoCookie=="" || videoCookie==null)
		videoCookie=false
	if (radioCookie=="" || radioCookie==null)
		radioCookie=false
			
	if (playerCookies=="" || playerCookies==null)
	{
		dnow=new Date();
		playerCookies=videoCookie+";"+radioCookie

		setCookie("videoPlayerOpened", "",dnow,"/")
		setCookie("radioPlayerOpened", "",dnow,"/")
	}
	playerCookiesArray = playerCookies.split(";")
	if (cookieName == "videoPlayerOpened")
		newPlayerCookie= newValue+";"+playerCookiesArray[1];
	if (cookieName == "radioPlayerOpened")
		newPlayerCookie= playerCookiesArray[0]+";"+newValue;
	
	setCookie("playersOpened", newPlayerCookie,expires,"/")
	
}
function isVideoPlayerOpen()
{
	r = getCookiePlayerOpened("videoPlayerOpened");
	//alert('isVideoPlayerOpen='+r);
	return (r=="true");
}

function loadDefaultPlaylist() {
	/*
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime()+30*60*1000);
	*/
	setCookie ("DefaultPLLoaded", "true", null,"/player/new");
	//setCookie ("DefaultPLLoaded", "true", now,"/player");
}

function defaultPlaylistLoaded() {
	r = getCookie("DefaultPLLoaded");
	//alert('DefaultPLLoaded=\'' + r + '\'' );
	return (r=="true")
}

function waitToLoadDefaultPL() {
	try {
		return !playerWindow.bDPLLoaded;
	} catch (e) {
		return true;
	}
}


function playerWindowAction(iAction,valTime) {
	if(playerWindow!=null) {
		try {
			playerWindow.opener = self;
		} catch (e) {}
//		playerWindow.opener = self;
		playerWindow.focus();
		switch(iAction) {
			case PLAY:
				window.setTimeout("try{playerWindow.playVideo(gGuid,gImage,gHeadline,gFrom);}catch(e){playerWindowAction(PLAY,200);}",valTime);
				break;
			case ADD:
				window.setTimeout("try{playerWindow.addVideo(gGuid,gImage,gHeadline,gFrom);if(bShowBumper) {playerWindow.showBumper(); bShowBumper = false;}}catch(e){playerWindowAction(ADD,200);}",valTime);
				break;
		}
	}
	window.setTimeout("try {playerWindow.repaintPlayListBox();}catch(e1){}",valTime+100);
 
}

	
function newPlayerWindow() {
	if(playerWindow==null)
		return (!isVideoPlayerOpen());
	if(playerWindow.closed) // not null but closed
		return playerWindow.closed;
}

/*********PLAY**********/ 

function openPlayer_Play(guid,image,headline,from) {	
	gGuid = guid;
	gImage=image;
	gHeadline=headline;
	gFrom=from;
	// assume that the player is already open, so:
	// no wait, try to get a reference of the window with an empty url
	var valTime = 100;
	var url = '';
	

	if(newPlayerWindow()) {
		valTime = 500;
		url = strPath;
	}
	
	
	playerWindow =  popUpWindow2(url, strName, strWidth, strHeight);
	//playerWindowAction(PLAY,valTime);
	
	var exec = false;
	exec = version();
   
	if (getCookie('speed')==null)  //cookie no set
	{  
		setCookieT('speed1','username',1);
	
		if (getCookie('speed1')!= null) //Cookie enabled en el browser
		{ 
			delete_cookie('speed1');

			if (exec) // version 9 o ++
			{	
				playerWindowAction(PLAY,valTime);
			}
		}	
	}
	else // cookie set asume vers OK
	{
		
		playerWindowAction(PLAY,valTime);
	}	
}

/*********ADD**********/ 

function openPlayer_Add(guid,image,headline,from) {
	gGuid = guid;
	gImage=image;
	gHeadline=headline;
	gFrom=from;
	// assume that the player is already open, so:
	// no wait, try to get a reference of the window with an empty url
	var valTime = 0;
	var url = '';
	
	if(newPlayerWindow()) {
		valTime = 500;
		url = strPath;
		bShowBumper = true;
	}
		

	playerWindow =  popUpWindow2(url, strName, strWidth, strHeight);
	playerWindowAction(ADD,valTime);
}

/*********STOP**********/ 
function openPlayer_Stop() {
	gGuid = "";
	gImage="";
	gHeadline="";
	gFrom="";
	openPlayer();
	window.setTimeout('playerWindowStop();',500);	
}

function playerWindowStop() {
	var bNew=openPlayerWindow();	
	if(playerWindow!=null) {
		playerWindow.opener = self;
		playerWindow.focus();
		valTime = bNew?1500:0;
		stopVideo(valTime);
	}
}

function stopVideo(valTime){
	//alert(playerWindow.playerFrame);
	//playerWindow.playerFrame.suspendVideo();
	window.setTimeout("try{playerWindow.suspendVideo()}catch(e){stopVideo()}",valTime);	
}

/****************************************************************************/
// FUNCIONES PARA EL PLAYER DE RADIO

var radioPlayerWindow = null;
var rGuid,rStationid,rStationBroadcaster,rStationname,rProvider,rCategoryFullName,rStrPath;
var r_strPath,r_strName,r_strWidth,r_strHeight;

r_strPath	= "/player/radio/default.asp";
r_strName	= "PTV3RadioPlayerWindow";
r_strWidth	= "690";
r_strHeight = "405";		

function openRadioPlayer() {
	//ALWAYS try to get a reference to an already opened player window
	radioPlayerWindow =  popUpWindow2('', r_strName, r_strWidth, r_strHeight);
}	

function openRadioPlayerWindow() {
	if (radioPlayerWindow == null) {
		radioPlayerWindow = popUpWindow2(rStrPath, r_strName, r_strWidth, r_strHeight);
		return true;
	}
	
	if(radioPlayerWindow.closed) {
		radioPlayerWindow.open();
		return true;
	}
	
	try {
		if(radioPlayerWindow.location.href.match(r_strPath)==null){ // player window wasn't already loaded, so it contains a blank page instead
			radioPlayerWindow = popUpWindow2(rStrPath, r_strName, r_strWidth, r_strHeight);
			return true;
		}
	} catch (e) {
		radioPlayerWindow = popUpWindow2(rStrPath, r_strName, r_strWidth, r_strHeight);
		return true;
	}
	return false;
}

function loadRadioPlayer()
{
	//alert("abriendo");
	setCookiePlayerOpened("radioPlayerOpened", "true",expires,"/")	
}
function unloadRadioPlayer()
{
	//alert("cerrando");
	setCookiePlayerOpened("radioPlayerOpened", "false",expires,"/")
}
function isRadioPlayerOpen()
{
	r = getCookiePlayerOpened("radioPlayerOpened");
	return (r=="true")
}
/*********PLAY**********/ 

function openRadioPlayer_Play(guid,stationid,stationBroadcaster,stationname,provider,categoryFullName)	
{	
	rGuid				= guid;
	rStationid			= stationid;
	rStationBroadcaster = stationBroadcaster;
	rStationname		= stationname;
	rProvider			= provider;
	rCategoryFullName	= categoryFullName;
	rStrPath = r_strPath+"?GUID="+guid+"&STATION_ID="+stationid+"&STATION_BROADCASTER="+stationBroadcaster+"&STATION_NAME="+stationname+"&PROVIDER="+provider+"&categoryFullName="+categoryFullName;
	
	//if (isVideoPlayerOpen())	//detener el player de video
	//	openPlayer_Stop();
	
	if (provider == "Live365")
	{	
		openRadioPlayer();
		window.setTimeout('radioPlayerWindowPlay();',500);
	}
	else if (provider == "Batanga")
	{
		openBatangaPlayer(stationid);		
	}
	else 
	{
		alert("Proveedor Desconocido");		
		return false;
	}		
}
function radioPlayerWindowPlay() {
	var bNew=openRadioPlayerWindow();	
	if(radioPlayerWindow!=null) {
		radioPlayerWindow.focus();
		valTime = bNew?1500:0;
		playStation(valTime);			
	}
}

function playStation(valTime){
	window.setTimeout("try{radioPlayerWindow.loadActualStation(rGuid,rStationid,rStationBroadcaster,rStationname,rProvider,rCategoryFullName)}catch(e){playStation()}",valTime);
}

/*********STOP**********/

function openRadioPlayer_Stop()	
{	
	openRadioPlayer();
	//hideRadioPlayer
	window.setTimeout('radioPlayerWindowStop();',500);	
}

function radioPlayerWindowStop() {
	var bNew=openRadioPlayerWindow();	
	if(radioPlayerWindow!=null) {
		radioPlayerWindow.focus();
		valTime = bNew?1500:0;
		stopStation(valTime);					
	}
}

function stopStation(valTime){
	window.setTimeout("try{radioPlayerWindow.hideRadioPlayer();}catch(e){stopStation()}",valTime);
}


/***************************************************/
function getAttributeValue(keywords,attSearchKey){
	//alert(keywords);
	kSplit = keywords.split(";");
	for (z=0;z<kSplit.length;z++){
		attSplit = kSplit[z].split("=");
		attKey= attSplit[0];
		if (attKey == attSearchKey)
			return attSplit[1];
	}	
}

//Cookie management

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "path=\/player") +
//  	   "; path=\/player"+
//  	  ((domain) ? "; domain=" + domain : "") +
  	  "; domain=batanga.com"+
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
  //alert('setCookie was called from somehwere with parameters: ('+name+","+value+")");
}

function setCookie2(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "path=\/player\/new") +
//  	   "; path=\/player"+
//  	  ((domain) ? "; domain=" + domain : "") +
  	  "; domain=batanga.com"+
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
  //alert('setCookie2 was called from somehwere with parameters: ('+name+","+value+")");
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

var timesChecked=0;
var gValue = null;

function monitorCookie(name,time) {
	var currValue;
	var action;
	currValue = getCookie(name);
	window.status='value of cookie "'+name+'" is: ' + currValue +' (' + timesChecked.toString() + ')';
	
	if(gValue!=currValue)
		alert('cookie "'+ name +'" changed from \'' + gValue + '\' to \'' + currValue +'\' at ' + new Date());
	gValue = currValue;
	
	action = "monitorCookie('" + name +"'," + time + ")";
	timesChecked++;
	window.setTimeout(action,time);
}
/*************************BATANGA PLAYER***********************/
function openBatangaPlayer(StationID)
{
	window.open('http://www.batanga.com/en/LaunchPlaylistEM.asp?StationID='+StationID+'&Format=wm&SiteID=5&Speed=1&context=planeta',
		'LaunchPlayerAndPlaylist',
		'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,width=470,height=426');
}



/*************************WMP version***********************/
function detectWMP()
{
	var wmpInfo = {
		installed: false,
		scriptable: false,
		type: null,
		versionInfo: null
	};

	var wmp64 = "MediaPlayer.MediaPlayer.1";
	var wmp7 = "WMPlayer.OCX.7";

	if((window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1) || window.GeckoActiveXObject)
	{
		
		wmpInfo.type = "ActiveX";
		var player = createActiveXObject(wmp7);
		
		if(player) //IE
		{
			wmpInfo.installed = true;
			wmpInfo.scriptable = true;
			wmpInfo.versionInfo = player.versionInfo;
			return wmpInfo;
		}
		else
		{
			player = createActiveXObject(wmp64);
			if(player)
			{
				wmpInfo.installed = true;
				wmpInfo.scriptable = true;
				wmpInfo.versionInfo = "6.4";
				return wmpInfo;
			}
			else // Firefox
			{ 
			  //MIMES SOPORTADOS PARA LAS VERSIONES 9 - 10 
			  //http://forums.mozillazine.org/viewtopic.php?t=206213
			  var player1 = window.navigator.mimeTypes['application/x-drm-v2'].enabledPlugin; //DRM Netscape Network Object
			  var player11 = window.navigator.mimeTypes['application/x-drm'].enabledPlugin;//DRM Store Netscape Plugin
			  
			  var player2 = window.navigator.mimeTypes['application/asx'].enabledPlugin;//Npdsplay dll
			  var player3 = window.navigator.mimeTypes['video/x-ms-asf-plugin'].enabledPlugin;	//Npdsplay dll
			  var player4 = window.navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;//Npdsplay dll
			  var player5 = window.navigator.mimeTypes['video/x-ms-asf'].enabledPlugin;//Npdsplay dll
			  var player6 = window.navigator.mimeTypes['video/x-ms-wm'].enabledPlugin;//Npdsplay dll
			  var player7 = window.navigator.mimeTypes['audio/x-ms-wma'].enabledPlugin;//Npdsplay dll
			  var player8 = window.navigator.mimeTypes['audio/x-ms-wax'].enabledPlugin;//Npdsplay dll
			  var player9 = window.navigator.mimeTypes['video/x-ms-wmv'].enabledPlugin;//Npdsplay dll
			  var player10 = window.navigator.mimeTypes['video/x-ms-wvx'].enabledPlugin;//Npdsplay dll
			  
			  var plug1 = (((((((((player1 && player2) && player3) && player4) && player5) && player6) && player7) && player8) && player9) && player10)
			  
			  if (player11 && plug1)	
			  { 
			    wmpInfo.installed = true;
				wmpInfo.versionInfo = "9 o Superior";
				return wmpInfo;
			  }
			}
		}
	}
	else if(navigator.mimeTypes)
	{
		wmpInfo.type = "NetscapePlugin";
		var player = window.navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
		if(player)
		{				
			wmpInfo.installed = true;
			//wmpInfo.scriptable = false;
			wmpInfo.versionInfo = "PluginVersion";
			return wmpInfo;
		}
		return wmpInfo;
	}
	
	
}
function createActiveXObject(id)
{
  var error;
  var control = null;

  try
  {
    if (window.ActiveXObject)
    {
      control = new ActiveXObject(id);
    }
    else if (window.GeckoActiveXObject)
    {
      control = new GeckoActiveXObject(id);
    }
  }
  catch (error)
  {
    ;
  }
  return control;
}
	


function version()
{
   var detectionResults = detectWMP();
   
   if(detectionResults.installed)
	{
	    //Verificando version WMP
		tmpChar = detectionResults.versionInfo.substring(0,1);
		//tmpChar="8";
		//alert (tmpChar);
		if ((Number(tmpChar)>8 )||(Number(tmpChar)==1 ))
			return true;		
		else
			return false;

	}
}


function delete_cookie (name, path)
{
	// Build expiration date string:
	var expiration_date = new Date();
	expiration_date.setYear(expiration_date.getYear() - 1);
	expiration_date = expiration_date.toGMTString();

	// Build set-cookie string:
	var cookie_string = escape(name)+"=;expires="+ expiration_date;
	if (path != null)
		cookie_string+=";path="+path;

	// Delete the cookie:
	document.cookie = cookie_string;
}

function setCookieT(c_name,value,expiredays)
{	
	var exdate=new Date();
	exdate.setTime(exdate.getTime()+(expiredays*24*3600*1000));
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate);
}
