
////////////////////////////////////////////////////////////////////////////////
// Begin general support functions                                            //
////////////////////////////////////////////////////////////////////////////////

$.preloadImages = function() {
	for (var i = 0; i < arguments.length; i++) { 
		var img = new Image();
		img.src = arguments[i]; 
	} 
}

function include_js(script_filename) { // call by: include_js("URL")
	var html_doc = document.getElementsByTagName("head").item(0);
	var js = document.createElement("script");
	js.setAttribute("language", "javascript");
	js.setAttribute("type", "text/javascript");
	js.setAttribute("src", script_filename);
	html_doc.appendChild(js);
	return false;
}

////////////////////////////////////////////////////////////////////////////////
// End general support functions                                              //
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Begin myAWAI login functions                                               //
////////////////////////////////////////////////////////////////////////////////

function myawaiCheckCapsLock(e, t) {
	e = (e) ? e : window.event;
	var ismyawai = (t.id.indexOf("myawai") == 0);
	var w = (ismyawai) ? "div.capslockwarning" : "p#capslockwarning";
	var kc = ((e.which) ? e.which : e.keyCode), sk = e.shiftKey;
	$(w).hide().html("");
	if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122 ) && sk)) {
		$(w).show().html("Warning: your 'Caps Lock' is on and your password is case sensitive.");
	}
	if (ismyawai) { $("div.myawaicontrol").css("height","auto"); }
}

function myawaiLogin(f) {
	var m = (f.id.indexOf("myawaimenu") == 0) ? "myawaimenu-" : "";
	var u = "input#" + m + "username", p = "input#" + m + "password";
	if (!$(u).val() || $(u).val() == "username") {
		alert("Please enter your email address in the username field");
		$(u).focus();
		return(false);
	}
	if (!$(p).val() || ($(p).val() == "password" && ($(p).css("color") == "#aaaaaa" || $(p).css("color") == "rgb(170, 170, 170)"))) {
		alert("Please enter your password or click the 'forgot password' link if you have lost it");
		$(p).focus();
		return(false);
	}
	return(true);
}

function myawaiForgotPass(f) {
	var m = (f.indexOf("myawaimenu") == 0) ? "myawaimenu-" : "";
	var u = "input#" + m + "username", p = "input#" + m + "password";
	if ($(u).val() == "" || $(u).val() == "username") {
		alert("Please enter your email address in the username field");
		$(u).focus();
		return(false);
	}
	$(p).val("forgotten-it");
	$("form#" + f).submit();
	return(false);
}

function myawaiFillField(field) {
	var f = $(field);
	if (!f.val()) {
		if (f.hasClass("searchbox")) {
			f.val("Search...");
		}
		else {
			f.css("color","#aaaaaa");
			if (f.hasClass("subscribe-email")) {
				f.val("email address");
			}
			if (f.hasClass("subscribe-name")) {
				f.val("first name");
			}
		}
	}
}

function myawaiEmptyField(field) {
	var f = $(field);
	if ((f.hasClass("searchbox") && f.val() == "Search...")) {
		f.val("");
	}
	else if ((f.hasClass("subscribe-email") && f.val() == "email address") ||
	(f.hasClass("subscribe-name") && f.val() == "first name")) {
		f.css("color","#000000").val("");
	}
}

function myawaiChangePass() {
	var newpassword = "input#newpassword";
	var verifynewpassword = "input#verifynewpassword";
	var error = "";
	var str = $(newpassword).val();
	if (str.length < 5) {
		error = "Password must be at least 5 characters long";
	}
	for (k = 0; k < str.length; k++) {
		if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(str.charAt(k)) == -1) {
			error = "Password contains invalid characters!";
			break;
		}
	}
	if ($(newpassword).val() != $(verifynewpassword).val()) {
		error = "Passwords do not match";
	}
	if (error != "") {
		alert(error);
		$(newpassword).val('');
		$(verifynewpassword).val('');
		$(newpassword).focus();
		return(false);
	}
	return(true);
}

////////////////////////////////////////////////////////////////////////////////
// End myAWAI login functions                                                 //
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Begin Page load functions                                                  //
////////////////////////////////////////////////////////////////////////////////

