//Global variables
var divs= new Array();
var actual=0;


//Animation
function animation(anim) {
	//Do it 10 times
	for (var i=0;i<11;i++){
		//If animation type is fading
		if(anim=='fade'||anim=='fade-resize'){
			setTimeout('setOpacity('+i+')',70*i);
		}
		//If animation type is resizing
		if(anim=='resize'||anim=='fade-resize'){
			setTimeout('setSize('+i+')',20*i);
			setTimeout('reSize('+i+')',10*i);
		}
	}
	//setTimeout('for (var i=0;i<11;i++){setTimeout(reSize('+i+'),10*i)}',5);
	//return false;
}

//Fade Animation
function setOpacity(value)
{
	eval("$(divs[actual].id).style.opacity=value/10");
	eval("$(divs[actual].id).style.filter='alpha(opacity=' + value*10 + ')'");
}

//Resize Animation
function setSize(value)
{
	width=eval("$(divs[actual].id).style.width");
	width=width.substr(0,width.indexOf("p"));
	width=parseInt(width)+parseInt(value);
	width=width+'px';
	
	height=eval("$(divs[actual].id).style.height");
	height=height.substr(0,height.indexOf("p"));
	height=parseInt(height)+parseInt(value);
	height=height+'px';

	eval("$(divs[actual].id).style.width=width");
	eval("$(divs[actual].id).style.height=height");
}

function reSize(value)
{
	width=eval("$(divs[actual].id).style.width");
	width=width.substr(0,width.indexOf("p"));
	width=parseInt(width)-parseInt(value);
	width=width+'px';
	
	height=eval("$(divs[actual].id).style.height");
	height=height.substr(0,height.indexOf("p"));
	height=parseInt(height)-parseInt(value);
	height=height+'px';

	eval("$(divs[actual].id).style.width=width");
	eval("$(divs[actual].id).style.height=height");
}


//Navigate through divs
function nav(action,anim){
//$('imagen3').hide();
//alert(document.getElementsByTagName('div').length);
	
	anim = typeof(anim) != 'undefined' ? anim : 'fade';
	
	//Initial action
	if (action=='none'){
		//If actual div is the first
		if (actual==0) {
			var f=0;
			//Loop all divs in the page
			for(i=0;i<$$('div').length;i++){
				//var ide=$$('div')[i].id;
				//alert('ide->'+ide+' index->'+ide.indexOf('imagen'));
				//if (ide.indexOf('imagen')>=0){
				//Add to an array all divs with id=imageXX
				if ($$('div')[i].id.indexOf('imagen')>=0){
					divs[f]=$$('div')[i];
					//alert(divs[f].id+' '+f);
					f++;
				}
			}
			//Enable first div
			eval("$(divs[0].id).style.visibility='visible'");
		}
	//If action is prev div
	}else if (action=='prev'){
		//If is not the first div
		if (actual>0){
			actual--;
			//Loop all the divs
			for(i=0;i<divs.length;i++){
				//Enable if is the actual div
				if(i==actual){
					eval("$(divs[i].id).style.visibility='visible'");
					animation(anim);
					//alert('mostrar->'+i+' lenght->'+divs.length+' actual->'+actual+' imagen->'+divs[i].id);
				//Disbale all other divs
				}else{
					eval("$(divs[i].id).style.visibility='hidden'");
				}
			}
		}
	//If action is next div
	}else if(action=='next'){
		//If is not the last div
		if (actual<divs.length-1){
			actual++;
			//Loop all the divs
			for(i=0;i<divs.length;i++){
				//Enable if is the actual div
				if(i==actual){
					eval("$(divs[i].id).style.visibility='visible'");
					animation(anim);
					//alert('mostrar->'+i+' lenght->'+divs.length+' actual->'+actual+' imagen->'+divs[i].id);
				//Disable all other divs
				}else{
					eval("$(divs[i].id).style.visibility='hidden'");
				}
			}
		}
	}
	
	//If is the first div, disable prev arrow
	if(actual==0){
		document.getElementById('prev').src="http://www.theinit.com/img/prev_0.png";
	}else{
		document.getElementById('prev').src="http://www.theinit.com/img/prev.png";
	}
	
	//If is the last div, disable next arrow	
	if (actual==divs.length-1){
		document.getElementById('next').src="http://www.theinit.com/img/next_0.png";
	}else{
		document.getElementById('next').src="http://www.theinit.com/img/next.png";
	}

}

/*
for (i=0; i<document.getElementsByTagName('div').length; i++){
//alert(document.getElementsByTagName('div')[i].id);
divs[i]=document.getElementsByTagName('div')[i];
//alert(divs[i].id);
divs[i].hide();
}*/
