// JavaScript Document
	
	function AlbumFotosHolder(){
		this.frm_element=null;
		this.img_element=null;
		this.nom_element="";
	}
	
	function AlbumFotos(){
		//alert(navigator.userAgent);
		
		this.id_album='-1';
		this.album_element=null;
		this.n_fotos=0;
		this.n_fotos_pagina=0;
		
		this.pagina_holders=null;

		this.fotos=null;
		this.pagines=null;
		this.pagina_act=null;
		this.foto_act=null;
		
		this.preloadok=false;
		
		this.createAlbum=function createAlbum(id_album,n_fotos_pagina){
		
			this.id_album=id_album;
			try{
				this.album_element=document.getElementById(id_album);
			}catch(e){
				alert("Album de fotos no trobat");
			}
			
			this.fotos=new Array();
			this.pagines=new Array();
			this.pagines[0]=new AlbumFotosPage();
			this.pagina_act=this.pagines[0];	
			this.pagina_act.index=0;
			this.pagina_act.album_pare=this;
			
			this.n_fotos_pagina=n_fotos_pagina;
			this.pagina_holders=new Array(n_fotos_pagina);
			for(i=0;i<n_fotos_pagina;i++){
				try{
					this.pagina_holders[i]=new AlbumFotosHolder();
					this.pagina_holders[i].frm_element=document.getElementById(id_album+"_thumb"+i+"_frame");
					this.pagina_holders[i].img_element=document.getElementById(id_album+"_thumb"+i);
					this.pagina_holders[i].nom_element=document.getElementById(id_album+"_thumb"+i+"_nom");
					this.pagina_holders[i].img_element.onload=function (){ album_fotos.centraTotesFotos(); }
				}catch(e){
					alert("holder "+i+" no trobat");
				}
			}
			
			document.getElementById(id_album+"_foto").onload=function (){ album_fotos.centraTotesFotos(); }
		
		}
		
			
		this.addFoto=function addFoto(nom,nom_thumb,src,src_thumb){
		
			
			if(this.pagines[this.pagines.length-1].fotos.length>=this.n_fotos_pagina){
				this.pagina_act=new AlbumFotosPage();
				this.pagina_act.index=this.pagines.length;
				this.pagina_act.album_pare=this;
				this.pagines[this.pagina_act.index]=this.pagina_act;
			}
			
			this.fotos[this.n_fotos]=this.pagina_act.addFoto(this.n_fotos,nom,nom_thumb,src,src_thumb);
			this.n_fotos++;
			
		}	



		this.goToPage=function goToPage(numpage,showpage){

			var topimg=0;
			var leftimg=0;

			if(numpage<this.pagines.length && numpage>=0){
				this.pagina_act=this.pagines[numpage];
				for(i=0;i<this.n_fotos_pagina;i++){
					try{
						//alert(this.pagina_holders[i].img_element);
						this.pagina_holders[i].img_element.onload=function (){ album_fotos.centraTotesFotos(); this.style.visibility='visible';  }										
						if(this.pagina_act.fotos[i].getThumbImage().width<30){
							this.pagina_holders[i].img_element.style.visibility='hidden';
						}else{
							this.pagina_holders[i].img_element.style.visibility='visible';	
						}

						this.pagina_holders[i].img_element.src=this.pagina_act.fotos[i].getThumbImage().src;
						
						this.pagina_act.fotos[i].getThumbImage().image_target=this.pagina_holders[i].img_element;			
						this.pagina_holders[i].img_element.width=this.pagina_act.fotos[i].getThumbImage().width;
						this.pagina_holders[i].img_element.height=this.pagina_act.fotos[i].getThumbImage().height;						
//						this.pagina_act.fotos[i].getThumbImage().onload=function (){ album_fotos.centraFoto(album_fotos.pagina_holders[i].img_element,198,166); }
//						this.pagina_holders[i].img_element.alt=this.pagina_act.fotos[i].nom_thumb;//getThumbImage().src;
//						if(!this.pagina_holders[i].img_element.loaded) this.pagina_holders[i].img_element.style.visibility='hidden';

						this.centraFoto(this.pagina_holders[i].img_element,198,166);

						this.pagina_holders[i].nom_element.innerHTML=this.pagina_act.fotos[i].nom_thumb;
						this.pagina_holders[i].frm_element.style.visibility='visible';
					}catch(e){
						this.pagina_holders[i].img_element.style.visibility='hidden';
						this.pagina_holders[i].frm_element.style.visibility='hidden';
						this.pagina_holders[i].nom_element.innerHTML="";
					}
				}
				

				document.getElementById(this.id_album+"_paginacio").innerHTML=this.HTMLpaginacio(numpage,this.pagines.length,"album_fotos.goToPage");
				
				if(showpage==null || showpage==true){
					amagaElement(this.id_album+"_frame_gran");
					mostraElement(this.id_album+"_thumbs");        			
					this.pagina_act.thumbLoaded(-1);
				}
			}
			album_fotos.centraTotesFotos();

			
		}
		
		
		this.preLoadFoto=function preLoadFoto(index){
			try{
				if(this.fotos[index].loaded==false){
					this.fotos[index].getImage();					
				}else{
					if(this.preloadok && index<this.n_fotos){
						preLoadFoto(index+1);
						preLoadFoto(index+2);
					}
				}
			}catch(e){
			}
			this.centraTotesFotos();
		}
		
		this.zoomFotoGlobal=function zoomFotoGlobal(index){
			
//			alert("pagina "+ parseInt(Math.floor(index/this.n_fotos_pagina)) );
//			alert("index_pag "+ (index%this.n_fotos_pagina));

			this.goToPage(parseInt(Math.floor(index/this.n_fotos_pagina)),false);
			this.zoomFoto(parseInt(index%this.n_fotos_pagina));
		}

		
		this.zoomFoto=function zoomFoto(index){
			

			
			var ele=this.pagina_act.fotos[index];
			
			this.preloadok=true;

			document.getElementById(this.id_album+"_foto").onload=function (){ album_fotos.centraTotesFotos(); this.style.visibility='visible';  }			
			if(ele.getImage().width<30){
				document.getElementById(this.id_album+"_foto").style.visibility='hidden';
			}

			document.getElementById(this.id_album+"_foto").style.visibility='hidden';
			document.getElementById(this.id_album+"_foto").src="";
			document.getElementById(this.id_album+"_foto").src=ele.getImage().src;
			ele.getImage().image_target=document.getElementById(this.id_album+"_foto");			
			document.getElementById(this.id_album+"_foto").width=ele.getImage().width;
			document.getElementById(this.id_album+"_foto").height=ele.getImage().height;
			
//			ele.getImage().onload=this.centraFoto(document.getElementById(this.id_album+"_foto"),416,352);
			//document.getElementById(this.id_album+"_foto").alt=ele.nom;
			document.getElementById(this.id_album+"_frame_gran_nom").innerHTML=ele.nom;

		
			this.centraFoto(document.getElementById(this.id_album+"_foto"),416,352);
						
			document.getElementById(this.id_album+"_foto_nav").innerHTML=this.HTMLpaginacio(ele.index,this.n_fotos,"album_fotos.zoomFotoGlobal");
			
			amagaElement(this.id_album+"_thumbs");
			mostraElement(this.id_album+"_frame_gran");
			//setTimeout('album_fotos.centraTotesFotos()',3000);
			
			
			album_fotos.centraTotesFotos();
			
			

			
		}
		
		

		
		
		this.zoomOutFoto=function zoomOutFoto(){
			this.preloadok=false;
			amagaElement(this.id_album+"_frame_gran");
			this.centraTotesFotos();
			mostraElement(this.id_album+"_thumbs");
			this.centraTotesFotos();			
			this.pagina_act.thumbLoaded(-1);
		}
		
		
		this.HTMLpaginacio=function HTMLpaginacio(pag_act,total_pags,func_call){ //DE 0 a N-1 total_pags=N
				//CODIFICANT HTML de PAGINACIO
				var codi_pag="";
				
				if(total_pags>1){
					codi_pag+="<a ";
					if(pag_act==0){ codi_pag+=" style='visibility:hidden; "; }
					codi_pag+="href='javascript:"+func_call+"("+(pag_act-1)+")'><img src='img/fletxa_left.png' alt='anterior' width='22' height='19'  /> anterior</a>";
					//if(numpage>0 && numpage<this.pagines.length-1){
						codi_pag+=" | ";					
					//}
					codi_pag+="<a ";
					if(pag_act==total_pags-1){ codi_pag+=" style='visibility:hidden; "; }
					codi_pag+=" href='javascript:"+func_call+"("+(pag_act+1)+")'>siguiente <img src='img/fletxa_right.png' alt='siguiente' width='22' height='19'  /></a>";
					codi_pag+="<br />"+(pag_act+1)+" de "+total_pags;	
				}

				return codi_pag;	
		}		
	
	
		this.centraFoto=function centraFoto(ele,frame_width,frame_height){
			//alert(ele.width);
			//alert(ele.height);
			
			//this.fitFoto(ele,frame_width,frame_height);
			
			var leftimg= (ele.width<2) ? 0 : parseInt( (frame_width - ele.width) / 2 );
			var topimg= (ele.height<2) ? 0 : parseInt( (frame_height - ele.height) / 2 );			
			
			ele.style.top=topimg+"px";//"0px";//
			ele.style.left=leftimg+"px";//"0px";//
		}	
		
		this.fitFoto=function fitFoto(ele,frame_width,frame_height){
			
			alert("fit foto");
			
			if(ele.width>=frame_width || ele.height>=frame_height || (ele.width<frame_width && ele.height<frame_height) ) {
			
				var frame_asp=parseFloat(frame_width)/parseFloat(frame_height);
				
				var ele_asp=parseFloat(ele.width)/parseFloat(ele.height);
				
	//			alert("frame asp: "+frame_asp+" - ele asp: ("+ele.width+","+ele.height+") "+ele_asp);
	
				if(frame_asp>ele_asp){
		//			ele.width="100%";
					ele.height=frame_height+2;
					ele.width=parseInt(parseFloat(frame_height)*parseFloat(ele_asp));
				}else{
			//		alert("frame asp: "+frame_asp+" - ele asp: ("+ele.width+","+ele.height+") "+ele_asp);
	//				ele.height="100%";
					ele.width=frame_width+2;	
					ele.height=parseInt(parseFloat(frame_width)/parseFloat(ele_asp));
				}
			
			}
		}
		
	
		this.centraTotesFotos=function centraTotesFotos(){
			var i;	
			for(i=0;i<this.n_fotos_pagina;i++){
				try{
					this.centraFoto(this.pagina_holders[i].img_element,198,166);	
				}catch(e){
				
				}
			}
//			alert("centrant foto");
			this.centraFoto(document.getElementById(this.id_album+"_foto"),416,352);				
		}
	
	}
	
	function AlbumFotosPage(){
		this.index=-1;
		this.fotos=new Array();
		this.album_pare=null;
		this.thumbs_loaded=new Array();
		
		this.pageloaded=false;
	
		this.addFoto=function addFoto(index,nom,nom_thumb,src,src_thumb){
			
			var index_foto=this.fotos.length;
			this.fotos[index_foto]=new AlbumFotosFoto();
			this.fotos[index_foto].nom=nom;
			this.fotos[index_foto].nom_thumb=nom_thumb;
			this.fotos[index_foto].src=src;
			this.fotos[index_foto].src_thumb=src_thumb;
			this.fotos[index_foto].pagina_mare=this;
			this.fotos[index_foto].index=index;
			this.fotos[index_foto].index_pagina=index_foto;
			this.thumbs_loaded[index_foto]=0;
			
			return this.fotos[index_foto];
		}
		
		this.thumbLoaded=function thumbLoaded(index){
			if(index>=0){ this.thumbs_loaded[index]=1; }
			var n_thumbs_loaded=0;
			for(i=0;i<this.fotos.length;i++){
				n_thumbs_loaded+=this.thumbs_loaded[i];
			}
			/*document.getElementById("ordrePagines").innerHTML+="<br />("+this.index+") "+n_thumbs_loaded+"/"+this.fotos.length;				*/
			if(n_thumbs_loaded>=this.fotos.length && this.album_pare.preloadok==false){
				try{
					this.album_pare.pagines[this.index+1].preLoadThumbs();
				}catch(e){}					
				
				try{
					this.album_pare.pagines[this.index-1].preLoadThumbs();
				}catch(e){}
				
				try{
					this.album_pare.preLoadFoto(this.fotos[parseInt(this.fotos.length/2)].index);
				}catch(e){}
				
			}
		}
		
		this.preLoadThumbs=function preLoadThumbs(){
			if(this.pageloaded==false){
				this.pageloaded=true;
				for(i=0;i<this.album_pare.n_fotos_pagina;i++){
					this.fotos[i].getThumbImage();
				}
			}
		}
	}
	
	
	
	function AlbumFotosFoto(){
		this.index=-1;
		this.index_pagina=-1;
		this.nom="";
		this.nom_thumb="";
		this.src="";
		this.src_thumb="";	
		
		this.loaded=false;
		
		this.image=null;
		this.image_thumb=null;
		
		this.fit_width=416;
		this.fit_height=352;
		
		this.fit_width_thumb=198;
		this.fit_height_thumb=166;
		
		this.pagina_mare=null;

		
		this.onLoadImage=function onLoadImage(){
	
			/*album_fotos.fitFoto(this,416,352);*/ 
			/*document.getElementById('ordreFotos').innerHTML+='<br />'+this.index;*/
			if(this.image_target!=null){ 
				this.image_target.width=this.width; 
				this.image_target.height=this.height; 
			}
			album_fotos.centraTotesFotos();			
			
			this.preLoadNext();
		}
		
		this.getImage=function getImage(){
			if(this.image==null){
				this.image=new Image();
				this.image_data=this;
				//document.getElementById("ordreFotos").innerHTML+="<br />"+this.index;
//				this.image.onload=function (){ eval("					document.getElementById('ordreFotos').innerHTML+='<br />'+this.image_data.index; 					/*album_fotos.fitFoto(this,416,352);*/  					if(this.image_target!=null){ 						this.image_target.width=this.width; 						this.image_target.height=this.height; 					}					album_fotos.centraTotesFotos();  					if(album_fotos.preloadok){   						album_fotos.preLoadFoto("+(this.index+1)+");						album_fotos.preLoadFoto("+(this.index-1)+"); 					}else{ 						if("+this.index_pagina+"<album_fotos.n_fotos_pagina-1){ 							album_fotos.preLoadFoto("+(this.index+1)+"); 						} 						if(this.image_data.index_pagina>0){ 							album_fotos.preLoadFoto("+(this.index-1)+"); 						} 					}   ");				}; //eval("function (){  document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"'; }");   // this.onLoadImage(); //function (this.index){ alert(this.index); /*album_fotos.fitFoto(this,416,352);*/ if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; } album_fotos.centraTotesFotos();  }				

/*************************/
				try{
					this.image.onload=eval("function (){ /*document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"';*/ /*album_fotos.fitFoto(this,416,352);*/  if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.centraTotesFotos();  if(album_fotos.preloadok){   album_fotos.preLoadFoto("+(this.index+1)+"); album_fotos.preLoadFoto("+(this.index-1)+"); }else{ if("+this.index_pagina+"<album_fotos.n_fotos_pagina-1){ album_fotos.preLoadFoto("+(this.index+1)+"); } if("+this.index_pagina+">0){ album_fotos.preLoadFoto("+(this.index-1)+"); } }   }"); //eval("function (){  document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"'; }");   // this.onLoadImage(); //function (this.index){ alert(this.index); /*album_fotos.fitFoto(this,416,352);*/ if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; } album_fotos.centraTotesFotos();  }
				}catch(e){
					this.image.onload=function (){ /*document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"';*/ /*album_fotos.fitFoto(this,416,352);*/  if(this.image_target!=null){  this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.centraTotesFotos(); }; //eval("function (){  document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"'; }");   // this.onLoadImage(); //function (this.index){ alert(this.index); /*album_fotos.fitFoto(this,416,352);*/ if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; } album_fotos.centraTotesFotos();  }
				}
/*************************/				
				//eval("this.image.onload=function (){ /*document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"';*/ /*album_fotos.fitFoto(this,416,352);*/  if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.centraTotesFotos();  if(album_fotos.preloadok){   album_fotos.preLoadFoto("+(this.index+1)+"); album_fotos.preLoadFoto("+(this.index-1)+"); }else{ if("+this.index_pagina+"<album_fotos.n_fotos_pagina-1){ album_fotos.preLoadFoto("+(this.index+1)+"); } if("+this.index_pagina+">0){ album_fotos.preLoadFoto("+(this.index-1)+"); } }   }"); //eval("function (){  document.getElementById('ordreFotos').innerHTML+='<br />"+this.index+"'; }");   // this.onLoadImage(); //function (this.index){ alert(this.index); /*album_fotos.fitFoto(this,416,352);*/ if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; } album_fotos.centraTotesFotos();  }
//				this.image.onload=function() { fitImage(this.image,this.fit_width,this.fit_height); }
				this.image.src=this.src;
				this.loaded=true;
			}
//			
			return this.image;
		}
		
	
		this.getThumbImage=function getThumbImage(){
			if(this.image_thumb==null){
				this.image_thumb=new Image();
				this.image_data=this;
//				this.image_thumb.onload=function (){ /*album_fotos.fitFoto(this,198,166);*/ if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.pagines[this.image_data.pagina_mare.index].thumbLoaded(this.image_data.index_pagina); album_fotos.centraTotesFotos();  };
/*****************************/
				try{
					this.image_thumb.onload=eval("function (){ /*album_fotos.fitFoto(this,198,166);*/  if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.pagines["+this.pagina_mare.index+"].thumbLoaded("+this.index_pagina+");  }");					
				}catch(e){
					this.image_thumb.onload=function (){ /*album_fotos.fitFoto(this,198,166);*/  if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.centraTotesFotos();  };
				}
/*****************************/				
			//eval("this.image_thumb.onload=function (){ /*album_fotos.fitFoto(this,198,166);*/  if(this.image_target!=null){ this.image_target.width=this.width; this.image_target.height=this.height; }  album_fotos.pagines["+this.pagina_mare.index+"].thumbLoaded("+this.index_pagina+");  }");
			//	this.image_thumb.onload=function() { fitImage(this.image_thumb,this.fit_width_thumb,this.fit_height_thumb); }
				this.image_thumb.src=this.src_thumb;
			}
			return this.image_thumb;		
		}
		

		
		
		/*
		function fitImage(ele,frame_width, frame_height){
			
			alert("ola");
			if(ele.width<frame_width && ele.height<frame_height){
			
				var frame_asp=parseFloat(frame_width)/parseFloat(frame_height);
				
				var ele_asp=parseFloat(ele.width)/parseFloat(ele.height);
				
				alert("frame asp: "+frame_asp+" - ele asp: ("+ele.width+","+ele.height+") "+ele_asp);
	
				if(frame_asp>ele_asp){
		//			ele.width="100%";
					ele.height=frame_height+2;
					//ele.width=parseInt(parseFloat(frame_height)*parseFloat(ele_asp));
				}else{
			//		alert("frame asp: "+frame_asp+" - ele asp: ("+ele.width+","+ele.height+") "+ele_asp);
	//				ele.height="100%";
					ele.width=frame_width+2;	
					//ele.height=parseInt(parseFloat(frame_width)/parseFloat(ele_asp));
				}
			
			}			
			
		}	*/	
	}
	
	

	

	

	
	
	
	
