﻿// JavaScript Document
//<script>
var barIs = false;
function switchMMixBar() {
	if (barIs) {
		closeMMixBar();
	}
	else {
		openMMixBar();
	}
}
function openMMixBar() {
	barIs = true;
	document.getElementById('reproductorFlotante').style.height = '260px';
//	document.getElementById('reproductorFlotante').style.marginLeft = '';
//	$('#reproductorFlotante').animate({width:980}, 400);
	$('#reproductorFlotanteCnt').animate({height:278}, 400);
}

function closeMMixBar() {
	barIs = false;
//	var animation = {};
//	animation.width = 980;
//	if ($('#reproductorFlotante').css('marginLeft') != 'auto') {
//		animation.marginLeft = $('#header').offset().left;
//	}
//	$('#reproductorFlotante').animate(animation, 200, function() {
//		$('#reproductorFlotante').css('marginLeft', 'auto');
//	});
	$('#reproductorFlotanteCnt').animate({height:46}, 200);
	setTimeout("document.getElementById('reproductorFlotante').style.height = '46px';/*document.getElementById('reproductorFlotante').style.width = '985px';document.getElementById('reproductorFlotante').style.marginLeft = '';*/",200);
}

function thirdMMixBar() {
	barIs = true;
	document.getElementById('reproductorFlotante').style.height = '450px';
	if ($('#reproductorFlotante').css('marginLeft') == 'auto') {
		$('#reproductorFlotante').css('marginLeft', $('#reproductorFlotante').offset().left + 'px');
	}	
	$('#reproductorFlotante').animate({marginLeft:764, width:360}, 200);
	$('#reproductorFlotanteCnt').animate({height:450}, 300);
}

var playerState = true;
var myGPlayer = null;
function playerReady(obj) {
	alert('said...');
}
function playerReadyCallback(obj) {
//	myGPlayer = document.getElementById(obj['id']);
//	myGPlayer.addEventListener("STATE","player_handle_STATE");
//	myGPlayer.addControllerListener("STOP","player_handle_STOP");
//	myGPlayer.sendEvent("PLAY", true);
//	setTimeout('myGPlayer.sendEvent("PLAY", false);', 8000);
//	setTimeout('myGPlayer.sendEvent("PLAY", true);', 8600);
};

function loadNew(hd) {
	if (!hd) hd = false;
	var $file = "http://przvll-tem.lookin.at:8000/;/stream.mp3";
	if (hd) {
		$file = "http://przvll-tem.lookin.at:8000/;/stream.mp3";
	}
	jwplayer("player").setup({
        flashplayer: "/mediaplayer/v2/player.swf",
        file: $file,
		id: 'playerjw',
		provider: 'sound',
		autostart: true,
        height: 18,
        width: 60
    });
/*
	so.addParam('allowscriptaccess','always');
	so.addVariable('allowscriptaccess','always');
	so.addParam('wmode','opaque');
	so.addVariable('provider', 'sound');

	so.addVariable('backcolor','666666');
	so.addVariable('frontcolor','EEEEEE');
	so.addVariable('lightcolor','FFFFFF');
	so.addVariable('duration','-1');
	so.addVariable('bufferlength','6');
	so.addParam('enablejs','always');
	so.addVariable('enablejs','true');
//	so.addVariable('autostart','true');
	so.addVariable('repeat','always');
    so.addVariable('playerready','playerReadyCallback');*/
}

function OlderloadNew(hd) {
	if (!hd) hd = false;
	document.getElementById('player').innerHTML = '';
	var so = new SWFObject('http://puertoremix/argoz/fp/player.swf','mmixplayer','60','18','9');
	so.addParam('allowscriptaccess','always');
	so.addVariable('allowscriptaccess','always');
	so.addParam('wmode','opaque');
	so.addVariable('provider', 'sound');
	if (hd) {
		so.addVariable('file','http://przvll-tem.lookin.at:8000/;stream.mp3');	
	}
	else {
		so.addVariable('file','http://przvll-tem.lookin.at:8000/;stream.mp3');
	}
	so.addVariable('backcolor','666666');
	so.addVariable('frontcolor','EEEEEE');
	so.addVariable('lightcolor','FFFFFF');
	so.addVariable('duration','-1');
	so.addVariable('bufferlength','6');
	so.addParam('enablejs','always');
	so.addVariable('enablejs','true');
//	so.addVariable('autostart','true');
	so.addVariable('repeat','always');
    so.addVariable('playerready','playerReadyCallback');
	so.write('player');

	/*div = document.createElement('div');
	div.className = 'swapPlayer';
	div.setAttribute('title', 'Cambiar el reproductor flash por Windows Media Player (el viejo)');
	div.onclick = function() {loadOld();};
	div.innerHTML = 'Cambiar a WMP';
	
	document.getElementById('player').appendChild(div);*/
	/*div = document.createElement('div');
	div.className = 'noListen';
	div.innerHTML = '<a href="http://movidamix.com/wmp.asx"><img src="images/wmp10.png" /></a>';
	div.innerHTML += '<a href="http://przvll-tem.lookin.at:8000/listen.pls">       <img src="images/wa.png" /></a>';
	document.getElementById('player').appendChild(div);*/
}

