/*--------*/
/* A.R.T. */
/*--------*/

jQuery(document).ready(function($) {
  
  /*********************/
  /* Image Pre-Loading */
  /*********************/
  
  // establish reusable
  // preloading method
  (function($){
    $('body').data('preloader', new Array());
    $('body').data('preload', function(src) {
      var arr = $('body').data('preloader');
      var img = new Image();
      img.src = src;
      arr[arr.length] = img;
    });
  })(jQuery);
  
  // use following to preload images as needed
  $('body').data('preload')('pics/itm__blank.gif');
  $('body').data('preload')('pics/bg__pinable_map_popup_top_400w.png');
  $('body').data('preload')('pics/bg__pinable_map_popup_middle_400w.png');
  $('body').data('preload')('pics/bg__pinable_map_popup_bottom_400w.png');
  $('body').data('preload')('pics/ajax__lightbox_loading.gif');
  $('body').data('preload')('pics/ajax__lightbox_form_activity.gif');
  $('body').data('preload')('pics/bg__lightbox_order_cart.png');
  $('body').data('preload')('pics/bg__lightbox_delivery_information.png');
  $('body').data('preload')('pics/itm__btn_review_order_hover.png');
  $('body').data('preload')('pics/itm__btn_review_order_pressed.png');
  
  /*****************/
  /* Form Behvaior */
  /*****************/
  
  // allow a special class to enable enter submission
  $('form.art-enter-submits').keypress(function(e) {
    if (e.which == 13) $(this).get(0).submit();
  });
  
  // in order to ensure that enter does 
  // not submit a form unless we want that
  // behavior standardize the times when
  // it should be allowed to submit
  $('form').keypress(function(e) {
    if (e.which != 13) return true;
    if ($(this).hasClass('art-enter-submits')) return true;
    if ($(this).find('input[type=submit]').size()) return true;
    return false;
  });
  
  // allow a special class to submit when clicked
  $('form .art-click-submits').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    $(this).parents('form').get(0).submit();
  });
  
  // the following function can be used to prevent enter
  // from submitting a form for all elements within container
  function art__disable_enter_submission(container) {
    var inputs = container.find('input:not(.art-enter-submission-disabled)');
    inputs.keypress(function(e) {
      if (e.which != 13) return;
      e.preventDefault();
      e.stopPropagation();
    });
    inputs.addClass('art-enter-submission-disabled');
  }
  
  /*********************************/
  /* Text Field/Area Auto-Labeling */
  /*********************************/
  
  function art__install_tfa_labeler() 
  {
    // this function may be called when additional form elements added
    var tf = $('input.art-auto-labeled, textarea.art-auto-labeled');
    tf.each(function(i, el) { 
      if ($(el).hasClass('art-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == $(this).attr('title')) $(this).addClass('art-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('art-focused').val($(this).attr('title')); });      
      $(el).addClass('art-auto-labeler');
      if ($(this).val() != $(this).attr('title')) $(this).addClass('art-focused');
    });
  }
  
  // run the installer once
  // for any existing elements
  art__install_tfa_labeler();
  
  // ensures that the auto label values are ignored when a form is sumitted by checking current value against label that was assigned
  function art__real_val(el) { if (el.hasClass('art-auto-labeled') && (el.val() == el.attr('title'))) return ''; else return el.val(); }

  /***************************/
  /* Generalized Slide Shows */
  /***************************/
  
  /********************/
  /* Basic Slide Show */
  /********************/
  
  var els = $('div.art-basic-slideshow');
  els.each(function(i, el) {
    el = $(el);
    
    // time to wait between transitions and duration of animation embedded on the page
    el.data('delay', parseFloat(el.find('span.art-slideshow-delay').text())); // seconds
    el.data('transition', parseFloat(el.find('span.art-slideshow-transition').text())); // seconds
    if (!el.data('delay') || el.data('delay') < 0.5) el.data('delay', 1);
    if (!el.data('transition') || el.data('transition') < 0.5) el.data('transition', 0.5);
    
    // remember this slide show
    // index among all shows
    el.data('index', i); 
    
    // esetablish the array of slides in the show element
    el.data('slides', el.find('div.slides').children().get());
    el.data('current', 0);
    el.data('preloader', new Array());
    
    // determine the current slide by locating the first visible slide within the array and saving the offset
    for (var j=0; j<el.data('slides').length; j++) if (!($(el.data('slides')[j]).hasClass('art-hidden'))) el.data('current', j);
    for (var j=0; j<el.data('slides').length; j++) { el.data('preloader')[j] = new Image(); var sl = $(el.data('slides')[j]); el.data('preloader')[j].src = sl.is('img') ? sl.attr('src') : sl.find('img').attr('src'); }
    
    // associate a rotation function with the
    // element requiring itself as parameter
    el.data('rotate_slides', function(el) {
      
      // when no slides present abort rotations
      if (el.data('slides').length < 1) return;
      
      // determine the next index using a modulus operator
      el.data('next', ((el.data('current') + 1) % el.data('slides').length));
      var c = $(el.data('slides')[el.data('current')]);
      var n = $(el.data('slides')[el.data('next')]);
      
      // stop animation if there is only one slide in show
      if (el.data('next') == el.data('current')) return;
      
      // begin animation of current and next
      c.fadeOut(el.data('transition')*1000);
      n.fadeIn(el.data('transition')*1000);
      
      // update our current offset
      el.data('current', el.data('next'));
      
      // ensure that the next slide
      // is visible when it fades in
      n.removeClass('art-hidden');
      
      // schedule the next rotation of the slide show images using absolute references to the slide show element
      setTimeout("jQuery(jQuery('div.art-basic-slideshow').get("+el.data('index')+")).data('rotate_slides')("+
                 "jQuery(jQuery('div.art-basic-slideshow').get("+el.data('index')+")))", el.data('delay') * 1000);
    });
    
    // schedule the first rotation of the slide show images using absolute references to the slide show element
    setTimeout("jQuery(jQuery('div.art-basic-slideshow').get("+el.data('index')+")).data('rotate_slides')("+
               "jQuery(jQuery('div.art-basic-slideshow').get("+el.data('index')+")))", el.data('delay') * 1000);
  });
  
  /**********/
  /* Footer */
  /**********/
  
  // add functionality to the contact form 
  var d = $('div#art-footer div.contact-form');
  if (d.size())
  {
    // when send button is clicked execute
    d.find('a.send').click(function(e) {
      e.preventDefault();
      var f = $(this).parents('form');
      
      // the first task in our attempt to 
      // send the message is to show status
      var ajs = f.find('div.ajax-status');
      ajs.find('.loading-indicator').show();
      ajs.find('.results').hide();
      ajs.find('.close').hide();
      ajs.show();
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) { 
        if (!(data instanceof Object)) data = new Object();
        var msg = null;
        ajs.find('.results span').hide();
        ajs.find('.results .error').html('');
        
        // determine if any error was reported by the
        // process of checking request and response data
        if ((!data.success) || (status != 'success'))
        {
          var error = data.error || 'Error; Please try again.';
          ajs.find('.results .error').html(error);
          ajs.find('.results .error').show();
        }
        else ajs.find('.results .success').show();
        
        // after message is determined show
        ajs.find('.loading-indicator').hide();
        ajs.find('.results').show();
        ajs.find('.close').show();
      };
      
      // compose and execute the ajax request
      $.ajax({ url: 'contact_form_processor.php',
               type: 'POST',
               cache: false,               
               data: { name: art__real_val(f.find('input[name=name]')),
                       email: art__real_val(f.find('input[name=email]')),
                       message: art__real_val(f.find('textarea[name=message]'))
                     },
               dataType: 'json',
               success: complete,
               error: complete });
    });
    
    // enable the close link on the window to function
    d.find('div.ajax-status .close a').click(function(e) {
      e.preventDefault();
      var ajs = $(this).parents('div.ajax-status');
      if (ajs.find('.results .error:visible').size()) { ajs.hide(); return; }
      ajs.hide();
      var f = ajs.parents('div.form');
      f.find('input[name=name]').val('');
      f.find('input[name=email]').val('');
      f.find('textarea[name=message]').val('');
      f.find('input, textarea').blur();
    });
  }
  
  /**********************/
  /* Lightbox (General) */
  /**********************/
  
  // encapsulate the method for showing indicator
  $('body').data('plb__show_activity', function() {
    var el = $('<img src="pics/ajax__lightbox_loading.gif" alt="Loading" style="display: block; width: 54px; height: 55px;"/>');
    plb__lightbox.open(el, true);
  });
  
  // encapsulate a method for displaying an error
  $('body').data('plb__show_error', function(msg) {
    var el = $('<div class="art-error-lightbox">'+
               '  <div class="art-message error">'+msg+'</div>'+
               '  <div class="plb__closer"><img src="pics/itm__lightbox_cancel_x.png" alt="X"/>CLOSE</div>'+
               '</div>');
    plb__lightbox.open(el);
  });
  
  /*************************/
  /* Lightbox'd Pinned Map */
  /*************************/
  
  // associate the map opener links on the map with
  // the proper functionality using the lightbox open
  $('.art-pinned-map-opener').click(function(e) { 
    e.preventDefault(); 
    var lb = $(this).parents('div.art-pinned-map').find('.art-embedded-pinned-map .art-pinned-map-lightbox').clone();
    
    // before opening the map ensure the
    // pins will show their popups well
    lb.find('div.map div.pin').hover(
      function() {
        var popup = $(this).find('div.popup');
        var pin = $(this);
        
        // compute the height of 
        // the popup thats shown
        var c = popup.children();
        var h = 0;
        c.each(function(i, el) {
          h += $(el).outerHeight();
        });
        
        // determine how much if any is off the window area
        var y = popup.offset().top - $(window).scrollTop();
        
        // save the original bottom offset of the popup one time then use to reposition popup
        if (!popup.data('bottom')) popup.data('bottom', popup.css('bottom').replace('px', ''));
        if (y < 0) popup.css('bottom', (parseInt(popup.data('bottom')) + y) + 'px');
      }      
    );
    
    // after adding behaviors
    // open the map lightbox
    plb__lightbox.open(lb);
  });
  
  /********************************/
  /* Lightbox'd Info Request Form */
  /********************************/
  
  var d = $('#art-embedded-info-request-lightbox .art-info-request-lightbox');
  if (d.size())
  {
    // enable the proper link to submit the form
    d.find('div.form .submit').click(function(e) {
      e.preventDefault();
      var f = $(this).parents('form');
      
      // the first task in our attempt to 
      // send the message is to show status
      var ajs = f.find('div.ajax-status');
      ajs.find('.activity').show();
      ajs.find('.results').hide();
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) { 
        if (!(data instanceof Object)) data = new Object();
        var msg = null;
        ajs.find('.results .success').hide();
        ajs.find('.results .failure').html('');
        
        // determine if any error was reported by the
        // process of checking request and response data
        if ((!data.success) || (status != 'success'))
        {
          var error = data.error || 'Error; Please try again.';
          ajs.find('.results .failure').html(error);
          ajs.find('.results .failure').show();
        }
        else
        { 
          ajs.find('.results .success').show();
          f.find('input[name=full_name]').val('');
          f.find('input[name=email]').val('');
          f.find('input[name=phone]').val('');
          f.find('textarea[name=message]').val('');
          f.find('input, textarea').blur();
        }
        
        // after message determined show
        ajs.find('.activity').hide();
        ajs.find('.results').show();
      };
      
      // compose and execute the ajax request
      $.ajax({ url: 'info_request_form_processor.php',
               type: 'POST',
               cache: false,               
               data: { recipient: art__real_val(f.find('input[name=recipient]')),
                       recipient_id: art__real_val(f.find('input[name=recipient_id]')),
                       subject: art__real_val(f.find('input[name=subject]')),
                       full_name: art__real_val(f.find('input[name=full_name]')),
                       email: art__real_val(f.find('input[name=email]')),
                       phone: art__real_val(f.find('input[name=phone]')),
                       message: art__real_val(f.find('textarea[name=message]'))
                     },
               dataType: 'json',
               success: complete,
               error: complete });
    });
  }
  
  // find all lightbox openers on the page and attach behavior
  $('.art-info-request-lightbox-opener').click(function(e) {
    e.preventDefault();
    var message = $(this).find('.message').text();
    var subject = $(this).find('.subject').text();
    var recipient = $(this).find('.recipient').text();
    var recipient_id = $(this).find('.recipient-id').text();
    var recipient_displayed = $(this).find('.recipient-displayed').text();
    var lb = $('#art-embedded-info-request-lightbox .art-info-request-lightbox').clone(true);
    if (!lb.size()) return;
    lb.find('.recipient-subject .subject').text(subject);
    lb.find('.recipient-subject input[name=subject]').val(subject);
    lb.find('.recipient-subject input[name=recipient]').val(recipient);
    lb.find('.recipient-subject input[name=recipient_id]').val(recipient_id);
    lb.find('.recipient-subject .recipient-displayed').text(recipient_displayed);
    if (message) lb.find('textarea[name=message]').val(message).addClass('art-focused');
    else lb.find('textarea[name=message]').val('Message');
    plb__lightbox.open(lb);
  });
  
  // similarly locate the terms and privacy openers and attach
  // behavior to the properly styled links to open their content
  $('.art-terms-of-use-lightbox-opener').click(function(e) {
    e.preventDefault();
    var lb = $('#art-embedded-terms-of-use-lightbox .art-terms-lightbox').clone(true);
    if (!lb.size()) return;
    plb__lightbox.open(lb);
  });
  $('.art-privacy-lightbox-opener').click(function(e) {
    e.preventDefault();
    var lb = $('#art-embedded-privacy-lightbox .art-terms-lightbox').clone(true);
    if (!lb.size()) return;
    plb__lightbox.open(lb);
  });
  
  /*******************************************************/
  /* Shopping Cart Features (Used Across Multiple Pages) */
  /*******************************************************/
  
  // add some pressed state behaviors to the review order button features at the top of all catalog pages
  $('#art-header-gray-navigation .review-order-link a').mousedown(function(e) { $(this).addClass('pressed'); });
  $('#art-header-gray-navigation .review-order-link a').mouseout(function(e) { $(this).removeClass('pressed'); });
  $('#art-header-gray-navigation .review-order-link a').mouseup(function(e) { $(this).removeClass('pressed'); });
  $('#art-header-gray-navigation .review-order-link a').focusout(function(e) { $(this).removeClass('pressed'); });
  
  // embed a simple function to contact the system and retrieve
  // the total quantity of items in the cart useful after additions
  $('body').data('art__refresh_total_quantity_of_items', function() {
    
    // define a function which will handle
    // both success and failure of our post
    var complete = function(data, status) { 
      if (!(data instanceof Object)) data = new Object();
                
      // for any type of error just dont update page content
      if ((!data.success) || (status != 'success')) return;
      
      // determine from the response if we use items
      var plural = data.total == '1' ? false : true;
      
      // simply update the plural status and
      // the count with the count string given
      var el = $('.art-whats-in-your-cart');
      if (plural) el.find('span.plural').show(); else el.find('span.plural').hide();
      el.find('span.count').text(data.total);
    };
    
    // compose and execute request
    $.ajax({ url: 'index.php',
             type: 'POST',
             cache: false,               
             data: { 'gtcms__catalog_store': 'ajax_total_quantity_of_items_in_cart' },
             dataType: 'json',
             success: complete,
             error: complete });
  });
  
  // this function which we will embed with the body can be
  // called whenever ajax purchasing links need to be powered
  // which might be useful when links are added via ajax
  $('body').data('art__enable_ajax_buy_links', function(root) {
    var links = root ? root.find('.art-ajax-buy') : $('body').find('.art-ajax-buy');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-ajax-buy-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        
        // extract the product and quantity from a hidden tag within buy link
        var product_id = parseInt($(this).find('.art-product-id').text());
        var quantity = parseInt($(this).find('.art-product-qty').text());
        if (!quantity || quantity < 1) quantity = 1;
        
        // show the indicator within a lightbox
        $('body').data('plb__show_activity')();                
        
        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) { 
          if (!(data instanceof Object)) data = new Object();                    
                    
          // for any type of error use lightbox error
          // function which has been embedded in the body
          if ((!data.success) || (status != 'success'))
          {
            var error = data.error || 'System/Network error; Please try again.';
            $('body').data('plb__show_error')(error);
          }
          
          // otherwise proceed
          // to open lightbox
          else
          {
            // create a jquery 
            // object from the
            // raw html data
            var lb = $(data.html);
            
            // after adding proper behaviors
            // display the lightbox content
            plb__lightbox.open(lb);
            
            // add behaviors to the lightbox cart after append
            $('body').data('art__enable_cart_checkout_links')();
            $('body').data('art__enable_cart_delete_item_links')();
            $('body').data('art__enable_cart_update_item_links')();
            
            // when each of the product thumbnails are loaded center in their container
            lb.find('table.art-cart tr.item td.thumbnail div.image-holder img').load(function() {
              var img = $(this);
              var cont = img.parent();
              img.css('margin-top', (cont.height() - img.height()) / 2);
            });
          }
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { 'gtcms__catalog_store': 'ajax_add_product',
                         'mode': 'single',
                         'product_id': product_id,
                         'quantity': quantity
                       },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
      
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('ajax-buy-enabled', true);
    });
  });
  
  // enable all purchasing links initially
  $('body').data('art__enable_ajax_buy_links')();
  
  // this function which we will embed with the body can be
  // called whenever the order cart opener links need to be 
  // enabled which might be useful when links are added via ajax
  $('body').data('art__enable_cart_opening_links', function(root) {
    var links = root ? root.find('.art-order-cart-opener') : $('body').find('.art-order-cart-opener');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-cart-opener-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        
        // show the indicator within a lightbox
        $('body').data('plb__show_activity')();     
        
        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) { 
          if (!(data instanceof Object)) data = new Object();                    
                    
          // for any type of error use lightbox error
          // function which has been embedded in the body
          if ((!data.success) || (status != 'success'))
          {
            var error = data.error || 'System/Network error; Please try again.';
            $('body').data('plb__show_error')(error);
          }
          
          // otherwise proceed
          // to open lightbox
          else
          {
            // create a jquery 
            // object from the
            // raw html data
            var lb = $(data.html);
            
            // after adding proper behaviors
            // display the lightbox content
            plb__lightbox.open(lb);
            
            // add behaviors to the lightbox cart after append
            $('body').data('art__enable_cart_checkout_links')();
            $('body').data('art__enable_cart_delete_item_links')();
            $('body').data('art__enable_cart_update_item_links')();
            
            // when each of the product thumbnails are loaded center in their container
            lb.find('table.art-cart tr.item td.thumbnail div.image-holder img').load(function() {
              var img = $(this);
              var cont = img.parent();
              img.css('margin-top', (cont.height() - img.height()) / 2);
            });
          }
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { 'gtcms__catalog_store': 'ajax_cart_fetch' },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
      
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('art-cart-opener-enabled', true);
    });
  });
  
  // enable all order cart opening links initially
  $('body').data('art__enable_cart_opening_links')();

  // this function which we will embed with the body can be
  // called whenever the order cart send links need to be 
  // enabled which might be useful when links are added via ajax
  $('body').data('art__enable_cart_checkout_links', function(root) {
    var links = root ? root.find('.art-order-cart-checkout-begin') : $('body').find('.art-order-cart-checkout-begin');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-cart-checkout-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        
        // when the checkout link is clicked from within an empty cart ignore the cl
        if ($(this).parents('.art-order-cart-lightbox').hasClass('empty-cart')) return;
        
        // show the indicator within a lightbox
        $('body').data('plb__show_activity')();     
        
        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) { 
          if (!(data instanceof Object)) data = new Object();                    
                    
          // for any type of error use lightbox error
          // function which has been embedded in the body
          if ((!data.success) || (status != 'success'))
          {
            var error = data.error || 'System/Network error; Please try again.';
            $('body').data('plb__show_error')(error);
          }
          
          // otherwise proceed
          // to open lightbox
          else
          {
            // create a jquery 
            // object from the
            // raw html data
            var lb = $(data.html);
            
            // after adding proper behaviors
            // display the lightbox content
            plb__lightbox.open(lb);
            
            // enable behaviors after the delivery details box opened
            $('body').data('art__enable_cart_submit_order_links')();
            art__install_tfa_labeler();
          }
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { 'gtcms__catalog_store': 'ajax_checkout_begin' },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
      
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('art-cart-checkout-enabled', true);
    });
  });
  
  // enable all order cart checkout links initially
  $('body').data('art__enable_cart_checkout_links')();
  
  // this function which we will embed with the body can be
  // called whenever the complete order links need to be 
  // enabled which might be useful when links are added via ajax
  $('body').data('art__enable_cart_submit_order_links', function(root) {
    var links = root ? root.find('.art-order-cart-submit') : $('body').find('.art-order-cart-submit');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-cart-submit-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        var f = $(this).parents('form');
        
        // the first task in our attempt to 
        // send the message is to show status
        var ajs = f.find('div.ajax-status');
        ajs.find('.activity').show();
        ajs.find('.results').hide();
        
        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) { 
          if (!(data instanceof Object)) data = new Object();
          var msg = null;          
          ajs.find('.activity').hide();
          ajs.find('.results .success').hide();
          ajs.find('.results .failure').html('');
          
          // determine if any error was reported by the
          // process of checking request and response data
          if ((!data.success) || (status != 'success'))
          {
            var error = data.error || 'Error; Please try again.';
            ajs.find('.results .failure').html(error);
            ajs.find('.results .failure').show();
            ajs.find('.results').show();
            plb__lightbox.reposition();
          }
          else
          { 
            // hide the form and show
            // the thank you message
            f.find('div.fields').hide();
            f.find('div.success').show();
            f.find('div.action span.submit').hide();
            f.find('div.action span.close').show();
            plb__lightbox.reposition();
            
            // clear the form fields although hidden
            f.find('input[name=full_name]').val('');
            f.find('input[name=email]').val('');
            f.find('input[name=phone]').val('');
            f.find('input[name=address1]').val('');
            f.find('input[name=address2]').val('');
            f.find('input[name=state]').val('');
            f.find('input[name=postcode]').val('');
            f.find('textarea[name=message]').val('');
            f.find('input, textarea').blur();
          }                    
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { gtcms__catalog_store: 'ajax_checkout',
                         gtcms__catalog_order: art__real_val(f.find('input[name=gtcms__catalog_order]')),
                         full_name: art__real_val(f.find('input[name=full_name]')),
                         email: art__real_val(f.find('input[name=email]')),
                         phone: art__real_val(f.find('input[name=phone]')),
                         address1: art__real_val(f.find('input[name=address1]')),
                         address2: art__real_val(f.find('input[name=address2]')),
                         state: art__real_val(f.find('input[name=state]')),
                         postcode: art__real_val(f.find('input[name=postcode]')),
                         message: art__real_val(f.find('textarea[name=message]'))
                       },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
      
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('art-cart-submit-enabled', true);      
    });
  });
  
  // enable all order cart complete links initially here
  $('body').data('art__enable_cart_submit_order_links')();
  
  // this function which we will embed with the body can be
  // called whenever a cart table needs its delete links enabled
  // which might be useful when cart tables are added via ajax
  $('body').data('art__enable_cart_delete_item_links', function(root) {
    var links = root ? root.find('.art-order-cart-delete-item') : $('body').find('.art-order-cart-delete-item');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-cart-delete-item-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        var oc = $(this).parents('div.art-order-cart');
        var tbl = $(this).parents('table.art-cart');
        var td = $(this).parents('td.action');
        var tr = $(this).parents('tr.item');
        var cart_item_id = parseInt(tr.find('td.code span.cart_item_id').text());
        var ajs = td.find('div.ajax-activity');
        ajs.show();

        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) {
          if (!(data instanceof Object)) data = new Object();
          
          // in case of error
          // hide the status
          ajs.hide();
          
          // for any type of error fail silently without message
          if ((!data.success) || (status != 'success')) return;
          
          // on success 
          // remove row
          tr.remove();
          
          // check for removal of last
          // row and show special text
          if (!tbl.find('tr.item').size())
          {
            // based on the type of container in which
            // this table exists show the no item message
            if (tbl.parents('div.just-added').size())
              tbl.parents('div.just-added').find('h3 span.no-items').show();
            
            // when the container is the already added one
            else if (tbl.parents('div.already-in-cart').size())
              tbl.parents('div.already-in-cart').find('h3 span.no-items').show();
            
            // the standard
            // cart lightbox
            else
            {
              // when the standard cart lighbox is being viewed we are now empty
              tbl.parents('div.art-order-cart-lightbox').addClass('empty-cart');
            }
            
            // when we have emptied both the addition table and the preexisting items table the cart is now empty
            if ( (tbl.parents('div.just-added').size() && !oc.find('div.already-in-cart table.art-cart').size()) ||
                 (tbl.parents('div.already-in-cart').size() && !oc.find('div.just-added table.art-cart').size()) )
                 tbl.parents('div.art-order-cart-lightbox').addClass('empty-cart');
            
            // the table can
            // be removed
            tbl.remove();
          }
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { 'gtcms__catalog_store': 'ajax_cart_delete',
                         'cart_item_id': cart_item_id
                       },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
        
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('art-cart-delete-item-enabled', true);      
    });
  });
  
  // this function which we will embed with the body can be
  // called whenever a cart table needs its update links enabled
  // which might be useful when cart tables are added via ajax
  $('body').data('art__enable_cart_update_item_links', function(root) {
    var links = root ? root.find('table.art-cart tr.item td.qty') : $('body').find('table.art-cart tr.item td.qty');
    links.each(function(i, el) {
      el = $(el);
      if (el.data('art-cart-update-item-enabled')) return;
      el.find('div.static a.change-qty').click(function(e) {
        e.preventDefault();
        $(this).parents('td.qty').addClass('editing');
      });
      el.find('div.editing a.save-qty').click(function(e) {
        e.preventDefault();
        var oc = $(this).parents('div.art-order-cart');
        var tbl = $(this).parents('table.art-cart');
        var td = $(this).parents('td.qty');
        var tr = $(this).parents('tr.item');
        var cart_item_id = parseInt(tr.find('td.code span.cart_item_id').text());
        var qty = parseFloat(td.find('input').val().replace(/,/, ''));
        if (qty < 0 || isNaN(qty)) return;
        var ajs = td.find('div.ajax-activity');
        ajs.show();

        // define a function which will handle
        // both success and failure of our post
        var complete = function(data, status) {
          if (!(data instanceof Object)) data = new Object();
          
          // in case of error
          // hide the status
          ajs.hide();
          
          // for any type of error fail silently without message
          if ((!data.success) || (status != 'success')) return;
          
          // when setting quantity
          // to zero act like delete
          if (qty == 0) 
          {
            // on success 
            // remove row
            tr.remove();
            
            // check for removal of last
            // row and show special text
            if (!tbl.find('tr.item').size())
            {
              // based on the type of container in which
              // this table exists show the no item message
              if (tbl.parents('div.just-added').size())
                tbl.parents('div.just-added').find('h3 span.no-items').show();
              
              // when the container is the already added one
              else if (tbl.parents('div.already-in-cart').size())
                tbl.parents('div.already-in-cart').find('h3 span.no-items').show();
              
              // the standard
              // cart lightbox
              else
              {
                // when the standard cart lighbox is being viewed we are now empty
                tbl.parents('div.art-order-cart-lightbox').addClass('empty-cart');
              }
              
              // when we have emptied both the addition table and the preexisting items table the cart is now empty
              if ( (tbl.parents('div.just-added').size() && !oc.find('div.already-in-cart table.art-cart').size()) ||
                   (tbl.parents('div.already-in-cart').size() && !oc.find('div.just-added table.art-cart').size()) )
                   tbl.parents('div.art-order-cart-lightbox').addClass('empty-cart');
              
              // the table can
              // be removed
              tbl.remove();
            }
          }
          
          // just redisplay
          // new quantity
          else
          {
            // simply update the static text quantity
            td.find('div.static span.numeric').text(qty);
            td.removeClass('editing');
          }
        };
        
        // compose and execute request
        $.ajax({ url: 'index.php',
                 type: 'POST',
                 cache: false,               
                 data: { 'gtcms__catalog_store': 'ajax_cart_update',
                         'cart_item_id': cart_item_id,
                         'quantity': qty
                       },
                 dataType: 'json',
                 success: complete,
                 error: complete });
      });
        
      // after enabling the link set flag
      // to prevent us from enabling again
      el.data('art-cart-update-item-enabled', true);      
    });
  });
  
  /*****************/
  /* Page Specific */
  /*****************/
  
  /*************/
  /* News Page */
  /*************/

  var d = $('div#art-content.news');
  if (d.size())
  {
    // enable the accordion feature of the archived news
    d.find('div.archived-news .accordion').accordion({
      active: 0,
      collapsible: true
    });
  }
  
  /**************/
  /* About Page */
  /**************/

  var d = $('div#art-content.about');
  if (d.size())
  {
    // enable the read bio links to open the embedded biography lightboxes
    d.find('div.staff-members div.staff-member div.read-bio a').click(function(e) {
      e.preventDefault();
      var s = $(this).parents('div.staff-member');
      var lb = s.find('.art-embedded-biography .art-biography-lightbox').clone();
      plb__lightbox.open(lb);
    });
  }
  
  /**************************/
  /* Division Product Pages */
  /**************************/
  
  var d = $('div#art-content.division');
  if (d.size())
  {
    // enable the catalog quick link flyouts to open on screen
    d.find('div.catalog-quick-links div.subcategory').hover(
      function() {
        var p = $(this).find('div.products');
        
        // locate the normal bottom edge of the flyout and maximum
        var bottom_edge_y = $(this).offset().top + 9 + p.height();
        var max_y = $(window).scrollTop() + $(window).height();
        
        // compute and apply any adjustment
        var adjust_y = bottom_edge_y - max_y;
        if (adjust_y < 0) adjust_y = 0;
        p.css('top', (9 - (adjust_y))+'px');
      },
      null
    );
    
    // enable the entire region of the subcategory to trigger the link within
    d.find('div.catalog-quick-links div.subcategory div.name').click(function(e) {
      e.preventDefault();
      var a = $(this).find('a');
      window.location.href = a.attr('href');
    });
    
    // when each of the product thumbnails are loaded center in their container
    d.find('div.catalog div.product div.image-holder img').load(function() {
      var img = $(this);
      var cont = img.parent();
      img.css('margin-top', (cont.height() - img.height()) / 2);
    });
    
    // enable the product lightbox opener links to locate
    // the embedded lightbox on the page and open it up
    $('.art-product-lightbox-opener').click(function(e) {
      e.preventDefault();
      var product_id = parseInt($(this).find('.product_id').text());
      if (!product_id) return;
      var lb = $('.art-embedded-product-lightbox.product-'+product_id+' .art-product-lightbox').clone(true);
      if (!lb.size()) return;
      plb__lightbox.open(lb);
    });
    
    // when the tabs on the product lightbox are clicked switch panel content
    $('.art-product-lightbox').find('.tabbed-panel .tab').click(function(e) {
      e.preventDefault();
      var lb = $(this).parents('.art-product-lightbox');
      var pnl = $(this).parents('.tabbed-panel');
      var panes = lb.find('div.panes');
      pnl.find('.tab').removeClass('active');
      panes.find('.pane').hide();
      
      // activate the tab and show
      // the appropriate pane now
      $(this).addClass('active');
      if ($(this).hasClass('overview')) panes.find('.pane.overview').show(); 
      if ($(this).hasClass('technical')) panes.find('.pane.technical').show(); 
      if ($(this).hasClass('installation')) panes.find('.pane.installation').show(); 
    });
  }
});



jQuery(window).load(function() {
  var $ = jQuery;
});
