
jQuery(document).ready(
 function(){
  setupRemoveDefault();
  externalLinks();
  jQuery("img").removeAttr("title", "");
 });


function setupRemoveDefault() {
 jQuery(".nolabel, #contact-us input, #contact-us textarea").bind("click focus",
  function () {
   if(!jQuery(this).attr("rel") || jQuery(this).attr("rel") == jQuery(this).val()) {
    if(!jQuery(this).attr("rel")) {
     jQuery(this).attr("rel", jQuery(this).val());
    }
    jQuery(this).val("");
    jQuery(this).blur(
     function() {
      if(!jQuery(this).val()) {
       jQuery(this).val(jQuery(this).attr("rel"));
      }
     }
    );
   }
  }
 );
}


function externalLinks() {
  jQuery('a[@href^="http://"]').each(function(i){
   var link = "" + this.href;
   var currentSite = "" + window.location;
   var cutOff = 0 + currentSite.indexOf( "/", 10 );
   if(link.substring(0, cutOff) != currentSite.substring(0,cutOff)) {
    this.target= "_blank";
    jQuery(this).addClass("externalLink");
   }
  });
  jQuery.expandingTA(jQuery('#contact-us textarea')); // initial call to all elems of textarea.expanding
 }

/* CART STUFF */
function loadAjax(loadIntoElID, URL) {
 var imgHtml = '<img src="/mysite/javascript/loading.gif" alt="loading" />';
 jQuery("#" + loadIntoElID).html(imgHtml);
 window.setTimeout( function() {
  jQuery("#" + loadIntoElID).load(URL);
 }, 100);
 return true;
}



jQuery.expandingTA = function(elems)  {
 // turn each element into an expanding text area
 elems.each(
  function()  {
   var _interval = null;
   var _dummy = null;
   var _self = this;
   var _prevHeight = 0;
   var _min_height;

   // magically expand the textarea
   jQuery(_self)
    .bind('focus', __checkExpand)
    .bind('blur', __stopExpand)
    .css('overflow', 'hidden');

   function __checkExpand() {
    _min_height = jQuery(_self).height();
    _interval = setInterval(
     function() {
      __expandUpdate();
     }
     ,
     150
    );
   }

   function __stopExpand() {
    clearInterval(_interval);
   }

   function __expandUpdate() {
    var line_height = 16;   // 16px assumed
    var paddingBottom = 50;
    if ( _dummy == null ) { // create dummy
     _dummy = jQuery('<div></div>')
     _dummy.css(
      {
       'font-size':    jQuery(_self).css('font-size'),
       'font-family':  jQuery(_self).css('font-family'),
       'width':        jQuery(_self).css('width'),
       'padding':      jQuery(_self).css('padding'),
       'xline-height':  line_height,
       'overflow-x':   'hidden',
       'display':      'none'
      }
     ).appendTo('body');
    }
    // update html in dummy div
    var newHtml = jQuery(_self).val().replace(/\n/g, '<br />');
    if( _dummy.html() != newHtml ) {
     _dummy.html( newHtml );
     var newHeight = Math.max(_min_height, _dummy.height() + line_height + paddingBottom);
     if (_prevHeight != newHeight) {
      jQuery(_self).height(newHeight);
      //jQuery(_self).animate({height:(newHeight)}, 100);    // animate?
      _prevHeight = newHeight;
     }
    }
   }
  }
 );
}