function toggleDisp( obj, OnOff ) {
  $( obj ).css( {
    "display" : (OnOff ? "block" : "none")
  } );
}

function toggleVis( obj, vis ) {
  $( obj ).css( {
    "visibility" : (vis ? "visible" : "hidden")
  } );
}

function layoutHeight( obj ) {
  return $( obj ).height() + parseInt( $( obj ).css( "padding-top" ) ) + parseInt( $( obj ).css( "padding-bottom" ) ) + parseInt( $( obj ).css( "margin-top" ) ) + parseInt( $( obj ).css( "margin-bottom" ) );
}

$( document ).ready( function() {
  initUI();
} );

//image preloader plugin
$.fn.preload = function() {
  this.each( function() {
    $( "<img />" )[0].src = this;
  } );
}

function initUI() {
  initHovers();
  initClicks();
  //preload contact map images
  if ( $( "#contacts_box" ).length > 0 ) {
    $( ["/images/us_map_az.png", "/images/us_map_ca.png", "/images/us_map_id.png", "/images/us_map_mo.png", "/images/us_map_ny.png", "/images/us_map_or.png", "/images/us_map_wa.png", "/images/us_map_il.png"] ).preload();
  }
  externalLinks();

  if ( $( "body" ).customScroller ) {
    $( ".contacts_modal_content.overview" ).customScroller();
    $( ".contacts_modal_content.career" ).customScroller();
    $( ".contacts_modal_content.people" ).customScroller();
    $( ".contacts_modal_content.culture" ).customScroller();
    $( ".contacts_modal_content.education" ).customScroller();
    $( ".contacts_modal_content.relationships" ).customScroller();
    $( ".contacts_modal_content.area" ).customScroller();
    $( "#proserv_content" ).customScroller();
    $( ".contacts_modal_content.bios" ).customScroller();
  }
  aboutMinHeightFix();
  modalHeightFix();
}

function homeFader(current){

  var total = $(".home_fading_image").size();
  var next = (current + 1) % (total);

  $("#home_fading_image_img_"+next).fadeIn(1000, function(){ });
  $("#home_fading_image_img_"+current).fadeOut(1000, function(){setTimeout("homeFader( "+next+" );", 10000);});

  /*var childrens = $("#pageId-1 div#main_wrapper div#outter_wrapper div#inner_wrapper").children();
  childrens.each(function(){
      $(this).css({"z-index":200});
  });
  $(".home_fading_image").css({"z-index":10});*/

  //setTimeout("homeFader();", 10000);
}

function mycarousel_checkArrows( recursiveBool ) {
  if ( $( ".jcarousel-next" ).hasClass( "jcarousel-next-disabled" ) ){
    $( "#carousel_nav_right" ).addClass( "carousel_nav_disabled" );
    $( "#carousel_nav_right" ).fadeTo( 200, 0, function() {
      $( this ).css( {
        "cursor" : "default"
      } );
    } );
  }else if ( $( "#carousel_nav_right" ).hasClass( "carousel_nav_disabled" )) {
    $( "#carousel_nav_right" ).removeClass( "carousel_nav_disabled" );
    $( "#carousel_nav_right" ).fadeTo( 200, 1, function() {
      $( this ).css( {
        "cursor" : "pointer"
      } );
    } );
  }
  if ( $( ".jcarousel-prev" ).hasClass( "jcarousel-prev-disabled" ) ){
    $( "#carousel_nav_left" ).addClass( "carousel_nav_disabled" );
    $( "#carousel_nav_left" ).fadeTo( 200, 0, function() {
      $( this ).css( {
        "cursor" : "default"
      } );
    } );
  }else if ( $( "#carousel_nav_left" ).hasClass( "carousel_nav_disabled" )) {
    $( "#carousel_nav_left" ).removeClass( "carousel_nav_disabled" );
    $( "#carousel_nav_left" ).fadeTo( 200, 1, function() {
      $( this ).css( {
        "cursor" : "pointer"
      } );
    } );
  }
  if ( recursiveBool == undefined ){
    setTimeout( function() {
      mycarousel_checkArrows( true );
    }, 500 );
  }
}

