$(function() {
	anchorslider.init();
	challenges.init();
	hash_check.init();
});

hash_check = {
	/** 
	 * Initalize stuff when page loads 
	 */
	"init": function() {
		this.section = self.location.hash;
		this.make_active();
		setInterval("hash_check.check()", 100);
	},
	
	/** 
	 * Check the hash and add the appropriate active tag 
	 */
	"check": function() {
		if ((self.location.hash != '') && (self.location.hash != this.section) && $("html:not(:animated),body:not(:animated)").length) {
			this.section = self.location.hash;
			this.make_active();
		} 
	},
	
	/** 
	 * Make the appropriate nav section active 
	 */
	"make_active": function() {
		$("#nav li a").removeClass('active');
		if (this.section != '') {
			window.location = this.section;
			$("." + this.section.substring(1) + " a").addClass('active');
		} else {
			window.location = "#home";
			$("#nav li.home a").addClass('active');
		}
	}
}

anchorslider = {
	"init": function() {
		$("#nav li a, .slide").click(function() {	
			elementClick = $(this).attr("href");
			$("#nav li a").removeClass('active').parent().filter("." + elementClick.substring(1)).children().addClass('active');
			destination = ($(elementClick).offset().top);
			$("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, 600, '', function(){
				self.location.hash = elementClick;
			});
			return false;
		});
	}
}

challenges = {
	forms:        $("form"),
	book_check:   $("input[type='radio']"),
	quote_field:  $("#quote"),
	path:         document.location.host,
	
	/** 
	 * Initalize stuff when page loads 
	 */
	"init": function() {
		// Add the two separate forms to the object
		this.quote_form      = this.forms.filter("#quote_form");
		this.newsletter_form = this.forms.filter("#newsletter_form");
		
		// Show the 2 radio buttons
		this.book_check.parent().show();
		
		// Add click handler to the radio buttons
		this.book_check.click(function() {	
			challenges.book_check_select($(this));
		});
		
		// Add submit handler to the last name quote form
		this.quote_form.submit(function() {	
			challenges.quote_form_submit();
			return false;
		});
		
		// Add validation to the newsletter form
		this.newsletter_form.validate();
		
		// Show the checked radio button content on load
		$("input[type='radio']:checked").parent().next().slideDown();
		
		// Clear any data in text fields on load
		this.forms.find('input[type="text"]').val('');
	},
	
	/** 
	 * When the radio button is selected,
	 *     - Remove any messages
	 *     - Hide the radio button content
	 *     - Show the content for the radio button that was clicked   
	 */
	"book_check_select": function(selected) {
		this.remove_messages();
		var message = selected.parent().next();
		if (!message.is(':visible')) {
			this.book_check.parent().next().slideUp();	
			selected.parent().next().slideDown();
		}
		
	},
	
	/** 
	 * Loop through the newsletter fields to get name/value of each
	 */
	"get_data": function() {
		var form_data = {};
		
		this.forms.find('input[type="text"]').each(function(i, field) {
			form_data[$(field).attr("name")] = $(field).val();
		});
		
		return form_data;
	},
	
	
	/** 
	 * When the last name quote form is submitted,
	 *     - Remove any messages
	 *     - If they entered the correct last name, hide the quote form and show the newsletter form
	 *     - If they did not enter the correct last name, show them an error message
	 */
	"quote_form_submit": function() {
		this.remove_messages();
		
		if (md5(this.quote_field.val().toLowerCase()) == '01bf662cc7f201097e1eacdb1d67ad87') {
			this.quote_form.slideUp();
			this.newsletter_form.slideDown();
		} else {
			this.quote_field.parent().before(
				$('<div class="message error">The last name entered is incorrect</div>').fadeIn(500)
			);
		}
	},
	
	/** 
	 * Removes any elements with the class "message"
	 */
	"remove_messages": function() {
		this.forms.find('.message').remove();
	}
}
