var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() { 
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}
function jsddm_close() {
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}
function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}
function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function updateAdvancedSearchForm() {
	type = $("#formtype").val();	
	if (type != undefined) {
		// hide venue
		if (type == 'schoolleavers' && type != '') { 
			$("#row5").fadeOut("fast"); 
			$("#row5 select option[value='SC']").attr('selected', 'selected'); 
		} else { 
			$("#row5").fadeIn("fast"); 
		}
		// hide leisure course option
		if (type != 'parttime' && type != '') { 
			$("#row6").fadeOut("fast"); 
			$('input[type=checkbox]').attr('checked', false);
		} else { 
			$("#row6").fadeIn("fast"); 
		}
		// hide day checkboxes
		if (type != 'parttime' && type != '') { 
			$("#row7").fadeOut("fast"); 
			$('input[type=checkbox]').attr('checked', false);
		} else { 
			$("#row7").fadeIn("fast"); 
		}
		// hide time checkboxes
		if (type != 'parttime' && type != '') { 
			$("#row8").fadeOut("fast"); 
			$('input[type=checkbox]').attr('checked', false);
		} else { 
			$("#row8").fadeIn("fast"); 
		}	
		// redraw the drop shadows
		$(".dropshadow").redrawShadow();
		setTimeout('$(".dropshadow").redrawShadow()', 500); // again just in case
	}
}

$(document).ready(function() {  

	$(document).pngFix();
	
	$(".dropshadow").dropShadow({left: 0, top: 0, blur: 1, opacity: 0.6});				
	
	$(".fancybox").fancybox();
	
	$(".fancyframe").fancybox({
		'zoomSpeedIn': 0, 
		'zoomSpeedOut': 0, 
		'overlayShow': true ,
		'frameWidth': 480,
		'frameHeight': 380
	});	
	
	$("#toggleLink").click(function() {
		$(this).fadeOut();
		$("#toggleBox").fadeIn();
		$(".dropshadow").redrawShadow();
		return false;
	});
		
	$("tr.foldouttoggle").click(function(){
		$(this).next("tr").toggle();
		$(this).toggleClass("foldoutopen");
	});	
	
	$(".activetab").parent().parent().css("display", "inline");
	$(".activetab").parent().children().css("display", "inline");
	$(".activetab").parent().parent().parent().children().css("display", "inline");
	
	updateAdvancedSearchForm();
	
	$("#formtype").change(function () {
		updateAdvancedSearchForm();
	});
	
	$("#day_all").click(function()	{
		var checked_status = this.checked;
		$(".daycheckbox").each(function()
		{
			this.checked = checked_status;
		});
	});
	
	$("#time_all").click(function()	{
		var checked_status = this.checked;
		$(".timecheckbox").each(function()
		{
			this.checked = checked_status;
		});
	});
	
	$("#formtype").change(function(){
		$.getJSON('/ajax/coursetypes.php', 'type='+$(this).attr("value"), function(data) {
			$("#formarea").html(data.html);
		});
	});
	
	$('#jsddm > li').bind('mouseover', jsddm_open)
	$('#jsddm > li').bind('mouseout',  jsddm_timer)
	
	
	/*$("form").submit(function () {
		error = false;
		i = 0;		
		$('.required', this).each(function() {
			if ($(this).val() == '') {
				$(this).addClass("requirederror");
				error = true;
			} else {
				$(this).removeClass("requirederror");
			}
			i++;
		});	
		if (i > 0) {
			if (error == true) {
				$('html, body').animate({scrollTop:0}, 'slow');
				return false;
			} else {
				return true;
			}
		}
	});*/
	
	$("#form1").validate();
	
});	

document.onclick = jsddm_close;		