function class_init(context){
	
	$("div.awai-info", context).each(function() {
		$(this).animate({backgroundColor:'#FEFFD1'}, 5000);
	});

	$("a.expandable", context).add("a.collapsible", context).add("a.expander", context).click(function() {
		var h = this.href;
		var a = h.substr(h.indexOf("#"));
		var div = $("div" + a);
		if ($(this).hasClass("expandable") || $(this).hasClass("collapsible")) {
			$(this).toggleClass("expandable").toggleClass("collapsible");
		}
		if (div.is(":visible")) {
			div.data("ref-height", div.height());
			div.animate({height:0}, {duration:"fast", complete:function() { $(this).hide(); }} );
		} else  {
			div.show().animate({height:div.data("ref-height")}, {duration:"fast"});
		}
		this.blur();
		return false;
	});

	$("a.expandall", context).click(function(){
		$("div.expandable", context).each(function(){
			var div = $(this);
			if (!div.is(":visible")) {
				div.show().animate({height:div.data("ref-height")}, {duration:"fast"});
			}
			div.addClass("collapsible").removeClass("expandable");
		});
		$("a.expandable", context).addClass("collapsible").removeClass("expandable");
		this.blur();
		return false;
	});

	$("a.collapseall", context).click(function(){
		$("div.collapsible", context).each(function(){
			var div = $(this);
			if (div.is(":visible")) {
				div.data("ref-height", div.height());
				div.animate({height:0}, {duration:"fast", complete:function() { $(this).hide(); }} );
			}
			div.addClass("expandable").removeClass("collapsible");
		});
		$("a.collapsible", context).addClass("expandable").removeClass("collapsible");
		this.blur();
		return false;
	});

	$("input.searchbox, input.subscribe-email, input.subscribe-name", context).each(function(){
		$(this).blur(function() {
			myawaiFillField(this);
			return true;
		});
		$(this).click(function() {
			myawaiEmptyField(this);
			return true;
		});
		$(this).focus(function() {
			myawaiEmptyField(this);
			return true;
		});
		myawaiEmptyField(this);
		myawaiFillField(this);
		return true;
	});

	$("a.myawaimenu-forgotpass", context).click(function(){
		myawaiForgotPass("myawaimenu-login");
		return false;
	});
	
	$("a.forgotpass", context).click(function(){
		myawaiForgotPass("login");
		return false;
	});
	
	$("a.js-hide", context).click(function(){
		var searchclass = this.rel || false;
		if (searchclass) {
			$("." + searchclass).hide();
		}
		this.blur();
		return false;
	});
	
	$("a.js-show", context).click(function(){
		var searchclass = this.rel || false;
		if (searchclass) {
			$("." + searchclass).show();
		}
		this.blur();
		return false;
	});
	
	$("a.js-toggle", context).click(function(){
		var searchclass = this.rel || false;
		if (searchclass) {
			$("." + searchclass).toggle();
		}
		this.blur();
		return false;
	});
	
	$("a.js_action", context).click(function(){
		var h = this.href || false;
		if (!h) {
			return false;
		}
		var actions = h.split('?')[1];
		if (!actions) {
			return false;
		}
		var actions = actions.split('&');
		for (var i = 0; i < actions.length; i++) {
			action = actions[i];
			parts = action.split('=');
			func = parts[0];
			param = parts[1];
			if (param) {
				params = param.split(',');
				param = '';
				for (var j = 0; j < params.length; j++) {
					if (param) {
						param = param + ",";
					}
					param = param + "'" + params[j] + "'";
				}
			}
			if (typeof window[func] != 'function') {
				continue;
			}
			eval("window[func](" + param + ")");
		}
		this.blur();
		return false;
	});
	
	$("a.js-confirm", context).click(function(){
		return js_confirm(this, "link");
	});

	$("form.js-confirm", context).submit(function(){
		return js_confirm(this, "form");
	});

	$("form.check-requirements", context).submit(function() {
		var complete = true;
		$(this).find(".required, select.required-if-list").each(function() {
			if ($(this).val() == "") {
				if (complete) {
					alert("Please fill in all required fields");
					if ($(this).attr("type") != "hidden") {
						$(this).focus();
					}
					complete = false;
				}
				if (!$(this).data("border-changed")) {
					$(this).data("previous-border-color", $(this).css("border-top-color"));
				}
				$(this).data("border-changed", true);
				$(this).css("border-color", "#ff0000");
				$(this).css("background-color", "#ffffcc");
			}
		});
		return complete;
	});
	
	$("form.check-requirements", context).find(".required, select.required-if-list").blur(function() {
		if ($(this).val() == "") {
			$(this).css("background-color", "#ffffcc");
		}
		else {
			$(this).css("background-color", "#ffffff");
			if ($(this).data("border-changed") && $(this).data("previous-border-color")) {
				$(this).css("border-color", $(this).data("previous-border-color"));
			}
		}
	});

    $("form.enter-submit input", context).keypress(function(e){
        if (e.keyCode == 13) {
			if (!$(this).attr("form").onsubmit || $(this).attr("form").onsubmit()) { // run onsubmit stuff if applicable
				$(this).parents("form").trigger("submit");
			}
 			return false;
       }
    });

	$("input", context).add("select", context).add("textarea", context).each(function() {
		showLength(this, false);
	}).keyup(function() {
		showLength(this, true);
/*
On keyup, force the "change" handler to trigger on blur, (sometimes the change doesn't trigger it)
*/
		$(this).blur(function() {
			$(this).triggerHandler("change");
		});
	}).click(function() {
		showLength(this, true);
		showNote(this);
	}).focus(function() {
		showNote(this);
	}).blur(function() {
		hideNote(this);
		showLength(this, false);
	});
		
	$("textarea.expanding", context).elastic();
	
	$("textarea.focus-expanding", context).one("focus", function() { $(this).elastic(); });
	
	$("button:has(a)", context).click(function() {
		var h = $(this).find("a").attr("href");
		window.location = h;
		return false;
	});

	$("input.copy-to-contact", context).change(function() {
		var id = $(this).attr("id").replace("_", "_contact_");
		if (!$("#" + id).val()) {
			$("#" + id).val($(this).val());
			$("#" + id).blur();
		}
	});

	$(".countries", context).change(function() {
		getStates($(this), true);
	});

	$(".comment-hidden", context).hide();
	$(".comment-content", context).focus(function() {
		$(".comment-hidden").show();
	});

	$("a.captcha_refresh", context).click(function(){
		$('.captcha_image').attr("src", "captcha.png?random=" + Math.random());
		this.blur();
		return false;
	});
	
	$("a.content-overlay", context).click(function() {
		var url = this.href;
		var temp_url = url.split("#");
		var anchor_hash = temp_url[1];
		temp_url = temp_url[0].split("?");
		var query = temp_url[1];
		var vars = new Array();
		if (query) {
			var vars = query.split("&");
		}
		vars.push("mode=content");
		query = vars.join("&");
		this.href = temp_url[0] + "?" + query;
		tb_show(this,"link");
		this.href = url;
		this.blur();
		return false;
	});

	$("a.remove-overlay", context).click(function() {
		if (self.parent.document.getElementById("TB_window")) {
			self.parent.tb_remove();
			return false;
		}
	});

	$(".clickable", context).each(function() {
		makeClickable(this);
	});
	
	$("a.no-click", context).click(function() {
		return false;
	});
	
	$("a.inline-video", context).each(function() {
		handleMedia(this, "video", "inline");
	});

	$("a.popup-video", context).click(function() {
		handleMedia(this, "video", "popup");
		return false;
	});

	$("a.hybrid-video", context).click(function() {
		handleMedia(this, "video", "hybrid");
		return false;
	});

	$("a.inline-audio", context).each(function() {
		handleMedia(this, "audio", "inline");
	});

	$("a.popup-audio", context).click(function() {
		handleMedia(this, "audio", "popup");
		return false;
	});

	$("a.hybrid-audio", context).click(function() {
		handleMedia(this, "audio", "hybrid");
		return false;
	});

	$("a", context).not(".nopop").not(".hybrid-video").not(".hybrid-audio").each(function() {
		var t = this, h = $(t).attr("href");
		var x = [location.hostname, "www.awaionline.com"];
		var p = $(t).hasClass("popup") ? true : false;
		if (!p && h && (h.indexOf("http:")!=-1 || h.indexOf("https:")!=-1)) {
			p = true;
			for (var i=0; i<=(x.length-1); i++){
				if (t.hostname.substr(t.hostname.length - x[i].length) == x[i]) {
					p = false;
					break;
				}
			}
		}
		if (p) {
			$(t).attr("target", "_blank");
/*			$(t).click(function(){ var w = window.open(h); w.focus(); return false; });*/
		}
	});
	
	$(".scrolltop", context).click(function() {
		var el = $(this), w = $(window);
		w.scrollTop(el.offset().top);
	});

/*	$(window).scroll(function() {
		$(".scrolltop").each(function() {
			var el = $(this), w = $(window);
			var f_1 = el.find("iframe").contents(), f_2 = f_1.find("iframe").contents();
			var els = el.add(el.find("*")).add(f_1.add(f_2).find("*"));
			els.one("click.scrolltop focus.scrolltop keypress.scrolltop", function() {
//				alert("test"); // DEBUG
				els.unbind("click.scrolltop focus.scrolltop keypress.scrolltop");
				w.scrollTop(el.offset().top);
			});
			f_1.add(f_2).unbind("scroll.scrolltop").bind("scroll.scrolltop", function() { w.scrollTop(el.offset().top); });
		});
	});*/

} // end of class_init() function

