$(document).ready(function(){
	// Flash h1 replacement using jQuery Flash plugin
	// http://jquery.lukelutman.com/plugins/flash/
	$('h1').flash(
		{ src: '/includes/swf/trajan_pro.swf', flashvars: { css: ['* { color: #4E6B8D; }'].join(' ')}},
		{ update: false },
		function(htmlOptions) {
			htmlOptions.flashvars.txt = this.innerHTML;
			this.innerHTML = '<span>'+this.innerHTML+'</span>';
			$("div#imageHolder").css({'margin-top' : '-15px'});
			var $alt = $(this.firstChild);
			htmlOptions.align = "absbottom";
			htmlOptions.height = $alt.height();
			htmlOptions.width = $alt.width();
			htmlOptions.wmode = "transparent";
			$alt.addClass('alt');
			$(this)
				.addClass('flash-replaced')
				.prepend($.fn.flash.transform(htmlOptions));						
		}
	);

	// information bars at top of page
	if ($("#message").html().length > 40) { $("#message").show(); };
	$("#message").click(function () { $("#message").slideUp("slow", function () { autoAdjustMenus(); }); });
	if ($("#warning").html().length > 40) { $("#warning").show(); };
	$("#warning").click(function () { $("#warning").slideUp("slow", function () { autoAdjustMenus(); }); });
	if ($("#error").html().length > 40) { $("#error").show(); };
	$("#error").click(function () { $("#error").slideUp("slow", function () { autoAdjustMenus(); }); });
	
	// auto adjust all drop-down menus to appear in correct place
	autoAdjustMenus();

	// main navigation rollover code
	// caters for rollovers on main menu and subsequent dropdown menu
	$("ul#mainnav li").mouseover( function(){ showMenu($("div#" + $(this).attr('id') + "_drop")); } );
	$("ul#mainnav li").mouseout( function(){ closeMenu(); } );
	$("div.dropdown").mouseover( function(){ showMenu($(this)); } );
	$("div.dropdown").mouseout( function(){ closeMenu(); } );

	// calendar rollover code
	// caters for rollovers on homepage calendar and subsequent dropdown menu
	$("div#calendar a.cal_date").mouseover( function(){ showCalendar("div#" + $(this).attr('id') + "_popup"); } );
	$("div#calendar a.cal_date").mouseout( function(){ closeCalendar(); } );
	$("div.cal_popup").mouseover( function(){ showCalendar($(this)); } );
	$("div.cal_popup").mouseout( function(){ closeCalendar(); } );

	// if link rel = external then open link in a popup window. Rather than have JS function call inside link onclick
	$('a[rel="external"]').click(function () { $(this).attr({'target':'_blank'}); });

	// image rotation on right hand side of pages
	if ($("#s1").length > 0) {
		$('#s1').cycle({ 
		    fx: 'fade', // one of: blindX, blindY, blindZ, cover, curtainX, curtainY, fade, fadeZoom, growX, growY, scrollUp, scrollDown, scrollLeft, scrollRight, scrollHorz, scrollVert, shuffle, slideX, slideY, toss, turnUp, turnDown, turnLeft, turnRight, uncover, wipe, zoom
		    timeout: 5000,  // milliseconds between slide transitions (0 to disable auto advance) 
		    speed: 1000,  // speed of the transition (any valid fx speed value) 
		    sync: 1     // true if in/out transitions should occur simultaneously 
		});
	};

	// SELECT element for the number of children to be enrolled to display the appropriate EnrolChild# OL elements
	$("select#EnrollChildSelection").change(function() {
		$("ol#EnrollChild2, ol#EnrollChild3").hide();
										   
		switch( $(this).val() ) {
			case "2":
				$("ol#EnrollChild2").show("fast");
				break;
			case "3":
				$("ol#EnrollChild2, ol#EnrollChild3").show("fast");
				break;				
		}
	});

	// comment blog form submit
	$("#submit_comment").click(function(event){
		var showAlert = '';

		if ($("#cName").val() == '') { showAlert += "- Your Name\n"; };
		if ($("#cEmail").val() == '') { showAlert += "- Your Email Address\n"; };
		if ($("#cComment").val() == '') { showAlert += "- Your Comment\n"; };
		if ($("#cSecurity").val() == '') { showAlert += "- Security Code"; };

		if (showAlert != '') {
			alert("Your comment could not be submitted. Please check the following:\n" + showAlert);
			return false;
		} else {
			// check captcha code via AJAX request
			$.ajax({
				type: "POST",
				url: "/includes/ajax/captcha_check.asp",
				data: "cSecurity=" + $("#cSecurity").val(),
				success: checkCaptchaReturn
			});
			return false;
		};
	});
	
	$(function() {
	    $("#maingallery a").lightBox();
	});

	// "bug" fix/hack where side navigation was taller than wrapper. Page background would not repeat down page correctly
	if ($("#navigation").height() > $("#wrapper").height()) {
		var menuHeight = $("#navigation").height();
		$("#wrapper").css( { "minHeight": menuHeight + "px" } )
	};
	
	// employment form - preshow or hide additional form fields
	if ($("form#employForm").length > 0) { 
		$("li.fTitle_related").hide();
		$("input#fDiscover_other").hide();
		$("textarea#fHealth_explain").hide();
		if ($("select#fTitle").val() == 'Other') { $("li.fTitle_related").show(); }; 
		if (($("input#fDiscover_other").val().length > 0) || ($("input[name='fDiscover']:checked").val() == 'Other')) { $("input#fDiscover_other").show(); };
		if (($("textarea#fHealth_explain").val().length > 0) || ($("input[name='fHealth']:checked").val() == 'yes')) { $("textarea#fHealth_explain").show(); };
	};



	// show or hide "Other" text field on employment form - title
	$("select[name='fTitle']").change(function(){
		if ($(this).val() == 'Other') { $("li.fTitle_related").show("fast"); } else { $("li.fTitle_related").hide(); };
	});
	
	// show or hide "Other" text field on employment form
	$("input[name='fDiscover']").click(function(event){
		if ($("input[name='fDiscover']:checked").val() == 'Other') { $("input#fDiscover_other").show("fast"); } else { $("input#fDiscover_other").hide(); };
	});

	// show or hide "Other" textarea on employment form
	$("input[name='fHealth']").click(function(event){
		if ($("input[name='fHealth']:checked").val() == 'yes') { $("textarea#fHealth_explain").show("fast"); } else { $("textarea#fHealth_explain").hide(); };
	});
	
});


