$(document).ready(function() {

// Global - Add extra markup to the DOM
	$("<div id='wam_home_bg'></div>").appendTo("#wam_home #site_wrapper");


// Home - Slideshow for each section
	$("#buildings ul.photos").cycle({
        fx:      'fade',
        timeout:  6000,
        random:   1
    });
 
    $("#environmental ul.photos").cycle({
        fx:      'fade',
        delay:	  2000,
        timeout:  6000,
        random:   1
    });	
    

// 	Careers - Get story param from url and sets startingSlide below
	var currStory = getQueryVariable("sid");
	
	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
				if (pair[0] == variable) {
					return pair[1];
				}
			} 
	}
		

// Careers - Changes the current story
	$('#story #content').cycle({ 
	    fx:			'scrollUp',
	    speed:		700,
	    timeout:	0,
	    cleartype:  1,
	    startingSlide:	currStory-1,
	    pager:		'#more_reasons',
	    next:		'a.next',
	    easing:		'easeInOutBack',
	    pager:		'ul#more_reasons',
	    after:	updateSlide	
	});
    
	function updateSlide() {  
		var relInfo=$(this).attr("rel");
		var parts=relInfo.split(" ");
    	$('#sidebar_image img').attr('src',parts[0]); 
    	$('#story_bg').css('background-image', 'url('+parts[1]+')'); 
    }
    

// Experts - Animate/show the selected expert's bio  
	$('#experts #content_right').hide();

	$("ul#experts_list li a").click(function(){
		$("#experts #content_left").animate( { width:"585px"}, "fast");
		$("#experts #content_left #experts_list_wrapper").animate( { width:"585px"}, "fast");
		$("#experts #content_left ul#experts_list").animate( { width:"573px"}, "fast");
		$("#experts #content_right").fadeIn().animate( { width:"269px"}, "fast");
	});

});


// New project thumbnail scroller 
function initGalleryScroller() {

	$(function() {
		$('#slideshow').cycle({ 
			fx:     'fade', 
	   		speed:  '300', 
	   		timeout: 0,
	   		after:   onAfter,
	   		pager:  '#gallery', 
	   		prev:  '#prev_button', 
	   		next:  '#next_button',
	   		nowrap: 1, 
		    pagerAnchorBuilder: function(idx, slide) { 
		        // return selector string for existing anchor 
		        return '#gallery li:eq(' + idx + ') a'; 
		    } 
		});
		
	     function onAfter(curr, next, opts) {
		     var index = opts.currSlide;
		     var pid = $('#li_'+index).attr("pid");
		     var ptype = $('#gallery').attr("ptype");
		     $('#prev_button')[index == 0 ? 'hide' : 'show']();
		     $('#next_button')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		     
		 	updatePhoto(ptype, pid);
		 }	

		$('a#next_button').click(function(){
			$("#experts #gallery_wrapper").stop().scrollTo( '+=65', 500, {axis:'x'} );
		});
		
		$('a#prev_button').click(function(){
			$("#experts #gallery_wrapper").stop().scrollTo( '-=65', 500, {axis:'x'} );
		});

		// Calculate the width of the ul#gallery
			 var galleryChildren = $('ul#gallery').children().size();
			 var itemWidth =  65;
			 var galleryWidth = galleryChildren * itemWidth;	
						
			 $('#experts ul#gallery').css('width', galleryWidth);
		});
	}
	initGalleryScroller();


// Emergency view
function doEmergency() {
	$("#emergency_window").show();
	$(".emergency_overlay").show();
	$("a.window_close").show();
	 $("#projects embed#rwdi_swf").hide();
}

$(function(){
	$("a#emergency").click(function(){
		doEmergency();
	});
	
	$(".emergency_overlay, a.window_close").click(function(){
		$(".emergency_overlay").hide();
		$("#emergency_window").fadeOut("fast");
		$("a.window_close").hide();
		$("#projects embed#rwdi_swf").show();
	});
});