function test_function(parameter, parameter2) {
	alert(parameter + " - " + parameter2);
}

function print_date(){
	
	var mydate=new Date();
	var year=mydate.getFullYear();
	var day=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday");
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	$(".printdate").html(dayarray[day]+" "+montharray[month]+" "+daym+", "+year);

}

////////////////////////////////////////////////////////////////////////////////
// End Page load functions                                                    //
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Begin Misc. functions                                                      //
////////////////////////////////////////////////////////////////////////////////

function js_hide(searchclass) {
	if (searchclass) {
		$("." + searchclass).hide();
	}
	return true;
}
	
function js_show(searchclass) {
	if (searchclass) {
		$("." + searchclass).show();
	}
	return true;
}
	
function js_toggle(searchclass) {
	if (searchclass) {
		$("." + searchclass).toggle();
	}
	return true;
}
	
function js_confirm(th, type){
	var t = th.title || th.name || null;
	var g = th.rel || false;
	var answer = confirm(t+"\n\nAre you sure you want to do this?");
	if (answer){
		var url = (type == "form") ? th.action : th.href;
		url = url.split("?");
		var query = url[1];
		url = url[0];
		if (query) {
			var vars = query.split("&"); 
			var newquery = "";
			for (var i=0;i<vars.length;i++) { 
				var pair = vars[i].split("="); 
				if (pair[0] != "jf_confirm" && pair[0] != "lp_confirm") {
					if (newquery.length > 0) {
						newquery = newquery + "&";
					}
					newquery = newquery + vars[i];
				}
			}
			if (newquery.length > 0) {
				url = url + "?" + newquery;
			}
		}
		if (type == "form") {
			th.action = url;
			if (th.jf_confirm) {
				th.jf_confirm.value = 0;
			}
			if (th.lp_confirm) {
				th.lp_confirm.value = 0;
			}
		}
		else {
			th.href = url;
		}
		return true;
	}
	th.blur();
	return false;
}

