/**
 * TK Javascript
 * 
 * @author klokie <@klokie.com>
 * @requires JQuery
 */
/**
 * global variables
 */
var preloadFlag = false, area = 'none', showing = 'none', timer = 0;
var menu = {
	about : [ 'howwework', 'kim_colley', 'location', 'terms', 'faq' ],
	clients : [ 'testimonials' ],
	boys : [ 'b_under5s', 'b_6-7years', 'b_8-9years', 'b_10-12years',
			'b_13plus' ],
	girls : [ 'g_under5s', 'g_6-7years', 'g_8-9years', 'g_10-12years',
			'g_13plus' ],
	families : [],
	siblings : [],
	applications : [ 'general', 'parents_testimonials', 'faq' ]
};

/**
 * jQuery loader:
 */
$(document).ready( function() {
	preloadImages();
	menu_init();

	// zebra striping
		$('table.quotes tr').filter(':even').addClass('odd').end().filter(
				':odd').addClass('even');
	});

/**
 * stops spammers in their tracks
 */
function email(name, domain, tld) {
	window.location.href = "mail" + "to" + ":" + name + String.fromCharCode(64)
			+ domain + String.fromCharCode(46) + tld;
}

function validateEmail(emailField, errorMsg) {
	var emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	if (!emailpat.test(emailField.value)) {
		alert(errorMsg);
		emailField.focus();
		emailField.select();
		return false;
	}
	return true;
}

function popup(url, w, h, name) {
	var w_name = (name != null ? name : 'microsite');
	var w_defaults = 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no'
			+ ',width=' + (w > 0 ? w : 800) + ',height=' + (h > 0 ? h : 600);
	self.open(url, w_name, w_defaults);
}

// on load = determine current area by url split
function menu_init() {
	area = self.location.pathname.substring(1).split('.')[0]; // get url area
	menu_on(area); // reset subnav
	document.getElementById('tblMainSub').style.display = 'block'; // unhide
}

// on mouseover = toggle on associated menu, toggle off others
function menu_on(id) {
	var target, i, j, k, el;
	if (timer != 0)
		self.clearTimeout(timer);
	if (id == showing)
		return;
	showing = id;
	for (i in menu) {
		for (j = 0, k = menu[i].length; j < k; ++j) {
			el = document.getElementById(menu[i][j]);
			if (el != null)
				el.style.display = 'none';
		}
	}
	if (menu[id] != null) {
		for (j = 0, k = menu[id].length; j < k; ++j) {
			el = document.getElementById(menu[id][j]);
			if (el != null)
				el.style.display = 'inline';
		}
	}
}
// on mouseout = wait 5 seconds; toggle on current menu, toggle off others
function menu_off() {
	timer = self.setTimeout('menu_on("' + area + '")', 2000); // reset menus
}

/**
 * ImageReady Preload Script
 */
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for ( var i = 0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
		}
	}
}

function preloadImages() {
	if (document.images) {
		nav_main_about_o = newImage("images/nav_main_about-o.png");
		nav_main_boys_o = newImage("images/nav_main_boys-o.png");
		nav_main_girls_o = newImage("images/nav_main_girls-o.png");
		nav_main_families_o = newImage("images/nav_main_families-o.png");
		nav_main_siblings_o = newImage("images/nav_main_siblings-o.png");
		nav_main_etc_o = newImage("images/nav_main_etc-o.png");
		preloadFlag = true;
	}
}