$( window ).load( function() {

  $( window ).resize( function() {
    $( "#timeline" ).height( $( document ).height() );
  } );

  //project carocell code
  function mycarousel_initCallback(carousel) {
    $('#carousel_nav_right').bind('click', function() {
      carousel.next();
      mycarousel_checkArrows();
      return false;
    });
    $('#carousel_nav_left').bind('click', function() {
      carousel.prev();
      mycarousel_checkArrows();
      return false;
    });
  };
  $('#projects_modal_carousel').jcarousel({
    initCallback: mycarousel_initCallback,
    visible: 6,
    scroll: 1
    //buttonNextHTML: null,
    //buttonPrevHTML: null
  });



  function mycarousel_initCallback2( carousel ) {
    $( "#timeline_next_button" ).live( "click", function() {
      carousel.next();
      return false;
    } );
    $( "#timeline_previous_button" ).live( "click", function() {
      carousel.prev();
      return false;
    } );
    $("#timeline_nav_item_wrapper").css({
      "display" : "inherit"
    } );
  };

  $( "#timeline_nav_item_wrapper_inner" ).jcarousel( {
    "initCallback" : mycarousel_initCallback2,
    "start" : $( ".timeline_nav_item_active" ).attr( "aid" ) - Math.floor( 29 / 2 ) + 1,
    "visible" : 29,
    "scroll" : 5,
    "buttonNextHTML" : null,
    "buttonPrevHTML" : null,
    "itemFallbackDimension" : 30
  } );
} );

var timeLineBits = Array(); // cuts down on load time.

function getActiveHistoryItem( pid ) {
  if ( timeLineBits[ "tl" + pid ] == undefined ) {
    $.ajax( {
      "data" : "pid=" + pid,
      "dataType" : "xml",
      "error" : function( xhr, stat, err ) {
      },
      "success" : function( dat, stat, xhr ) {
        var rowsReturned = $( dat ).find( "rowsReturned" ).text();
        timeLineBits[ "tl" + pid ] = Array();
        timeLineBits[ "tl" + pid ]["bg_img_src"] = $( dat ).find( "bg_img_src" ).text();
        timeLineBits[ "tl" + pid ]["box_title"] = $( dat ).find( "box_title" ).text();
        timeLineBits[ "tl" + pid ]["box_text"] = $( dat ).find( "box_text" ).text();
        timeLineBits[ "tl" + pid ]["year"] = $( dat ).find( "year" ).text();

        $( "#timeline_bg_img" ).css( {
          "background-image" : "url( '/tools/img_scaler.php?data=0|960|560|" + timeLineBits[ "tl" + pid ]["bg_img_src"] + "' )"
        } );
        $( "#timeline_info_title" ).html( timeLineBits[ "tl" + pid ]["box_title"] );
        $( "#timeline_info_date" ).html( timeLineBits[ "tl" + pid ]["year"] );
        $( "#timeline_info_copy" ).html( timeLineBits[ "tl" + pid ]["box_text"] );
      },
      "type" : "post",
      "url" : "/ajax/getActiveHistoryItem.php"
    } );
  } else {
    $( "#timeline_bg_img" ).css( {
      "background-image" : "url( '/tools/img_scaler.php?data=0|960|560|" + timeLineBits[ "tl" + pid ]["bg_img_src"] + "' )"
    } );
    $( "#timeline_info_title" ).html( timeLineBits[ "tl" + pid ]["box_title"] );
    $( "#timeline_info_date" ).html( timeLineBits[ "tl" + pid ]["year"] );
    $( "#timeline_info_copy" ).html( timeLineBits[ "tl" + pid ]["box_text"] );
  }
}

