// JavaScript Document



function swapTabs_old(sel,tot) {

var id1, id2;
for(var i=1;i<=tot;i++){
	var id1 = 'tab'+i;
	var id2 = 'tab_cont_'+i;
	document.getElementById(id1).className = '';
	document.getElementById(id2).className = 'tab_contingut_hidden';
		if(i == sel){
		document.getElementById(id1).className = 'selected';
		document.getElementById(id2).className = 'tab_contingut';
		}
}
//ajaxSite_DoHit();

}



// FUNCIÓ SWAP STYLE

// Utilities

/*function changeStyle(id, newClass) {

identity=document.getElementById(id);

identity.className=newClass;

}*/

function swapTabs(sel,tot,uniqueId) {

var id1, id2;
for(var i=1;i<=tot;i++){
	var id1 = 'tab'+i+'_'+uniqueId;
	var id2 = 'tab_cont_'+i+'_'+uniqueId;
	document.getElementById(id1).className = '';
	document.getElementById(id2).className = 'tab_contingut_hidden';
		if(i == sel){
		document.getElementById(id1).className = 'selected';
		document.getElementById(id2).className = 'tab_contingut';
		}
}

//	ajaxSite_DoHit();

}



function swapId(count,idPre,id,className) {
for(var i=1;i<=count;i++){
	var idNow = idPre+i;
	document.getElementById(idNow).className = 'nodisp';		
		if(i == id){
		document.getElementById(idNow).className = className;
		}
}

}



function swapCss(id,className) {
document.getElementById(id).className = className;
}


var tempSize = 100;
var minFontSize = 80;
var maxFontSize = 160;

function swapFontSize(id,symbol) {
if(symbol == "++" && tempSize < maxFontSize){
	tempSize += 20;
	document.getElementById(id).style.fontSize = tempSize+"%";
}else if(symbol == "--" && tempSize > minFontSize){
	tempSize -= 20;
	document.getElementById(id).style.fontSize = tempSize+"%";
	}
	//alert(tempSize);
}


function swapIdCss(count,idPre,id,className) {
for(var i=1;i<=count;i++){
	var idNow = idPre+i;
	document.getElementById(idNow).className = 'className';		
}

}



function selectVideoDesc(count,idPre,id) {
for(var i=1;i<=count;i++){
	var idNow = idPre+i;
	document.getElementById(idNow).className = '';
		if(i == id){
			document.getElementById(idNow).className = 'selected_video';
		}
}

//	ajaxSite_DoHit();

}



function showVideoDesc(id) {	
document.getElementById(id).className = 'show_video';

}

function hideVideoDesc(id) {
if(document.getElementById(id).className != 'selected_video'){
	document.getElementById(id).className = '';
}

}


function swapMenu(count,className,id) {
for(var i=1;i<=count;i++){
	var idNow = className+i;
	var liIdNow = className+'_li_'+i;
	document.getElementById(idNow).className = 'submenu_off';
	document.getElementById(liIdNow).className = '';
		if(i == id){
		document.getElementById(idNow).className = className;
		document.getElementById(liIdNow).className = 'actiu';
		}
}

//	ajaxSite_DoHit();

}

function mostraLogin(){
document.getElementById('login').className = 'login';
document.getElementById('loginlink').className = 'actiu';
document.getElementById('preloginlink').className = 'noborder';

}

function amagaLogin(){
document.getElementById('login').className = 'hide';
document.getElementById('loginlink').className = '';
document.getElementById('preloginlink').className = '';

}


function submitform(formname) {
var formName=formname;
document.formname.submit();

}

function insertSimpleFlash(url,w,h) {
var str='';
str+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+ w +'" height="'+ h +'">';
str+='<param name="movie" value="'+url+'" />';
str+='<param name="quality" value="high" />';
str+='<param name="wmode" value="opaque" />';
str+='<param name="menu" value="false" />';
str+='<param value="exactfit" name="scale" />';
str+='<embed src="'+url+'" quality="high" wmode="opaque" scale="exactfit" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="sameDomain" width="'+ w +'" height="'+ h +'"></embed>';
str+='</object>';
document.write(str);
}

