jQuery(document).ready(function() {
  // functions for header search
  jQuery("#s").attr({ value: 'Search' }).focus(function(){
    // darken color on focus
    jQuery(this).css('color', '#333');
    // clears header search field on focus
    if(jQuery(this).val()=="Search"){
      jQuery(this).val("");
    }
  }).blur(function(){
    // lighten color on blur
    jQuery(this).css('color', '#aaa');
    // replaces default field value if field is empty on blur
    if(jQuery(this).val()==""){
      jQuery(this).val("Search");
    }
  });
  
  // get last child and add bottom
  jQuery('#menu-main li ul > li:last-child').addClass('bottom');

  // each loop to position the subnav
  jQuery('#menu-main > li').each(function() {
    // w = parent li width
    w = jQuery(this).width() * 1;  // measure width of parent li
    
    // sw = child/sub ul width
    sw = jQuery('ul', this).width() * 1;  // measure width of child/sub ul
    
    // d = difference between child/sub ul and parent li
    d = sw - w;  // find difference between child ul and parent li
    d = d * .5;  // divide difference by half
    d = d * -1;  // make difference negative
    
    // p = sliding door link padding
    p = jQuery('a', this).css('padding-right'); // measure sliding door link padding
    p = p.replace('px', '') * 1; // remove 'px' from string
    p = p * .5;  // divide sliding door link padding by half
    
    d = d + p;  // adjust for half of parent li padding

    // position sub ul
    jQuery('ul', this).css('left', d);
  });
  
  // testimonial functions
  jQuery(".testimonial-list li").hide();
  jQuery(".testimonial-list li:first-child").show();
  var total = jQuery(".testimonial-list li").size();
  var count = 1;
  jQuery("#nextlink").click( function() {
    jQuery(".testimonial-list li:nth-child("+count+")").hide();
    count++;
    if (count > total) {
      count = 1;
    }
    jQuery(".testimonial-list li:nth-child("+count+")").show();
    return false;
  });
  jQuery("#prevlink").click( function() {
    jQuery(".testimonial-list li:nth-child("+count+")").hide();
    count--;
    if (count == 0) {
      count = total;
    }
    jQuery(".testimonial-list li:nth-child("+count+")").show();
    return false;
  });
  
  // form valiation calls
  jQuery('#redbar-form').validate();
  
  jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");
    return this.optional(element) || phone_number.length > 9 &&
    phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  }, "Please specify a valid phone number");
  
  jQuery('#cp-form').validate({
    rules: {
      phone: {
        required: true,
        phoneUS: true
      } // end field
    } //end rules
  }); // end #cp-form validate
  
});


// enable tabs in right column
jQuery(function() {
  setTimeout('jQuery("#content-tabs").tabs();',50);
});

// enable tabs in the red bar
jQuery(function() {
  jQuery("#red-bar-tabs").tabs();
});


