var initList = initList || []; 

initList.push(function(){

	$(".content_nav").each(function(){
	var contentNav=$(this);
	var buttons = contentNav.parent().find(".buttons");
	var sections = contentNav.find(".sections");
    var stage = 1;
    var num_sections = sections.children().length;
    var width_1 = 0;
    if ( sections.attr("data-sg-width") )
    	width_1 = parseInt(sections.attr("data-sg-width"));
    else
    	width_1 = 1210;
    console.log(width_1);
    var width_sections = width_1 + parseInt(sections.children().css("marginRight") )  + parseInt(sections.children().css("marginLeft") );
	var time_factor = 1 * width_sections;

	buttons.click(function(){
			contentNav.find(" > .controls > span").removeClass("active");
			var variation;
			var timeStop = 0;

			if ($(this).is(".last")){
				variation = "+=";
				stage--;
			}else{
				variation = "-=";
				stage++;		
			}

			$(this).fadeOut(50,
				function(){ 

					if(stage == 2){
						timeStop = 1400;
						
						$(this).removeClass("ini").addClass("next");
						setTimeout(function() {
						}, 1000);
					}else{
						timeStop = 0;
					}if ( sections.attr("data-sg-width") ){
						timeStop = 0;
					}


					setTimeout(function() {					
				    	sections.animate({
				    		marginLeft: variation+width_sections
				    	}, time_factor, function(){
							
							if (stage > 1) {
								buttons.last().fadeIn();
							}
							if (stage < num_sections) {
								buttons.first().fadeIn();
							}else{
								// $(".restart").fadeIn();
							}
							contentNav.find(" > .controls > span:nth-child(" + stage + ")").addClass("active");
				    	});
			    	}, timeStop);		
			    });			

	  });
	contentNav.find(" .controls span").click(function(){
		var index = $(this).index();
		var margin = index * width_sections *-1;

	    stage = index + 1;
		contentNav.find(" > .controls > span").removeClass("active");
		buttons.fadeOut(50,
				function(){ 
			    	sections.animate({
			    		marginLeft: margin
			    	}, time_factor, function(){
						if (stage > 1) {
							buttons.last().fadeIn();
						}
						if (stage < num_sections) {
							buttons.first().fadeIn();
						}
						contentNav.find(" > .controls > span:nth-child(" + stage + ")").addClass("active");
			    	});		
			    });
	})

	});

});