//  FUNCIONS GESTIO AUDIO GALERIA
function insertAudio(url,w,h) {
var str='';
str+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+ w +'" height="'+ h +'"  id="audioPlayer">';
str+='<param name="movie" value="'+url+'" />';
str+='<param name="quality" value="high" />';
str+='<param name="wmode" value="opaque" />';
str+='<param name="menu" value="false" />';
str+='<param value="exactfit" name="scale" />';
str+='<embed src="'+url+'" quality="high" wmode="opaque" scale="exactfit" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="sameDomain" width="'+ w +'" height="'+ h +'" name="audioPlayer"></embed>';
str+='</object>';
document.write(str);
}

var playInterval;
var galInterval;
var isPlaying = false;

function getFlashMovieObject(movieName){
  if (window.document[movieName])   {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)  {
    if (document.embeds && document.embeds[movieName]){
      alert("MOZILLA");
	  return document.embeds[movieName]; 
	}
  }  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
 
// FUNCION STOP AUDIO
function flStopAudio() {
getFlashMovieObject("audioPlayer").flStopAudio();
}

// FUNCION PLAY AUDIO
function flReproduirAudio() {
getFlashMovieObject("audioPlayer").flReproduirAudio();
clearInterval(playInterval);
}




function autoplayGaleria(forceAudioPlay){	
	galInterval = setInterval(updateGaleria,3500,'>>',true);
	//flReproduirAudio();
	if(forceAudioPlay){
		playInterval = setInterval(flReproduirAudio,500);
	}
}

function stopAutoplayGaleria(){
	clearInterval(galInterval);
}


function galeriaInicia(gal){
//show audio, hide play, show pause
isPlaying = true;
pre=gal+'_';
audio=pre+'audio';
para=pre+'para';
inicia=pre+'inicia';
document.getElementById(audio).className='audio';
document.getElementById(inicia).className='nodisp';
document.getElementById(para).className='';
document.getElementById('songName').className = 'audio_info';
autoplayGaleria(true);
//and probably some more start swf, start slideshow...
}

function galeriaPara(gal){
//hide audio, show play, hide pause
isPlaying = false;
pre=gal+'_';
audio=pre+'audio';
para=pre+'para';
inicia=pre+'inicia';
flStopAudio();
document.getElementById(audio).className='nodisp';
document.getElementById(inicia).className='';
document.getElementById(para).className='nodisp';
document.getElementById('songName').className = 'nodisp';
stopAutoplayGaleria();
}

function updateGaleria(symbol,auto){	
auto = (auto == undefined)?false:auto;
if(isPlaying && !auto){stopAutoplayGaleria(); autoplayGaleria();}
if (symbol == ">>") {
	ind++;
	if(ind >= tot){
		ind = 0;
	}
} else {
	ind--;
	if(ind < 0){
		ind = (tot-1);
	}
}
//
document.getElementById("counter").innerHTML = (ind+1)+"/"+tot;
document.getElementById("autor").innerHTML = dataArray.autor[ind];
document.getElementById("titol").innerHTML = dataArray.titol[ind];
document.getElementById("texto").innerHTML = dataArray.texto[ind];
document.getElementById("foto").src = dataArray.foto[ind];
}

// FUNCTION MODULO MULTI CONTENT

function updateMulti(symbol){	
if (symbol == ">>") {
	ind++;
	if(ind >= tot){
		ind = 0;
	}
} else {
	ind--;
	if(ind < 0){
		ind = (tot-1);
	}
}
// PINTAMOS LOS NUEVOS DATOS
document.getElementById("counter").innerHTML = (ind+1)+"/"+tot;
document.getElementById("atitol").innerHTML = dataArray.atitol[ind];
document.getElementById("titol").innerHTML = dataArray.titol[ind];
document.getElementById("foto").src = dataArray.foto[ind];
document.getElementById("fecha").innerHTML = dataArray.fecha[ind];
document.getElementById("duracio").innerHTML = dataArray.duracio[ind];
}

// FUNCIONS PER GESTIONAR MILLOR EL OBJECTE SVP. HEM DE PODER-HO PARAMETRITZAR ON-THE-FLY
//  NOTE: el index comença amb el numero 1.
function insertHomeSVPObject(placeHolderId,dataObj,index){
			
			index = index-1;
			
			var ConfigData = new Object()
				ConfigData.VIDEO_ID =		dataObj.videoID[index];                                
				ConfigData.POSTER_URL =	dataObj.posterURL[index];				
				ConfigData.WIDTH =	320;
				ConfigData.HEIGHT =		240;
				ConfigData.USE_LINK_TOCONTEXT = "true";

				
			
			var FlashParams = new Object()
				
				FlashParams.bgcolor = 					"#1B597A";
				FlashParams.width  =					320;
				FlashParams.height =					277;
				FlashParams.quality =					"high";
				FlashParams.swLiveConnect =				"true";
				FlashParams.menu =						"true";
				FlashParams.scale =						"noscale";
				FlashParams.salign =					"tl";
				FlashParams.align =						"l";
				FlashParams.allowFullScreen =			"true";

								
			insertaSVP( placeHolderId, ConfigData , FlashParams );
			
			// ACTUALITZEM LES DADES DEL ITEM CORRESPONDENT
			
			document.getElementById("currentAtitol").innerHTML = dataObj.atitol[index];
			document.getElementById("currentTitol").innerHTML = "<a href=\"/videos/"+dataObj.videoID[index]+"\">"+dataObj.titol[index]+"</a>";
			//document.getElementById("currentTitol").innerHTML = dataObj.titol[index];
			document.getElementById("currentDesc").innerHTML = dataObj.desc[index];		

		}

function manageFormContent(form,focused,val){	
	if(focused){
		if(form.value == val){
			form.value = "";
		}
	}else{
		if(form.value == ""){
			form.value = val;
		}
	}
}

function popup(url,wname,width,height){
openIt=window.open(url,wname,'scrollbars=yes,resizable=yes,width='+width+',height='+height);
}


function buidaEnviaForm(){
	document.getElementById("correu_amic_nom").value = "";
	document.getElementById("correu_amic").value = "";	
	document.getElementById("correu_de_nom").value = "";	
	document.getElementById("correu_de").value = "";
	document.getElementById("missatge").value = "";	
}



//***************************************************************
//### INI VOTEBAR ### //TODO: extend to tv3.cat?
var tStars = 5;
function updateVistaVotos(value,time,uniqueId,tipoId,contentId){
	var result;
	var roundResult;
	var openLink = '';
	var closeLink = '';
	var fullStar;
	var halfStar;
	var emptyStar;
	var puntsSeleccionats = value;
	punts = eval("punts"+uniqueId);
	tVots = eval("tVots"+uniqueId);
	if(value == undefined || value == 0){
		value = 0;
	}
	if(time==2){
		tVots += 1;
	}
	var i,j;
	var counter = 1;
	punts += value;
	if(tVots == 0){
		result = 0;
		roundResult = 0;
	}else{
		var maxPunt = tVots*tStars;
		result = (punts*tStars)/maxPunt;
		roundResult = Math.floor((punts*tStars)/maxPunt);
	}
	var dif = Math.abs(result-roundResult);
	var empty = tStars-roundResult;
	if(dif > 0.5){
		roundResult++;
		empty--;
		dif = 0;
	}else if(dif <= 0.5 && dif != 0){
	dif = 0.5;
	}
	var openLink = '<a href="javascript:updateVotos(';
	var closeLink = ',2,\''+uniqueId+'\',\''+tipoId+'\',\''+contentId+'\');">';
	if(time==1){
		fullStar = '<img src="/img/ico_estel.gif" alt="" width="10" height="9" border="0" onmouseover="this.src=\'/img/ico_estel_on.gif\'" onmouseout="this.src=\'/img/ico_estel.gif\'" />';
		halfStar = '<img src="/img/ico_estel_half.gif" alt="" width="10" height="9" border="0" onmouseover="this.src=\'/img/ico_estel_on.gif\'" onmouseout="this.src=\'/img/ico_estel_half.gif\'" />';
		emptyStar = '<img src="/img/ico_estel_off.gif" alt="" width="10" height="9" border="0" onmouseover="this.src=\'/img/ico_estel_on.gif\'" onmouseout="this.src=\'/img/ico_estel_off.gif\'" />';
	}
	else{
		fullStar = '<img src="/img/ico_estel.gif" alt="" width="10" height="9" border="0" />';
		halfStar = '<img src="/img/ico_estel_half.gif" alt="" width="10" height="9" border="0" />';
		emptyStar = '<img src="/img/ico_estel_off.gif" alt="" width="10" height="9" border="0" />';
	}
	var str = '<div class="ranking">';
	if(time==1) str+='<span>Vota\'m</span>';
	var endStr = '&nbsp;&nbsp;'+tVots+' vots</div>';//+punts+' punts,  '+result+'</div>';
	// PINTO LAS ESTRELLAS FULL
	for(i=0;i<roundResult;i++){
		if(time==1) str+=(openLink+counter+closeLink);
		str+=fullStar;
		if(time==1) str+='</a>'
		counter++;
	}
	// PINTO LAS ESTRELLAS HALF SI EXISTEN
	if((dif >0 && roundResult<tStars) || (result > 0 && roundResult == 0)){
		if(time==1) str+=(openLink+counter+closeLink);
		str+=halfStar;
		if(time==1) str+='</a>'			
		counter++;
		empty--;
	}
	// ACABO RELLENANDO LAS QUE FALTAN CON ESTRELLAS VACIAS
	for(j=0;j<empty;j++){
		if(time==1) str+=(openLink+counter+closeLink);
		str+=emptyStar;
		if(time==1) str+='</a>'			
		counter++;
	}
	// CIERRO EL DIV
	str+=endStr;
	// PINTO
	document.getElementById("stars"+uniqueId).innerHTML = str;
}

function startLoader(uniqueId){
	var loader = '<div class="ranking_loader"><img src="/img/enviant.gif" alt="" width="21" height="9" />Enviant</div>';
	document.getElementById("stars"+uniqueId).innerHTML = loader;
}

function updateVotos(value,time,uniqueId,tipoId,contentId){	
	if(time==1){
		updateVistaVotos(value,time,uniqueId,tipoId,contentId);
	}

	if(time==2){ //Lanzamos la peticion a SU
		puntsSeleccionats = value;
		startLoader(uniqueId);
		//ajaxSite_DoHit();
		if(tipoId=="DTY_PARTICIPACIO"){ //Votacions sobre participacions (TODO saber que llega en este param)
			var toLoad = 'loadXMLDoc("/su/Register?numPunt='+value+'&participationId='+contentId+'&tipoId=DTY_PARTICIPACIO&hiTarget=*.swf&hiAction=206&hiRegServiceId=CCRTVI_CONT&hiServiceId=3XL_REG&hiPortal=3xl&numVot=1&localeParticipation=ca",false,\''+uniqueId+'\');';
		}else{ //Votacions sobre tipologies
			var toLoad = 'loadXMLDoc("/su/Register?num_punt='+value+'&contentId='+contentId+'&tipoId='+tipoId+'&hiTarget=*.swf&hiAction=220&hiRegServiceId=3XL_REG&hiServiceId=3XL_VOTE'+tipoId+contentId+'&hiPortal=3xl&num_vot=1&domainId=TXL&language=ca&hiGroupServiceId=3XL_VOTE",false,\''+uniqueId+'\');';
		}
		setTimeout(toLoad,1000);
	}
}
//### FIN VOTEBAR ###