function initClicks() {

  $( ".timeline_box_item" ).live( "click", function() {
    getActiveHistoryItem( $( this ).attr( "rel" ) );

    var clicked_on_year = $( this ).attr( "id" ).replace( /y/, "" ).split( "|" )[0];
    var old_active_year = $( ".timeline_nav_item_active" ).attr( "id" ).replace( /y/, "" ).split( "|" )[0];
    $( ".timeline_nav_item_active" ).removeClass( "timeline_nav_item_active" ).addClass( "timeline_nav_item" ).css( {
      "background-image" : "none",
      "background-repeat" : "no-repeat"
    } ).children().replaceWith( '<img alt="' + old_active_year + '" src="/images/timeline_dot.png">' );
    $( this ).removeClass( "timeline_nav_item" ).addClass( "timeline_nav_item_active" ).children().replaceWith( '<div class="timeline_item_year">' + clicked_on_year + '</div>' );
  } );

  $( ".timeline_extra_img" ).click( function() {
    var img = $( this ).find( "img:first" );
    var url = $( img ).attr( "src" ).split( "|" );
    url = url[ url.length - 1 ];
    $( "#timeline_info_title" ).html( $( img ).attr( "title" ) );
    $( "#timeline_info_copy" ).html( $( img ).attr( "description" ) );
    $( "#timeline_bg_img" ).css( {
      "background-image" : "url( '/tools/img_scaler.php?data=0|960|560|" + url + "' )"
    } );
  } );

  // Home page fade
  $(".home_fading_image").fadeOut(0);
  $("#home_fading_image_img_0").fadeIn(0);
  $("#pageId-1 div#main_wrapper div#outter_wrapper div#inner_wrapper").css({"z-index":200, 'background-image': 'none'});
  setTimeout("homeFader(0);", 10000);

  //this is the close popup code
  $( "div.modal" ).mouseleave( function() {
    $( ".modal_wrapper" ).click( function() {
      if ( $( this ).hasClass( "single_serv" ) ) {
        window.location.replace( "/services-3" );
      } else if ( $( this ).hasClass( "single_proj" ) ) {
        window.location.replace( "/projects-4" );
      } else {
        $( ".modal_wrapper" ).css( {
        "margin-top" : "-10000px"
        } );
      }
    } );
  } );
  $( "div.modal" ).mouseenter( function() {
    $( ".modal_wrapper" ).unbind( "click" );
  } );
  $( ".modal_close" ).click( function() {
    $( "div.modal" ).mouseleave();
    $( ".modal_wrapper" ).click();
  });

  $( ".proserv_thumb_item" ).click( function() {
    var data = $( "img", this ).attr( "src" ).split( "|" );
    $( "#proserv_bg" ).attr( "src", "/tools/img_scaler.php?data=0|650|560|" + data[3] );
    var the_id = $( "img", this ).attr( "rel" );
    var code = $( "#cap_" + the_id ).html();
    if ( code != "" ) {
      $( "#image_caption" ).html( code );
      $( "#image_caption" ).css( {
        "display" : "block"
      } );
    } else {
      $( "#image_caption" ).css( {
        "display" : "none"
      } );
    }
  } );

  $(".contacts_thumb_item").click( function() { // hide/unhide photo's in modals.
// needs to crossfade.
// the centering-math is now obsolete due to img_scaler.
    var url = $( "img", this ).attr( "src").split( "|" );
    url = url[ url.length - 1 ];
    var theImgBox = $( this ).closest( ".contacts_modal_box_wrapper" ).find( ".contacts_modal_imgbox" );
    $( theImgBox ).children( "div" ).each( function() { // hide all the large photo's
      $( this ).css( {
        "visibility" : "hidden"
      } );
    } );
    var myTarget = $( theImgBox ).find( 'img[src*="/tools/img_scaler.php?data=0|650|560|' + url + '"]' )[0];
    $( myTarget ).css( { // targeting the img tag
      "top" : (560 < $( myTarget ).height() ? $( myTarget ).height() - 560 : 560 - $( myTarget ).height()) / -2 + "px",
      "left" : (650 < $( myTarget ).width() ? $( myTarget ).width() - 650 : 650 - $( myTarget ).width()) / -2 + "px",
      "position" : "absolute"
    } ); // without simplifying:  -1 * ( (photo height / 2) - (box height / 2) )  ::: with simp:  (A - B) / -2
    $( $( myTarget ).parent() ).css( {
      "visibility" : "visible"
    } );
  } );

  $( ".contacts_thumb_wrapper" ).each( function() { // hide contact_thumb_wrapper, if it has only 1 entry.
    if ( $( this ).find( "img" ).length < 2 ) {
      $( this ).css( {
        "visibility" : "hidden"
      } ); // may need "display" : "none"
    }
  } );

  // container generator -- image containers on modal's that hide/unhide
  setTimeout( function() { // pre loads images. also creates spaces to show/hide instead of live-swapping the src.
    $( ".contacts_thumb_wrapper" ).each( function() { // these objects are only on certain pages (contact-us, etc)
      var imgBox = $( this ).closest( ".contacts_modal_box_wrapper" ).find( ".contacts_modal_imgbox" );
      $( this ).find( "img" ).each( function() {
        var url = $( this ).attr( "src" ).split( "|" );
        url = url[ url.length - 1 ];
        var copy_text = $( this ).closest( ".contacts_thumb_item" ).find( ".img_copy_desc" ).text();
        $( imgBox ).append( '<div class="contacts_modal_imgboxes"><img class="img_box_img_loader" alt="" src="/tools/img_scaler.php?data=0|650|560|' + url + '" /><div class="img_copy_data" style="display:' + (copy_text.length > 0 ? "block" : "none") + '">' + (copy_text.length > 0 ? "&copy;&nbsp;" : "") + copy_text + '</div></div>' );
      } );
// code to tag the first container with a speical class, to use with xfade better.
    } );
  }, 900 ); // give markup & bakcgrounds a chance to handle itself first. these are low-priority pre-loading's

  $( "#logo" ).click( function() {
      window.location = "/";
  } );
  $( "#notable_events_button" ).click( function() {
    $( "#timeline.modal_wrapper" ).css( "margin-top", "0px" );
  } );
  $( "#notable_events_button" ).mouseenter( function() {
    $( this ).attr( "src", "/images/notable_events_button_on.png" );
  } );
  $( "#notable_events_button" ).mouseleave( function() {
    $( this ).attr( "src", "/images/notable_events_button.png" );
  } );

// about - awards
  var awards_ani_speed = 500;
  var awards_is_animating = false;
  if ( $( "#awards_item_wrapper" ).length > 0 ) {
    $( ["/images/awards_icon_minus.png"] ).preload();
  }
  $( ".awards_item_button" ).click( function() {
    if ( !awards_is_animating ) {
      awards_is_animating = true;
      var lag_on_the_blue = $( ".awards_item_button.opened" ).closest( ".awards_item" );
      var close_self = false;
      if ( $( this ).hasClass( "opened" ) ) {
        close_self = true;
      }
      $( ".awards_item_button" ).removeClass( "opened" );
      if ( !close_self ) {
        $( this ).addClass( "opened" );
      }
      $( ".awards_item" ).not( lag_on_the_blue ).css( {
        "background-image" : "url( '/images/transparent_px.png' )"
      } );
      if ( !close_self ) {
        $( this ).closest( ".awards_item" ).css( {
          "background-image" : "url( '/images/dark_blue_px.png' )"
        } );
      }
      if ( close_self ) {
        $( this ).closest( ".awards_item" ).find( ".awards_item_content" ).slideUp( awards_ani_speed, function() {
          $( lag_on_the_blue ).css( {
            "background-image" : "url( '/images/transparent_px.png' )"
          } );
          awards_is_animating = false;
        } );
      } else {
        $( ".awards_item_content" ).slideUp( awards_ani_speed );
        $( this ).closest( ".awards_item" ).find( ".awards_item_content" ).slideDown( awards_ani_speed, function() {
          $( lag_on_the_blue ).css( {
            "background-image" : "url( '/images/transparent_px.png' )"
          } );
          awards_is_animating = false;
        } );
      }
    }
  } );

// end about - awards

  $('#services_box_header_nav').click(function(){
      var navButtonSrc = $('#services_box_header_nav img').attr('src');
      if (navButtonSrc == '/images/expand_category_listing.png') {
          $('#services_box_header_nav img').attr('src', '/images/close_category_listing.png');
          $('#services_box_header_nav img').attr('alt', 'Close Category Listing');

          $("#services_cat_nav_next a").attr("href", $("#services_cat_nav_next a").attr("href") + "#open");
      $("#services_cat_nav_previous a").attr("href", $("#services_cat_nav_previous a").attr("href") + "#open");
      $(".services_cat_nav_pagination_item a").each(function()
      {
        $(this).attr("href", $(this).attr("href") + "#open");
      });

      } else {
          $('#services_box_header_nav img').attr('src', '/images/expand_category_listing.png');
          $('#services_box_header_nav img').attr('alt', 'Expand Category Listing');
          if($("#services_cat_nav_next a").attr("href").substr(-5) == "#open")
          {
        $("#services_cat_nav_next a").attr("href", $("#services_cat_nav_next a").attr("href").substr(0, $("#services_cat_nav_next a").attr("href").length - 5));
        $("#services_cat_nav_previous a").attr("href", $("#services_cat_nav_previous a").attr("href").substr(0, $("#services_cat_nav_previous a").attr("href").length - 5));
        $(".services_cat_nav_pagination_item a").each(function(){
          $(this).attr("href", $(this).attr("href").substr(0, $(this).attr("href").length - 5));
        });

      }
      }
      $('#projects_dropdown').slideToggle('500', function(){});
  });
  $('.modal_top_bar_left.projects').click(function(){
      if ( !$( "#the_projects_dropdown_modal_is_empty" ).length ){
        var navButtonSrc = $('.modal_top_bar_left.projects img').attr('src');
        if (navButtonSrc == '/images/expand_similar_projects.png') {
            $('.modal_top_bar_left.projects img').attr('src', '/images/close_similar_projects.png');
            $('.modal_top_bar_left.projects img').attr('alt', 'Close Similar Projects');
        } else {
            $('.modal_top_bar_left.projects img').attr('src', '/images/expand_similar_projects.png');
            $('.modal_top_bar_left.projects img').attr('alt', 'Expand Similar Projects');
        }
        $('#projects_modal_dropdown_wrapper').slideToggle('500', function(){});
        if ( $( ".jcarousel-item" ).length > 6 ){
          mycarousel_checkArrows();
        }
      }
  });

  var timelineViewMoreIsOpen = false;
  $('#timeline_view_more').click(function(){
      $('#timeline_modal_dropdown_wrapper').slideToggle('500', function(){});
      if(!timelineViewMoreIsOpen)
      {
      $(this).attr("src", "/images/modal_close_more_images.png");
      timelineViewMoreIsOpen = true;
    }
    else{
      $(this).attr("src", "/images/modal_view_more_images.png");
      timelineViewMoreIsOpen = false;
    }
  });

  /* locations map */
  //$('#us_map_ca').click(function(){ $('.state_box.california').css({ "display" : "block" }); });
  /* end loactions map */

  $('.contacts_modal_trigger').click(function(){
      //$('.contacts_modal.overview').css({"margin-top" : "0px"});
  });

  /* contact modal nav */

  $('.contacts_nav_overview').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.overview').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_career').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.career').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_team, #bio_to_team').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.team').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_team_culture').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.team_culture').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_ongoing_education').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.ongoing_education').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_relationships').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.relationships').css({"margin-top" : "0px"});
  });
  $('.contacts_nav_about_area').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.about_area').css({"margin-top" : "0px"});
  });
  $('.contacts_team_item_img img, .team_item_name').click(function(){
      $('.modal_wrapper').css({"margin-top" : "-10000px"});
      $('.contacts_modal.bios').css({"margin-top" : "0px"});
  });

  $( "#header_search_right" ).click( function() {
    $( "#header_search_input" ).val( $.trim( $( "#header_search_input" ).val() ) );
    if ( $( "#header_search_input" ).val() != "" ) {
      $("#header_search_form").submit();
    }
  } );
  /* people menu */
  $( "ul.people li span" ).click( function() {
    $( this ).siblings().slideToggle( 500, function() {
      $( window ).trigger( "resize" );
    } );
  } );

  $( ".people_menu_item" ).click( function() {
    var id = $( this ).attr( "id" ).split( "-" )[1];
    $.ajax( {
      "data" : "a=contacts_page&page=" + id,
      "dataType" : "xml",
      "error" : function( xhr, stat, err ) {
      },
      "success" : function( dat, stat, xhr ) {
        var img = $( "#mugshot" ).find( "img" )[0];
        $( img ).attr( "src", $( dat ).find( "mugshot" ).text() );
        $( img ).attr( "alt", $( dat ).find( "alt" ).text() );
        $( ".contacts_modal_title.bios" ).html( $( dat ).find( "name" ).text() );
        $( ".contacts_modal_subtitle.bios" ).html( $( dat ).find( "subtitle" ).text() );
        var bio = $( ".contacts_modal_content.bios" ).find( ".customScrollerContent" );
        if ( bio == null || bio == undefined ) { // if fail
          bio = $( ".contacts_modal_content.bios" );
          // $( bio ).customScroller();
          // and try again?
        }
        $( bio ).html( "" ); // clean up for next cycle.
        var info_label = $( dat ).find( "scrolltext" ).find( "infolabel" ); // class="location_info_label"
        var info_item = $( dat ).find( "scrolltext" ).find( "infoitem" ); // class="location_info_item"
        for ( x = 0; x < info_label.length; ++x ) { // they always come in pairs
          $( bio ).append( '<div class="location_info_label">' + $( info_label[ x ] ).text() + '</div><div class="location_info_item">' + $( info_item[ x ] ).text() + '</div>' );
        }
        $( ".contacts_modal_content.bios" ).css( { // fix for stupid IE
          "height" : $( "#contacts_modal_right_box" ).height() - $( ".contacts_modal_title.bios" ).height() - parseInt( $( ".contacts_modal_title.bios" ).css( "padding-top" ) ) - $( ".contacts_modal_subtitle.bios" ).height() - parseInt( $( ".contacts_modal_content.bios" ).css( "padding-top" ) ) - 20 + "px"
        } ); // the - 20 is the little gap at the bottom.
        $( window ).trigger( "resize" );
      },
      "type" : "post",
      "url" : "/controllers/ajax.php"
    } );
    $( "#contacts_modal_right_box.team" ).css( {
      "display" : "block"
    } );
  } );
}

