/*
Base 1.0

note:
This file contains default scripts that are used throughout the site.
1. add <head> link to this file
2. call loadBase function within jquery

Contents:
- loadBase / quickcontact
- setJS
- Additional validation methods
- phone mask
- form highlights

This file compiled by Manning.
Last modified 12/3/2009 MJC
*/

// the rest of the functions in this file should be added here
function loadBase(theme_dir, quickContact) {
	setJS();
	initMenus(); // accordion menus from 'accordion.js'	
	
	// quick contact form
	quickContact = typeof(quickContact) != 'undefined' ? quickContact : true; // sets default value of 'quickContact' to true	
	if (quickContact == true) { // if true, loads quick contact form
		$("#quick_contact_wrap").load("/wp-content/themes/"+theme_dir+"/includes/form_quick_contact.php", {limit: 25}, function(){
			// setFormValidation(['quick_contact']);
			$("#quick_contact").validate({
				rules: {
					single_name: {
						required: true,
						minWords: 2
					}
				}
			});
		});
	}
}


// adds class="js" to body to simplify coding of CSS styles written for javascript
function setJS() {
	var addClass = document.getElementsByTagName("body");
	for (var i=0; i<addClass.length; i++) {
		addClass[i].className = "js";
	}
}

// Acordion Menu
function initMenus() {
	$('ul.accordion ul').hide();
	$.each($('ul.accordion'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.accordion li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}

// Additional form validation methods
$(function(){
	// phone validation
	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.");
	
	// minimum words validation
	jQuery.validator.addMethod("minWords", function(value, element, params) { 
		return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
	}, jQuery.validator.format("Please enter first and last name."));
	
	// letters, number, spaces or underscores
	jQuery.validator.addMethod("alphanumeric", function(value, element) {
		return this.optional(element) || /^\w+$/i.test(value);
	}, "Letters, numbers, spaces or underscores only please.");
	
	// zipcode at least 5 digits and may contain a hyphen
	jQuery.validator.addMethod("ziprange", function(value, element) {
		return this.optional(element) || /^[0-9-]{5,}$/.test(value); //  /^\d{5,}$/
	}, "Your Zipcode must be at least 5 digits.");
	
	/* validation function includes additional methods */
	function form_validate(form_class) {
		$("form .phone").mask("999-999-9999",{placeholder:"_"}); /* phone mask */
		$("form .ssn").mask("999-99-9999",{placeholder:"_"}); /* ssn mask */
		$(form_class).validate({
			rules: {
				phone: {
					required: true,
					phoneUS: true
				},
				single_name: {
					required: true,
					minWords: 2
				},
				zipcode: {
					required: true,
					ziprange: true
				},
				lease_or_finance: {
					required: true
				}
			}
		});
	}
	
	/* apply validation */
	form_validate(".form_validate");
	
	/* form highlights */
	if ($.browser.msie && $.browser.version <= 6 ) { } else {
		$('.form_highlight input[type!=submit],.form_highlight select,.form_highlight textarea').focus(function() {
			$(this).parent().addClass('form_field_highlight')
		});
		$('.form_highlight input[type!=submit],.form_highlight select,.form_highlight textarea').blur(function(){
			$(this).parent().removeClass('form_field_highlight')
		});
	}
});