/* INICIALIZAR */
$j(document).ready(function(){
	
	if($j("#slide-top-home").length){
		inicializo_SLIDE("#slide-top-home");
	}
	if($j("#producto").length){
		inicializoToggle("#producto");		
	}
	if($j("#mensaje").length){
		$j("#mensaje").delay(3000).slideUp(1200,'easeInOutExpo', function() {$j("#mensaje").css({display:'none'});});
	}
	if($j("#botonera-productos").length){
		inicializoToggle("#botonera-productos");	
	}
	if($j("#solucion").length){
		inicializoToggle("#solucion");		
	}
	if($j("#map_canvas").length){
		initialize();
	}
	
	
	/*///  PRODUCTOS  NAV  ////*/	
	function inicializoToggle(cual){
		
		var total=$j(cual+' > dd').length;		
		
		for(var i=1;i<=total;i++){
			
			var bot = $j(cual+' > dd:nth-child('+i+') > a')
			
			
				bot.click(function(){
					if($j(this).next().length){
						$j(this).next().stop(false,true).slideToggle(600,'easeInOutExpo');
						if(!$j(this).hasClass('sel')){
							$j('html,body').stop(false,true).delay(300).animate({scrollTop:$j(this).offset().top-20}, 700,'easeInOutExpo');
						}
						$j(this).toggleClass('sel');						
						return false;	
					}			
				});
			
			
			if(!$j(cual+' > dd:nth-child('+i+')').children().hasClass('sel')){
				$j(cual+' > dd:nth-child('+i+')').children().next().delay((i*300)+300).slideUp(800,'easeInOutExpo');
			}
			
		}
		
	}

	
	function inicializo_SLIDE(gal){	
			
			total = $j(gal+" dl dd").length;
			
			$j(gal+" dl").data('cual',1);		
						
			if(total>1){creoInterval_bucle(gal,total,5000);}		
			
			function bucle(gal,total){
			
					var slide= $j(gal+" dl"),
						ant=total;
					
					clearInterval(timer_bucle);	
					
					ant	= slide.data('cual');			
					slide.data('cual',slide.data('cual')+1);				
					
					if( slide.data('cual') > (total) ){
						slide.data('cual',1);
						ant=total;
					}
					
					animo_bucle(gal,slide.data('cual'),ant);		
					creoInterval_bucle(gal,total,7000);	
					
			}		
			
			function creoInterval_bucle(gal,total,lapso){timer_bucle = setInterval(function () {bucle(gal,total);},lapso);}
			
			
			function animo_bucle(gal,cual,ant){							
				
				var slide = $j(gal+" dl dd:nth-child("+cual+")"),
					slideCont = $j(gal+" dl dd:nth-child("+cual+") .bd"),
					slideAnt = $j(gal+" dl dd:nth-child("+ant+")"),
					slideAntCont = $j(gal+" dl dd:nth-child("+ant+") .bd");
				
				if(cual==4){
					ani='easeOutElastic';
					tiempo=1200;
				}else{
					ani='easeOutExpo';
					tiempo=600;
				}
				
				slideCont.css({opacity:0});
				slide.css({display:'block'});
				
				slideAnt.css({zIndex:99});
				slide.css({zIndex:100});
				
				slideAnt.stop(true,false).animate({top:-415}, 800,'easeInBack', function() {slideAnt.css({top:415,display:'none'});slideAntCont.css({opacity:0});});
				slide.stop(true,false).delay(700).animate({top:0}, tiempo,ani, function() {slideAnt.css({top:415,display:'none'});slideCont.animate({opacity:1},900,'easeInOutExpo');});
			
				
			}		
			
	}

});


