$(function() {
	//hide items on page load
	$('.hide-on-load').hide();
	
	//apply clear input plugin to all inputs with class ".clear-input"
	$('input.clear-input').clearInput();
	
	//activate form validation for forms without a manual-validation class
	if ($("form:not(.manual-validation)").length > 0) {validate_form = $("form:not(.manual-validation)").validator({position: 'bottom left', offset: [-1, 0]});}
	
	//scroll to the first error when form submitted
	$("form").bind("onFail", function(e, errors)  {
		
		//re-enable submit buttons
		$('input[type=submit]').removeAttr('disabled');
		
		//don't scroll if form is in overlay
		if (!($("#exposeMask").is(':visible'))) {
			$.each(errors, function(i,e) {
			if( i == 0 ) {
				var inputTop = this.input.offset().top - 90;
				$('html, body').animate({scrollTop: inputTop}, 'slow');
			}
		});
		}
	});
	
	//disable submit button when pressed temporarily to prevent multiple submissions
	$('form').submit(function(){
		$('input[type=submit]').attr('disabled', 'disabled');
		setTimeout(enableButton, 10000);
	});
	function enableButton(){
		$('input[type=submit]').removeAttr('disabled');
	}
	
	//effects for url input button
	if ($("#submit")) {
		//preload images
		$("").attr("src","button-hover.gif");
		$("").attr("src","button-click.gif");
		$('#submit').hover(
			function(){$(this).attr({src : '_share/img/button-hover.gif'})},
			function(){$(this).attr({src : '_share/img/button.gif'})}
		);
		$('#submit').click(
			function(){$(this).attr({src : '_share/img/button-click.gif'});
			window.setTimeout(submitClick, 50);
			window.setTimeout("return true", 50);
			}
		);
	}
	function submitClick(){$("#submit").attr({src : '_share/img/button-hover.gif'})}
	
	//analytics for url input form
	$("form#urlForm").bind("submit", function() {
		if (validate_form.data("validator").checkValidity()){
			try { _gaq.push(['_trackPageview', '/goal/get-started/'+$("#urlInput").val()]); }
			catch(e){}
		}
	});
	
});

//default input clear
$.fn.initialValue = function(value) {
	if (value) {
		return this.attr('initial-value', value);
	} else {
		return this.attr('initial-value');
	}
};

$.fn.clearInput = function() {
	return this
		.focus(function(){
			//exception case for default url field
			if (this.value == 'http://www.MyGreatEvent.com') {
				this.value = 'http://';
			}
			//exception case for event titles
			else if ($(this).hasClass('event-title') && this.value != 'Event Title'){
				return false;
			}
			else if (this.value == $(this).initialValue()) {
				this.value = '';
			}
		})
		.blur(function(){
			if (this.value == '') {
				this.value = $(this).initialValue();
			}
		})
		.each(function(index, elt) {
			$(this).initialValue(this.value);
		});
};
