$(function() {
	Careers.init();
	Careers.keepAlive();
	Careers.removeThankyouMessage();
	Careers.showApplicationHints();
});

var Careers = {
	init: function() {
		$("#save-close").click(function() {
			$(this).siblings("input[name='save_close']").val("1");
			return true;
		});
		
		$("#pclookup").click(function() {
			$(this).siblings("input[name='pclookup']").val("1");
			return true;
		});
		
		$("#previous_step").click(function() {
			$(this).siblings("input[name='save_back']").val("1");
			return true;
		});
		
		$("#po_address").change(function() {
			$(this).siblings("input[name='useaddress']").val("1");
			$("#step1form").submit();
			return true;
		});
		$("input").focus(function() {
			$(this).addClass("focus");
		});
		$("input").blur(function() {
			$(this).removeClass("focus");
		});
	
		$("textarea").focus(function() {
			$(this).addClass("focus");
		});
		$("textarea").blur(function() {
			$(this).removeClass("focus");
		});
		
		$("form/job_search #category").change(function() {
			if ($(this).val() == "head-office-opportunities" || $(this).val() == "web-sales") {
				// locations dropdown should only show doncaster
				$("form/job_search #region").html("<option value=''>Doncaster</option>");
			} else {
				// populate locations with all available, let request them!
				$.get(base_href + "careers/get-regions-list/", {}, function(response){
					if (response.msg == "OK") {
						$("form/job_search #region").html(response.regions);
					}
				}, "json");
			}
		});
	},
	
	keepAlive: function() {
		$.get(base_href + "careers/session-keep-alive/", {}, function(response){
			if (response.msg == "OK") {
				// set timout of next run
				setTimeout(function() {
					Careers.keepAlive();
				}, 300000);
			}
		}, "json");
	},
	
	removeThankyouMessage: function() {
		if ($("#app-thank-you")) {
			setTimeout(function() {
				$("#app-thank-you").fadeOut();
			}, 5000);
		}
	},
	
	showApplicationHints: function() {
		if ($("select.po_address")) {
			var select_width = $("select.po_address").width();
		}
		
		$(".tip").mouseover(function() {
			if ($("select.po_address") && browserIsIE6() && (select_width > 280)) {
				// if we have a select box, hide and show the fake one
				if ($("select.po_address")) {
					$("select.po_address").width("280px");
				}
			}
		
			
			//now make it follow the mouse
			$(this).mousemove(function(e){
				// make sure to check for the left edge of the screen
				var x_offset = e.pageX + 1;
				var y_offset = e.pageY - ($(this).next().height() + 1);
				
				$(this).next().css({ "left": x_offset, "top": y_offset});
				$(this).next().show();
			});
		});
		
		$(".tip").mouseout(function(){
			$(this).next().hide();
			
			if ($("select.po_address") && browserIsIE6() && (select_width > 280)) {
				// if we have a select box, hide and show the fake one
				if ($("select.po_address")) {
					$("select.po_address").width(select_width);
				}
			}
		});
	}
};

function browserIsIE6() {
	if ($.browser.msie && $.browser.version=="6.0") {
		return true;
	}
	
	return false;
}



