$(document).ready(function() {
	
	//initMap("small-map-canvas",false,true);
	//initMap("large-map-canvas",true,false);
	stripe();
});
////////////////////////////////////////////////////////////
// Add row stripes to table with class="stripe"
// Params:
//        none
////////////////////////////////////////////////////////////

var stripe = function(){
	$('.stripe').each(function(i,e){
	    $(e).find('li:odd').addClass('alt')
	});
}

////////////////////////////////////////////////////////////
// Initialize location map
// Params:
//        mapId(String) Id of map canvas div
//        showInfowindow(bool) Should clicking the map 
//                             marker cause the display of 
//                             the location info window; 
//                             False will take the user to
//                             the location page on click
//        recenter(bool) Should map be centered on the 
//                       selected item
////////////////////////////////////////////////////////////
var initMap = function(mapId,showInfoWindow,recenter) {
  var initialLocation = new google.maps.LatLng(44.166444664458595,-88.4619140625);
  var mapOptions = {
    zoom: 4,
    center: initialLocation,
    disableDefaultUI: true,
    maxWidth:150,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById(mapId), mapOptions);
  var contentString = new Array(); 
  $("#locations li").each(function(item){	
    var locationLink = $(this).children("div").children(".location-link").attr("href");
	if(recenter==true && $(this).attr("selected")=="true"){
	  map.setCenter(new google.maps.LatLng($(this).attr("lat"),$(this).attr("lon")));
	}
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng($(this).attr("lat"),$(this).attr("lon")), 
      map: map, 
      title: $(this).children("div").children("span").html()
    });
    if(showInfoWindow == true){
      contentString[item] = $(this).html();
      infowindow = new google.maps.InfoWindow({
        content: contentString[item]
      });
    }
    google.maps.event.addListener(marker, 'click', function() {
      if(showInfoWindow == true){  
        infowindow.close();
        infowindow.setContent(contentString[item]);
        infowindow.open(map,marker);
      }else{
        location.href=locationLink;
      }
    });
  });
}

////////////////////////////////////////////////////////////
// Initialize School Selector
// Params:
//        none
////////////////////////////////////////////////////////////
var initSchoolSelector = function(){
  $(".droparrow").click(function(){
    $(".school-selector-field p a").toggleClass("open");
    $(".school-selector-list").toggle();
  });
  $(".school-selector-list a").click(function(e){
    e.preventDefault();
    $("#school-content").load("/html/parts/"+$(this).attr("school")+".html",null,function(response){
      $(".school-selector-field p a").toggleClass("open");
      $(".school-selector-list").toggle();
    }); // abstract path and ext for release
  });
}

////////////////////////////////////////////////////////////
// Initialize Story module
// Params:
//        none
////////////////////////////////////////////////////////////
var initStory = function(){
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
			.mousedown(function() {	scrollbar.width( handleHelper.width() );})
			.mouseup(function() {scrollbar.width( "100%" );})
			.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
			.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({width: handleSize,"margin-left": -handleSize / 2});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
}

////////////////////////////////////////////////////////////
// Initialize Job Search
// Params:
//        control(string) Relevant search control id
//        text(test) Watermark text to be displayed in the 
//                   control; Prompt
////////////////////////////////////////////////////////////
var initJobSearch = function(control,text){
  $("#"+control).defaultvalue(text);
}

////////////////////////////////////////////////////////////
// Initialize USA and Canada Hero module
// Params:
//        none
////////////////////////////////////////////////////////////
var initUSAHero = function() {
	//preload images
	$.loadImages(['/Careers/images/usa_hero_dale.png', '/Careers/images/usa_hero_maria.png', '/Careers/images/usa_hero_tim.png', '/Careers/images/usa_hero_bg3.jpg'], function(){
	
	  //hide preloader
	  $('#usa-hero .usa-hero-loader').hide();
	  
	  //set up elements for animation/interaction
	  var strokeTop = $('#usa-hero span.usa-hero-ruleN');
	  var strokeBottom = $('#usa-hero span.usa-hero-ruleS');
	  var copyBar = $('#usa-hero div.usa-hero-copy-wrap');
	  var copyQuote = $('#usa-hero div.usa-hero-copy-wrap blockquote');
	  var copyCite = $('#usa-hero div.usa-hero-cite');
	  var miniMap = $('#usa-hero span.usa-hero-map');
	  var usaHeroNavItem = $('#usa-hero .usa-hero-nav a');
	  
	  //set flags
	  var currentSlide = '#slide1';
	  var hasAnimationRun = false;
	  
	  //show first slide 
	  $('#usa-hero #slide1').fadeIn(400);
	  
	  //run animation        
	  if (!hasAnimationRun) {
		  strokeTop.animate({marginLeft: ['0px', 'easeInCirc']}, 1000,
			  function() {
				  copyBar.animate({opacity: 1, marginLeft: ['0px', 'easeInCirc']}, 1000,
					  function() {
						  copyQuote.animate({opacity: 1}, 800);
						  copyCite.animate({opacity: 1}, 800);
						  miniMap.animate({opacity: 1}, 800);
						  $('.usa-hero-nav').fadeIn(900);
					  }
				  );
			  }
		  );
		  strokeBottom.animate({marginLeft: ['0px', 'easeInCirc']}, 1000);
	  
		  hasAnimationRun = true;
	  };
			   
	  //navigation interaction         
	  usaHeroNavItem.click(function(e) {
		  e.preventDefault();
		  usaHeroNavItem.removeClass('current');
		  $(this).addClass('current');
		  
		  var intendedSlide = $(this).attr('href');
		  
		  if (currentSlide != intendedSlide) {
			  $(currentSlide).fadeOut(400);
			  $(intendedSlide).fadeIn(400);
			  currentSlide = intendedSlide;
		  }    
	  });

	});
}

////////////////////////////////////////////////////////////
// Initialize Careers Hero module
// Params:
//        none
////////////////////////////////////////////////////////////
var initCareersHero = function() {

	$.loadImages(['/Careers/images/careers_hero_talent.png', '/Careers/images/careers_hero_map.jpg', '/Careers/images/careers_hero_smap.png'], function(){
		
		//hide loader gif
		$('#careers-hero .careers-hero-loader').hide();
		
		//show hero
		$('#careers-hero .careers-hero-wrap').fadeIn(400);
		
		var strokeTop = $('#careers-hero span.careers-hero-copy-ruleN');
		var strokeBottom = $('#careers-hero span.careers-hero-copy-ruleS');
		var copyBar = $('#careers-hero div.careers-hero-copy');
		var copyText = $('#careers-hero div.careers-hero-copy-wrap');
		var miniMap = $('#careers-hero img.careers-hero-map');

		strokeTop.animate({left: ['0px', 'easeInCirc']}, 1000, 
			function() {
				copyBar.animate({left: ['0px', 'easeOutCirc'], opacity: 1}, 1000, 
					function() {
						miniMap.animate({opacity: 1}, 800, 
							function() {
								copyText.animate({opacity: 1}, 800);
							}
						);
					}
				);
			}  
		);
		strokeBottom.animate({left: ['0px', 'easeInCirc']}, 1000);
	});
};

