$(document).ready(function() {
	
	/************************
	 * GLOBAL
	 ************************ */
	 //fade in then fade out the feedback box
	//$(".feedbackBox").fadeIn("slow",function(){window.setTimeout(function(){$(".feedbackBox").fadeOut("slow")}, 2500);});
	window.setTimeout(function(){$(".feedbackBox").fadeOut("slow")}, 2500);
	
	//focus the cursor in any input with the id #formFocus
	$("#formFocus").focus();
	
	//help
	//make sure 'tipsy' function exist first as we only include it on some pages
	if (typeof $.fn.tipsy == "function") {
		//gravity: n = icon is North, e = icon is East, etc
		$(".hoverHelp").tipsy({gravity:'e',fade:true});
		$(".hoverHelp-n").tipsy({gravity:'n',fade:true});
		$(".hoverHelp-e").tipsy({gravity:'e',fade:true});
		$(".hoverHelp-s").tipsy({gravity:'s',fade:true});
		$(".hoverHelp-w").tipsy({gravity:'w',fade:true});
		
		$(".hoverInfo").tipsy({gravity:'e',fade:true});
		$(".hoverInfo-n").tipsy({gravity:'n',fade:true});
		$(".hoverInfo-e").tipsy({gravity:'e',fade:true});
		$(".hoverInfo-s").tipsy({gravity:'s',fade:true});
		$(".hoverInfo-w").tipsy({gravity:'w',fade:true});
	}
	//carousel
	if ($("#homeCorpMembers").length) {
		$("#homeCorpMembers").carousel({
			dispItems: 3,
			autoSlide: true,
			autoSlideInterval: 5000,
			delayAutoSlide: 1000,
			loop: true
		});
		var ph = $("#homeCorpMembers ul").height(); 
		$("#homeCorpMembers li a img").each(function(i) {
			var ah = $(this).height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	}
	
	//load flash
	//home page
	if($("#flashMainBanner").length) {
		var flashvars = {};
		flashvars.clickURL = "/executive.htm";
		var params = {};
		params.wmode = "transparent";
		swfobject.embedSWF("/images/banners/ei_banner_01REV.swf", "flashMainBanner", "639", "242", "8.0.0", false, flashvars, params, attributes);
	} else if($("#flashMainBanner-execBasic").length) {
		var flashvars = {};
		flashvars.clickURL = "/executive.htm";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/images/banners/ei_banner_01REV.swf", "flashMainBanner-execBasic", "639", "242", "8.0.0", false, flashvars, params, attributes);
	} else if($("#flashMainBanner-execPremium").length) {
		var flashvars = {};
		flashvars.clickURL = "/executive.htm";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/images/banners/ei_banner_01REV.swf", "flashMainBanner-execPremium", "639", "242", "8.0.0", false, flashvars, params, attributes);
	} else if($("#flashMainBanner-corpStandard").length) {
		var flashvars = {};
		flashvars.clickURL = "/recruiter.htm";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/images/banners/ei_banner_02.swf", "flashMainBanner-corpStandard", "639", "242", "8.0.0", false, flashvars, params, attributes);
	} else if ($("#flashMainBanner-corpPlus").length) {
		var flashvars = {};
		flashvars.clickURL = "/recruiter.htm";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/images/banners/ei_banner_02.swf", "flashMainBanner-corpPlus", "639", "242", "8.0.0", false, flashvars, params, attributes);
	}
	
	/***********************
	 * Latest Exec Jobs / other pages
	 *********************** */
	$("select#salary").change(function(){
		//$("select#interim option:first-child").text('Disabled');
		$("select#interim").attr("disabled", true);
		if ($(this).val() == '' || $(this).val() == 0) {
			$("select#interim").removeAttr("disabled");
			//$("select#interim option:first-child").text('All');
		}
	});
	
	$("select#interim").change(function(){
		//$("select#salary option:first-child").text('Disabled');
		$("select#salary").attr("disabled", true);
		if ($(this).val() == '' || $(this).val() == 0) {
			$("select#salary").removeAttr("disabled");
			//$("select#salary option:first-child").text('All');
		}
	});	
	
	$("select#jFunction").change(function(){
		$('#jobWrapper').html('');
	 	$("#loader").show();
		$.post("/includes/jobsbox_ajax.htm", { jFunction:$(this).attr('value')},
		function(data){
			$("#loader").hide();
			$('#jobWrapper').html(data); 
	    }, "html");
		return false;
	});	
	
	/************************
	 * Registration
	 ************************ */
	/*
	$(".required > input").blur(function() {
		if($(this).val() == "") {
			$(this).parent().addClass("fail");
		} else {
			$(this).parent().removeClass("fail");
		}
	});
	*/
	$("#btn-already-registered").click(function(){
		$("#alreadyMember").slideToggle("400",function(){
			$('#memberEmail').focus();
		});
		return false;
	});
	
	$("#btn-membership-options").click(function(){
		$("#membershipOptions").slideToggle();
		return false;
	});
	
	(function(){
		var selectedOpt = $("input[name='optionID']:checked");
		if(selectedOpt.length > 0){
			var arrId = selectedOpt[0].id.split("_");
			var uId = arrId[1];
			$("#membershipOptions").slideDown();
		}
		checkOption(uId);
	})();
	
	$("div[id^='memebershipOption_']").click(function(){
		var arrId = this.id.split("_");
		var uId = arrId[1];
		checkOption(uId);
	});
	
	function checkOption(uId){
		if(!$("#memebershipOption_"+uId).hasClass("optionSelect")){
			$(".memebershipOption").removeClass("optionSelect");
			$("#memebershipOption_"+uId).addClass("optionSelect");
			$("#opt_"+uId).attr("checked",true);
		}else{
			$(".memebershipOption").removeClass("optionSelect");
			$("#opt_"+uId).attr("checked",false);
			$("#freetrial").attr("checked",true);
		}
	}
	
	/************************
	 * Advance search
	 ************************ */
	//INDUSTRY
	//check and disable all child checkboxes when parent is checked 
	$("input[id^='pIndustryID_']").change(function(){
		var arrPId = this.id.split("_");
		var pId = arrPId[1];
		if($("input#pIndustryID_"+pId).is(":checked"))
			$("input[id^='industryID_"+pId+"_']").attr('checked', true).attr('disabled', true);
		else
			$("input[id^='industryID_"+pId+"_']").attr('checked', false).attr('disabled', false);
	});
	//if all children are selected, select the parent and disable the children
	$("input[id^='industryID_']").change(function() {
		var arrPId = this.id.split("_");
		var pId = arrPId[1];
		var totalChildren = $("input[id^='industryID_"+pId+"_']").length;
		var childrenChecked = $("input[id^='industryID_"+pId+"_']:checked").length;
		if(childrenChecked == totalChildren) {
			$("input[id^='pIndustryID_"+pId+"']").attr('checked', true);
			$("input[id^='industryID_"+pId+"_']").attr('disabled', true);
		}
	});
	//LOCATIONS
	//check all child checkboxes when parent is checked
	$("input#pLocation").change(function(){
		if($("input#pLocation").is(":checked"))
			$("input[id^='locationID_']").attr('checked', true).attr('disabled', true);
		else
			$("input[id^='locationID_']").attr('checked', false).attr('disabled', false);
	});
	//if all children are selected, select the parent and disable the children
	$("input[id^='locationID_']").change(function(){
		var totalChildren = $("input[id^='locationID_']").length;
		var childrenChecked = $("input[id^='locationID_']:checked").length;
		if(childrenChecked == totalChildren) {
			$("input#pLocation").attr('checked', true);
			$("input[id^='locationID_']").attr('disabled', true);
		}
	});
	//SALARY / INTERIM RATES
	 //if any Salary checkboxes are checked, disable Interim checkboxes 
	$("input[id^='salaryRangeID_']").change(function(){
	 	var isChecked = $("input[id^='salaryRangeID_']:checked").length;
	 	if(isChecked > 0)
	 		$("input[id^='interimRateID_']").attr("checked", false).attr("disabled", true);
	 	else
	 		$("input[id^='interimRateID_']").attr("disabled", false);
	});
	 //if any Interim checkboxes are checked, disable Salary checkboxes
	$("input[id^='interimRateID_']").change(function(){
	 	var isChecked = $("input[id^='interimRateID_']:checked").length;
	 	if(isChecked > 0)
	 		$("input[id^='salaryRangeID_']").attr("checked", false).attr("disabled", true);
	 	else
	 		$("input[id^='salaryRangeID_']").attr("disabled", false);
	});
	//disable options after limit is reached
	$("#jobRoleLimit input[id^='jobRoleID_']").change(function(){
		var limit = 3;
		var isChecked = $("input[id^='jobRoleID_']:checked").length;
		if(isChecked >= limit) {
			$("input[id^='jobRoleID_']:not(:checked)").attr("disabled", true);
			$("#roleMessage").addClass("errorBox").text("Only a maximum of "+limit+" Roles can be selected.");
		} else {
			$("input[id^='jobRoleID_']:disabled").attr("disabled", false);
			$("#roleMessage").removeClass("errorBox").text("Please select up to "+limit+" Roles.");
		}
	});
	//disable options after limit is reached
	$("#functionLimit input[id^='functionID_']").change(function(){
		functionIDLimit(4);
	});
	//disable options after limit is reached
	$("#functionVacanciesLimit input[id^='functionID_']").change(function(){
		functionIDLimit(3);
	});
	//disable options after limit is reached
	$("#industriesLimit input[id^='industryID_']").change(function(){
		industryIDLimit(4);
	});
	
	
	if ($("#datepickerAdded").length || $("#datepickerExpire").length) {
		$(function(){
			// Datepicker
			$("#datepickerAdded").datepicker({
				dateFormat: 'dd/mm/yy',
				minDate: '0d',
				showOn: 'button',
				buttonImage: '/images/global/calendar.png',
				buttonImageOnly: true,
				duration: ''
			//,onClose: function(dateText, inst) { console.log(dateText);console.log(inst.selectedMonth); }
			
			});
			
			$("#datepickerExpire").datepicker({
				dateFormat: 'dd/mm/yy',
				minDate: '0d',
				showOn: 'button',
				buttonImage: '/images/global/calendar.png',
				buttonImageOnly: true,
				duration: ''
			});
			
			//hover states on the static widgets
			$('#dialog_link, ul#icons li').hover(function(){
				$(this).addClass('ui-state-hover');
			}, function(){
				$(this).removeClass('ui-state-hover');
			});
		});
	}
	
	$("#btnMembershipOptions").click(function(){
		$("#membershipOptions").slideToggle();
		return false;
	});
	
	try{
		$("a#showMembershipFeatures").fancybox({
			'hideOnContentClick': true
		});
	
		$("a.showPremiumJob").fancybox({
			'hideOnContentClick': true
		});
		$("a.showFeaturesJob").fancybox({
			'hideOnContentClick': true
		});
	} catch(e) {}

 
//end (document).ready(function() 	
});

function functionIDLimit(limit) {
	var limit = limit || 4;
	var isChecked = $("input[id^='functionID_']:checked").length;
	if(isChecked >= limit) {
		$("input[id^='functionID_']:not(:checked)").attr("disabled", true);
		$("#functionMessage").addClass("errorBox").text("You have now selected the maximum number of "+limit+" Skillsets.");
	} else {
		$("input[id^='functionID_']:disabled").attr("disabled", false);
		$("#functionMessage").removeClass("errorBox").text("Please select up to "+limit+" Skillsets.");
	}
}

function industryIDLimit() {
	var limit = limit || 4;
	var isChecked = $("input[id^='industryID_']:checked").length;
	if(isChecked >= limit) {
		$("input[id^='industryID_']:not(:checked)").attr("disabled", true);
		$("#industriesMessage").addClass("errorBox").text("You have now selected the maximum number of "+limit+" Industries.");
	} else {
		$("input[id^='industryID_']:disabled").attr("disabled", false);
		$("#industriesMessage").removeClass("errorBox").text("Please select up to "+limit+" Industries.");
	}
}
