/* Declare a namespace for the site */
var Site = window.Site || {};
var seccion_actual = 'section#home';
var boton_actual = '#menu_nav_home';


/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	//same as $(document).ready();
	$(function() {
	});
	$(window).bind("load", function() {
	});	
})(jQuery);

$(window).load(function () {
	  	fixSizeWindow();		
});


$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});
$(window).bind('resizeEnd', function() {
									 
		fixSizeWindow();		
		goToArea(boton_actual, seccion_actual, 'easeInOutQuint'); 	
});


function fixSizeWindow(){
		var altoVentana = $(window).height();
		
		if (altoVentana > 790){	
				$('section').height(altoVentana-160);
		}else{	
				$('section').height(790-160);
		};
		

}


function goToArea(boton, seccion, efecto,letra){
		$('#workgallery').hide('slow'); 
		$("body").css("overflow", "auto");
		$.scrollTo(seccion,800, { easing:'easeInOutQuint' });
		$('nav > a').removeClass( 'activo' );
		$('#menu_nav_0').removeClass( 'activo' );
		$(boton).addClass( 'activo' );
		$('#slider_contacto').cycle(0); // resetea la seccion de contacto
		seccion_actual = seccion;
		boton_actual = boton;
		$('#letra').cycle(letra);
		return false;
};

function setMenuSeccion(boton, seccion, efecto,letra){
		$('#workgallery').hide('slow'); 
		$('nav > a').removeClass( 'activo' );
		$('#menu_nav_0').removeClass( 'activo' );
		$(boton).addClass( 'activo' );
		seccion_actual = seccion;
		boton_actual = boton;
		$('#letra').cycle(letra);
		return false;
};


function iniciar_sliders(){
	$('.sliderFeatured').cycle({ 
		fx:     'scrollRight', 
		timeout: 9000, 
		next:   '#footfeatures p a.next', 
		prev:   '#footfeatures p a.prev',
		easing:  'easeInOutQuad',
		speed: 2000,		
		pager: 'div#footfeatures ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		} 
	});
	
	
	$('#contWorks').cycle({ 
		fx:     'scrollRight', 
		timeout: 0, 
		next:   '#works p a.next', 
		prev:   '#works p a.prev',
		easing:  'easeInOutQuad',		
		speed: 2000
	});
	
	$('#contNews').cycle({ 
		fx:     'scrollRight', 
		timeout: 0, 
		next:   '#news p a.next', 
		prev:   '#news p a.prev',
		easing:  'easeInOutQuad',
		speed: 2000
	});	
	
	$('#slider_contacto').cycle({ 
		fx:     'fade', 
		timeout: 0, 
		easing:  'easeInOutQuad',
		speed: 2000,		
		before: function() {
				// stop all inner slideshows
				//$('#slider_contacto').cycle('stop');
				$('#slider_contacto_sanjuan').cycle(0);			
				$('#slider_contacto_buenosaires').cycle(0);
				$('#slider_contacto_medellin').cycle(0);          
				$('#slider_contacto_cordoba').cycle(0);                
				$('#slider_contacto_miami').cycle(0);          
				// start the new slide's slideshow
			}
	
	});	
	$('#slider_contacto_sanjuan').cycle({
		fx: 'fade',
		timeout: 0,
		pager: '#pr ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		}
	});
	$('#slider_contacto_buenosaires').cycle({
		fx: 'fade',
		timeout: 0,
		pager: '#bsas ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		}
	});
	$('#slider_contacto_medellin').cycle({
		fx: 'fade',
		timeout: 0,
		pager: '#medellin ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		}
	});			
	$('#slider_contacto_cordoba').cycle({
		fx: 'fade',
		timeout: 0,
		pager: '#cba ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		}
	});			
	 $('#slider_contacto_miami').cycle({
		fx: 'fade',
		timeout: 0,
		pager: '#miami ul',
		pagerAnchorBuilder: function(idx) { 
			return null;
		}
	});
	 
	$('#letra').cycle({ 
		fx:     'fade', 
		timeout: 0, 
		speed: 1000
	});
	
    //goToArea('#menu_nav_1', 'section#featured', 'easeInOutQuint');

	

};

function goToPortfolio(slide_start){
	//$('#workgallery').cycle('stop');
	goToArea('.menu_nav_3', 'section#works', 'easeInOutQuint',3);
	
	$('#workgallery').css('top',$('section#works').offset().top);  
	
	
	$("body").css("overflow", "hidden");
	
	$('#workgallery').show('slow',function(){
  		$('.sliderPortfolio').cycle({ 
			startingSlide: slide_start,
			fx:     'fade', 
			timeout: 9000, 
			next:   '#nextPortfolio', 
			prev:   '#prevPortfolio',
			easing:  'easeInOutQuad',
			speed: 2000,		
			pager: 'div#footfeatures ul',
			pagerAnchorBuilder: function(idx) { 
				return null;
			} 
			
		});  			
	}); 

	return false;
};


