$(document).ready(function(){

	$current = "who-we-are";
	$hovering = false;
	
	//Adding <span> tag to menu
	if (!$.browser.msie){
	
	$("ul#menu li a").each(function(){
		
			$(this).append("<span class='hover' />");						
					
	});
	}

	//Fix the PNG IE issue
	$("img.transparent, #container, h2, #footer a, #footer p, #end_container").pngfix();
	
	function loadContent(class_name,pointer_name){
	



				
		if($current!=class_name){	
		
			$('#container h2').fadeTo(200,0.0,function(){
			
				$('#text').fadeTo(500, 0.0, function(){	
					
					$('#content').prepend('<span id="load">LOADING...</span>');  
					$('#load').css("display","block").fadeIn('normal');  
					$('body').removeClass().addClass(class_name);
					$('#container h2').removeClass().addClass(pointer_name)
									  .fadeTo(200,1.0,function(){
					
						$('#text').load("html/"+ class_name +'.html div#text >','',function(){
							
							$("#load").fadeOut("normal", function(){
								$(this).remove();
								$("#text").fadeTo(800,1.0);
								if (pointer_name=="what")
										activateScroll();
								
							});

							
							});

						$hovering = false;

					});						
				});
			});
			class_name = class_name.substr(5,class_name.length-10);			
			$current = class_name;

		}
		
	}

	function hideAllBut(this_element){
		
		$hovering = true;
		$(' span.hover').each(
			
			function(){
			
			active_element = $(this_element)[0]
			if (this != active_element)
				$(this).stop().fadeTo(600,0);
		
		});
		
	}

	if (!$.browser.msie){
	//Fix for FF2 and IE6 (¿probably?)
	$('li#learn a').css('backgroundPosition',"0px 0px");
	$('li#how a').css('backgroundPosition', '-240px 0px');
	$('li#contact a').css('backgroundPosition', '-460px 0px');

}
	$("a#learn_more").click(function(){
		loadContent($(this).attr('href'),'what')
		return false;
	});

  	$('ul#menu li#learn a').click(function() { 
	
		var hash = $(this).attr('href');
		hash = hash.replace(/^.*#/, '');
		hash = hash.substr(0,hash.length-5);
		hideAllBut($(this).find("span.hover"));	
		loadContent($(this).attr('href'),'who');
//		window.location.hash = $(this).attr('href').substr(5,$(this).attr('href').length-5); 
		return false;
  	}); 

  	$('ul#menu li#how a').click(function() { 
		var hash = $(this).attr('href');
		hash = hash.replace(/^.*#/, '');
		hash = hash.substr(0,hash.length-5);
		hideAllBut($(this).find("span.hover"));
		loadContent($(this).attr('href'),'what')
		return false;
  	}); 

  	$('ul#menu li#contact a').click(function() { 
		var hash = $(this).attr('href');
		hash = hash.replace(/^.*#/, '');
		hash = hash.substr(0,hash.length-5);
		hideAllBut($(this).find("span.hover"));
    	loadContent($(this).attr('href'),'contact')
		return false;
  	}); 

	
	function isThisActive(element)
	{
			var name = $(element).parent().attr("id");
			var body_name = $("body").attr("class");

			if ($hovering){ return true; }

				if ((name =="learn")&&(body_name =="who-we-are"))
					return true;

				if ((name =="how")&&(body_name =="what-we-do"))
					return true;

				if ((name =="contact")&&(body_name =="contact-us"))
					return true;

			return false;
	}
		

	if (!$.browser.msie){
	
	$('.hover').parent().hover( 
		
		function(){
			if (!isThisActive(this)){

				$(".hover", this).stop().animate({ "opacity": 1 },600);		
			}

		}
		
		,

		function(){
			if(!isThisActive(this)){
				$(".hover", this).stop().animate({ "opacity": 0 },600);		
			}

		}
	
	);
}


	//Animaciòn Formulario
	$("#content").listen('focus','input, textarea',function(){ 
			
			$(this).addClass("active");
		
		});
		
	$("#content").listen('blur','input, textarea', function(){
			
			$(this).removeClass("active");
			
		})	
	




  	var fadeSpeed = $.browser.safari ? 600 : 900;	

	
	//Animación de entrada sutil
	if (!$.browser.msie){

	$('.hover').css("opacity",0);
	$('body.who-we-are h3#who').fadeTo(0,0.0);
	$('body.who-we-are div#content a#learn').fadeTo(0,0.0);
	$('body.who-we-are #wrapper').css("opacity",0).fadeTo(1500,1.0,function(){
		$("li#learn span.hover").css("opacity",1);	
		$('h3#who').fadeTo('slow', 1.0, function(){

			$('div#content a#learn').fadeTo('slow', 1.0, function(){
						
			});			
		});
	});
	}
	else 
	{
//		$('body.who-we-are h3#who').css("opacity",0);
//		$('body.who-we-are div#content a#learn').css("opacity",0);
$('.hover').hide();		
$('body.who-we-are #wrapper').show("slow", fadeSpeed);
	
	//,function(){
	//	$("li#learn span.hover").css("opacity",1).fadeIn().fadeTo(0,1);			
	//	$('div#container').css("opacity",1).fadeIn().fadeTo(0,1);


/*  This works.. kinda
		$('body.who-we-are #wrapper').animate({ "opacity": 1, "display" : "visible"  }, fadeSpeed,function(){
			$("li#learn span.hover").css("opacity",1).fadeIn().fadeTo(0,1);			
			$('div#container').css("opacity",1).fadeIn().fadeTo(0,1);
*/
			
    	


	//	});
		
	};
	
	function activateScroll(){
	var $panels = $('#apart .scrollContainer > p');
	var $panels_works = $('#works .scrollContainer > div');
	var $container = $('#apart .scrollContainer');
	var $container_works = $('#works .scrollContainer');

	// if false, we'll float all the panels left and fix the width 
	// of the container
	var horizontal = true;
	// float the panels left if we're going horizontal
	if (horizontal) {
	  $panels.css({
	    'float' : 'left',
	    'position' : 'relative' // IE fix to ensure overflow is hidden
	  });

	  $panels_works.css({
	    'float' : 'left',
	    'position' : 'relative' // IE fix to ensure overflow is hidden
	  });


	  // calculate a new width for the container (so it holds all panels)
	  $container.css('width', $panels[0].offsetWidth * ($panels.length + 1));
	  $container_works.css('width', $panels_works[0].offsetWidth * ($panels_works.length + 1));	
	}
	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $('#apart .scroll').css('overflow', 'hidden');
	var $scroll_works = $('#works .scroll').css('overflow', 'hidden');	

	// apply our left + right buttons
	$scroll
	  .before('<img class="scrollButtons left apart" src="images/previous.png" alt="Previous reasons" title="Previous reason" />')
	  .after('<img class="scrollButtons right apart" src="images/next.png" alt="More reasons" title="More reasons" />');

	$scroll_works
	  .before('<img class="scrollButtons left works" src="images/previous.png" alt="Previous Work" title="Previous work" />')
	  .after('<img class="scrollButtons right works" src="images/next.png" alt="Next Work" title="Next Work" />');
	
	// offset is used to move to *exactly* the right place, since I'm using
	// padding on my example, I need to subtract the amount of padding to
	// the offset.  Try removing this to get a good idea of the effect
	var offset = parseInt((horizontal ? 
	  $container.css('paddingTop') : 
	  $container.css('paddingLeft')) 
	  || 0) * -1;


	var scrollOptions = {
	  target: $scroll, // the element that has the overflow
	  // can be a selector which will be relative to the target
	  items: $panels,
	//  navigation: '.navigation a',
	  // selectors are NOT relative to document, i.e. make sure they're unique
	  prev: 'img.left.apart', 
	  next: 'img.right.apart',
	  // allow the scroll effect to run both directions
	  axis: 'x',
	  //onAfter: trigger, // our final callback
	  offset: offset,
	  // duration of the sliding effect
	  duration: 500,
	  // easing - can be used with the easing plugin: 
	  // http://gsgd.co.uk/sandbox/jquery/easing/
	  easing: 'swing'
	};
	
	var scroll_worksOptions = {
	  target: $scroll_works, // the element that has the overflow
	  // can be a selector which will be relative to the target
	  items: $panels_works,
	//  navigation: '.navigation a',
	  // selectors are NOT relative to document, i.e. make sure they're unique
	  prev: 'img.left.works', 
	  next: 'img.right.works',
	  // allow the scroll effect to run both directions
	  axis: 'x',
	  //onAfter: trigger, // our final callback
	  offset: offset,
	  // duration of the sliding effect
	  duration: 500,
	  // easing - can be used with the easing plugin: 
	  // http://gsgd.co.uk/sandbox/jquery/easing/
	  easing: 'swing'
	};
	// apply serialScroll to the slider - we chose this plugin because it 
	// supports// the indexed next and previous scroll along with hooking 
	// in to our navigation.
	$('#apart').serialScroll(scrollOptions);
	$('#works').serialScroll(scroll_worksOptions);	

	// now apply localScroll to hook any other arbitrary links to trigger 
	// the effect
	$.localScroll(scrollOptions);
	$.localScroll(scroll_worksOptions);	
	}
});
