// syntactic sugar for checking if a selector has items
$.fn.exists = function() {
   return $(this).length !== 0;
}

// useful link selecting/deselecting stuff
$.fn.selectLink = function() {
   $(this).addClass("selected");
}

$.fn.deselectLink = function() {
   $(this).removeClass("selected");
}

$(document).ready(function() {
   $('#clientDiv').cycle();

   if ($("ul#contentNav").exists()) {
      // set up current viewable div based on location hash (default to the first one)
      var hash = window.location.hash;
      var currentLinkID = $("ul#contentNav li a").first().attr("id");
      var currentDiv = currentLinkID.substr(0, currentLinkID.length - 4);
      
      $("ul#contentNav li a").each(function() {
         if (hash == $(this).attr("href")) {
            currentDiv = hash.substr(1);
            $(this).selectLink();
         }
      });
      
      // if the hash is bad or not there, select the first link
      if (!($("ul#contentNav a.selected").exists()))
         $("#" + currentLinkID).selectLink();
         
      // hide all content divs
      $("div#content div.panel").hide();
      // show the currently selected div
      $("#" + currentDiv + "Div").show();;
      
      // function for clicking on an "actionable" link
      $("a.actionable").click(function() {
         var newDiv = $(this).attr("href").substr(1);
         $("#" + currentDiv + "Link").deselectLink(); // the currently selected link can now be clicked on again
         $("#" + newDiv + "Link").selectLink(); // select the clicked-on link
         $("#" + currentDiv + "Div").hide(); // hide current div, and show the new one upon completion
         $("#" + newDiv + "Div").show();
         window.location.hash = "#" + newDiv; // update the location hash
         
         // show the new div (keeping this just in case I want to do the fades again)
         //function showDiv(divID) {
         //   $("#" + divID + "Div").fadeIn("slow");
         //}
         
         currentDiv = newDiv; // we have a new current div!               
         return false;
   });
   }
}); 
