
// index of the current photo, 0 means not set, else between 1 and length of array
// wbphotoset is the rotary photo set (globally declared)
var curphoto = 0;

// -------------------------------- Functions

if( window.jQuery )  // Avoid more errors in IE 5.0
{
  // Directly set flag for CSS to indicate JavaScript is enabled.
  // At this point "HTML" is the only accessable element.
  $(document.documentElement).addClass("jsEnabled");

  $(document).ready( onReady );
}

function showImage(imgindex)
{
  // No position fixed, allow users to scroll in case there are large descriptions.
  $("#wb-body").css("top", $("html").scrollTop() );
  
  $("#wb-content-videocontainer").css("display", "none");
  //$("#wb-content-inner").css("display", "none");
  $("#wb-content-img").css("display", "block");
  $("#wb-content-prev").css("display", "block");
  $("#wb-content-next").css("display", "block");
  $("#wb-content-count").css("display", "block");

  $("#wb-content-img").attr({ src: wbphotoset[curphoto-1].url, title: wbphotoset[curphoto-1].name });
  $("#wb-content-title").text(wbphotoset[curphoto-1].title);
  if (wbphotoset[curphoto-1].description == "") {
    $("#wb-content-description").css("display", "none");
  } else {
    $("#wb-content-description").text(wbphotoset[curphoto-1].description);
    $("#wb-content-description").css("display", "block");
  }
  $("#wb-content-count").text((curphoto) + " / " + wbphotoset.length);

  // cache previous and next images if available
  if (curphoto>1)
  {
    preload_img_prev = new Image();
    preload_img_prev.src = wbphotoset[curphoto-2].url;
  }
  if (curphoto<wbphotoset.length)
  {
    preload_img_next = new Image();
    preload_img_next.src = wbphotoset[curphoto].url;
  }

  // check navigation
  if (curphoto==1)
  {
    $("#wb-content-prev").addClass("disabled");
    $("#wb-content-prev").css("display", "none");
  }
  else {
    $("#wb-content-prev").css("display", "block");
  }
  if (curphoto==wbphotoset.length)
  {
    $("#wb-content-next").addClass("disabled");
    $("#wb-content-next").css("display", "none");
  }
  else {
    $("#wb-next").css("display", "block");
  }
  return false;
}

function onReady()
{

$(".wb-gallery").click(
    function(e) {
      var showindex = $(this).attr("rel");
      curphoto = parseInt(showindex)+1;
      showImage(curphoto);
      $("#wb-container").css("height", $("#wrapper").height());
      $("#wb-container").css("display", "block");
      return false;
    }
  );

$("#wb-overlay, #wb-close").click(
    function(e) {
      $("#wb-container").css("display", "none");
      return false;
    }
  );

$("#wb-content-next").click(
    function(e) {
      // catch multiple clicks
      if (curphoto > wbphotoset.length) {
        curphoto = wbphotoset.length;
      } else {
        curphoto = curphoto + 1;
      }
      $("#wb-content-img").fadeTo("normal", 0, function() {
        showImage(curphoto)
        $("#wb-content-img").fadeTo("normal", 1);
      });
      return false;
    }
  );

$("#wb-content-prev").click(
    function(e) {
      // catch multiple clicks
      if (curphoto < 1) {
        curphoto = 1;
      } else {
        curphoto = curphoto - 1;
      }
      $("#wb-content-img").fadeTo("normal", 0, function() {
        showImage(curphoto)
        $("#wb-content-img").fadeTo("normal", 1);
      });
      return false;
    }
  );

$(".wb-video").click(
    function(e) {
      // No position fixed, allow users to scroll in case there are large descriptions.
      $("#wb-body").css("top", $("html").scrollTop() );

      $("#wb-content-videocontainer").css("display", "block");
      //$("#wb-content-inner").css("display", "none");
      $("#wb-content-img").css("display", "none");
      $("#wb-content-description").css("display", "none");
      $("#wb-content-prev").css("display", "none");
      $("#wb-content-next").css("display", "none");
      $("#wb-content-count").css("display", "none");
      $("#wb-content-title").text(this.title);
      var videoContainerDiv = $("#wb-content-videocontainer");
      $("#wb-container").css("height", $("#wrapper").height());
      $("#wb-container").css("display", "block");
      swfobject.embedSWF(this.href, "wb-content-videoobject", videoContainerDiv.css("width"), videoContainerDiv.css("height"), "9.0.0");
      return false;
    }
  );


};


