
var portImgID = 1;
var showPg = 1;
var showMaxPg = 8;
var portfolioPg = 1;
var portfolioCount = 8;
var portfolioMaxPg = portfolioCount - 3;

$(document).ready(function() {

   $("#priNav ul li.about a").mouseover(function() { MoveImage($(this), "-200px", "0px", false) });
   $("#priNav ul li.about a").mouseout(function() { MoveImage($(this), "-400px", "0px", true) });
   $("#priNav ul li.portfolio a").mouseover(function() { MoveImage($(this), "-200px", "-53px", false) });
   $("#priNav ul li.portfolio a").mouseout(function() { MoveImage($(this), "-400px", "-53px", true) });
   $("#priNav ul li.services a").mouseover(function() { MoveImage($(this), "-200px", "-151px", false) });
   $("#priNav ul li.services a").mouseout(function() { MoveImage($(this), "-400px", "-151px", true) });
   $("#priNav ul li.contact a").mouseover(function() { MoveImage($(this), "-200px", "-103px", false) });
   $("#priNav ul li.contact a").mouseout(function() { MoveImage($(this), "-400px", "-103px", true) });

   function MoveImage(el, x, y, reset) {
      if (!el.hasClass('on')) {
         var speed = 300;
         if (reset) speed = 200;
         el.stop().animate({ backgroundPosition: x + " " + y }, speed, function() {
            if (reset) {
               el.css("background-position", "0px " + y);
            }
         });
      }
   }

   $('#foot .inner .email a img').mouseover(function() {
      Bounce($(this));
   });
   $('#foot .inner .email a img').mouseout(function() {
      BounceStop($(this));
   });

   $('.showcase .more').click(function() {
      showPg++;
      if (showPg > showMaxPg) showPg = 1;
      $('.showcase ul.items').animate({ marginLeft: ((1 - showPg) * 350) + "px" }, 200);
      return false;
   });

   $('.portfolioListing .more').click(function() {
      portfolioPg++;
      if (portfolioPg > portfolioMaxPg) portfolioPg = 1;
      $('.portfolioSelection ul').animate({ marginTop: ((1 - portfolioPg) * 112) + "px" }, 200);
      return false;
   });

   $('.process ul li').hover(function() { Bounce($(this)); });
   $('.process ul li div').hover(function() {
      $(this).stop().animate({ backgroundColor: "#e4a02d" }, 300);
   }, function() {
      $('.process ul li div').stop().animate({ backgroundColor: "#87b2f7" }, 300);
   });

   $('.btn').hover(function() {
      $(this).stop().animate({ backgroundColor: "#e4a02d", borderColor: "#d0932c" }, 100);
   }, function() {
      $(this).stop().animate({ backgroundColor: "#87b1f7", borderColor: "#75a1eb" }, 450);
   });

   $('.altImg a').hover(function() {
      $(this).stop().animate({ backgroundColor: "#e5ab49" }, 150);
   }, function() {
      if (!$(this).hasClass('on')) {
         $(this).stop().animate({ backgroundColor: "#87b1f7" }, 500);
      } else {
         $(this).stop().animate({ backgroundColor: "#e4a02d" }, 150);
      }
   });

   $('.input').focus(function() {
      if ($(this).val() == "Email Address") {
         $(this).val('');
      }
   });

   $('.portfolioItem .altImg a').click(function() {
      var elThis = $(this);
      var imgNo = parseInt(elThis.attr('title').replace("Image ", ""));
      var el = $('.portfolioItem .mainImg img');
      $('.altImg a.on').text('viewed');
      elThis.addClass('on');
      elThis.text('loading');
      el.stop().animate({ width: "0px", height: "0px" }, 300, function() {

         //Set the new image url
         var src = "/img/temp/" + portImgID + "/" + imgNo + ".jpg";

         //Create a new image object
         var img = new Image();
         img.onload = function() {
            $('.portfolioItem .mainImg img').attr('src', src);
            el.animate({ width: "385px", height: "260px" }, 300, function() {
               elThis.text('viewing');
            });
         };

         //Load the image
         img.src = src;
      });

      return false;
   });

   $('.contactForm .btnJS').click(function() {
      var thisReturn = false;
      var el = $(this);
      el.val('Loading');
      $('input.error').removeClass('error');
      var name = $('.clsContactName').val();
      var email = $('.clsContactEmail').val();
      var phone = $('.clsContactPhone').val();
      var details = $('.clsContactDetails').val();
      var s = "f=contact&name=" + name + "&email=" + email + "&phone=" + phone + "&details=" + details;
      $.ajax({
         type: 'POST',
         url: "/_handlers/form.ashx",
         dataType: "json",
         data: s,
         success: function(data) {
            if (data.success) {
               thisReturn = true;
            }
            else {
               AddErrorClass(data.errorField1, "clsContactName");
               AddErrorClass(data.errorField2, "clsContactEmail");
               AddErrorClass(data.errorField3, "clsContactDetails");
               FireBounce();
               el.val('Send!');
               $('.theMsg').html(data.msg);
            }
         },
         error: function(XMLHttpRequest, textStatus, errorThrown) {
         },
         complete: function(XMLHttpRequest, textStatus) {
            if (thisReturn) {
               $('.contactForm .btnActual').trigger('click');
            }
         }
      });
      return false;
   });

   $('.notifyForm .btnJS').click(function() {
      var thisReturn = false;
      var el = $(this);
      el.val('Loading');
      $('input.error').removeClass('error');
      var clsName = 'notifyFormInk';
      var clsNameFull = '.' + clsName;
      var email = $(clsNameFull + ' .clsNotifyEmail').val();
      var s = "f=notify&type=Ink&email=" + email;
      $.ajax({
         type: 'POST',
         url: "/_handlers/form.ashx",
         dataType: "json",
         data: s,
         success: function(data) {
            if (data.success) {
               thisReturn = true;
            }
            else {
               AddErrorClass(data.errorField1, clsName + " .clsNotifyEmail");
               FireBounce();
               el.val('Notify me on launch');
               $(clsNameFull + ' .theMsg').html(data.msg);
            }
         },
         error: function(XMLHttpRequest, textStatus, errorThrown) {
         },
         complete: function(XMLHttpRequest, textStatus) {
            if (thisReturn) {
               $('.notifyForm .btnActual').trigger('click');
            }
         }
      });
      return false;
   });

});

function Bounce(el) {
   el.stop().animate({ marginTop: "-5px" }, 100)
            .animate({ marginTop: "0" }, 100)
            .animate({ marginTop: "-5px" }, 100)
            .animate({ marginTop: "0" }, 100);
}
function BounceHoriz(el) {
   el.stop().animate({ marginLeft: "-10px" }, 100)
            .animate({ marginLeft: "0" }, 100)
            .animate({ marginLeft: "-10px" }, 100)
            .animate({ marginLeft: "0" }, 100)
            .animate({ marginLeft: "-10px" }, 100)
            .animate({ marginLeft: "0" }, 100);
}
function BounceStop(el) {
   el.stop().animate({ marginTop: "0" }, 100);
}

function FireBounce() {
   BounceHoriz($('input.error,textarea.error'));
}

function AddErrorClass(add, className) {
   if (add) {
      $('.' + className).addClass('error');
   } else {
      $('.' + className).removeClass('error');
   }
}