function showNote(input) {
	if ($(input).attr("id")) {
		$("#" + $(input).attr("id") + "_note").oneTime(150, function() {
			$(this).show("fast");
		});
	}
}

function hideNote(input) {
	if ($(input).attr("id")) {
		$("#" + $(input).attr("id") + "_note").oneTime(150, function() {
			$(this).hide("fast");
		});
	}
}

function showLength(input, to_cursor) {
	if ($(input).attr("id")) {
		$("#" + $(input).attr("id") + "_length").stopTime("show_length").oneTime(150, "show_length", function() {
			var len = $(input).val().length;
			if (len) {
				var s = (len == 1) ? "" : "s";
				var message = "[<strong>" + len + "</strong> character" + s;
				var cursor = (to_cursor) ? getCursor(input) : 0;
				if (cursor > 0 && cursor < len) {
					message += " (<strong>" + cursor + "</strong> to cursor)";
				}
				message += "]";
				$(this).html(message);
			}
			else {
				$(this).html("");
			}
		});
	}
}

function getCursor(input) {
	if (input.setSelectionRange) {
		return input.selectionStart;
	}
	else if (document.selection != null) {
		var val = $(input).val();
		var range = document.selection.createRange();
		var len = range.text.length;
		var marker = '|';
		while (val.indexOf(marker) != -1) {
			marker = marker + '|';
		}
		range.text = marker + range.text;
		var val2 = $(input).val();
		var cursor = val2.indexOf(marker);
		$(input).val(val);
		var range = input.createTextRange();
		range.moveStart('character', cursor);
		range.moveEnd('character', (cursor + len - val.length));
		range.select();
		return cursor;
	}
	return false;
}

