//***************************************************************
//### INI VOTEBAR ### //TODO: extend to tv3.cat?
var tStars = 5;
function updateVistaVotos(value,time,uniqueId,tipoId,contentId,isHome){
	var result;
	var roundResult;
	var openLink = '';
	var closeLink = '';
	var fullStar;
	var halfStar;
	var emptyStar;
	var tipoStar = (isHome)?"_verd":"";
	//alert(isHome+" ; "+tipoStar);
	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+'\',\''+isHome+'\');">';
	if(time==1){
		fullStar = '<img src="/img/ico_star'+tipoStar+'.gif" alt="" border="0" onmouseover="this.src=\'/img/ico_star_on.gif\'" onmouseout="this.src=\'/img/ico_star'+tipoStar+'.gif\'" />';
		halfStar = '<img src="/img/ico_star_half'+tipoStar+'.gif" alt="" border="0" onmouseover="this.src=\'/img/ico_star_on.gif\'" onmouseout="this.src=\'/img/ico_star_half'+tipoStar+'.gif\'" />';
		emptyStar = '<img src="/img/ico_star_off.gif" alt="" border="0" onmouseover="this.src=\'/img/ico_star_on.gif\'" onmouseout="this.src=\'/img/ico_star_off.gif\'" />';
	}
	else{
		fullStar = '<img src="/img/ico_star'+tipoStar+'.gif" alt="" border="0" />';
		halfStar = '<img src="/img/ico_star_half'+tipoStar+'.gif" alt="" border="0" />';
		emptyStar = '<img src="/img/ico_star_off.gif" alt="" border="0" />';
	}
	if(isHome){
		var str = '<div class="ranking_home">';
	}else{
		var str = '<div class="ranking">';
	}
	if(time==1 && (!isHome || isHome == undefined)) { str+='<span>Vota\'m</span>'; }
	if(isHome){
		var endStr = '</div>';
	}else{
		//var endStr = '<span class="numvots">('+tVots+')</span></div>';
		var endStr='';
		}	
	// 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,isHome){
	if(eval(isHome)){
		var loader = '<div class="ranking_loader_home"><span>Enviant</span></div>';
	}else{
		var loader = '<div class="ranking_loader"><span>Enviant</span></div>';	
	}
	document.getElementById("stars"+uniqueId).innerHTML = loader;
}

function updateVotos(value,time,uniqueId,tipoId,contentId,isHome){	
	if(time==1){
		updateVistaVotos(value,time,uniqueId,tipoId,contentId,isHome);
	}

	if(time==2){ //Lanzamos la peticion a SU
		puntsSeleccionats = value;
		startLoader(uniqueId,isHome);
		//ajaxSite_DoHit();
		if(tipoId=="WMU_FRAGMENT" || tipoId=="WMU_ALBUM"){ //Votacions sobre participacions (TODO saber que llega en este param)
			//var toLoad = 'loadXMLDoc("/su/Register?num_punt='+value+'&participationId='+contentId+'&tipoId=WMU_FRAGMENT&hiTarget=*.swf&hiAction=220&hiRegServiceId=CR_REG&hiServiceId=CM_VOTE1_'+contentId+'&hiPortal=tags&num_vot=1&domainId=WMU&language=ca&hiGroupServiceId=3XL_VOTE&hiRetrievalXsl=login.xsl",false,\''+uniqueId+'\');';
			
			var toLoad = 'loadXMLDoc("/su/Register?num_punt='+value+'&contentId='+contentId+'&tipoId='+tipoId+'&hiTarget=*.swf&hiAction=220&hiRegServiceId=CR_REG&hiServiceId=CM_VOTE__'+contentId+'&hiPortal=cm&num_vot=1&domainId=WMU&language=ca&hiGroupServiceId=3XL_VOTE&hiRetrievalXsl=login.xsl",false,\''+uniqueId+'\');';
			
			
			
			//num_punt=1&contentId=10212&tipoId=WMU_FRAGMENT&hiTarget=*.swf&hiAction=220&hiRegServiceId=CR_REG&hiServiceId=CM_VOTE20&hiPortal=tags&num_vot=1&domainId=TAG&language=ca&hiGroupServiceId=3X_VOTE&hiRetrievalXsl=login.xsl
			//var toLoad ='loadXMLDoc("num_punt=1&contentId='+idint+'&tipoId='+tipologia+'&hiTarget=*.swf&hiAction=220&hiRegServiceId=SP3_REG&hiServiceId=3XL_VOTETAG_LIST_ELEMENT10&hiPortal=tags&num_vot=1&domainId=TAG&language=ca&hiGroupServiceId=3XL_VOTE&hiRetrievalXsl=login.xsl",false,\''+uniqueId+'\');';
	}else{ //Votacions sobre tipologies
			//var toLoad = 'loadXMLDoc("/su/Register?num_punt='+value+'&contentId='+contentId+'&tipoId='+tipoId+'&hiTarget=*.swf&hiAction=220&hiRegServiceId=TVC_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 ###

function PromptConcurs(idConcurs,idPregunta)
{
if(!parent.alreadyvote){parent.newModalBox('/concurs.htm?idConcurs='+idConcurs+'&idPregunta='+idPregunta,undefined);}
}
