jQuery(document).ready(function($) {
   var curHover = '';
   var curSwapImg = $('#hiddenBg');
   var curFade = '';

   if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4)
   {
      $('#container').css('filter','alpha(opacity=90)');
   } else {
      $('#projRightCol #projContent').jScrollPane({showArrows: true});
      $('#catDescription').jScrollPane({showArrows: true});
   }

   $('#thinkEducate').hover(function() { doSwap(TEMPLATE_DIR + "/images/homeeducatenew.gif", 'educate');  $(this).css('color','#ef4523');  }, function() { $(this).css('color','#444'); });
   $('#thinkMainstreets').hover(function() { doSwap(TEMPLATE_DIR + "/images/homemainstreetnew.gif", 'mainstreet'); $(this).css('color','#a5bd6e'); }, function() { $(this).css('color','#444'); });
   $('#thinkStorefront').hover(function() {  doSwap(TEMPLATE_DIR + "/images/homestorefrontnew.gif", 'storefront'); $(this).css('color','#f47c20');}, function() { $(this).css('color','#444'); });

   $('#thinkCollaborate').hover(function() {
      doSwap(TEMPLATE_DIR + "/images/homecollaboratenew.gif", 'collaborate');
      $(this).css('color','#94cdd2');
      $('#collaborateIcon').attr('src', TEMPLATE_DIR + '/images/rss_icon_blue.png');
   }, function() {
      $(this).css('color','#444');
      $('#collaborateIcon').attr('src', TEMPLATE_DIR + '/images/rss_icon.png');
   });

   $('#thinkLiving').hover(function() { doSwap(TEMPLATE_DIR + "/images/homelivingnew.gif", 'living'); $(this).css('color','#413160');}, function() { $(this).css('color','#444'); });
   $('#thinkAbout').hover(function() { doSwap(TEMPLATE_DIR + "/images/homeAboutnew.gif", 'about'); $(this).css('color','black');}, function() { $(this).css('color','#444'); });

    $('#navSpan').click(function(event) {
      var imageY = event.pageY - $('#footer').offset().top;
      var imageX = event.pageX - $('#bgImage').offset().left;

      if(imageX >= 284 && imageY >= 162 && imageX <= 458 && imageY <= 244)
      {
         window.location = '/storefront';
      }
      else if(
         (imageX >= 0 && imageY >= 0 && imageX <= 149 && imageY <= 244) ||
         (imageX >= 149 && imageY >= 0 && imageX <= 244 && imageY <= 86))
      {
         window.location = '/educate';
      }
      else if(
         (imageX >= 149 && imageY >= 86 && imageX <= 244 && imageY <= 244) ||
         (imageX >= 244 && imageY >= 0 && imageX <= 284 && imageY <= 244) ||
         (imageX >= 284 && imageY >= 0 && imageX <= 458 && imageY <= 162) ||
         (imageX >= 458 && imageY >= 0 && imageX <= 490 && imageY <= 244))
      {
         window.location = '/mainstreets';
      }
      else if(
         (imageX >= 490 && imageY >= 0 && imageX <= 790 && imageY <= 244) ||
          (imageX >= 790 && imageY >= 106 && imageX <= 896 && imageY <= 244)
      )
      {
         window.location = '/collaborate';
      }
      else if(
         (imageX >= 790 && imageY >= 0 && imageX <= 896 && imageY <= 106) ||
          (imageX >= 896 && imageY >= 0 && imageX <= 945 && imageY <= 244)
      )
      {
         window.location = '/about/who-we-are';
      }
      else if(imageX >= 945 && imageY >= 0 && imageX <= 1028 && imageY <= 244)
      {
         window.location = '/living';
      }
    });

   $('#navSpan').mousemove(function(event) {
      var imageY = event.pageY - $('#footer').offset().top;
      var imageX = event.pageX - $('#bgImage').offset().left;

      if(imageX >= 284 && imageY >= 162 && imageX <= 458 && imageY <= 244)
      {
         if(curHover != 'storefront') // change curSwapImage
         {
            doSwap(TEMPLATE_DIR + "/images/homestorefrontnew.gif", 'storefront');
            $('#thinkStorefront').css('color','#f47c20');
         }
      }
      else if(
         (imageX >= 0 && imageY >= 0 && imageX <= 149 && imageY <= 244) ||
         (imageX >= 149 && imageY >= 0 && imageX <= 244 && imageY <= 86))
      {
         if(curHover != 'educate') // change curSwapImage
         {
            doSwap(TEMPLATE_DIR + "/images/homeeducatenew.gif", 'educate');
            $('#thinkEducate').css('color','#ef4523');
         }
      }
      else if(
         (imageX >= 149 && imageY >= 86 && imageX <= 244 && imageY <= 244) ||
         (imageX >= 244 && imageY >= 0 && imageX <= 284 && imageY <= 244) ||
         (imageX >= 284 && imageY >= 0 && imageX <= 458 && imageY <= 162) ||
         (imageX >= 458 && imageY >= 0 && imageX <= 490 && imageY <= 244))
      {
         if(curHover != 'mainstreet') // change curSwapImage
         {
            doSwap(TEMPLATE_DIR + "/images/homemainstreetnew.gif", 'mainstreet');
            $('#thinkMainstreets').css('color','#a5bd6e');
         }
      }
      else if(
         (imageX >= 490 && imageY >= 0 && imageX <= 790 && imageY <= 244) ||
          (imageX >= 790 && imageY >= 106 && imageX <= 896 && imageY <= 244)
      )
      {
          if(curHover != 'collaborate') // change curSwapImage
          {
            doSwap(TEMPLATE_DIR + "/images/homecollaboratenew.gif", 'collaborate');
            $('#thinkCollaborate').css('color','#94cdd2');
            $('#collaborateIcon').attr('src', TEMPLATE_DIR + '/images/rss_icon_blue.png');
          }
      }
      else if(
         (imageX >= 790 && imageY >= 0 && imageX <= 896 && imageY <= 106) ||
          (imageX >= 896 && imageY >= 0 && imageX <= 945 && imageY <= 244)
      )
      {
         if(curHover != 'about') // change curSwapImage
         {
            doSwap(TEMPLATE_DIR + "/images/homeAboutnew.gif", 'about');
            $('#thinkAbout').css('color','black');
         }
      }
      else if(imageX >= 945 && imageY >= 0 && imageX <= 1028 && imageY <= 244)
      {
         if(curHover != 'living') // change curSwapImage
         {
            doSwap(TEMPLATE_DIR + "/images/homelivingnew.gif", 'living');
            $('#thinkLiving').css('color','#413160');
         }
      }
      else
      {
         $('#bgImage').mouseout();
      }
   });

   $('#navSpan').mouseleave(function(event) {
      doSwap(TEMPLATE_DIR + "/images/homebgnew.gif");
      curHover = "";
   });

   function doSwap(img, hover)
   {
      if(curHover != hover) // change curSwapImage
      {
         $('#navLink div a').css('color','#444');
         $('#collaborateIcon').attr('src', TEMPLATE_DIR + '/images/rss_icon.png');
         curSwapImg.attr('src', img);
         swapOpacity();
         curSwapImg = curSwapImg.attr('id') == 'hiddenBg' ? $('#bgImage') : $('#hiddenBg');
         curHover = hover;
      }
   }

   function swapOpacity()
   {
      $("#bgImage").stop().css('opacity',curFade == 'out' ? 0 : 1).animate({
          opacity: curFade == 'out' ? 1 : 0
      }, 500);

      curFade = curFade == 'out' ? 'in' : 'out';
   }

   // set up styles correctly
   if ($.browser.msie || $.browser.mozilla) {
       $("#bgImage").css({'top' : this.offsetTop});
   } else if ($.browser.opera && $.browser.version < 9.5) {
       $("#bgImage").css({'top' : "0"});
   }

   $(window).scroll(function(event) {
     $('#scrollContainer').attr('scrollLeft',$(window).scrollLeft());
     $('#scrollContainer').css('right', -$(window).scrollLeft());
   });

   $(window).resize(function(event) {
      $(window).scrollLeft(0);
      $('#scrollContainer').css('height', parseInt($(window).height()) - 43);
      $('#scrollContainer').attr('scrollLeft',$(window).scrollLeft());
      $('#scrollContainer').css('right', -$(window).scrollLeft());
      $("#footer").css('width', $(window).width() < 1024 ? '1024px' : '100%');
   });

   $(window).resize();

   // project pages
   $('#nextProjImage').click(function()
   {
      if(parseInt($('#curProjImage').text()) < $('#curProjImage').data('numImages'))
      {
         $('#curProjImage').text(parseInt($('#curProjImage').text()) + 1)
         $('#projImageWrapper').animate({marginLeft: '-=370'});
      }
   });

   $('#prevProjImage').click(function()
   {
      if(parseInt($('#curProjImage').text()) > 1)
      {
         $('#curProjImage').text(parseInt($('#curProjImage').text()) - 1)
         $('#projImageWrapper').animate({marginLeft: '+=370'});
      }
   });

   $('#nextThumbnailImage').click(function()
   {
      if(parseInt($('#thumbnailWrapper').css('marginLeft')) != -98 * ($('#thumbnailWrapper img').length - 8 > 0 ? $('#thumbnailWrapper img').length - 8 : 0))
      {
         $('#thumbnailWrapper').animate({marginLeft: '-=98'});
      }
   });

   $('#prevThumbnailImage').click(function()
   {
      if(parseInt($('#thumbnailWrapper').css('marginLeft')) < 0)
         $('#thumbnailWrapper').animate({marginLeft: '+=98'});
   });
});