$(document).ready(function() {
						   
					   
	// Function para navegar verticalmente.
			
		
		///////
		
		////////////////////////////
			/// INICIO Menu Superior		
			
			$('.menu_nav_home').click(function(){		return goToArea('.menu_nav_home', 'section#home', 'easeInOutQuint',0);  });
			$('.menu_nav_0').click(function(){			return goToArea('.menu_nav_0', 'section#home', 'easeInOutQuint',0);  });
			$('.menu_nav_1').click(function(){			return goToArea('.menu_nav_1', 'section#featured', 'easeInOutQuint',1);  });
			$('.menu_nav_2').click(function(){			return goToArea('.menu_nav_2', 'section#about', 'easeInOutQuint',2);		});	
			$('.menu_nav_3').click(function(){			return goToArea('.menu_nav_3', 'section#works', 'easeInOutQuint',3);	   	});	
			$('.menu_nav_4').click(function(){			return goToArea('.menu_nav_4', 'section#news', 'easeInOutQuint',4);	  	});	
			$('.menu_nav_5').click(function(){			return goToArea('.menu_nav_5', 'section#contacto', 'easeInOutQuint',5);	});		
			
			/// FIN  Menu Superior
		//////////////////////////	
			
		//////////////////////////
			///INICIO Menu Home
			
			$('.whoweare').mouseover(function(){ $('#whoweare_span').css({backgroundPosition:"0 -146px"});  });
			$('.whoweare').mouseout(function(){ $('#whoweare_span').css({backgroundPosition:"0 0"});  });	
			$('.whoweare').click(function(){ return goToArea('.menu_nav_2', 'section#about', 'easeInOutQuint',2); });	
			
			$('.whatwedo').mouseover(function(){ $('#whatwedo_span').css({backgroundPosition:"0 -146px"});  });
			$('.whatwedo').mouseout(function(){ $('#whatwedo_span').css({backgroundPosition:"0 0"});  });	
			$('.whatwedo').click(function(){ return goToArea('.menu_nav_1', 'section#featured', 'easeInOutQuint',1);	 });	
			
			$('.outoffices').mouseover(function(){ $('#outoffices_span').css({backgroundPosition:"0 -146px"});  });
			$('.outoffices').mouseout(function(){ $('#outoffices_span').css({backgroundPosition:"0 0"});  });	
			$('.outoffices').click(function(){ return goToArea('.menu_nav_5', 'section#contacto', 'easeInOutQuint',5); });	
			
			///FIN Menu Home	
		////////////////////////	
		
		
		
		

		
		
		$(".iframe").fancybox({
		'overlayColor' : '#000' ,
		'overlayOpacity'	:	0.8,
		'scrolling' : 'no'
		
		});

		////////////////
		// animaciones inicio
		if(callSeccion != 1){
			$('body').delay(1000).fadeIn(1000);
			$('header').delay(2000).fadeIn(1000);
			$('footer').delay(2000).fadeIn(1000);
			$('section#home').delay(3000).fadeIn(1000);
			$('section').delay(3000).fadeIn(100);
			$('#preloading').delay(6000).fadeOut(1000);
			setTimeout("iniciar_sliders();",3000);
		} else{
			$('body').show();
			$('header').show();
			$('footer').show();
			$('section#home').show();
			$('section').show();
			$('#preloading').hide();
			fixSizeWindow();
			iniciar_sliders();
		};
		
		
		/// detectar en que seccion esta segun el scroll
		function isScrolledIntoView(elem){
   			 var docViewTop = $(window).scrollTop();
 			 var docViewBottom = docViewTop + $(window).height();
	
		    var elemTop = $(elem).offset().top+300;
 			var elemBottom = elemTop + $(elem).height()-300;

 			return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
		}
		
		var lastSeccion = '' ;
		
		$(window).scroll(function() {
			if(isScrolledIntoView('section#home')== true && lastSeccion != 'section#home'){
				lastSeccion = 'section#home';
				setMenuSeccion('.menu_nav_0', 'section#home', 'easeInOutQuint',0);
			};
			if(isScrolledIntoView('section#featured')== true && lastSeccion != 'section#featured'){
				lastSeccion = 'section#featured';
				setMenuSeccion('.menu_nav_1', 'section#featured', 'easeInOutQuint',1); 
			};
										
			if(isScrolledIntoView('section#about')== true && lastSeccion != 'section#about'){
				lastSeccion = 'section#about';
				setMenuSeccion('.menu_nav_2', 'section#about', 'easeInOutQuint',2);
			};
			
			if(isScrolledIntoView('section#works')== true && lastSeccion != 'section#works'){
				lastSeccion = 'section#works';
				setMenuSeccion('.menu_nav_3', 'section#works', 'easeInOutQuint',3);
			};
			
			if(isScrolledIntoView('section#news')== true && lastSeccion != 'section#news'){
				lastSeccion = 'section#news';
				setMenuSeccion('.menu_nav_4', 'section#news', 'easeInOutQuint',4);
			};	
			
			if(isScrolledIntoView('section#contacto')== true && lastSeccion != 'section#contacto'){
				lastSeccion = 'section#contacto';
				setMenuSeccion('.menu_nav_5', 'section#contacto', 'easeInOutQuint',5);
			};			
		
		
		
		});

});




