var currentDate = new Date();
var dateIni = new Date();
var dateFin = new Date();
var currentEndirecte = "NO";
var intervalDirecte;
var intervalTime = 10000; //2 segundos para la encuesta
var videoDiferitStr = "";
var dateStr = currentDate.getFullYear().toString()+((currentDate.getMonth()+1)>9?"":"0")+(currentDate.getMonth() + 1).toString()+(currentDate.getDate()>9?"":"0")+currentDate.getDate().toString();

//Obtenemos fecha actual de server
loadXMLDoc("http://www.3xl.cat/su/time.jsp?format=HH:mm&type=xml&xdays=0",false,"Hora");
function genericFunctionHora(id, result){
	var rootElement = result.documentElement;
	var resultado = rootElement.attributes[0].value.split(":");			
	currentDate.setHours(resultado[0]);
	currentDate.setMinutes(resultado[1]);
}

//Buscamos info sobre la emisión
loadXMLDoc("http://www.3xl.cat/xml_dinamic/endirecte.xml?dummy="+Math.random(),false,"Directe1");
function genericFunctionDirecte1(id, result){
	var rootElement = result.documentElement;
	var horaIni = rootElement.attributes[0].value.split(":");
	var horaFin = rootElement.attributes[1].value.split(":");
	dateIni.setHours(horaIni[0]);
	dateIni.setMinutes(horaIni[1]);
	dateFin.setHours(horaFin[0]);
	dateFin.setMinutes(horaFin[1]);		

	//alert("CURRENT:" + currentDate + "INI:" + dateIni + "FIN:" + dateFin);
	if( (currentDate >= dateIni) && (currentDate <= dateFin)){
		//alert("Setamos intervalo!!!");
		intervalDirecte = setInterval("checkDirecte()", intervalTime);
	}
	
}

//Cada intervalTime...		
function checkDirecte(){
	loadXMLDoc("http://www.3xl.cat/xml_dinamic/endirecte.xml?dummy="+Math.random(),false,"Directe2");			
}
function genericFunctionDirecte2(id, result){
		
	var rootElement = result.documentElement;
	var endirecte = rootElement.getElementsByTagName("endirecte")[0].firstChild.data;

	if(currentEndirecte=="NO" && endirecte =="SI"){
		//if(confirm("Comença l'emissió en directe")){
		if(document.getElementById("videoDiferit")!=undefined){
			videoDiferitStr = document.getElementById("videoDiferit").innerHTML;
			document.getElementById("videoDiferit").innerHTML="";
		}
		insertVideoDirecte();
		currentEndirecte="SI";
	}
	else{
		if(currentEndirecte=="SI" && endirecte =="NO"){
			//alert("Ha acabat l'emissió en directe");
			clearInterval(intervalDirecte);
			if(document.getElementById("videoBlock")!=undefined){
				document.getElementById("videoBlock").innerHTML="";
			}
			if(document.getElementById("videoDiferit")!=undefined){
				document.getElementById("videoDiferit").innerHTML = videoDiferitStr;
			}
			currentEndirecte="NO";
		}			
	}
}


function enviaComentariOnline(form){
	var queryString = "";
	var numberElements = form.elements.length;
	for(var i = 0; i <numberElements; i++){
		queryString += form.elements[i].name+"="+escape(form.elements[i].value);
		if(i<numberElements-1){
			queryString+="&";
		}
	}
	form.COMMENT_TEXT.value="";
	loadXMLDoc("http://www.3xl.cat/su/Register",false,"Dummy",queryString);
}

function genericFunctionDummy(id, result){
	return;
}