function getStates(countries, go_to_states) {
	var country_code = countries.val();
	var index = $("*").index( $("#" + countries.attr("id"))[0] );
	var countries_with_states = Array("AU", "BR", "CA", "EI", "NL", "UK", "US");
	var is_select = false;
	for (i=0; i<countries_with_states.length; i++) {
		if (country_code === countries_with_states[i]) {
			is_select = true;
			break;
		}
	}
	var attribute_names = Array("id", "class", "title", "style", "disabled", "name", "readonly", "value");
	var oldstates = $("*:lt(" + index + ")").filter(".states").slice(-1);
	var states = (is_select) ? $("<select></select>") : $("<input type=\"text\" />");
	for (i=0; i<attribute_names.length; i++) {
		states.attr(attribute_names[i], oldstates.attr(attribute_names[i]));
	}
	if (is_select) {
		states.load("/states.php?country=" + country_code, function() {
			states.val(oldstates.val());
		});
	}
	oldstates.replaceWith(states);
	$("*").unbind();
	class_init();
	if (go_to_states) {
		countries.blur();
		states.focus();
	}
}

function makeClickable(obj) {
	$(obj).addClass("clickable").click(function() {
		var t = this;
		var a = $(t).find("a");
		if (h = a.attr("href")) {
			if (a.attr("target") == "_blank") {
				var w = window.open(h); 
				w.focus(); 
				return false;
			}
			window.location = h;
			return false;
		}
	});
}

function handleMedia(a, type, placement) {
	var href = $(a).attr("href");
	var url_parts = href.split("?");
	var query = url_parts[1];
	var toolbox = "http://www.awaionline.com/toolbox/";
	var handler = toolbox + "quicktime.php";
	var width = (type == "audio") ? "300" : "320";
	var height = (type == "audio") ? "16" : "256";
	if (type == "audio") {
		if (href.indexOf(".wav") != -1) {
			width = "300";
			height = "20";
		}
		else {
			handler = toolbox + "audio.php";
		}
	}
	else {
		if (href.indexOf("viddler.com") != -1) {
			handler = toolbox + "viddler.php";
			width = "437";
			height = "348";
		}
		else if (href.indexOf("youtube.com") != -1) {
			handler = toolbox + "youtube.php";
			width = "425";
			height = "344";
		}
		else if (href.indexOf("player.vimeo.com") != -1) {
			handler = href;
			width = "400";
			height = "225";
		}
		else if (href.indexOf("vimeo.com") != -1) {
			handler = toolbox + "vimeo.php";
			width = "400";
			height = "225";
		}
	}
	if (href.indexOf("player.vimeo.com") != -1) {
		var newquery = "title=0&byline=0&portrait=0";
	}
	else {
		var newquery = "media=" + escape(href) + "&type=" + type;
	}
	if (query) {
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == "width") {
				width = pair[1];
			}
			if (pair[0] == "height") {
				height = pair[1];
			}
			newquery += "&" + vars[i];
		}
	}
	if (placement == "hybrid" || placement == "popup") {
		newquery += "&autoplay=true";
	}
	var frame_html = '<iframe src="' + handler + '?' + newquery + '" width="' + width + '" height="' + height + '" frameborder="0" scrolling="no" class="' + placement + '-player"></iframe>';
	$(".hybrid-player").remove();
	$(".hybrid-audio").add(".hybrid-video").show();
	if (placement == "inline" || placement == "hybrid") {
		var obj = $(frame_html);
		$(a).hide();
		$(a).after(obj);
		$(obj).css("cursor","default");
	}
	if (placement == "popup") {
//		newquery += "&popup=true";
		var h1 = 'AWAI Media Player';
		if (a.title) {
//			newquery += "&title=" + encodeURIComponent(a.title);
			h1 = a.title;
		}
		var windowid = Math.ceil(Math.random() * 10000);
		var windowwidth = (width * 1) + 60;
		var windowheight = (height * 1) + 200;
		var win = window.open('', windowid, 'width=' + windowwidth + ',height=' + windowheight + ',top=50,left=50,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
		win.document.write('<div style="text-align:center;">' + '<br />' + '<h1>' + h1 + '</h1>' + frame_html + '<br /><br />' + '<a href="javascript:window.close();">close this window</a>' + '</div>');
		win.focus(); 
//		var obj = window.open(handler + '?' + newquery, windowid, 'width=' + width + ',height=' + height + ',top=50,left=50,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
//		obj.focus(); 
	}
}


