$(
	function(){

		//Hide (Collapse) the toggle containers on load
		$(".toggle_container").hide();
		$(".toggle_blog_container").hide();			
		
		//Hide (Collapse) the fake container for footer on load
		$("#fake_content").height("900px");			
		$("#fake_content").hide();			
	
		// Fade in contentWrapper (first hidden through css)
		//$(".fade_page_content").fadeIn("fast");
	
		// In career stories in Join us page, the first story is selected by default
		$("#story_1").addClass('selected');
		
		// Add fade trigger to the pagination plugin links
		$(".wp-paginate a").addClass('blog_fade_trigger');
		
		$(".blog_fade_trigger").click(function(event){
			$(".blog_post").animate({"opacity":0}, "fast");
			return true;
		});
		
		$(".front_trigger").click(function(event){
			event.preventDefault();
			var t = $(event.target);
			
			$(".header_image").slideUp("slow", 
				function(){
					location.href = t.attr("href");						
				}
			);
			$(".descriptions").slideUp("slow");
			$("#fake_content").slideDown("slow");
			$("#menu_fade_background").fadeIn("slow");
			
			return false;
		});
		
		
		$(".fade_out_trigger").click(function(event){
			
			var digits = $.browser.version.split(".");
			
			if ($.browser.msie) {
				// && digits[0] == "7"
				return true;
			}
			else {
				/*
				$(".fade_page_content").animate({
					"opacity": 0
				}, "fast");
				*/
				$(".fade_page_content").fadeTo("fast", 0);
				return true;
			}
		});

		$('#navigation li a').mouseover(function(){
			var me = $(this);
			if($('#navigation #' + me.attr('id') + '_desc').length > 0){
				me.addClass("fpactive");
				$('#navigation #' + me.attr('id') + '_desc').addClass("fpactive");
			}
		}).mouseout(function(){
			var me = $(this);
			if($('#navigation #' + me.attr('id') + '_desc').length > 0){
				me.removeClass("fpactive");
				$('#navigation #' + me.attr('id') + '_desc').removeClass("fpactive");
			}
		});

		$('#navigation .descriptions a').mouseover(function(){
			var me = $(this);
			var me_id = me.attr('id').replace("_desc", "");
			if($('#navigation li #' + me_id).length > 0){
				me.addClass("fpactive");
				$('#navigation li #' + me_id).addClass("fpactive");
			}
		}).mouseout(function(){
			var me = $(this);
			var me_id = me.attr('id').replace("_desc", "");
			if($('#navigation li #' + me_id).length > 0){
				me.removeClass("fpactive");
				$('#navigation li #' + me_id).removeClass("fpactive");
			}
		});

		
		// Career story navigation buttons
		for(var i = 1; i <= $('#career_story_navigation div').length; i++){
			$("#story_" + i).click(function(event){
				event.preventDefault();
				
				// Extract which story to show from navi element id-attribute
				var targ = $(event.target);
				var thisInd = targ.attr("id").charAt(targ.attr("id").length - 1);
				
				// Adjust the text block height to fit story
				$('#storycarousel_wrap').animate({"height" : $('#story_content_' + thisInd).height()});
				
				// Scroll to right story
				$('#storycarousel_wrap2').animate({"left": ((thisInd-1) * -566) + "px"});
				
				// Highlight the correct navi button
				changeStoryNaviSelection(thisInd);
			});
		}
	
	
		// Adjust the career stories text block to accommodate the height of the 1st story
		var carousel = $('#storycarousel_wrap');
		if(carousel.length >= 1){
			var s1 = $('#story_content_1');
			var h = s1.height();
			carousel.css("height", h);
		}
	
		// Show more contacts
		$("#show_all_staff").click(function(event){
			event.preventDefault();
			$("#more_contacts").slideToggle("slow");
			$(this).toggleClass('show_less_staff');
			});
		
		//Toggle showing more content
		$("div.trigger").click(function(event){
			var me = $(this);
			var thisid = me.attr("id");
			if(me.is(".active")){
				$.bbq.pushState("-", 2);	
			}else{
				$.bbq.pushState("-" + thisid, 2);	
			}
			
			event.preventDefault();
			me.toggleClass("active").next(".toggle_container").slideToggle("slow");
			});
	
		// Show more of individual blog post
		$(".more_link").click(function(event){
			event.preventDefault();
			$(this).toggleClass("active").prev(".toggle_blog_container").slideToggle("slow");
			
			$(this).toggleClass('less_link');				
			});
	
			if($('#meetus_carousel').length > 0){
				$('#meetus_carousel').cycle({
					speed: 2000,
					pause: 1,
					timeout: 7000
				});	
			}


		/* direct hashtag links to job listings! */
		if($.param.fragment().length > 1){
			var frag = $.param.fragment();
			if(frag.substr(0, 1) == "-"){
				frag = frag.replace("-", "");
				var me = $('#' + frag);
				if(me.length > 0){
					$(this).scrollTop(me.position().top);
					me.toggleClass("active").next(".toggle_container").slideDown("fast");
				}
			}
		}
		

	}
);		
		
			
/* Function for changing the focus of the career story navi */			
function changeStoryNaviSelection(ind){
	for(var i = 1; i <= $('#career_story_navigation div').length; i++){
		var thisElement = $('#story_' + i);
		if(thisElement.length <= 0) return;
		
		if(i == ind){
			thisElement.addClass('selected');
		}else{
			thisElement.removeClass('selected');
		}
	}
	return true;		
}


