$(document).ready(function() {
	overlay();
	formlabels()
	//entry_form_submit();
	validation();
});

function overlay() {
	var overlays = $('div.overlay_wrapper');
	$(overlays).hide();
	
	for (var i=0,j=overlays.length; i<j; i++) {
		var overlay = overlays[i];
		overlay.className += ' overlay_initialized';
		var id = overlay.id;
		for (var i2=0,j2=document.links.length; i2<j2; i2++) {
			var re = '.*#' + id + '$';
			var link = document.links[i2];
			if (link.href.match(re)) {
				link.overlay = overlay;
				$(link).bind('click',function() {	
					$(this.overlay).fadeIn('fast');
					return false;
				});
			}
		}
		for (var i2=0,j2=overlay.getElementsByTagName('a').length; i2<j2; i2++) {
			var re = '.*#wrapper$';
			var link = overlay.getElementsByTagName('a')[i2];
			if (link.href.match(re)) {
				link.overlay = overlay;
				$(link).bind('click',function() {	
					$(this.overlay).fadeOut('medium');
					return false;
				});
			}
		}
	}
}

function formlabels() {
	var containers = $('.innerlabel');
	for (var i=0,j=containers.length; i<j; i++) {
		var container = containers[i];
		var labels = container.getElementsByTagName('label');
		for (var i2=0,j2=labels.length; i2<j2; i2++) {
			var label = labels[i2];
			label.input = document.getElementById(label.htmlFor);
			for (var i3=0,j3=label.childNodes.length; i3<j3; i3++) {
				var node = label.childNodes[i3];
				label.input.value = node.data;
				label.input.defaultValue = node.data;
			}
			$(label.input).bind('focus',function() {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
			});
			$(label.input).bind('blur',function() {
				if (this.value == '') {
					this.value = this.defaultValue;
				}
			});
		}
	}
}




function validation() {
	
	jQuery.validator.messages.required = "";
	
	// a custom method making the default value for first_nm/last_nm invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number");
	
	
	$("#entryform").validate({
		rules: {
			"entry_form[first_nm]": { required: true },
			"entry_form[last_nm]": { required: true },
			"entry_form[zip_code]": { required: true },
			"entry_form[email_addr]": { required: true, email: true },
			"entry_form[email_addr_confirmation]": { required: true, email: true, equalTo: "#email" },
			"entry_form[phone]": { required: true, phoneUS: true },
			"entry_form[agree_rules]": { required: true }
		},
		messages: {
			"entry_form[email_addr]": { 
				email: "Your email address must be in the format of name@domain.com"
			},
			"entry_form[email_addr_confirmation]": { 
				email: "Your email address must be in the format of name@domain.com",
				equalTo: "Please verify your email address is correct"
			},
			"entry_form[phone]": {
				phoneUS: "You must enter a valid phone number (ie: 222-333-4444)"
			},
			"entry_form[agree_rules]": { 
				required: "You must agree to the terms of the rules"
			}
			
		},
		submitHandler: function(form) {
			$("#submit_button_div").html('<div class="throbber"><img src="/images/throbber.gif"> Saving...</div>');
			$.post($(form).attr("action") + '.js', $(form).serialize(), null, "script");
			return false;
		},
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				//var message = errors == 1
				//? 'You missed 1 field. It has been highlighted below'
				//: 'You missed ' + errors + ' fields.  They have been highlighted below';
				//$("div.error span").html(message);
				//$("div.error").show();
				//alert('blah');
				//$("#zip").addClass('error');
				$("#req_text").addClass('req_text_error');
				
			} else {
				//$("div.error").hide();
				$("#req_text").removeClass('req_text_error');
			}
		}/*,
		showErrors: function(errorMap, errorList) {
				//$("#summary").html("Your form contains "
		        //                           + this.numberOfInvalids() 
		        //                           + " errors, see details below.");
				//this.defaultShowErrors();
				//$("#zip").addClass('error');
				
			}
			*/
		
		
		
	});
	
	$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
	};
	
}


// Google Analytics
// [idpk] Changed by Bryan B 2009-05-05 (Moved to app/views/entry_form/_analytics.html.erb)
//try {var pageTracker = _gat._getTracker("UA-5011342-7"); pageTracker._trackPageview(); } catch(err) {}