////////////////////////////////////////////////////////////////////////////////
// End Misc. functions                                                        //
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Begin jQuery Plugins                                                       //
////////////////////////////////////////////////////////////////////////////////

(function($) {

////////////////////////////////////////////////////////////////////////////////
// jQuery Elastic Textareas

	$.fn.extend({
		elastic: function() {
			var props = ["paddingTop", "paddingRight", "paddingBottom", "paddingLeft", "fontSize", "lineHeight", "fontFamily", "fontWeight", "border", "textAlign"];
			return this.each(function() {
				if (this.type != "textarea") { return false; }
				var el = $(this),
					div = $("<div />"),
					lh = parseInt(el.css("line-height"), 10) || parseInt(el.css("font-size"), 10),
					minh = parseInt(el.css("height"), 10) || lh * 3,
					maxh = parseInt(el.css("max-height"), 10) || Number.MAX_VALUE,
					goalh = 0,
					i = props.length;
				if (maxh < 0) { maxh = Number.MAX_VALUE; }
				div.appendTo(el.parent());
				while (i--) {
					div.css(props[i].toString(),el.css(props[i].toString()));
				}
				el.css({overflow: "hidden"});
				div.width(el.width());
				div.css({position:"absolute", display:"none", wordWrap:"break-word", whiteSpace:"pre-wrap"});
//				div.css({display:"block", background:"red"}); // DEBUG
				var update = function() {
					var elcontent = el.val();
					if (elcontent + "." != div.text()) {
						div.text(elcontent + ".");
						if (Math.abs(div.height() + lh - el.height()) > 3) {
							var goalh = div.height() + lh;
							var h = goalh, o = "hidden";
							h = Math.max(minh, Math.min(maxh, h));
							if (goalh >= maxh) { o = "auto"; }
							var y = Math.floor(parseInt(h, 10));
							if (el.height() != y) { el.css({height:y + "px", overflow:o}); }
						}
					}
				}
				el.css({overflow:"hidden"}).keyup(function() {
					update();
				}).live("input paste", function(e) {
					setTimeout(update, 250);
				});
				update();
			});
        }
    });

////////////////////////////////////////////////////////////////////////////////
// jQuery Timers

	$.fn.extend({
		everyTime: function(interval, label, fn, times, belay) {
			return this.each(function() {
				$.timer.add(this, interval, label, fn, times, belay);
			});
		},
		oneTime: function(interval, label, fn) {
			return this.each(function() {
				$.timer.add(this, interval, label, fn, 1);
			});
		},
		stopTime: function(label, fn) {
			return this.each(function() {
				$.timer.remove(this, label, fn);
			});
		}
	});
	
	$.extend({
		timer: {
			guid: 1,
			global: {},
			add: function(element, interval, label, fn, times, belay) {
				var counter = 0;
				
				if ($.isFunction(label)) {
					if (!times) 
						times = fn;
					fn = label;
					label = interval;
				}
				
				if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
					return;
	
				if (times && times.constructor != Number) {
					belay = !!times;
					times = 0;
				}
				
				times = times || 0;
				belay = belay || false;
				
				if (!element.$timers) 
					element.$timers = {};
				
				if (!element.$timers[label])
					element.$timers[label] = {};
				
				fn.$timerID = fn.$timerID || this.guid++;
				
				var handler = function() {
					if (belay && this.inProgress) 
						return;
					this.inProgress = true;
					if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
						$.timer.remove(element, label, fn);
					this.inProgress = false;
				};
				
				handler.$timerID = fn.$timerID;
				
				if (!element.$timers[label][fn.$timerID]) 
					element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
				
				if ( !this.global[label] )
					this.global[label] = [];
				this.global[label].push( element );
				
			},
			remove: function(element, label, fn) {
				var timers = element.$timers, ret;
				
				if ( timers ) {
					
					if (!label) {
						for ( label in timers )
							this.remove(element, label, fn);
					} else if ( timers[label] ) {
						if ( fn ) {
							if ( fn.$timerID ) {
								window.clearInterval(timers[label][fn.$timerID]);
								delete timers[label][fn.$timerID];
							}
						} else {
							for ( var fn in timers[label] ) {
								window.clearInterval(timers[label][fn]);
								delete timers[label][fn];
							}
						}
						
						for ( ret in timers[label] ) break;
						if ( !ret ) {
							ret = null;
							delete timers[label];
						}
					}
					
					for ( ret in timers ) break;
					if ( !ret ) 
						element.$timers = null;
				}
			}
		}
	});
	
	if ($.browser.msie) {
		$(window).one("unload", function() {
			var global = $.timer.global;
			for ( var label in global ) {
				var els = global[label], i = els.length;
				while ( --i ) $.timer.remove(els[i], label);
			}
		});
	}
	

////////////////////////////////////////////////////////////////////////////////
// jQuery UI Plugins

////////////////////////////////////////////////////////////////////////////////
// jQuery UI Tabs Extension

	if (typeof($.ui) == "object") {
		
		$.extend($.ui.tabs.prototype, {
			fit: function(equal) {
				var self = this;
				var fit = self._fit || (self._fit = function() {
					var ul = self.list, a = self.anchors, li = self.lis,  n = li.length,
						s = {pl:"padding-left", pr:"padding-right", bl:"border-left-width", br:"border-right-width", ml:"margin-left", mr:"margin-right"};
					var pad = c(ul, s.pl) + c(ul, s.pr);
					$(li[n - 1]).css(s.mr, 0);
					a.css(s.pl, 0).css(s.pr, 0).css("text-align", "center");
					for (var i = awt = mrp = 0, aw = []; i < n; awt += aw[i], i++) {
						for (var j = 0, e = [], el = [$(li[i]), $(a[i])]; j <= 1; j++) {
							for (var k in s) { e[k] = c(el[j], s[k]); }
							pad += e.bl + e.br;
							if (j == 0) { pad += Math.max(mrp, e.ml); mrp = e.mr; }
							else { pad += e.ml + e.mr; aw[i] = el[j].innerWidth(); }
						}
					}
					var x = ((ul.innerWidth() - 1) - pad - ((equal) ? 0 : awt));
					var y = Math.floor(x / n), z = x % n;
					for (var i = 0; i < n; i++) {
						$(a[i]).width(y + ((equal) ? 0 : aw[i]) + ((z > i) ? 1 : 0));
					}
				});
				var c = (function(el, p) {
					var v = Math.round(el.css(p).replace("px","")) || 0; el.css(p, v); return v;
				});
				fit(); $(window).resize(fit);
			}
		});
		
		$(function() {
			$(".tabs").tabs();
			$(".tabs-fit").tabs().tabs("fit");
			$(".tabs-equal").tabs().tabs("fit", true);
		});
	}

})(jQuery);

////////////////////////////////////////////////////////////////////////////////
// End jQuery Plugins                                                         //
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Call page load functions                                                   //
////////////////////////////////////////////////////////////////////////////////

$(function() {
	class_init(document);
	print_date();
	$("div.expandable").each(function() {
		$(this).data("ref-height", $(this).height());
		$(this).hide().css({ height:0 });
	});
	$("form .input-note").hide();
	$(".countries").each(function() {
		getStates($(this), false);
	});
});

