var hiha_avisos=0
var scr_div;
var scr_width;

var scr_fun=1;
var scr_estat = 1;
var scr_reg=0;
var scr_temps=20;
var scr_pausa=2000;
var nom_div_contenidor = 'DIVSCROLLCONTENIDOR'
var nom_div_gran = 'DIVSCROLL';
var prefix_div = 'DIVREG';
var imatge_pausa = '/imgfiles/1/icona_pausa.gif';
var text_pausa = '';
var imatge_play = '/imgfiles/1/icona_play.gif';
var text_play = '';

function montar_scroller(){
	document.getElementById(nom_div_gran).style.overflow='hidden';
	document.getElementById(nom_div_gran).style.height='1.2em';
	scr_width = parseInt(document.getElementById(nom_div_gran).style.width);
	for(a=1; a<=scr_reg; a++) {
		document.getElementById(prefix_div+a).style.position='absolute';
		document.getElementById(prefix_div+a).style.left=scr_width+'px';
		//document.getElementById(prefix_div+a).style.whiteSpace='no-wrap';
	}
	document.getElementById('scr_stop').style.display='inline';
	scr_div = 1;
	setTimeout('scrollear()',scr_pausa);
}

function desmontar_scroller(){
	//scr_estat = scr_fun;
	if (scr_fun==1){
		scroll_stop();
		//scr_estat=0;
	}
	scr_estat=0;
	document.getElementById(nom_div_gran).style.overflow='visible';
	document.getElementById(nom_div_gran).style.height='auto';
	document.getElementById(nom_div_gran).style.width='auto';
	document.getElementById(nom_div_contenidor).style.width='auto';
	//scr_width = parseInt(document.getElementById(nom_div_gran).style.width);
	for(a=1; a<=scr_reg; a++) {
		document.getElementById(prefix_div+a).style.position='';
		document.getElementById(prefix_div+a).style.left='0px';
		//document.getElementById(prefix_div+a).style.whiteSpace='normal';
	}
	document.getElementById('scr_stop').style.display='none';
}

function scroll_Mov(num){
	var vleft=(parseInt(document.getElementById(prefix_div+num).style.left) -1)
	var div_width = parseInt(document.getElementById(prefix_div+num).offsetWidth);
	if (vleft<(-div_width+100)){
		vleft = scr_width;
		if (scr_div<scr_reg){
			scr_div++;
		}else{
			scr_div=1;
		}
		
	}
	document.getElementById(prefix_div+num).style.left=vleft+'px';
}

function scroll_stop(){
	if (scr_fun == 0) {
		scr_fun = 1;
		document.getElementById('scr_stop').src = imatge_pausa;
		document.getElementById('scr_stop').alt = text_pausa;
		document.getElementById('scr_stop').title = text_pausa;
		setTimeout('scrollear()',scr_temps);
	} else {
		scr_fun = 0;
		document.getElementById('scr_stop').src = imatge_play;
		document.getElementById('scr_stop').alt = text_play;
		document.getElementById('scr_stop').title = text_play;
	}
}

function scrollear(){
	if (scr_fun == 1) {
		scroll_Mov(scr_div);
		vcon=parseInt(document.getElementById(prefix_div+scr_div).style.left);
		if (vcon % scr_width == 0) {
			setTimeout('scrollear()',scr_pausa);
		} else{
			setTimeout('scrollear()',scr_temps);
		}
	}
}

$(function(){
	function ocultar_titol(){
		var filtre = "div[id*='AVIS']";
		//busquem tots els avisos que tenim per actualitzar-los
		$("div[id*='AVIS']").each(function(){
			var box = $(this);
			var bloc=$(box.parents()[0]);
			var espai =$(bloc.parents()[0]).width();
			var widthCol = bloc.width();
			var pctColumna = (widthCol / espai) * 100;
			$("#"+window.nom_div_contenidor).css('width',widthCol-30);
			$("#"+window.nom_div_gran).css('width',widthCol-55);
			divTitolAvisos = $("#box_AVIS_0 div[class^='titol_seccio']");
			divTitolAvisos.hide();
		});

		if (window.scr_estat==0){
			window.scroll_stop();
			//scr_estat=1;
		}

		window.montar_scroller();
	}

	if(window.hiha_avisos && ambMoviment=='S'){
		ocultar_titol();
	}	
});