var player;
function playerReady(thePlayer) {
	//alert('Just called ' + thePlayer);
	player = document.getElementById(thePlayer.id);
}

var lastState = null;
var lastSong = null;
var selectedInfoBox = null;
function refreshDj() {
	makeHttpRequest('../onair', '_refreshDj', false);
}

function parseSongData(songdata) {
	var r = songdata.toLowerCase().replace('.mp3', '').replace(/\[[^\]]*\]/, '').replace(/\([^\)]*\)/, '').replace(/\_/g, ' ').split('-')
	
	var r2 = Array();
	for (var i = 0; i < r.length; i++) {
		if (!r2.in_array(r[i].trim())) {
			r2.push(r[i].trim());
		}
	}
	
	
	
	return r2.join(" - ").replace(/^[^a-z0-9]*/gm, '').replace(/[^a-z0-9]*$/gm, '').replace("\n", ' <br /> ').replace('unknown', '');
}

function _refreshDj(data) {
	eval('var myData = ' + data);
	data = myData;
	
/*	for (i = 0; i < staff.length; i++) {
		staff[i].obj.recover();
	}*/
	var nowPlayingBox = document.getElementById('nowPlaying');
   	var nowPlayingTop = document.getElementById('nowPlayingTop');
	if (!nowPlayingTop) {
		nowPlayingTop = {innerHTML : function () {} };
	}
	if (data.onair == 'yes') {
		if (data.type == 'dj') {
/*			if (typeof(staffMapping[data.dj]) != 'undefined') {
				idsStaff = staffMapping[data.dj];
				
				for (i=0; i < idsStaff.length; i++) {
					if (staff[idsStaff[i]].obj.src != staff[idsStaff[i]].c) {
						staff[idsStaff[i]].obj.src = staff[idsStaff[i]].c;
					}
				}
				nowPlayingBoxText = '** En vivo **';
			}
			else {*/
			if (data.dj == 'cien') {
				nowPlayingBoxText = '100 interactivo - Vota desde la web lo que queres escuchar www.radioexitofm.com';
			}
			else if (data.dj == 'cast' && data.song != null) {
				songdata = parseSongData(data.song);
				nowPlayingBoxText = songdata;
			}
			else {
				nowPlayingBoxText = '** En vivo **';
			}
		}
		else if (data.type == 'playlist' && data.song != null) {
			songdata = parseSongData(data.song);
			
			nowPlayingBoxText = songdata;		
		}
		else {
			nowPlayingBoxText = 'Sin informaci&oacute;n';
		}
	}
	else {
		nowPlayingBoxText = 'Sin informaci&oacute;n';
	}
	
    nowPlayingBox.innerHTML = nowPlayingBoxText;
    var lio = nowPlayingBoxText.lastIndexOf('-');
	var artist = nowPlayingBoxText.substr(0,lio);
	var title = nowPlayingBoxText.substr(lio+1);

	document.getElementById('trackInfoArtist').innerHTML = artist;
	document.getElementById('trackInfoTitle').innerHTML = title;
        
    var nowPlayingBoxText2 = artist + "<br />" + title;
    
    var lio1 = nowPlayingBoxText2.lastIndexOf('(');
    if (lio1 > nowPlayingBoxText2.length / 2) {
	    nowPlayingTop.innerHTML = nowPlayingBoxText2.substr(0,lio1);
    }
    else {
    	nowPlayingTop.innerHTML = nowPlayingBoxText2;
    }
    
/*	if (lastState != null && lastState != data.type) {
		myFrame = window.parent.frames[0];
		
		if (typeof(myFrame.popChange) == 'function') {
			if (data.type == 'dj') {
				msg = 'un programa en vivo';
			}
			else {
				msg = 'la mejor musica';
			}
			myFrame.popChange(msg);
		}
	}*/
	
	lastState = data.type;
	if (barIs && lastSong != songdata) {
		if (selectedInfoBox == 'Letras'){
			refreshLyric();
		}
		else if (selectedInfoBox == 'Temas') {
			barra_refreshLast();
		}
		lastSong = songdata;
	}
	
}

function player_init() {
	loadNew();
	refreshDj();
	djRefresher = setInterval('refreshDj();', 15000);
}

function refreshLyric() {
	//añadir comprobacion de que sea el tema actual
	makeHttpRequest('/lyrics', '_refreshLyric', false);
}

function _refreshLyric(data) {
	$('#contenedorLetras').html(data);
}

function barra_refreshLast() {
	//añadir comprobacion de que sea el tema actual
	makeHttpRequest('/lastSongs', '_barra_refreshLast', false);
}

function _barra_refreshLast(data) {
	$('#contenedorTemas').html(data);
}


function barra_fondo(nom) {
	selectedInfoBox = nom;
	_gaq.push(['_trackEvent', 'Botonera Roja', nom]);
	document.getElementById('barraContenidos').style.background = 'url(http://movidamix.com/imagenes/barra/fondo' + nom + '.png)';
	$('#barraContenidos>div').hide();
	$('#contenedor' + nom).show();
}

