var mosaic=false;

var timerBackground;
var timerOpacity;
var alpha=1;
var index=0;
var backgroundChangeTime=6000;

var pagina=1;
var fpp;
var rows;
var cols;

var maxWidthMenu=140;
var maxHeightServices=470;
var clipBottom=0;
var transp=0.0;
var timer;

function changeBackground(id,id2){
	document.getElementById(id).style.backgroundImage="url(fotos/"+imatgesFons[index]+")";
	nextIndex=index+1;
	if(nextIndex>=imatgesFons.length) nextIndex=0;
	for(i=1;i<imatgesFons.length;i++) document.getElementById(id2).style.backgroundImage="url(fotos/"+imatgesFons[i]+")";
  document.getElementById(id2).style.backgroundImage="url(fotos/"+imatgesFons[nextIndex]+")";
	
	if(!mosaic && imatgesFons.length>1) setTimeout("swapBackground('"+id+"','"+id2+"')",backgroundChangeTime);
	else clearTimeout(timerBackground);
}

function swapBackground(id,id2){
	if(!mosaic){
		clearTimeout(timerBackground);
		clearTimeout(timerOpacity);
		
		changeOpacity(id,id2);

		nextIndex=index+1;
		if(nextIndex>=imatgesFons.length) nextIndex=0;
		document.getElementById(id2).style.backgroundImage="url(fotos/"+imatgesFons[nextIndex]+")";
		
		index++;
		if(index>=imatgesFons.length) index=0;

		aux=id;
		id=id2;
		id2=aux;
			
		timerBackground=setTimeout("swapBackground('"+id+"','"+id2+"')",backgroundChangeTime);
	}
	else clearTimeout(timerBackground);
}

function changeOpacity(id,id2){
	alpha=parseFloat(alpha-0.1);
	document.getElementById(id).style.opacity=alpha;
	document.getElementById(id).style.filter="alpha(opacity="+alpha*100+")";
	document.getElementById(id2).style.opacity=1-alpha;
	document.getElementById(id2).style.filter="alpha(opacity="+(1-alpha)*100+")";
	
	if(alpha<=0){
		clearTimeout(timerOpacity);
		document.getElementById(id).style.opacity=0.0;
		document.getElementById(id).style.filter="alpha(opacity=0)";
		document.getElementById(id2).style.opacity=1.0;
		document.getElementById(id2).style.filter="alpha(opacity=100)";
		alpha=1;
	}
	else timerOpacity=setTimeout("changeOpacity('"+id+"','"+id2+"')",100);
}

function activarMosaic(p_fpp,p_rows){
	fpp=p_fpp;
	rows=p_rows;
	cols=fpp/rows;
	
	mosaic=true;
	
	clearTimeout(timerBackground);
	clearTimeout(timerOpacity);
	timerBackground=null;
	timerOpacity=null;
	index=0;

	if(document.getElementById('background1').style.opacity>document.getElementById('background2').style.opacity)
		changeOpacity('background1','div_mosaic');
	else changeOpacity('background2','div_mosaic');

	document.getElementById('background1').style.visibility="hidden";
	document.getElementById('background2').style.visibility="hidden";
	document.getElementById('div_descriptions').style.visibility="hidden";
	
	document.getElementById('div_mosaic').style.visibility="visible";

	if(imatgesFons.length>pagina*fpp) document.getElementById('fletxa_dre').style.visibility="visible";
}

function desactivarMosaic(){
	document.getElementById('div_mosaic').style.opacity=0.0;
	document.getElementById('div_mosaic').style.filter="alpha(opacity=0)";
	document.getElementById('div_mosaic').style.visibility="hidden";

	document.getElementById('background1').style.visibility="visible";

	document.getElementById('background1').style.opacity=1.0;
	document.getElementById('background1').style.filter="alpha(opacity=100)";
	document.getElementById('background2').style.opacity=0.0;
	document.getElementById('background2').style.filter="alpha(opacity=0)";

	document.getElementById('background2').style.visibility="visible";
	document.getElementById('div_descriptions').style.visibility="visible";
	
	mosaic=false;
	
	changeBackground('background1','background2');
}

function canviarPaginaMosaic(inc,fpp,rows){
	cols=fpp/rows;
		
	pagina=pagina+inc;
	
	indexMosaic=(pagina-1)*fpp;
	for(i=0;i<fpp;i++){
		if(indexMosaic<imatgesFons.length) document.getElementById('imgMosaic'+i).src= "includes/thumbnail.php?file=../fotos/"+imatgesFons[indexMosaic]+"&w=170";
		else document.getElementById('imgMosaic'+i).src="img/transp.gif";
		indexMosaic++;
	}
	
	if(pagina>1) document.getElementById('fletxa_esq').style.visibility="visible";
	else document.getElementById('fletxa_esq').style.visibility="hidden";
	
	if((pagina*fpp)<imatgesFons.length) document.getElementById('fletxa_dre').style.visibility="visible";
	else document.getElementById('fletxa_dre').style.visibility="hidden";
}

function openMenu(id,id2){
  document.getElementById(id2).style.clip="rect(0px,0px,0px,0px)";
  size=parseInt(document.getElementById(id).style.width);
	inc=parseFloat(((maxWidthMenu+10-size)/10));
  if(size>maxWidthMenu || inc<1){
    clearTimeout(timer);
    showServices(id2);
  } 
  else{
    document.getElementById(id).style.width=size+inc+"px";
    timer=setTimeout("openMenu('"+id+"','"+id2+"')",40);
  }
}

function showServices(id){
  clipBottom=clipBottom+((maxHeightServices+10-clipBottom)/60);
  if(clipBottom>=maxHeightServices) clearTimeout(timer);
  else{
    document.getElementById(id).style.clip="rect(0px,"+maxWidthMenu+"px,"+clipBottom+"px,0px)";
		document.getElementById(id).style.opacity=parseFloat(clipBottom)/parseFloat(maxHeightServices);
		document.getElementById(id).style.filter="alpha(opacity="+parseInt(parseFloat(clipBottom)/parseFloat(maxHeightServices)*100)+")";
    timer=setTimeout("showServices('"+id+"')",5);
  }
}

function showItem(id){
	document.getElementById(id).style.visibility="visible";
}

function hideItem(id){
	document.getElementById(id).style.visibility="hidden";
}
