
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){
		//si el ratio és més petit que el de la imatge fixem l'alt sió al revés
		wratio=$(window).width() / ($(window).height()-100);
		if( qer < wratio)
		{
			var escala = ($(window).height()-100) / qe.height();
			nh = $(window).height()-100;
			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() - 100 - nh;
			y += Math.floor(dy/2);
		}
		qe.width(nw);
		qe.height(nh);
		//$('#menu').width(nw-100);
	
		var espacioEntreBotones=nw/40;
		var alturaBoton=nw/30;
		if (alturaBoton>40) {
			alturaBoton=40;
			espacioEntreBotones=espacioEntreBotones+(nw/30-42)*5;
		}
	
		$('#sm1').css({
			width: espacioEntreBotones+ 'px'
		});	

		$('#sm2').css({
			width: espacioEntreBotones+ 'px'
		});	

		$('#sm3').css({
			width:espacioEntreBotones+ 'px'
		});	

		$('#sm4').css({
			width: espacioEntreBotones+ 'px'
		});	

		$('#sm5').css({
			width:espacioEntreBotones+ 'px'
		});	
	
		$('#m1').css({
			height: alturaBoton+ 'px'
		});	
			
		$('#m1').show();
	
		$('#m2').css({
			height: alturaBoton+ 'px'
		});	
		$('#m2').show();

		$('#m3').css({
			height: alturaBoton+ 'px'
		});	
		$('#m3').show();

		$('#m4').css({
			height: alturaBoton+ 'px'
		});	
		$('#m4').show();

		$('#m5').css({
			height: alturaBoton+ 'px'
		});	
		$('#m5').show();

		$('#m6').css({
			width: (alturaBoton*6) + 'px'
		});	
		$('#m6').show();

		$('#col1').css({
			width: (alturaBoton*7) + 'px'
		});	
		
		$('#col2').css({
			width: (alturaBoton*7) + 'px'
		});		

		$('#col3').css({
			width: (alturaBoton*7) + 'px'
		});	
		
		$('#col4').css({
			width: (alturaBoton*7) + 'px'
		});	
		
		$('#col5').css({
			width: (alturaBoton*7) + 'px'
		});		
		
		$('#contenido').show();

		$('#divAnterior').css({
			top: $(window).height()-80-nh/2 + 'px',
			left: x + 'px'
		});			
		$('#divAnterior').show();
		
		
		$('#divSiguiente').css({
			top: $(window).height()-80-nh/2 + 'px',
			left: x+nw-20 + 'px'
		});			
		$('#divSiguiente').show();
		
		$('#divCerrar').css({
			top: $(window).height()-80-nh + 'px',
			left: x+nw-20 + 'px'
		});			
		$('#divCerrar').show();
	
		$('#linea').css({
			width: nw-15 + 'px',
			visibility: ''
		});			
	
		$('#foto').css({
			top: $(window).height()-80-nh + 'px',
			left: x + 'px'
		});			
		if(navigator.appName == "Microsoft Internet Explorer") {
			y2 = $(window).scrollTop();	
			x2 = $(window).scrollLeft();							
			
			$('#slide').css({
				top: y2 + 'px',
				left: x2 + 'px',
				width: $(window).width() +'px',
				height: $(window).height() +'px'			
			});
			
			$('#divAnterior').css({
				top: $(window).height()-80-nh/2+y2 + 'px',
				left: x + x2 +'px'
			});	
			
			$('#divSiguiente').css({
				top: $(window).height()-80-nh/2+ y2 + 'px',
				left: x+nw-20 + x2 + 'px'
			});
			
			$('#divCerrar').css({
				top: $(window).height()-80-nh+ y2 + 'px',
				left: x+nw-20+ x2 + 'px'
			});
			
		}
		
	} else {
		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: 0 + '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 inicia(){
	index=0;
	total=fotos.length;
	WRatio();
	cambiaFoto();
	//EVENTOS DE PANTALLA
	$(window).bind('resize',function(){
		WRatio();
		reajusta();
	});
							
	$(window).bind('scroll',function(){
		WRatio();
		reajusta();
	});
}
