  var current;
  window.scrollTo(0,0);
  function buildImages(company,num_images)
  {
    htmlStr = "";

    for (i=0;i<num_images;i++)
    {
      htmlStr = htmlStr + "<img src='images/work/"+company+"/th_"+i+".jpg' id='p"+i+"' class='thumb' />";
    }
    htmlStr += "<div id='mainImage'>";
    if (num_images > 0) {
      htmlStr += "<img src='images/work/"+company+"/big_0.jpg' id='bigImage' class='gallery' />";
    }
    htmlStr += '</div>';
    location.url = location.url + "#" + whichCompany;
   $(".mainContent").html(htmlStr);
    $(".mainContent .thumb").fadeTo(0,0.3);

    $("#p0").fadeTo(0,1);
    current = "p0";

    $(".thumb").click(function(){
      current = $(this).attr("id");
      $(this).fadeTo(0,1);
      // update the main image to match this items thumbnail
      loadBig($(this))

      $.each($(".thumb"),function() {
        if ($(this).attr("id") != current) {$(this).fadeTo(0,0.3)};
      });
    });


    $(".thumb").hover(function() {
      if ($(this).attr("id") != current) {  $(this).fadeTo(0,1)};
      },function() {
      if ($(this).attr("id") != current) {    $(this).fadeTo(0,0.3)}
    });
    init();
  }
  window.onload = function() { window.scrollTo(0,0); }
  function pageload()
  {// build the images and set the first one to selected

        hash = location.hash.replace('id-', '');
		hash != "" ?   whichCompany = hash :  whichCompany = whichCompany;
	      if (hash != "" ) {hash = whichCompany }  
        whichCompany = whichCompany.substr(1);
        buildImages(whichCompany.split(/_/)[0],whichCompany.split(/_/)[1]);
        $(".workQuote").attr("src","images/work/"+whichCompany.split(/_/)[0]+"/pullquote.gif");
        $("ul.company li span").css("text-decoration","none").css("color","black").css("cursor","default")
        $("#"+whichCompany).css("text-decoration","none").css("color","#e6b33c")

  }
  $(function() {

    // set the initial state from the url hash
    $.historyInit(pageload)
    $("ul.company li span").click(function () 
    {
    	    if (whichCompany == $(this).attr("id")) { return false; } 
          whichCompany = $(this).attr("id"); 
          location.hash = 'id-' + whichCompany;
          var hash = location.hash;
          hash = hash.replace(/^.*#/, '');
          // moves to a new page.
          // pageload is called at once.
          $.historyLoad(hash);
          $(".workQuote").attr("src","images/work/"+whichCompany.split(/_/)[0]+"/pullquote.gif");
          return false;
    });

    $("ul.company li span").hover(
    function()
    {
      if ($(this).attr("id") != whichCompany)
      {
        $(this).css("text-decoration","underline").css("color","#e6b33c").css("cursor","pointer")
      }
    },
    function()
    {
      if ($(this).attr("id") != whichCompany)
      {
        $(this).css("text-decoration","none").css("color","black").css("cursor","default")
      }
    });


  });
  function loadBig(obj)
  {
    $(".gallery").fadeIn(800);
    src = obj.attr("src").replace(/th_/,"big_"); // replace the big image
    $("#bigImage").attr("src",src);
  }
  
  function init() {
    if (window.location.toString().indexOf('otherprojects') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('otherprojects').innerHTML;
    }
    if (window.location.toString().indexOf('otherfinancial') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('otherfinancial').innerHTML;
    }
    if (window.location.toString().indexOf('othereducation') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('othereducation').innerHTML;
    }
    if (window.location.toString().indexOf('othergovernment') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('othergovernment').innerHTML;
    }
    if (window.location.toString().indexOf('otherhousing') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('otherhousing').innerHTML;
    }    
	if (window.location.toString().indexOf('otherhealthcare') > -1) {
      document.getElementById('mainContent').innerHTML = document.getElementById('otherhealthcare').innerHTML;
    }
  }

