/**
* inputTip - puts helper text in an input field
*/
(function(jQuery){
jQuery.fn.inputTip = function(options) {
	var defaults = { /* no options for now */  };
	// Extend our default options with those provided.
	var opts = jQuery.extend(defaults, options);
	jQuery(this).focus(function(){
		if (jQuery(this).val() == jQuery(this).attr('title')) {
			jQuery(this).val('');
		}
	});
	jQuery(this).blur(function(){
		if (jQuery(this).val() == '') {
			jQuery(this).val(jQuery(this).attr('title'));
		}
	});
	jQuery(this).blur();
};
})(jQuery);

jQuery(document).ready(function(){
	jQuery('#ballardSearch input:text').inputTip();
	jQuery('#sidebar #searchform input:text').inputTip();
	jQuery('#newsletterForm input:text').inputTip();
	// look for a code and stash in a cookie for this session only if found
	var paramKey = 'SourceCode';
	var defaultSourceCode = 'ISEO';

	var url = location.search;
	if (url.indexOf('?') > -1){
		var query = url.split('?').pop() + '&';
		if (query.toLowerCase().indexOf('sourcecode=') > -1){
			var pat = new RegExp(paramKey+"\=(.*?)\&","gi");
			var code = pat.exec(query);
			if (code != null){
				jQuery.cookie(paramKey, code[1], { path: '/' });
			}
		}
	}
	// add code to all ballarddesigns.com links
	var c = jQuery.cookie(paramKey);
	if (c == null){
		c = defaultSourceCode;
	}
	var href = '';
	jQuery('a[href*=ballarddesigns.com/]').each(function(){
		href = jQuery(this).attr('href');
		var hash = '';
		if (href.indexOf('#') > 0){
			hash = href.substring(href.indexOf('#'));
			href = href.substring(0, href.indexOf('#')-1);
		}
		// handle hashes
		href += ((href.indexOf('?') == -1) ? '?' : '&') + paramKey + '=' + c + hash;
		jQuery(this).attr('href', href);
	});
	
});

