// JavaScript Document
/*credits: Andrés Fernández*/
function compensar(delta){
		var mx=0;
		for(var i=1,l=arguments.length;i<l;i++){
			if($(arguments[i]).offsetHeight>mx)
				mx=$(arguments[i]).offsetHeight;
		}
		for(var ii=1,ll=arguments.length;ii<ll;ii++)
			$(arguments[ii]).css('height',mx+delta+'px');
}

addLoadEvent(
	function(){
		var counter=0;
		var req=[];
		
		if($('ajaxDiseno'))
			req.push($('ajaxDiseno'));
		if($('ajaxArte'))
			req.push($('ajaxArte'));
		if($('ajaxApple'))
			req.push($('ajaxApple'));
		if($('ajaxPHP'))
			req.push($('ajaxPHP'));
		if($('ajaxJS'))
			req.push($('ajaxJS'));
		if($('ajaxAS'))
			req.push($('ajaxAS'));
		
		
		if($('ajaxDiseno')){
			$('ajaxDiseno').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxDiseno',{'key':window.keyword,'sec':'DISENO'},function(){counter++;if(counter==req.length){fun();}});
			
		}
		
		if($('ajaxArte')){
			$('ajaxArte').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxArte',{'key':window.keyword,'sec':'ARTE'},function(){counter++;if(counter==req.length){fun();}});
			
		}
		
		if($('ajaxApple')){
			$('ajaxApple').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxApple',{'key':window.keyword,'sec':'APPLE'},function(){counter++;if(counter==req.length){fun();}});
			
		}
		
		if($('ajaxPHP')){
			$('ajaxPHP').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxPHP',{'key':window.keyword,'sec':'PHP'},function(){counter++;if(counter==req.length){fun();}});
			
		}
		
		if($('ajaxJS')){
			$('ajaxJS').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxJS',{'key':window.keyword,'sec':'JS'},function(){counter++;if(counter==req.length){fun();}});
			
		}
		
		if($('ajaxAS')){
			$('ajaxAS').innerHTML='<img src="images/ajax-loader2.gif" style="position:relative; left:50%; margin-left:-33px;" />';
			requestGET('busquedaAjax.php','ajaxAS',{'key':window.keyword,'sec':'AS'},function(){counter++;if(counter==req.length){fun();}});
			
		}

		
		if(!req.length)
			fun();
	}		 
);



function fun(){
	compensar(0,'principal','lateral');
	compensar(0,'cajaResultados','DNSEC3','lateral');
	if(Math.max(parseInt($('cajaResultados').css('height')),parseInt($('DNSEC3').css('height')))<233){
		$('cajaResultados').css('height','405px');
		$('DNSEC3').css('height','405px');
		$('lateral').css('height','472px');
		compensar(0,'principal','lateral');
	}
	window.altoPagina=Math.max(parseInt($('principal').css('height')),parseInt($('lateral').css('height')));
	window.altoPartes=Math.max(parseInt($('cajaResultados').css('height')),parseInt($('DNSEC3').css('height')));;

}

function reAjustar(d){
	window.altoPagina-=d;
	window.altoPartes-=d;
	$('principal').css('height',window.altoPagina+'px');
	$('lateral').css('height',window.altoPagina+'px');
	$('cajaResultados').css('height',window.altoPartes+'px');
	$('DNSEC3').css('height',window.altoPartes+'px');

}
function getElementPosition() {
		var offsetTrail = this;
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		return {left:offsetLeft, top:offsetTop};
}

function puntero(puntero,seccion,e){
	var div,actual,delta,pre,n;
	switch(seccion){
		case 'DISENO':
		div='ajaxDiseno';
		break;
		case 'ARTE':
		div='ajaxArte';
		break;
		case 'APPLE':
		div='ajaxApple';
		break;
		case 'PHP':
		div='ajaxPHP';
		break;
		case 'JS':
		div='ajaxJS';
		break;
		case 'AS':
		div='ajaxAS';
		break;
	}
	actual=$(div).offsetHeight;
	pre=$(div).cloneNode(false);
	pre.style.backgroundColor='#CCC';
	pre.style.backgroundImage='url(images/ajax-loader_CCC.gif)';
	pre.style.backgroundRepeat='no-repeat';
	pre.style.backgroundPosition='50%';
	pre.style.opacity = .5;
    pre.style.MozOpacity = .5;
    pre.style.KhtmlOpacity = .5;
    pre.style.filter = 'alpha(opacity=' + .5*100 + ')';
    pre.style.zoom=1;
	pre.style.position='absolute';
	n=getElementPosition.call($(div));
	pre.style.top=n.top+'px';
	pre.style.left=n.left+'px';
	pre.style.width=$(div).offsetWidth+'px';
	pre.style.height=$(div).offsetHeight+'px';
	pre.id='tempPosMos';
	document.getElementsByTagName('body')[0].appendChild(pre);
	var referencia=document.getElementById('tempPosMos');
	window.scrollTo(0,n.top-35);
	requestGET('busquedaAjax.php',div,{'key':window.keyword,'sec':seccion,'puntero':puntero},function(){delta=actual-$(div).offsetHeight;reAjustar(delta);referencia.style.height=$(div).offsetHeight+'px';setTimeout(function(){document.getElementsByTagName('body')[0].removeChild($('tempPosMos'))},500);});
}
