/**
 * Library for user-interface functions
 * 
 */

function makeInputClearOnFocus(element){
	jQuery(element).bind('focus', function(){
		var stored_val = jQuery(this).val();
		jQuery(this).val("");
		jQuery(this).bind('blur', function(){
			if(jQuery(this).val() == ""){
				jQuery(this).val(stored_val);
			} else {
				stored_val = jQuery(this).val();
			}
		});	
	});	
};

function bindFormSubmit(element){
	jQuery(element+' .s-btn-wrap').bind('click', function(){
		jQuery(this).parent().get(0).submit();
	});
};

function scroller(lPath, speed) {
	if (!speed) {
		var speed = 400;
	}
	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || lPath;
		if ( lPath == thisPath
			&& (location.hostname == this.hostname || !this.hostname)
			&& this.hash.replace(/#/,'') ) {
		  		var $target = $(this.hash), target = this.hash;
	  			if (target) {
					var targetOffset = $target.offset().top;
					$(this).click(function(event) {
			  			event.preventDefault();
			  			$('html, body').animate({scrollTop: targetOffset}, speed, function() {
							location.hash = target;
			  			});
					});
		  		}
			}
	});
};

function filterPath(string) {
  return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
};
