// JavaScript Document

jQuery(document).ready( function(){
							
	jQuery("ul#navigation>li").hover( function(){
			jQuery(this).children(".subnavigation, .children").css("display","block");
			jQuery(this).siblings(".under_index, .current-cat-parent").children(".subnavigation, .children").css("display","none");
				
			},function(){
				if(!jQuery(this).hasClass('current-cat-parent') && !jQuery(this).hasClass('under_index') ){
		jQuery(this).children(".subnavigation, .children").css("display","none");
				}
		jQuery(this).siblings(".under_index, .current-cat-parent").children(".subnavigation, .children").css("display","block");
		});
	
	var nfeatures = jQuery("#features_roll > div").size();
	var wfeature = jQuery("#features_roll").width();
		//alert (wfeature);
	var wroll = wfeature * nfeatures;
	var newpos = 0;
	jQuery("#features_roll").css("width",wroll);
	
	
leftClick =	function () {
				jQuery("#nav_arrow_right").unbind('click', rightClick);
					jQuery("#nav_arrow_left").unbind('click', leftClick);
      			
				var position = parseInt(jQuery("#features_roll").css("left"));
				  var offset = jQuery("#features_roll").width() - wfeature;
				 // alert (offset);
					var autostart = 0;
					
					if( (position) < 0) {
					newpos = (position+wfeature);
					
											  jQuery("#features_roll").animate( { left:newpos+"px"}, 1000 , function() {
																			jQuery("#nav_arrow_left").bind('click', leftClick);
																			jQuery("#nav_arrow_right").bind('click', rightClick);
																													 });
											  
					}else{
					newpos = -1*offset;
											 
							  jQuery("#features_roll").animate( { left:newpos+"px"}, 1000 , function() {
														   
																			jQuery("#nav_arrow_left").bind('click', leftClick);
																			jQuery("#nav_arrow_right").bind('click', rightClick);
																													 });
					}
    }
	
	jQuery("#nav_arrow_left").one('click', function() {
										autostart = 0;
									  leftClick();
									  
									});
	
//	jQuery("#nav_arrow_left").click( function(){
//				  var position = parseInt(jQuery("#features_roll").css("left"));
//				  var offset = jQuery("#features_roll").width() - wfeature;
//				 // alert (offset);
//					if( (-1*position) < offset) {
//					newpos = (position-wfeature);
//											  jQuery("#features_roll").animate( { left:newpos+"px"}, 1000 , function() {alert ('hola');});
//											  
//					}
//			  });
	
	rightClick =	function () {
		
				jQuery("#nav_arrow_right").unbind('click', rightClick);
					jQuery("#nav_arrow_left").unbind('click', leftClick);
		
    			  var position = parseInt(jQuery("#features_roll").css("left"));
				 
				 var offset = jQuery("#features_roll").width() - wfeature;
				  
				  var autostart = 0;
				 // alert (offset);
					if( (-1*position) < offset) {
					newpos = (position-wfeature);
					
											  jQuery("#features_roll").animate( { left:newpos+"px"}, 1000 , function() {
																	jQuery("#nav_arrow_right").bind('click', rightClick);
																	jQuery("#nav_arrow_left").bind('click', leftClick);
																													 });
											  
					}else{
	
											   jQuery("#features_roll").animate( { left:0}, 1000 , function() {
																	jQuery("#nav_arrow_right").bind('click', rightClick);
																	jQuery("#nav_arrow_left").bind('click', leftClick);
																													 });
					}
    }
	
	jQuery("#nav_arrow_right").one('click', 
									
									function() {
										autostart = 0;
									  rightClick();
									  
									}
									
									);
	
//	jQuery("#nav_arrow_right").click( function(){
//					var position = parseInt(jQuery("#features_roll").css("left"));
//				  var offset = jQuery("#features_roll").width() - wfeature;
//				 // alert (offset);
//					if( (position) < 0) {
//					newpos = (position+wfeature);
//											  jQuery("#features_roll").animate( { left:newpos+"px"}, 1000 );
//											  
//					}
//											   
//			  });


var autostart = 1;
//Tiempo en milisegundos entre cada elemento
var timeSeconds = 2000;
//Guarda el id del timer en caso de querer detenerlo con la función clearTimeout()
var timeOut = null;

loop = function (){

 if (autostart) {
        //Check if was a click event
 //       if (event == 1) {
  //          clearTimeout(timeOut);
   //     }        
        timeOut = setTimeout('rightClick();', timeSeconds);
		timeOut = setTimeout('loop();', 3*timeSeconds);
    }
	
}


loop();
//jQuery("#nav_arrow_right").click();

// BUSCANDO EL MODO DE INICIAR LA ANIMACION SOLITA

//	jQuery("#features_roll").queue(function(){	setTimeout(alert('hola'), 80000);	} );

//	jQuery("#nav_arrow_left").show(50000).pause();
//	jQuery("#nav_arrow_left").click();

// jQuery("#header").click(function(){alert("hola")});
	
});
