/********************************/
/*	Circo, maroma y teatro..	*/
/*	funciones para el show		*/
/********************************/

function doSize(w, h, cual){
	var foto = $('foto');
	var fotoIMG = $('mainIMG');
	fWidth = w;
	fHeight = h;
	fUri = cual;
	cW = parseInt(getComputedStyle(foto, '').getPropertyValue('width').slice(0, -2));
	cL = parseInt(getComputedStyle(foto, '').getPropertyValue('right').slice(0, -2));
	cH = parseInt(getComputedStyle(fotoIMG, '').getPropertyValue('height').slice(0, -2));
	cT = parseInt(getComputedStyle(foto, '').getPropertyValue('top').slice(0, -2));
	
	check = Math.abs(cW - fWidth);
		if (check < 10) {
			int = check;
		} else {
			int = 10;
		}
	
	if (w < h) { //si es vertical
		var nW = parseInt(cW) - int;
		var nL = parseInt(cL) + int/2;
		var trick = cW > fWidth;
	} else { // si es horizontal
		var nW = parseInt(cW) + int;
		var nL = parseInt(cL) - int/2;
		var trick = cW < fWidth;
	}
	
	if (trick) {
		foto.style.width = nW + 'px';
		foto.style.right = nL + 'px';
		fotoIMG.style.width = nW + 'px';
		sombras();
		setTimeout("doSize(fWidth, fHeight, fUri)", 20);	
	} else { // ya se modificó el width
		clearTimeout();
		
		check = Math.abs(cH - fHeight);
		if (check < 10) {
			int = check;
		} else {
			int = 10;
		}
		

		if (fHeight > cH){
			nH = parseInt(cH) + int;
			nT = parseInt(cT) - int/2;
			var trick = cH < fHeight;
		} else {
			nH = parseInt(cH) - int;
			nT = parseInt(cT) + int/2;
			var trick = cH > fHeight;
		}
		
		if (trick){	
		foto.style.height = nH + 'px';
		foto.style.top = nT + 'px';
		fotoIMG.style.height = nH + 'px';
		sombras();
		setTimeout("doSize(fWidth, fHeight, fUri)", 20);
		} else {
			clearTimeout();
			cambiaFoto(cual);
		}
	}
}

function prepFoto(cual, title, ref) {
	if (ref.id == 'activa') return;
	var nextImg = new Image;
	
	$('loading').style.visibility = 'visible';
	$('activa').id = '';
	$(ref).setAttribute('id', 'activa');
	var uri = cual;
	titulo = title;
	prevW = $('mainIMG').width;
	prevH = $('mainIMG').height;
	if (document.all) {prevW = $('mainIMG').width-20;prevH = $('mainIMG').height-20;}
	$('caption').innerHTML = 'cargando...';
	$('mainIMG').src = '/includes/img/K80.png';
	$('mainIMG').width = prevW;
	$('mainIMG').height = prevH;
	
	if (document.all) {
		nextImg.onload = preIE;
		nextImg.src = cual;
	} else {
		nextImg.onload = pre;
		nextImg.src = cual;
	}
	
	function preIE() {
		$('caption').innerHTML = title;		
		$('mainIMG').setAttribute('alt', title);
		$('foto').style.right = (550-nextImg.width)/2;
		$('foto').style.top = (300-nextImg.height)/2;
		
		$('mainIMG').style.width = nextImg.width;
		$('mainIMG').style.height = nextImg.height;
		cambiaFoto(cual);
		sombras();
	}
	function pre() {
		var niW = eval(nextImg.width);
		var niH = eval(nextImg.height);
		$('caption').innerHTML = titulo; 
		
		if ((niH==prevH) && (niW==prevW)){
			//alert(prevW + '->' + niW + '||' + prevH + '->' + niH);
			cambiaFoto(uri);
			$('caption').innerHTML = title;		
			$('mainIMG').setAttribute('alt', title);
		} else {
			doSize(niW, niH, uri);
		}
	}
}

function cambiaFoto(cual) {
	$('loading').style.visibility = 'hidden';
	$('mainIMG').src = cual;
}


function next() {
	var setup = $('activa').nextSibling.nextSibling;
	if (setup.nodeName == 'DIV'){
		setup = $('mini').firstChild.nextSibling;
	}
	prepFoto(setup.href, setup.title, setup);
}

function prev(){
	var setup = $('activa').previousSibling.previousSibling;
	if ((!setup) || (setup.nodeName == 'div')){
		setup = $('mini').lastChild.previousSibling.previousSibling.previousSibling;
	}
	prepFoto(setup.href, setup.title, setup);
}


function circo(){	
	init();
	$('cover').innerHTML = $('cover').innerHTML + '<div id="prev"></div><div id="next"></div><div id="play"></div><img src="/includes/img/loading.gif" id="loading"/>';
	
	$('prev').onclick = prev;
	$('next').onclick = next;
	
	$('next').title = 'Siguiente fotografía';
	$('prev').title = 'Fotografía anterior';
	
	
	var list = $("mini").getElementsByTagName('a');
	for (i=0;i<list.length;i++){
		list[i].onclick = function(evt) {
			if(evt.preventDefault){
				evt.preventDefault();
				console.log('wtf?');
			}
			prepFoto(this.href, this.title, this);
			return false;
		}
	}
}


function keyDown(e) {
	evento = (!document.all) ? e.which : event.keyCode;
	
	switch (evento) {
		case 37: prev(); stopD = true; break;
		case 39: next(); stopD = true; break;
		default: stopD = false; break;
	}
}

window.onload = circo;

var stopD = false;
if (!document.all) {
	window.document.addEventListener("keydown", keyDown, true);
}