// functions to show and hide dropdown menus
var TID, TID_cal;

function hideAll () {
	$("ul#mainnav li").each(function() { $("div#" + $(this).attr('id') + "_drop").hide(); });
}

function showMenu (obj) {
	if (TID != "undefined") { clearTimeout(TID); };
 	hideAll();
	$(obj).show();
}

function closeMenu () {
	TID = setTimeout(function() { hideAll(); }, 1000);
}

function checkCaptchaReturn (responseXML) {
    // 'responseXML' is the XML document returned by the server; we use
    // jQuery to extract the content of the message node from the XML doc

	var captchaStatus = $('status', responseXML).text();
	var captchaMessage = $('message', responseXML).text();
	switch (captchaStatus) {
		case "ok":
//	  		alert(captchaMessage);
			$('#commentForm').submit();
	  		break;
		case "error":
			$("#cSecurity").val('');
			$("img#captcha").attr("src", "/includes/captcha.asp?random=" + new Date().getTime());
	  		alert(captchaMessage);
	  		break;
		default:
	  		alert(captchaMessage);
	}
}

function autoAdjustMenus() {
	$("ul#mainnav li").each(function() {
		var pos = $(this).offset();  
  		$("div#" + $(this).attr('id') + "_drop").css( { "left": (pos.left - 1) + "px", "top":(pos.top + $(this).height() + 0) + "px" } );
	});
}

function hideCalendars() {
	$("div.cal_popup").each(function() { $(this).hide(); });
}
		
function showCalendar(obj) {	
	//alert(obj);
	if (TID_cal != "undefined") { clearTimeout(TID_cal); };
 	hideCalendars();
	$(obj).show();
}

function closeCalendar() {
	TID_cal = setTimeout(function() { hideCalendars(); }, 1000);
}