
var wratio;
var indexAnterior;
var index;
var total;
var fotoActual=new Array();	
var full;

var velocidad;
var fotos;

function WRatio()
{
	wratio = $(window).width() / $(window).height();
}
	
function ERatio(elemento)
{
	return (qe.width()/qe.height());
}

function redimensiona(){
	qe=$(fotoActual[index]);
	var qer = ERatio(qe);
	var nw = 0;
	var nh = 0;
	var x = 0;
	var y = 0;
	if (full==0){
		//si el ratio és més petit que el de la imatge fixem l'alt sió al revés
		if( qer < wratio)
		{
			var escala = $(window).height() / qe.height();
			nh = $(window).height();
			nw = Math.floor(qe.width() * escala);		
			var dx = $(window).width() - nw ;
			x += Math.floor(dx/2);
		}else{
			var escala = $(window).width() / qe.width();
			nw = $(window).width();
			nh = Math.floor(qe.height() * escala);
			var dy = $(window).height() - nh;
			y += Math.floor(dy/2);
		}
		$('#divAnterior').css({
			top: y+nh-60 + 'px',
			left: x+nw-150 + 'px'
		});			
		$('#divAnterior').show();
		
		
		$('#divSiguiente').css({
			top: y+nh-60 + 'px',
			left: x+nw-100 + 'px'							   
		});			
		$('#divSiguiente').show();
		
		$('#divCerrar').css({
			top: y+nh-60 + 'px',
			left: x+nw-50 + 'px'							   
		});			
		$('#divCerrar').show();
		
		$('#divDown').css({
			top: y+nh-60 + 'px',
			left: x+50 + 'px'							   
		});			
		$('#divDown').show();
		document.getElementById('linkDown').href='bajarFotoGaleria.php?idEsquiador='+idEsquiador+'&idGaleria='+idGaleria+'&idFoto='+(index+1);
		
		$('#divLogoFull').css({
			top: y+20 + 'px',
			left: x+20 + 'px'							   
		});			
		$('#divLogoFull').show();		
		
	} else if (full==1) {

		if( qer > wratio)
		{
			var escala = $(window).height() / qe.height();
			nh = $(window).height();
			nw = Math.floor(qe.width() * escala);
			var dx = nw - $(window).width();
			x -= Math.floor(dx/2);
		}else{
			var escala = $(window).width() / qe.width();
			nw = $(window).width();
			nh = Math.floor(qe.height() * escala);
			var dy = nh - $(window).height();
			y -= Math.floor(dy/2);
		}
	} else {
		
		var izq=$(window).width()/2 - 535;
		if (izq<5) izq=5;
		
		$('#pie').css({
			top: $(window).height() - 240 + $(window).scrollTop() + 'px',
			left: izq + 'px'							   
		});	
		//NO RECORTA IMAGEN
		if( qer < wratio)
		{
			var escala = $(window).height() / qe.height();
			nh = $(window).height();
			nw = Math.floor(qe.width() * escala);		
			var dx = $(window).width() - nw ;
			x += Math.floor(dx/2);
		}else{
			var escala = $(window).width() / qe.width();
			nw = $(window).width();
			nh = Math.floor(qe.height() * escala);
			var dy = $(window).height() - nh;
			y += Math.floor(dy/2);
		}
		/*
		//RECORTA IMAGEN
		if( qer > wratio)
		{
			var escala = $(window).height() / qe.height();
			nh = $(window).height();
			nw = Math.floor(qe.width() * escala);
			var dx = nw - $(window).width();
			x -= Math.floor(dx/2);
		}else{
			var escala = $(window).width() / qe.width();
			nw = $(window).width();
			nh = Math.floor(qe.height() * escala);
			var dy = nh - $(window).height();
			y -= Math.floor(dy/2);
		}*/
	}
	qe.width(nw);
	qe.height(nh);
	$('#foto').css({
		top: y + 'px',
		left: x + 'px'
	});	

	if(navigator.appName == "Microsoft Internet Explorer") {
		y = $(window).scrollTop();	
		x = $(window).scrollLeft();							
		
		$('#slide').css({
			top: y + 'px',
			left: x + 'px',
			width: $(window).width() +'px',
			height: $(window).height() +'px'			
		});
	} 
}

function muestraLoading(){
	$('#slide').css('background', "url(/fondo/loader.gif) no-repeat center center"); 
}

function ocultaLoading(){
	$('#slide').css('background', "white"); 
}

function reajusta(){
	if (!fotoActual[index].complete) {
		window.setTimeout('reajusta()',100);
	} else {
		ocultaLoading();
		redimensiona();
		$(fotoActual[index]).fadeIn(velocidad);
	}
}

function cambiaFoto(){
	if (fotoActual[indexAnterior]!=null){
		$(fotoActual[indexAnterior]).fadeOut(velocidad);
		window.setTimeout('muestraLoading()',velocidad+50);		
	}
	if (fotoActual[index]==null){
		fotoActual[index] = new Image();		
		fotoActual[index].src=fotos[index];		
		$(fotoActual[index]).hide();	
		window.setTimeout('reajusta()',velocidad+50);		
		document.getElementById('foto').appendChild(fotoActual[index]);			
	} else {
		$(fotoActual[index]).hide();			
		window.setTimeout('reajusta()',velocidad+50);		
	}
}

function fotoAnterior(){
	indexAnterior=index;	
	index--;
	if (index<0) index = (total-1);
	cambiaFoto();
	//PREFETCHING
	if ((index-1)>0){
		var aux = new Image();		
		aux.src=fotos[index-1];	
	} else {
		var aux = new Image();		
		aux.src=fotos[total-1];		
	}
}

function fotoSiguiente(){
	indexAnterior=index;		
	index++;
	if (index>=total) index=0;
	cambiaFoto();
	//PREFETCHING
	if ((index+1)<total){
		var aux = new Image();		
		aux.src=fotos[index+1];	
	}
}

function paseDiapositivas(){
	window.setTimeout('paseDiapositivas()',4000);
	fotoSiguiente();
}

function inicia(){
	if (!full)
		index=primera-1;
	else 
		index=0;
	total=fotos.length;
	WRatio();
	cambiaFoto();
	if (full==2 && diapo==1){
		paseDiapositivas();	
	}
	//EVENTOS DE PANTALLA
	$(window).bind('resize',function(){
		WRatio();
		reajusta();
	});
							
	$(window).bind('scroll',function(){
		WRatio();
		reajusta();
	});
}