function btnPlay_switch(i) {
	if (i == 'l') {
		$('#btnPlay').css('backgroundPosition', '-210px 0');
	}
	else if (i == 'a') {
		$('#btnPlay').css('backgroundPosition', '-180px 0');
	}
}


function btnPlay_click() {
//	if (myGPlayer == null) {
//		alert('El reproductor se esta cargando, intenta en unos instantes.');
//		return false;
//	}
	if (playerState) {
		playerState = false;
//		myGPlayer.sendEvent("PLAY", false);
		jwplayer('player').pause();
		_gaq.push(['_trackEvent', 'Reproductor', 'Pause']);
		btnPlay_switch('l');
	}
	else {
		playerState = true;
//		myGPlayer.sendEvent("PLAY", true);
		jwplayer('player').play();
		_gaq.push(['_trackEvent', 'Reproductor', 'Play']);
		btnPlay_switch('a');
	}
}
	
	function modMsg_send() {
		if (document.getElementById('msgMensaje').value == '') {
			$('#modMsg_dialogText').html('No se puede enviar un mensaje en blanco.');
			$('#modMsg_dialog').dialog({width: 400, title: 'Fallo envio de mensaje', modal: true, closeText: 'cerrar', buttons: {"Aceptar": function() {$(this).dialog('close'); } }});
			return false;
		}
		
		data = 'msgNombre=' + escape(document.getElementById('msgNombre').value) + 
			'&msgMail=' + escape(document.getElementById('msgMail').value) +
			'&msgMensaje=' + escape(document.getElementById('msgMensaje').value);
		makeHttpRequest('IMS.send', '_modMsg_send', false, data);
		
		return false;
	}
	function _modMsg_send(data) {
		$('#modMsg_dialogText').html('El mensaje ha sido enviado con exito.');
		$('#modMsg_dialog').dialog({width: 400, title: 'Mensaje', modal: true, closeText: 'cerrar', buttons: {"Aceptar": function() {$(this).dialog('close'); } }});
		
		document.getElementById('msgNombre').value = '';
		document.getElementById('msgMail').value = '';
		document.getElementById('msgMensaje').value = '';
	}
	
	function myMarq(id, d) {
		var obj = document.getElementById(id);
		var dest;
		
		if (d == 1) {
			dest = $($(obj).children('div')[0]).width();
		}
		else {
			dest = 0;
		}
		$(obj).animate({scrollLeft: dest}, 5000);
		d = d*-1;
		
		setTimeout("myMarq('" + id + "', " + d + ");", 6000);
	}
	
	myMarq('nowPlayingCnt', 1);

	function setMouseToggle(id, pos) {
		$('#' + id).mouseover(
			function() { $(this).css('backgroundPosition', pos + 'px -30px'); }
		).mouseout(
			function() { $(this).css('backgroundPosition', pos + 'px 0'); }
		);
	}
	
	setMouseToggle('btnVolume', '-150');
	
	setMouseToggle('btnBad', '-270');
	setMouseToggle('btnGood', '-240');
	
	setMouseToggle('btnLyric', '-540');
	setMouseToggle('btnInfo', '-660');
	setMouseToggle('btnLast', '-780');
	setMouseToggle('btnHD', '-420');

	//$("#btnHD").click(function() {
	//	_gaq.push(['_trackEvent', 'Botonera Roja', 'Escuchar en HD']);
	//	$('#modMsg_dialogText').html('Proximamente podras disfrutar el sonido de la radio con gran fidelidad usando RadioExito HD.');
	//	$('#modMsg_dialog').dialog({width: 400, title: 'RadioExito', modal: true, closeText: 'cerrar', buttons: {"Aceptar": function() {$(this).dialog('close'); } }});
	//});
var playerDef = 'sd';
   $("#btnHD").toggle(
		function () {
			_gaq.push(['_trackEvent', 'Botonera Roja', 'Escuchar en HD']);
			$('#modMsg_dialogText').html('<br />Este modo genera m&aacute;s transferencia de datos, por lo tanto se puede entrecortar la transmisi&oacute;n si tu conexi&oacute;n no la soporta, o si te encuentras descargando archivos.<br />');
			$('#modMsg_dialog').dialog({width: 400, title: 'Se activar&aacute; el modo HD.', modal: true, closeText: 'cancelar', buttons: {"Aceptar":
	function() {
		$('#barraLogo').css('backgroundPosition', '0 -30px');
		loadNew(true);
		playerDef = 'hd';
		$(this).dialog('close');
	}, "Cancelar": function() { $("#btnHD").click(); $(this).dialog('close'); } }});
		
		},
		function () {
			if (playerDef == 'hd') {
				_gaq.push(['_trackEvent', 'Botonera Roja', 'Escuchar en HD']);
				loadNew();
				$('#barraLogo').css('backgroundPosition', '0 0');
			}
		}
	);


//</script>
