// JavaScript Document
function init(){
	var l=document.getElementsByTagName('div');
	contenido=[];

	for(i=0;i<l.length;i++){
		var h=l[i].id;
		//alert(h.substr(0,5));
		if(h.substr(0,5)=='acab_'){ 
			var head=$(h);
			head.cont="acon_"+h.substr(5,(h.length-5));
			contenido.push(head.cont);//agregarlo al array de contenidos
			var tempc=$(head.cont);
			tempc.maxheight=tempc.offsetHeight;
			tempc.parent=tempc.parentNode.id;
			tempc.style.height=0+'px';
			tempc.style.display='none';
			head.onclick = function(){
				activarcont(this.cont);
			}
		}
	}
	setpopuptrabajos();
	sacarloader();

}
function setpopuptrabajos(){
	var l=document.getElementsByTagName('div');
	trabajo=[];
	
	for(i=0;i<l.length;i++){
		var h=l[i].className;	
		if(h=='trabajopoplink'){ 
			trabajo.push(l[i].id);
			l[i].trabajoindex=trabajo.length-1;
			/*alert(l[i].id+" "+l[i].popindex+ " " +poptrabajo[l[i].popindex])*/
		}
	}
	/*link_trabajo=[];*/

	linktrabajo=$("listatrab").innerHTML.split("__");

	setbotones();
	$('mainimg').onload=function(){
		$('mainimg').style.display='block';
		/*alert("sasd")*/
		$('mainimg').tween('opacity',[0,1]);
	}
	
}
function setbotones(){
	$('btn_next').onclick=function(){
		var nextrabajo=curtrabajo;
		var nextpop=curpop+1;
		if(nextpop==5){
			nextpop=1
			nextrabajo=curtrabajo+1
			if(nextrabajo==trabajo.length){
				nextrabajo=0;
			}
		}
		mostrartrabajo(trabajo[nextrabajo],nextpop)
	}
	$('btn_prev').onclick=function(){
		var nextrabajo=curtrabajo;
		var nextpop=curpop-1;
		if(nextpop==0){
			nextpop=4;
			nextrabajo=curtrabajo-1;
			if(nextrabajo==-1){
				nextrabajo=trabajo.length-1;
			}
		}
		mostrartrabajo(trabajo[nextrabajo],nextpop)
	}
	$('btn_close').onclick=function(){
		cerrarpop('popup_trabajos');
	}
}
function activarcont(contname){//funcion que se llama en el cliqueo de los acordeones
	var obj = $(contname);
	cerrartodoslospopups();
	if(obj.offsetHeight==0){
		mostrar(contname);
	}else{
		ocultar(contname);
	}
}
function cerrartodoslospopups(){
	cerrarpop('popup_trabajos');
}
function mostrar(micontenido){
	var fxmostrar= new Fx.Tween (micontenido, {
		property: 'height',
		duration: 500, 
		transition: Fx.Transitions.Quart.easeInOut,
		link: 'chain'});
		/*fxmostrar.onComplete=function(){
		imgshow.style.display='none';
		}*/	
		cerrarhermanos(micontenido);
		$(micontenido).style.display='block';
		fxmostrar.start($(micontenido).maxheight);
}
function ocultar(micontenido){
	var fxocultar= new Fx.Tween (micontenido, {
		property: 'height',
		duration: 500, 
		transition: Fx.Transitions.Quart.easeInOut,
		link: 'chain'});
		fxocultar.owner=micontenido;
		fxocultar.onComplete=function(){
			$(fxocultar.owner).style.display='none';
		}
		//$(micontenido).maxh=$(micontenido).offsetHeight;
		fxocultar.start(0);
}
function cerrarhermanos(contname){
	var i=0
	for(i=0;i<contenido.length;i++){
		if(contenido[i] != contname && $(contenido[i]).parent==$(contname).parent ){
			ocultar(contenido[i]);
		}
	}
}
function sacarloader(){
	/*var fxsacar= new Fx.Tween ("preloader", {
		property: 'height',
		duration: 500, 
		transition: Fx.Transitions.Quart.easeInOut,
		link: 'chain'});
		fxsacar.onComplete=function(){
		$("preloader").style.display='none';
		}
		fxsacar.start(0);*/
}
function mostrarpop(newpop){
	var newtop=0;
	
	//  NEWPOP sirve para calcular la posición top del pop up variable
	/*if (navigator.appName == "Microsoft Internet Explorer"){
		newtop=document.documentElement.scrollTop;
	} else{
		newtop=window.pageYOffset-100;
		//newtop=window.pageYOffset;
	}*/
	 /*window.scrollTo(0,0)*/
	 /*alert($(newpop).offsetTop)*/
	
	if (navigator.appName == "Microsoft Internet Explorer"){
		$(newpop).style.top=124+'px';
	}else{
		$(newpop).style.top=24+'px';
	}
	$(newpop).style.left=100+'px';
	$(newpop).style.display='block';
	$(newpop).tween('opacity',[0,1]);
}
function cerrarpop(closeobj){
		myfx= new Fx.Tween (closeobj, {
		property: 'opacity',
		duration: 250, 
		transition: Fx.Transitions.Quart.easeInOut,
		link: 'chain'});
		
		myfx.start(1,0);
		myfx.onComplete=function(){
			$(closeobj).style.display='none';
			$(closeobj).style.visibility='hidden';
			
		}
		
	//$(closeobj.id.slice(6)).tween('opacity',[1,0]);
	}
function mostrartrabajo(trabajonom,popindex){
		curtrabajo=Number($(trabajonom).trabajoindex);
		curpop=Number(popindex);
		
		if ($('popup_trabajos').style.display=='block'){
			fadefx= new Fx.Tween ('mainimg', {
			property: 'opacity',
			duration: 250, 
			transition: Fx.Transitions.Quart.easeInOut,
			link: 'chain'});
			
			fadefx.start(1,0);

			fadefx.onComplete=function(){
				preparartrabajo(trabajonom,popindex);
			}
		
		}else{
			preparartrabajo(trabajonom,popindex);
			mostrarpop('popup_trabajos');
		}

	
	
	/*alert("curttrab "+curtrabajo+" "+trabajo[curtrabajo])*/
}
function preparartrabajo(trabajonom,popindex){
	$('mainimg').style.display='none';
	$('mainimg').style.visibility='hidden';
	$('mainimg').src="";
	$('mainimg').src=trabajonom+popindex+".jpg";
	
	if(popindex==1){
		$('btn_prev').style.display='none';
		$('mainimg').src=trabajonom+popindex+".gif";
	}else{
		$('btn_prev').style.display='inline'
	}
	
	
	$('link_trabajo').style.display='block';
	if(linktrabajo[curtrabajo]!="nolink"){
		$('link_trabajo').href="http://"+ linktrabajo[curtrabajo];
	}else{
			$('link_trabajo').style.display='none';
		}
	
	/*if(curpop==1){
		$('link_trabajo').style.display='block';
		if(linktrabajo[curtrabajo]!="nolink"){
			$('link_trabajo').href="http://"+ linktrabajo[curtrabajo];
		}else{
			$('link_trabajo').style.display='none';
		}
	}else{
		$('link_trabajo').style.display='none';
	}*/
	
}