﻿
// READY/ONLOAD
var isIE6 = ($.browser.msie && $.browser.version.substr(0, 1) < 7);
$(document).ready(
   function()
   {
      //INIT
      loadPhoto(0);
      $("#date_label").html(new Date().getFullYear());

      if (isIE6)
      {
         // PNG FIX FOR IE6
         $("#header").css("background", "#131313");
         $("#footer").css("background", "#131313");
         $(document).pngFix();

         // DOCUMENT-WIDE CLICK HANDLER
         document.onclick = function() { loadPhoto(1); };
      }

      // PREVDIV
      var prevDiv = $("#prev_div");
      $(function()
      {
         // PREVDIV INIT
         prevDiv.click(function() { loadPhoto(-1); });

         // PREVDIV HOVER
         prevDiv.hover(
            function() { $(this).stop().animate({ opacity: 1 }, 600); },
            function() { $(this).stop().animate({ opacity: 0 }, 600); });
      });

      // NEXTDIV
      var nextDiv = $("#next_div");
      $(function()
      {
         // NEXTDIV INIT
         nextDiv.click(function() { loadPhoto(1); });

         // NEXTDIV HOVER
         nextDiv.hover(
            function() { $(this).stop().animate({ opacity: 1 }, 600); },
            function() { $(this).stop().animate({ opacity: 0 }, 600); });
      });
   });

// IMPORTANT - THERE SHOULD BE AN EVEN NUMBER OF PHOTOS IN EACH SET
function loadPhoto(increment)
{
   // IMAGE PREFIX
   var imagePage;
   var maxImage;
   if (location.href.indexOf("commercial.html") != -1)
   {
      imagePage = "commercial_";
      maxImage = 2;
   }
   else if (location.href.indexOf("portraits.html") != -1)
   {
      imagePage = "portraits_";
      maxImage = 6;
   }
   else if (location.href.indexOf("seniors.html") != -1)
   {
      imagePage = "seniors_";
      maxImage = 10;
   }
   else if (location.href.indexOf("weddings.html") != -1)
   {
      imagePage = "weddings_";
      maxImage = 6;
   }
   else
   {
      imagePage = "home_";
      maxImage = 14;
   }

   // GET PHOTO
   var imageIndex;
   var photoEven = $("#photo_even");
   var photoOdd = $("#photo_odd");
   if (increment == 0)
   {
      imageIndex = Math.floor(Math.random() * maxImage + 1);
      increment = 1;
   }
   else
      imageIndex = parseInt($("#image_index").val()) + increment;
   if (imageIndex > maxImage)
      imageIndex = 1;
   else if (imageIndex == 0)
      imageIndex = maxImage;
   var imageFile = "url(images/" + imagePage + ((imageIndex < 10) ? "0" : "") + imageIndex + ".jpg)";
   $("#image_index").val(imageIndex);
   $("#header_page").html(imageIndex + " / " + maxImage);

   if (isIE6)
   {
      // USE BODY BACKGROUND FOR IE6
      $("body").css("background-image", imageFile);
   }
   else
   {
      // FADE-IN THE NEXT IMAGE
      if (imageIndex % 2 == 0)
      {
         photoEven.css("opacity", 0);
         photoEven.css("background-image", imageFile);
         photoEven.stop().animate({ opacity: 1 }, 1000);
         photoOdd.stop().animate({ opacity: 0 }, 1000);
      }
      else
      {
         photoOdd.css("opacity", 0);
         photoOdd.css("background-image", imageFile);
         photoOdd.stop().animate({ opacity: 1 }, 1000);
         photoEven.stop().animate({ opacity: 0 }, 1000);
      }

      // DELAY PRE-LOAD OF FOLLOWING IMAGE
      setTimeout(
      function()
      {
         imageIndex += increment;
         if (imageIndex > maxImage)
            imageIndex = 1;
         else if (imageIndex == 0)
            imageIndex = maxImage;
         imageFile = "url(images/" + imagePage + ((imageIndex < 10) ? "0" : "") + imageIndex + ".jpg)";
         if (imageIndex % 2 == 0)
            photoEven.css("background-image", imageFile);
         else
            photoOdd.css("background-image", imageFile);
      }, 
      1000);
   }
}