// CALL WRITE FUNCTION DEPENDING ON DETECTION
//La detecció es fa en util.js
function insertVideoDirecte(){
	writeVideoDirecte(VideoModuleCompactVersion);
}  
// WRITE VIDEO FUNCTION
function writeVideoDirecte(compact){
	var css = (compact)?"video_player_directeNN":"video_player_directe";
	var w = (compact)?320:380;
	var h = (compact)?285:330;
	// WRITE MODULE
	var moduleStr = '<div class="'+css+'">'+
	'<table width="996" border="0" cellpadding="0" cellspacing="0"><tr><td>'+
	'<!-- VIDEO -->'+
	'<div class="video_obj">'+
	'<div style="height="'+h+'";" id="innerVideo">vìdeo directe 3xl.cat'+
	'</div></div>'+
	'</td><td>'+
	'<!-- FORM PARTICIPA -->'+
	'<div class="participa">'+
	'<h2>En directe <img src="/img/videodir_logotip_3ac.gif" width="24" height="24" /></h2>'+
	'<h3>Participa!</h3>';
	if(code==0){
		moduleStr+='Envia un missatge en directe al programa';
	}
	else{
		moduleStr+='Per afegir comentaris, escriu el teu nom d\'usuari i contrasenya a la part superior de la pàgina ';
	}
	moduleStr+='<p><form name="conline" method="post">'+	
	'<input name="hiServiceId"        type="hidden" value="3XL_CS"/>'+
	'<input name="hiGroupServiceId"   type="hidden" value="3XL_CS_GRAL"/>'+
	'<input name="hiPortal"           type="hidden" value="3xl"/>'+
	'<input name="hiAddUsridintField" type="hidden" value="USER_ID" />'+
	'<input name="hiAddUsrField"      type="hidden" value="ALIAS_ID" />'+
	'<input name="hiAction"           type="hidden" value="217"/>'+
	'<input name="COMMENT_PORTAL"     type="hidden" value="www.3xl.net" />'+
	'<input name="COMMENT_CONTENT_ID" type="hidden" value="'+dateStr+'" id="COMMENT_CONTENT_ID" />'+
	'<input name="COMMENT_TYPE"       type="hidden" value="TXL_HOME" />'+
	'<input name="COMMENT_URL"        type="hidden" value="'+escape(document.location)+'" id="COMMENT_URL" />'+
	'<input name="hiTarget"           type="hidden" value="*.swf"/>'+
	'<input name="hiRegServiceId"     type="hidden" value="3XL_REG"/>'+
	'<label for="msg"><textarea cols="25" rows="4" wrap="virtual" id="msg" class="tipomsg" name="COMMENT_TEXT"></textarea></label><br />';
	if(code==0){
		moduleStr+='<img src="/img/bot_envia.gif" alt="envia" class="btn_envia" onClick="enviaComentariOnline(conline)" />';
	}
	moduleStr+='</form></p></div>'+
	'</td><td class="comments">'+
	'<!-- MISSATGES -->'+
	'<div id="innerMsg"><iframe marginheight="0" marginwidth="0" frameborder="0" id="com" name="com" scrolling="no" allowtransparency="true" src="/comments_refresh_tech.htm">'+
	// AQUI SE INSERTARÁN LOS MENSAJES
	'</iframe></div>'+
	'</td></tr></table>'+
	'</div><br/>'
	// PINTAMOS EL MODULO
	document.getElementById("videoBlock").innerHTML = moduleStr;
	// VIDEO ACTIVE-X ELEM.
	var videoStr;
	if(mac){
	videoStr = '<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+w+'" height="'+h+'" >'+
	'<param name="src" value="http://www.3xl.cat/su/3xl/xlConditionalAccess.jsp?ID=K3D" >'+
	'<embed  src="http://www.3xl.cat/su/3xl/xlConditionalAccess.jsp?ID=K3D" width="'+w+'" height="'+h+'" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"  />  '+
	'</object><br />'+
	'<div style="background-color:#FFFFFF;"><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=915D874D-D747-4180-A400-5F06B1B5E559&displaylang=en" title="Descarrega\'t el plugin de windows media para mac">Si no pot visualitzar el video, descarrega\'t el plugin de windows media per Mac</a></div>';
	}else{
	videoStr = '<object type="video/x-ms-wmv" data="http://www.3xl.cat/su/3xl/xlConditionalAccess.jsp?ID=K3D" width="'+w+'" height="'+h+'">'+
	'  <param name="src" value="http://www.3xl.cat/su/3xl/xlConditionalAccess.jsp?ID=K3D" />'+
	'  <param name="autostart" value="true" />'+
	'  <param name="controller" value="true" />'+
	'  <param name="AutoSize" value="0">'+
	'</object>';
	}
	// SMALL IE BUG FIXED, WE REPLACE ID WITH ACTIVEX
	document.getElementById("innerVideo").innerHTML = videoStr;
	// INIT CALL
	//UpdateMessages();
}
// UPDATER FUNCTIONS
var msgTimeOut;
var timeOut = 3000; // en ms
// UPDATE FUNCTIONS
function UpdateMessages(){
	var msgStr = "";
}
// STOP UPDATING FUNCTION
function clearUpdateMessages(){
	clearTimeout(msgTimeOut);
}
