function update_jobs()
{
	
	$.get("ajax_job.php", { industry: $("#industry").val(), location: $("#location").val(), contract: $("#type").val(), job_title: $("#keywords").val(), ajax: "yes" },
	  function(data){
	   $("#content_main").html(data);
		$("#job_count").html("<strong>" + $("#job_total_original").val() + "</strong>");
		$(".job_search_area").html( $("#job_search_original").val());
	  });
	$(".noshow").show("slow");
}

function check_salaries()
{
	var maxv = ($("#max_salary").val() /1000);
		var minv = ($("#min_salary").val() /1000);
	if (maxv <= minv)
	{
		$(".salary_error").show("slow");
	}
	else
	{
		$(".salary_error").hide("slow");
	}
	
	
}


$(function(){
	
	if ($("#alertcheck").is(':checked'))
		$(".last_check").nextAll().show();
	else
		$(".last_check").nextAll().hide();
	$(".buttons").show();	
	
	$("#alertcheck").click( function() {
	
	$(".last_check").nextAll().toggle();	
	$(".buttons").show();	
	});
	
	$('.delete_link').click(function() {
		
		if (confirm('Are you sure, this will permanently remove your account?'))
		{
			return true;
			
		}	
		else
		{
			return false;
		}
	});
	
	$(".salary_error").hide();
	
	$('a.clear').click( function() {
		$("#keywords").val("");
		return false;
	});
	
	$('a.keyword').click( function() {
		var content = $(this).html();
		var boxval = $("#keywords").val();
		if (boxval.indexOf(content) == -1)
		{
			if ($("#keywords").val() != "")
				$("#keywords").val($("#keywords").val() + ", ");
			$("#keywords").val($("#keywords").val() + content);
		
		}
		else
		{

				if ((boxval.indexOf(content) + content.length) == boxval.length)
					$("#keywords").val($("#keywords").val().replace(", " + content , ""));
				$("#keywords").val($("#keywords").val().replace(content + ", ", ""));
				$("#keywords").val($("#keywords").val().replace(content, ""));
		}
	

		
		return false;
	});
	
	
	$('#min_salary').bind('change', function() {
		check_salaries();
	});
	
	$('#max_salary').bind('change', function() {
		check_salaries();
	});
	
	$(".keywordsspan").hide();
	$('#sector').bind('change', function() {
		$(".keywordsspan").hide("slow")
		$('#keywords_' + $('#sector').val()).show("fast");
	});
	
	$('.search_string').bind('keyup', function() {
		update_jobs();
	});
	
	$('.update_jobs').bind('change', function() {
		update_jobs();
	});
	
	
	$('.default-value').each(function() {
	    var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            this.value = default_value;
	        }
	    });
	});
	
	$('#skills').hoverPanels();
	
	$('#contact_details').showHideButton();
	
});

$.fn.hoverPanels = function() {
	return this.each(function(){
		
		var list = $('li',this);
		var panels = $('>div',list).hide();
		var images = $('>h2>a>img',list);
		
		var showPanel = function(e){
			e.stopPropagation();
			
			$(this).parent().addClass('on').siblings().removeClass('on');
			var panel = $('+div',this);
			var image = $('>a>img',this);
			
			if(panel.is(':hidden')) {
				panels.filter(':visible').fadeOut(400);
			//	panel.fadeIn(400);
				$('#skills').css('background-image', panel.css('background-image'));
				images.animate({opacity:1},{queue:false,duration:400});
				image.animate({opacity:0},{queue:false,duration:400});
			}
		}
		
		// hover to show the quotes
		$('>h2',list).mouseover(showPanel);
		
		// bind clicking on quotes to h2 link
		panels.css('cursor','pointer').click(function(){
			window.location = $(this).prev('h2').children('a:first').attr('href');
		});
		
		// cycle on page load
		var currentIndex = 1;
		var cycling = window.setInterval(function(){
			list.eq(currentIndex%4).children('h2').trigger('mouseover');
			currentIndex++
		}, 5000);
		$('>ul',this).mouseover(function(){ window.clearInterval(cycling) });
		list.eq(0).children('h2').trigger('mouseover');
		
	});
}

$.fn.showHideButton = function() {
	return this.each(function(){
		var apply = $(this).next().hide();
		var button = $('<img src="/images/button_apply_closed.png" alt="Show the application fields" id="toggle_button" />').toggle(function(){
			apply.slideDown();
			button.attr({
				src: "/images/button_apply_open.png",
				alt: "Hide the application fields"
			});
		}, function(){
			apply.slideUp();
			button.attr({
				src: "/images/button_apply_closed.png",
				alt: "Show the application fields"
			});
		}).insertAfter(this);
		
	});
}