function initHovers() {

  /* header nav on/off states */
  function navState( str ) { // 658px wide. except on about, and inovation.
    if ( $( "#nav_" + str ).attr( "src" ).match( /off/i ) != null ) {
      $( "#nav_" + str ).hover( function() {
        $( this ).attr( "src", "/images/nav_" + str + "_on.png" );
        if ( str != "services" && str != "projects" ) {
          $( "#menu_dropdown_wrapper" ).css( {
            "display" : "none"
          } );
        }
      }, function() {
        $( this ).attr( "src", "/images/nav_" + str + "_off.png" );
      } );
    }
  }

  navState( "about" );
  navState( "services" );
  navState( "projects" );
  navState( "innovation" );
  navState( "join" );
  navState( "contact" );
  /* END header nav on/off states */

  /* nav dropdown */
  initNavCallouts();
  $( "#menu_dropdown_wrapper" ).mouseleave( function() {
    $( "#menu_dropdown_wrapper" ).css( {
      "display" : "none"
    } );
  } );

  $( "#header_menu ul li" ).mouseleave( function() {
    var closeIt = true;
    $( "#menu_dropdown_wrapper, #header_menu ul li" ).mouseenter( function() {
      closeIt = false;
    } );
    setTimeout( function() {
      if ( closeIt ) {
        $( "#menu_dropdown_wrapper" ).css( {
          "display" : "none"
        } );
      }
    }, 350 );
  } );
  /* end nav dropdown */

  timelineDots();

  /* locations map */

    // california
    $('#us_map_ca').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_ca.png');
        $('.state_box.california').css({ "display" : "block" });
    });
    $('.state_box.california').mouseleave(function(){
        $('.state_box.california').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // oregon
    $('#us_map_or').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_or.png');
        $('.state_box.oregon').css({ "display" : "block" });
    });
    $('.state_box.oregon').mouseleave(function(){
        $(this).css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // washington
    $('#us_map_wa').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_wa.png');
        $('.state_box.washington').css({ "display" : "block" });
    });
    $('.state_box.washington').mouseleave(function(){
        $('.state_box.washington').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // idaho
    $('#us_map_id').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_id.png');
        $('.state_box.idaho').css({ "display" : "block" });
    });
    $('.state_box.idaho').mouseleave(function(){
        $('.state_box.idaho').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // arizona
    $('#us_map_az').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_az.png');
        $('.state_box.arizona').css({ "display" : "block" });
    });
    $('.state_box.arizona').mouseleave(function(){
        $('.state_box.arizona').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // missouri
    $('#us_map_mo').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_mo.png');
        $('.state_box.missouri').css({ "display" : "block" });
    });
    $('.state_box.missouri').mouseleave(function(){
        $('.state_box.missouri').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // new york
    $('#us_map_ny').mouseenter(function(){
        $('.state_box').css({ "display" : "none" });
        $('#us_map_base_image').attr('src', '/images/us_map_ny.png');
        $('.state_box.new_york').css({ "display" : "block" });
    });
    $('.state_box.new_york').mouseleave(function(){
        $('.state_box.new_york').css({ "display" : "none"});
        $('#us_map_base_image').attr('src', '/images/us_map.png');
    });

    // illinois
    $( "#us_map_il" ).mouseenter( function() {
      $( ".state_box" ).css( {
        "display" : "none"
      } );
      $( "#us_map_base_image").attr( "src", "/images/us_map_il.png" );
      $( ".state_box.illinois" ).css( {
        "display" : "block"
      } );
    });
    $( ".state_box.illinois" ).mouseleave( function() {
      $( ".state_box.illinois" ).css( {
        "display" : "none"
      } );
      $( "#us_map_base_image" ).attr( "src", "/images/us_map.png" );
    } );

    /* end locations map */

  /* contacts -> team -> gallery images */
  $( ".contacts_team_item_img img" ).hover( function() {
    $( this ).attr( "src", $( this ).attr( "src" ).replace( "bw", "color" ) );
  }, function() {
    $( this ).attr( "src", $( this ).attr( "src" ).replace( "color", "bw" ) );
  } );

  // services hover bar
  $( ".services_item_image, .services_cat_item_image" ).hover( function() {
    $( this ).children( ".services_hover_bar, .projects_hover_bar" ).show();
  }, function() {
    $( this ).children( ".services_hover_bar, .projects_hover_bar" ).hide( "slide", {
      "direction" : "down"
    }, 300 );
  } );

  // projects hover bar
  /* $('.services_item_image, .services_cat_item_image').hover(
        function(){
            $(this).children('.services_hover_bar').show();
        },
        function(){
            $(this).children('.services_hover_bar').hide("slide", {direction: 'down'}, 300);
        }
    );*/
}

function navCallout( str, isOpened ) {
  $( "#nav_" + str ).mouseenter( function() {
    // grab content from hideout div, populate dropdown with content
    $( "#menu_dropdown" ).html( $( "#" + str + "_hideout" ).html() );
    switch ( $( this ).attr( "id" ) ) { // width of the container
      default :
      case "nav_services" :
      case "nav_projects" : {
        $( "#menu_dropdown_wrapper" ).css( {
          "width" : "658px"
        } );
        break;
      }
      case "nav_about" :
      case "nav_innovation" : {
        $( "#menu_dropdown_wrapper" ).css( {
          "width" : "auto"
        } );
        break;
      }
    }
    switch ( $( this ).attr( "id" ) ) { // width of the inner ul's, and left-position of the container
      case "nav_about" : {
        $( "#menu_dropdown_wrapper" ).css( {
          "left" : "322px"
        } );
        $( ".projects_list_wrapper" ).css( {
          "width" : "auto"
        } );
        break;
      }
      case "nav_innovation" : {
        $( "#menu_dropdown_wrapper" ).css( {
          "left" : "610px"
        } );
        $( ".projects_list_wrapper" ).css( {
          "width" : "auto"
        } );
        break;
      }
      default : {
        $( "#menu_dropdown_wrapper" ).css( {
          "left" : "322px"
        } );
        $( ".projects_list_wrapper" ).css( {
          "width" : "140px"
        } );
        break;
      }
    }
    if ( $( "#menu_dropdown_wrapper" ).css( "display" ) == "none" ) {
      $( "#menu_dropdown_wrapper" ).slideDown( 500, function() {
        $( ".services_gallery_image" ).hover( function() {
          $( this ).children( ".services_menu_hover_bar" ).show();
        }, function() {
          $( this ).children( ".services_menu_hover_bar" ).hide( "slide", {
            "direction" : "down"
          }, 300 );
        } );
      } );
    }
  } );
}

function initNavCallouts() {
  navCallout( "about" );
  navCallout( "projects" );
  navCallout( "services" );
  navCallout( "innovation" );
}

function timelineDots() {
  $( ".timeline_box_item" ).each( function() {
    $( this ).data( "year", $( this ).attr( "id" ).replace( /y/i, '' ).split( "|" )[0] );
    $( this ).data( "some_id", $( this ).attr( "id" ).split( "|" )[1] );
  } );
  var hover_dot; // currently mouse-overed dot.
  $( ".timeline_box_item" ).mouseenter( function() {
    hover_dot = $( this ).data( "some_id" );
    $( this ).html( '<div class="timeline_item_year">' + $( this ).data( "year" ) + '</div>' ).css( {
      "background-image" : "url( '/images/bright_red_px.png' )",
      "background-repeat" : "repeat"
    } );
    var that = $( this );
    setTimeout( function() { // prevent laggy javascript from showign all reds.
      if ( hover_dot != $( that ).data( "some_id" ) && !$( that ).hasClass( "timeline_nav_item_active" ) ) {
        $( that ).html( '<img src="/images/timeline_dot.png" alt="' + $( that ).data( "year" ) + '" />' ).css( {
          "background-image" : "none",
          "background-repeat" : "no-repeat"
        } );
      }
    }, 25 );
  } );

  $( ".timeline_box_item" ).mouseleave( function () {
    if ( !$( this ).hasClass( "timeline_nav_item_active" ) ) {
      $( this ).html( '<img src="/images/timeline_dot.png" alt="' + $( this ).data( "year" ) + '" />' ).css( {
        "background-image" : "none",
        "background-repeat" : "no-repeat"
      } );
    }
  } );
}

function externalLinks() {
  if ( !document.getElementsByTagName ) {
    return;
  }
  var anchors = document.getElementsByTagName( "a" );
  for ( var i = 0; i < anchors.length; ++i ) {
    var anchor = anchors[i];
    if ( anchor.getAttribute( "href" ) && anchor.getAttribute( "rel" ) == "external" ) {
      anchor.target = "_blank";
    }
  }
}

function aboutMinHeightFix() {
  $( "#about_box" ).css( "min-height", $( "#about_menu_wrapper" ).height() - 35 );
}

function projectPaginationFix() {
  if ( window.location.toString().substr( -5 ) == "#open" ) {
    $( "#services_box_header_nav" ).trigger( "click" );
  }
}

function modalHeightFix() {
  $( ".modal_wrapper" ).css( {
    "height" : $( document ).height()
  } );
}

$( document ).ready( function() {
  $( window ).trigger( "resize" ); // IE 7 scrollbar fix
} );

function searchUI() { // search-page UI
  var pID = parseInt( $( "#s_nav_what_page" ).text() ); // Page ID, math starts at 1. but no results = no pages.
  var pages = $( ".s_disp_page" ).length; // total pages running a muk
  function updatePageDisp() {
    toggleDisp( ".s_disp_page", false );
    $( ".s_nav_page" ).addClass( "off" );
    var m = Math.floor( (pID - 1) / 5 ); // where are we? (base multiplier, or "page group" etc...)
    for ( var x = 1; x < 6; ++x ) { // re-paint numbers
      $( "#s_nav_p" + x ).html( m * 5 + x ); // re-alignment of page numbers
      if ( m * 5 + x > pages ) {
        toggleVis( "#s_nav_p" + x, false ); // don't show numbers to click on, when we've gone past the bounds.
      } else {
        toggleVis( "#s_nav_p" + x, true );
        if ( pID == m * 5 + x ) {
          $( "#s_nav_p" + x ).removeClass( "off" );
          toggleDisp( "#s_disp_page_" + (m * 5 + x), true );
        }
      }
    }
    //toggleVis( "#s_nav_first", pID != 1 ); // && pages > 1
    $( "#s_nav_prev" ).toggleClass( "off", pID <= 1 ); // when not on page 1 or no results..
    toggleVis( "#s_nav_prev5", pID > 5 );
    toggleVis( "#s_nav_next5" , (pID - (pID - 1) % 5) + 4 < pages );
    $( "#s_nav_next" ).toggleClass( "off", pID == pages || pages <= 1 );
    //toggleVis( "#s_nav_last", pID != pages && pages > 1 );
  }

  updatePageDisp();

  $( "#s_nav_first" ).click( function() { // goto first page
    if ( !$( this ).hasClass( "off" ) ) {
      pID = 1;
      updatePageDisp();
    }
  } );

  $( "#s_nav_prev" ).click( function() { // just go back one page
    if ( !$( this ).hasClass( "off" ) ) {
      --pID;
      updatePageDisp();
    }
  } );

  $( "#s_nav_prev5" ).click( function() { // go back a set of pages
    pID = (pID - (pID - 1) % 5) - 5; // reduce to "first page of previous set"
    updatePageDisp();
  } );

  $( ".s_nav_page" ).click( function() { // go to a page
    pID = parseInt( $( this ).text() );
    updatePageDisp();
  } );

  $( "#s_nav_next5" ).click( function() { // go foward a set of 5
    pID = (pID - (pID - 1) % 5) + 5; // reduce to "first of of next set"
    updatePageDisp();
  } );

  $( "#s_nav_next" ).click( function() { // go foward one page
    if ( !$( this ).hasClass( "off" ) ) {
      ++pID;
      updatePageDisp();
    }
  } );

  $( "#s_nav_last" ).click( function() { // goto last page
    if ( !$( this ).hasClass( "off" ) ) {
      pID = pages;
      updatePageDisp();
    }
  } );
}
