
$(document).ready(function () {

   /* --- NAVIGATION BAR MENUS --------------------------------------------- */

   $('#nav-left .menu').hover(function () {
      $(this).children('.submenu').css('display', 'block');
      $(this).children('a').addClass('hovered');
   }, function () {
      $(this).children('.submenu').css('display', 'none');
      $(this).children('a').removeClass('hovered');
   });

   /* --- SEARCH IN THE NAVIGATION BAR ------------------------------------- */

   // removes the word 'Search' when field is clicked
   $('#txtSearchTerm').click(function () {
      var searchTerm = $(this).val();
      if (searchTerm.trim) { searchTerm = searchTerm.trim(); }

      if (searchTerm == 'Search') {
         $(this).val('');
      }
   });

   // replaces the word 'Search' when the field is blank
   $('#txtSearchTerm').blur(function () {
      var searchTerm = $(this).val().trim();
      if (searchTerm == '') {
         $(this).val('Search');
      }
   });

   /* --- CATALOGS --------------------------------------------------------- */

   // zoom the catalogs
   $('.cat-catalog img').hover(function () {
      $(this).animate({
         width: 122,
         height: 160,
         top: -2,
         left: 5
      }, 'fast');
   }, function () {
      $(this).animate({
         width: 100,
         height: 130,
         top: 15,
         left: 20
      }, 'fast');
   });

   // outline the catalogs and select the  
   // checkbox to show that they are selected
   $('.cat-catalog img').click(function () {
      var relatedCheckbox = $(this).siblings('input');
      if (relatedCheckbox.attr('checked') != 'checked') {
         relatedCheckbox.attr('checked', 'checked');
         $(this).addClass('outlined');
      } else {
         relatedCheckbox.removeAttr("checked");
         $(this).removeClass('outlined');
      }
   });

   // if the checkbox is clicked, add/remove 
   // the outline on the image 
   $('.cat-catalog input').click(function () {
      if ($(this).attr('checked') == 'checked') {
         $(this).siblings('img').addClass('outlined');
      } else {
         $(this).siblings('img').removeClass('outlined');
      }
   });

   // form validation and error display
   $('#cat-field-name').blur(function () {
      var nameVal = $('#cat-field-name').val().trim();
      if (nameVal == '') {
         $('#cat-error-name').attr('title', 'Name is required').show();
      } else {
         $('#cat-error-name').hide();
      }
   });

   $('#cat-field-email').blur(function () {
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      var emailaddressVal = $('#cat-field-email').val().trim();

      if (emailaddressVal == '') {
         $('#cat-error-email').attr('title', 'Email is required').show();
      } else if (!emailReg.test(emailaddressVal)) {
         $('#cat-error-email').attr('title', 'Not an email address').show();
      } else {
         $('#cat-error-email').hide();
      }
   });

   $('#cat-field-zip').blur(function () {
      var zipReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
      var zipVal = $('#cat-field-zip').val().trim();

      if (zipVal == '') {
         $('#cat-error-zip').attr('title', 'Zip code is required').show();
      } else if (!zipReg.test(zipVal)) {
         $('#cat-error-zip').attr('title', 'Not a zip code').show();
      } else {
         $('#cat-error-zip').hide();
      }
   });



   $('#cat-field-submit').click(function () {

      $("#catalogs .error").hide();
      var hasError = false;
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      var zipReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

      var nameVal = $('#cat-field-name').val().trim();
      if (nameVal == '') {
         $('#cat-error-name').attr('title', 'Name is required').show();
         hasError = true;
      }

      var emailaddressVal = $('#cat-field-email').val().trim();
      if (emailaddressVal == '') {
         $('#cat-error-email').attr('title', 'Email is required').show();
         hasError = true;
      }
      else if (!emailReg.test(emailaddressVal)) {
         $('#cat-error-email').attr('title', 'Not an email address').show();
         hasError = true;
      }

      var zipVal = $('#cat-field-zip').val().trim();
      if (zipVal == '') {
         $('#cat-error-zip').attr('title', 'Zip code is required').show();
         hasError = true;
      }
      else if (!zipReg.test(zipVal)) {
         $('#cat-error-zip').attr('title', 'Not a zip code').show();
         hasError = true;
      }




      if (hasError == true) { return false; }



   });


   /* --- SUBSCRIPTION FORM ------------------------------------------------ */

   // form validation and error display
   $('#sub-field-fire').click(function () {
      if ($("#footer-sub input[type=checkbox]:checked").length == 0) {
         $('#sub-error-fire-checkbox').attr('title', 'List is required').show();
         $('#sub-error-law-checkbox').attr('title', 'List is required').show();

      } else {
         $('#sub-error-fire-checkbox').hide();
         $('#sub-error-law-checkbox').hide();
      }
   });

   $('#sub-field-law').click(function () {
      if ($("#footer-sub input[type=checkbox]:checked").length == 0) {
         $('#sub-error-fire-checkbox').attr('title', 'A list is required').show();
         $('#sub-error-law-checkbox').attr('title', 'A list is required').show();

      } else {
         $('#sub-error-fire-checkbox').hide();
         $('#sub-error-law-checkbox').hide();
      }
   });

   $('#sub-field-email').change(function () {
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      var emailaddressVal = $('#sub-field-email').val().trim();

      if (emailaddressVal == '' || emailaddressVal == 'Email Address') {
         $('#sub-error-email').attr('title', 'Email is required').show();
         $(this).val('Email Address');
      } else if (!emailReg.test(emailaddressVal)) {
         $('#sub-error-email').attr('title', 'Not an email address').show();
      } else {
         $('#sub-error-email').hide();
      }
   });

   // form validation and error display
   $('#sub-field-submit').click(function () {

      $("#footer-sub .error").hide();
      var hasError = false;
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

      varNumCheckedBoxes = $("#footer-sub input[type=checkbox]:checked").length;
      if (varNumCheckedBoxes == 0) {
         $('#sub-error-fire-checkbox').attr('title', 'List is required').show();
         $('#sub-error-law-checkbox').attr('title', 'List is required').show();
         hasError = true;
      }

      var emailaddressVal = $('#sub-field-email').val().trim();
      if (emailaddressVal == '') {
         $('#sub-error-email').attr('title', 'Email is required').show();
         hasError = true;
      }
      else if (!emailReg.test(emailaddressVal)) {
         $('#sub-error-email').attr('title', 'Not an email address').show();
         hasError = true;
      }

      if (hasError == true) { return false; }

   });

   // removes the word 'Email Address' when field is clicked
   $('#sub-field-email').click(function () {
      var searchTerm = $(this).val().trim();
      if (searchTerm == 'Email Address') {
         $(this).val('');
      }
   });

   // replaces the word 'Search' when the field is blank
   $('#sub-field-email').blur(function () {
      var searchTerm = $(this).val().trim();
      if (searchTerm == '') {
         $(this).val('Email Address');
      }
   });

   /* --- SEO FOOTER ------------------------------------------------------- */

   $('#seo .seo-tab span').click(function () {
      if ($('#seo .seo-content').css('display') == 'block') {
         $('#seo .seo-content').slideUp();
      } else {
         $('#seo .seo-content').show();
         $('html, body').animate({ scrollTop: $(document).height() });
      }
   });

   /* --- HOME PAGE BANNER ROTATOR ------------------------------------------------------------- */


   $('#mast-banners .banner-wrap').cycle({
      fx: 'scrollHorz',
      speed: 1100,
      timeout: 8000,
      random: 1,
      next: '#mast-banners .banner-nav.right',
      prev: '#mast-banners .banner-nav.left'
   });

   var featureWidth = 960;
   var halfMargin = Math.round(($(window).width() / 2) - (featureWidth / 2));
   $('#mast-banners .banner-nav.left').css('margin-left', halfMargin - $('.banner-nav.right').width() - 5 + 'px');
   $('#mast-banners .banner-nav.right').css('margin-left', halfMargin + featureWidth + 'px');

   // as the window is resized the margin is recalculated
   $(window).resize(function () {
      var halfMargin = Math.round(($(window).width() / 2) - (featureWidth / 2));
      $('#mast-banners .banner-nav.left').css('margin-left', halfMargin - $('.banner-nav.right').width() + 'px');
      $('#mast-banners .banner-nav.right').css('margin-left', halfMargin + featureWidth + 'px');
   });



   /* --- SCROLLER --------------------------------------------------------- */
   if ($('.scroller-product').length) {

      // the size of an individual product
      var prodWidth = $('.scroller-product').width();
      var prodRightMargin = $('.scroller-product').css('margin-right').replace('px', '');
      var prodBorderLeft = $('.scroller-product').css('border-left-width').replace('px', '');
      var prodBorderRight = $('.scroller-product').css('border-right-width').replace('px', '');
      var productWidth = prodWidth + parseInt(prodRightMargin) + parseInt(prodBorderLeft) + parseInt(prodBorderRight);

      // set the width of the product container
      $('.scroller-products').width(function () {
         var numProducts = $(this).children('.scroller-product').length;
         return (productWidth * numProducts);
      });

      // Left navigation
      $('.scroller-nav.prev').click(function () {
         var container = $(this).siblings('.scroller-container');
         container.stop(true, true);
         var containerPosition = container.scrollLeft();
         if (containerPosition <= 0) {
            var lastPanel = container.find('.scroller-product').last().detach();
            container.children('.scroller-products').prepend(lastPanel);
            container.scrollLeft(productWidth);
            container.animate({ scrollLeft: containerPosition }, 'normal');
         } else {
            container.animate({ scrollLeft: containerPosition - productWidth }, 'normal');
         }
      });
      /* --- REPOSITION/RESIZE SCROLLER IMAGES ------------------------------------- */
      RecenterProductImages();
      setTimeout('RecenterProductImages()', 100);
      setTimeout('RecenterProductImages()', 250);
      setTimeout('RecenterProductImages()', 1000);
      setTimeout('RecenterProductImages()', 3000);

      // Right navigation
      $('.scroller-nav.next').click(function () {
         var container = $(this).siblings('.scroller-container');
         container.stop(true, true);

         var containerPosition = container.scrollLeft();
         var containerWidth = container.width();
         var difference = containerWidth - containerPosition;
         //window.alert(productWidth + '::' + containerWidth + ' - ' + containerPosition + ' = ' + difference);

         //hack
         var useProductWidth = productWidth;
         var blnLoopPanels = (difference <= productWidth);
         var numProducts = container.find('.scroller-products').children('.scroller-product').length;
         if (numProducts == 4 && containerPosition > 0) {
            blnLoopPanels = true;
            useProductWidth = 1;
         }

         if (blnLoopPanels) {
            var firstPanel = container.find('.scroller-product').first().detach();
            container.children('.scroller-products').append(firstPanel);
            container.scrollLeft(useProductWidth);
            container.animate({ scrollLeft: containerPosition }, 'normal');
         } else {
            container.animate({ scrollLeft: containerPosition + useProductWidth }, 'normal');
         }
      });
   }
   /* --- POP-UP PRODUCT PANE ---------------------------------------------- */

   //select all the a tag with name equal to modal
   $('.modal').click(function (e) {
      //Cancel the link behavior
      e.preventDefault();

      //Get the right popup
      var id = $(this).attr('id').replace('link', '#popup');

      //Get the screen height and width
      var maskHeight = $(document).height();
      var maskWidth = $(window).width();

      //Set height and width to mask to fill up the whole screen
      $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

      //transition effect     
      $('#mask').show();

      //Get the window height and width
      var winH = $(window).height();
      var winW = $(window).width();

      //Set the popup window to center
      $(id).css('top', winH / 2 - $(id).height() / 2 + $(window).scrollTop() - 50);
      $(id).css('left', winW / 2 - $(id).width() / 2);

      //transition effect
      $(id).fadeIn();

   });

   //if close button is clicked
   $('.popup-close').click(function (e) {
      //Cancel the link behavior
      e.preventDefault();
      $('#mask').fadeOut('fast');
      $(this).parents('.popup').fadeOut('fast');
   });

   //if mask is clicked
   $('#mask').click(function () {
      $(this).fadeOut('fast');
      $('.popup').fadeOut('fast');
   });

   /* --- POP-UP BRANDS PANE & SCROLLER ------------------------------------ */
   if ($('.brand-scroll').length) {
      // the size of an individual brand column
      var numColumns = 3;
      var brandColWidth = $('.brand-scroll ul').width();
      var brandColRightMargin = $('.brand-scroll ul').css('margin-right').replace('px', '');
      var brandColBorder = $('.brand-scroll ul').css('border-right-width').replace('px', '');
      var brandColWidth = brandColWidth + parseInt(brandColRightMargin) + parseInt(brandColBorder);

      // set the width of the brand scroller
      $('.brand-scroll').width(function () {
         var numBrandCols = $(this).children('ul').length;
         return (brandColWidth * numBrandCols);
      });

      // Scroll Left
      $('.popup-nav .prev').click(function (e) {
         e.preventDefault();
         var container = $(this).parents('.brand-wrap');
         container.stop(true, true);
         var containerPosition = container.scrollLeft();
         if (containerPosition >= 0) {
            container.animate({ scrollLeft: containerPosition - (brandColWidth * numColumns) }, 'fast');
         }
      });

      // Right navigation
      $('.popup-nav .next').click(function (e) {
         e.preventDefault();
         var container = $(this).parents('.brand-wrap');
         container.stop(true, true);
         var containerPosition = container.scrollLeft() + (brandColWidth * numColumns);
         var containerWidth = $('.brand-scroll').width();
         var difference = containerWidth - containerPosition;

         if (difference >= brandColWidth) {
            var intScroll = container.scrollLeft() + (brandColWidth * numColumns);
            container.animate({ scrollLeft: intScroll }, 'fast');
         }
      });
   }
   /* ---------------------------------------------------------------------- */

});

function RecenterProductImages() {
   $('a.scroller-product').each(function (i) {
      var cellInfo = $(this);
      if (cellInfo.length) {

         var prodInfo = cellInfo.find("div.prod-info");
         if (prodInfo.length) {
            var intCellHeight = cellInfo.height();
            var intProdTextHeight = prodInfo.height();

            var imgCell = cellInfo.find('img.scroller-product-image');

            if (imgCell.length) {
               var intWidth = imgCell.width();
               var intHeight = imgCell.height();

               if (intWidth > intHeight) {
                  var intPixRemaining = (intCellHeight - intProdTextHeight) - intHeight;
                  var intPixToAdjust = parseInt(intPixRemaining / 2);
                  if (intPixToAdjust > 1) { imgCell.css("margin-top", intPixToAdjust + "px"); }
               }
            }
         }
      }
   });
}
