// JavaScript Document

	var possibles = new Array();
	possibles[0]='css/saf.css';
	possibles[1]='css/saf_aa.css';
	possibles[2]='css/saf_aaa.css';
	

	/*aqui venen els parmetres per customitzar la rotacio*/
	
	var gblPhotoShufflerDivId = "presentacioUab2";
	var gblPhotoShufflerImgId = "imgPortada"; 
	var gblPauseSeconds = 3;
	var gblFadeSeconds = .85;
	var gblRotations = 1000;
	var gblUpperDiv = "presentacioUab";

	// End Customization section
	
	var gblDeckSize = arrayImgs.length;
	var gblOpacity = 100;
	
	try {
		if(ban==0) var gblOnDeck = 0;
		else var gblOnDeck = ban-1;
	}
	catch(e)
	{
		var gblOnDeck = 0;
	}
	var gblStartImg;
	var gblImageRotations = gblDeckSize * (gblRotations+1);
	

	// FUNCIONS
	
	function init()
	{
		var hz=window.screen.height;
		var wz=window.screen.width;
		var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
		var divport=document.getElementById("presentacioUab");
		
		if(ie7)
		{
			if(wz==800){
				document.getElementById("presentacioUab").style.left="25px";
			}
			else if(wz==1024){
				document.getElementById("presentacioUab").style.left="54px";
			}
			else if(wz==1152){
				document.getElementById('presentacioUab').style.left="118px";	
			}
			else {
				document.getElementById("presentacioUab").style.left="182px";
			}
		}
		
		if (document.cookie.indexOf("lettersize") != -1)
		{
			var cookieStart = document.cookie.indexOf("lettersize");
			// get the beginning index of the cookie value by looking for the equal sign after the name
			var cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
			// get the end index of the cookie value by looking for the semi-colon after the value
			var cookieValEnd = document.cookie.indexOf(";", cookieStart);
			// if no semi-colon, then use the whole length
			if (cookieValEnd == -1)
			{
				cookieValEnd = document.cookie.length;
			}
			// use substring to get the text between the two indices and that is the value of the cookie
			var cookieValue = document.cookie.substring(cookieValStart,	cookieValEnd);
			canviaCSS(cookieValue);
		}
		
		photoShufflerLaunch();
	}
		
	
	function pause(milisec)
	{
		var d = new Date();
		var begin = d.getTime();
		
		while ((d.getTime() - begin ) > milisec){
		// nothing...
		}
	}
	

	
	function canviaCSS(quina)
	{
		var l = document.getElementById('styleId');
		if(l) 
		{			
			l.href=possibles[quina];
		}
		document.cookie="lettersize="+quina+";expires=";
		return false;
	}
	
	function photoShufflerLaunch()
	{
		var theimg = document.getElementById(gblPhotoShufflerImgId);
		gblStartImg = theimg.src; // save away to show as final image	

		document.getElementById(gblPhotoShufflerDivId).style.backgroundImage='url(' + arrayImgs[gblOnDeck] + ')';
		setTimeout("photoShufflerFade()",gblPauseSeconds*1);
	}

	function photoShufflerFade()
	{
		var theimg = document.getElementById(gblPhotoShufflerImgId);
		// determine delta based on number of fade seconds
		// the slower the fade the more increments needed
			var fadeDelta = 100 / (30 * gblFadeSeconds);

		// fade top out to reveal bottom image
		if (gblOpacity < 2*fadeDelta ) 
		{
		  gblOpacity = 100;
		  // stop the rotation if we're done
		  if (gblImageRotations < 1) return;
		  photoShufflerShuffle();
		  // pause before next fade
			  setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
		}
		else
		{
		  var theUpperDiv = document.getElementById(gblUpperDiv);
		  gblOpacity -= fadeDelta;
		  setOpacity(theimg,gblOpacity);
		  setOpacity(theUpperDiv,100);
		  setTimeout("photoShufflerFade()",30);  // 1/30th of a second
		}
	}
	
	function photoShufflerShuffle()
	{
		var thediv = document.getElementById(gblPhotoShufflerDivId);
		var theimg = document.getElementById(gblPhotoShufflerImgId);
		var theUpperDiv = document.getElementById(gblUpperDiv);
		
		document.getElementById('linkpresentacioUab2').href=arrayLinks[gblOnDeck];
		
		// copy div background-image to img.src
		theimg.src = arrayImgs[gblOnDeck];
		// set img opacity to 100
		setOpacity(theimg,100);
		setOpacity(theUpperDiv,100);
		
		// shuffle the deck
		gblOnDeck = ++gblOnDeck % gblDeckSize;
		// decrement rotation counter
		if (--gblImageRotations < 1)
		{
		  // insert start/final image if we're done
		  arrayImgs[gblOnDeck] = gblStartImg;
		}
		

		// slide next image underneath
		thediv.style.backgroundImage='url(' + arrayImgs[gblOnDeck] + ')';
	}

	function setOpacity(obj, opacity) 
	{
		opacity = (opacity == 100)?99.999:opacity;

		// IE/Win
		obj.style.filter = "alpha(opacity:"+opacity+")";

		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;

		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;

		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	}
	
	// Secció
	function section(id)
	{
		var sid = document.getElementById('session_id').value;
		location.href = "?id="+id+"&sid="+sid+"&ban="+gblOnDeck;
	}
	
	function section_new_window(id)
	{
		var sid = document.getElementById('session_id').value;
		window.open("?id="+id+"&sid="+sid+"&ban="+gblOnDeck);
	}
								
	
	// Secció nova finestra
	function section_newwin(id)
	{
		var top = window.screen.height/2-(800/2);
		var left = window.screen.width/2-(1024/2);	
		var sid = document.getElementById('session_id').value;
		window.open('./?id='+id+'&sid='+sid,'','top='+top+',left='+left+',width=1024,height=800,menubar=no,scrollbars,status=no,titlebar=no,toolbar=no')
	}
	
	// Informació ampliada de la notícia
	function info_noticia(id,noticia)
	{
		var sid = document.getElementById('session_id').value;
		location.href = "?opt=info_noticia&id="+id+"&noticia="+noticia+"&sid="+sid+"&ban="+gblOnDeck;
	}
	
	// Informació ampliada de l'activitat
	function info_agenda(id,activitat)
	{
		var sid = document.getElementById('session_id').value;
		location.href = "?opt=info_agenda&id="+id+"&activitat="+activitat+"&sid="+sid+"&ban="+gblOnDeck;
	}
	
	// Filtre categoria arxiu de notícies
	function filtre_noticies()
	{
		document.form1.categoria.value = document.getElementById('categoria_noticies').value; 
		document.form1.ban.value = gblOnDeck;
		document.form1.submit();
	}
	
	
	// Per passar de pàgina mantenint els paràmetres de cerca
	function next_page(page)
	{
		document.form1.pagina.value = page;
		validarCerca();
		document.form1.submit();
	}

	// Valida la cerda de l'arxiu de notícies
	function validarCerca()
	{
		document.form1.ban.value = gblOnDeck;
		if(document.form1.mes_inici.value!=-1 && document.form1.any_inici.value==-1)
		{
			alert('Si us plau selecciona l\'any d\'inici');
			return false;
		}
		else if(document.form1.mes_inici.value==-1 && document.form1.any_inici.value!=-1)
		{
			alert('Si us plau seleciona el mes d\'inici');
			return false;
		}
		else if(document.form1.mes_fi.value!=-1 && document.form1.any_fi.value==-1)
		{
			alert('Si us plau selecciona l\'any de fi');
			return false;
		}
		else if(document.form1.mes_fi.value==-1 && document.form1.any_fi.value!=-1)
		{
			alert('Si us plau seleciona el mes de fi');
			return false;
		}
		else if(document.form1.any_fi.value!=-1  && document.form1.any_inici.value!=-1
		&& (eval(document.form1.any_fi.value)<eval(document.form1.any_inici.value)
		||(eval(document.form1.any_fi.value)==eval(document.form1.any_inici.value)
		&& eval(document.form1.mes_fi.value)<eval(document.form1.mes_inici.value))))
		{
			alert('La data d\'inici ha d\'anar abans que la de fi');
			return false;
		}
		else return true;
	}
	
	//Imprimir notícia
	function imprimir(id,tipus,opt)
	{
		var top = window.screen.height/2-(800/2);
		var left = window.screen.width/2-(1024/2);	
		var sid = document.getElementById('session_id').value;
		window.open('./?'+tipus+'='+id+'&opt='+opt+'&sid='+sid,'','top='+top+',left='+left+',width=1024,height=800,menubar=no,scrollbars,status=no,titlebar=no,toolbar=no')	
	}
	
	//Resposta oculta
	function resposta(id)
	{
		try
		{
			for(var i=1;i>0;i++)
			{
				if(i==id) 
				{
					if(document.getElementById('resp'+i).style.display == "block") document.getElementById('resp'+i).style.display = "none";
					else document.getElementById('resp'+i).style.display = "block";
				}
				else document.getElementById('resp'+i).style.display = "none";
			}
		}
		catch(e){}
	}
		
		
	
	