
jQuery(document).ready(
 function(){
	setupRemoveDefault();
	jQuery("img").removeAttr("title", "");
	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");
	 }
	});
	if(jQuery("#secondaryMenu")) {
	 jQuery("#layout .typography").css("width", "500px");
	 jQuery("#layout .pageTitle").css("width", "500px");
	}
	if(jQuery("body#contact-us")) {
	 jQuery(
		function() {
		 jQuery.expandingTA(jQuery('#contact-us textarea')); // initial call to all elems of textarea.expanding
		}
	 );
	}
 }
);


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"));
			}
		 }
		);
	 }
	}
 );
}




/* CART STUFF */
function loadAjax(loadIntoElID, URL) {
 var imgHtml = '<img src="/mysite/javascript/loading.gif" alt="loading" />';
 jQuery("#" + loadIntoElID).html(imgHtml);
 if(!strpos( URL, "?")) {
	URL += "?"
 }
 URL += "&ts=" + new Date().getTime();
 window.setTimeout( function() {
	jQuery("#" + loadIntoElID).load(URL);
 }, 100);
 return true;
}

function highLightProduct(listItemID) {
 jQuery("#ProductList li").removeClass("currentProduct");
 jQuery("#" + listItemID).addClass("currentProduct");
}

function highLightPage(pageNumber) {
 jQuery("#PageViewer a").removeClass("current");
 jQuery("#pageChooser a").removeClass("current");
 jQuery("#pageNumber" + pageNumber).addClass("current");
}

function isInCartHighlight(id) {
 jQuery("#"+id).addClass("isInCart");
 jQuery("#image").addClass("isInCart");
 jQuery("#"+id+" span").text("Product in Cart");
 jQuery("#left span.cartIconSpan").text("Product in Cart");
}

function isInCartRemoveHighlight(id) {
 jQuery("#"+id).removeClass("isInCart");
 jQuery("#image").removeClass("isInCart");
 jQuery("#"+id+" span").html("Product not in Cart");
 jQuery("#left span.cartIconSpan").text("Product not in Cart");
}




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;
		 }
		}
	 }
	}
 );
}
function loadCloseUp(loadIntoElID, URL, URLSegment) {
 jQuery(document).ready(
	 function(){
		 highLightProduct('productListItem' + URLSegment);
		 var imgHtml = '<img src="/mysite/javascript/loading.gif" alt="loading" />';
		 jQuery("#" + loadIntoElID).html(imgHtml);
		 window.setTimeout( function() {
			 jQuery("#" + loadIntoElID).load(URL);
		 }
		 , 100);
		 return true;
	 }
 );
}

function strpos( haystack, needle, offset){
	var i = (haystack+'').indexOf( needle, offset );
	return i===-1 ? false : i;
}
