// -----------------------------------------------------------------------------------
// FILE LOCATIONS
// -----------------------------------------------------------------------------------
var wsw_url_sign_in		= '/inloggen/';
var wsw_url_sign_out	= '/uitloggen/';
var wsw_url_job_respond	= '/vacatures-reageren/';
var wsw_url_job_search	= '/vacatures-zoeken/';

var wsw_id_job_title	= "FieldCode01";
var wsw_id_job_code		= "FieldCode99";
var wsw_id_job_coworker	= "FieldCode96";


// MESSAGES
var wsw_msg_signing_in	= "Bezig met inloggen...";
var wsw_msg_signing_out = "Bezig met uitloggen...";

var wsw_msg_save_job_respond	= "Bezig met opslaan van je reactie...";
var wsw_msg_loading_jobopenings = "Bezig op ophalen van het vacatureoverzicht...";
var wsw_msg_loading_error		= "Er is een storing";




// -----------------------------------------------------------------------------------
// JQUERY INITS
// -----------------------------------------------------------------------------------
 $(document).ready(function(){ 

	// set hovers
	// -------------------------------------------------------------------------------
	$('#wsw_job_overview tr').live({
		click: function() { var jLink = $(this).find('A'); if (jLink[0]!=undefined) { self.location.href = jLink[0];} },
		mouseover: function(){ if ($(this).children('th').length==0) { $(this).addClass('hover'); } },
		mouseout: function(){ $(this).removeClass('hover') }
	});

	$('#job_filter_keyword').focus( function(){  $(this).addClass('form_focus'); });
	$('#job_filter_keyword').focusout( function(){  $(this).removeClass('form_focus'); });

	$('#wsw_job_filter_form select').mousedown( function(){  $(this).addClass('form_focus'); });
	$('#wsw_job_filter_form select').focusout( function(){  $(this).removeClass('form_focus'); });

	$('#wsw_job_filter_submit').hover( function(){  $(this).addClass('form_main_button_hover');  },
								       function(){  $(this).removeClass('form_main_button_hover'); });

	$('#wsw_job_respond_text').focus( function(){  $(this).addClass('form_focus'); });
	$('#wsw_job_respond_text').focusout( function(){  $(this).removeClass('form_focus'); });

	$('#wsw_job_respond_submit').hover( function(){  $(this).addClass('form_main_button_hover');  },
								       function(){  $(this).removeClass('form_main_button_hover'); });



	/* GENERAL SCROLL TOP TOP */
	$('.wsw_up_link').click( function(event) { event.preventDefault(); $('#ContBox_cnt').animate({scrollTop:0}, 'slow');  });		// onClick
	$('.wsw_back_link').click( function(event) { event.preventDefault(); history.back();  });									// onClick





	// Signin Form Validator
	// -------------------------------------------------------------------------------
	$("#SignIn_close").click(function(){ $("#SignIn_cnt").fadeOut('fast');  });
	$(".SignIn_open").click(function(){ $("#SignIn_cnt").fadeIn('fast');  });


   $("#wsw_signin_form").validate({
		errorClass: "form_error",

		rules: {
			wsw_signin_username: { required: true, minlength: 3, email: true },
			wsw_signin_password: { required: true, minlength: 3 }
		},
		messages: {
			wsw_signin_username: '',
			wsw_signin_password: ''
		},
		submitHandler: function(form){
			var jData = $("#wsw_signin_form").serialize();
			$("#wsw_signin").html('<div class=wsw_ajax_loader>'+wsw_msg_signing_in+'</div>');
			wsw_getExternalData(wsw_url_sign_in, 'wsw_signin', jData);
		}
	});



	// Job SubScription Validator
	// -------------------------------------------------------------------------------
	$("#wsw_job_respond_jobtitle").val($("#"+wsw_id_job_title+" .wsw_col_2").html());
	$("#wsw_job_respond_jobnr").val($("#"+wsw_id_job_code+" .wsw_col_2").html());
	$("#wsw_job_respond_jobcoworker").val($("#"+wsw_id_job_coworker+" .wsw_col_2").html());

   $("#wsw_job_respond_form").validate({
		errorClass: "form_error",
		rules: { 
			wsw_job_respond_text: { required: true, minlength: 3}
		},
		messages: {
			wsw_job_respond_text: ''
		},
		submitHandler: function(form){
			var jData = $("#wsw_job_respond_form").serialize();
			$("#wsw_job_respond").html('<div class="wsw_ajax_loader">'+wsw_msg_save_job_respond+'</div>');
			wsw_getExternalData(wsw_url_job_respond, 'wsw_job_respond', jData);
		}
	});



	// Job Search Filter
	// -------------------------------------------------------------------------------
   $("#wsw_job_filter_form").validate({
		submitHandler: function(form){
				// get formdata
				var jKeyWords	= $('#job_filter_keyword').val() + ' ';
				var jKeyWord = jKeyWords.substring(0, jKeyWords.indexOf(' ')); // ensure we have only one keyword
				$('#job_filter_keyword').val(jKeyWord);

				// do search
				//$('#ContBox_cnt').animate({scrollTop:0}, 'fast');
				$("#wsw_job_overview").html('<div class="wsw_ajax_loader">'+wsw_msg_loading_jobopenings+'</div>');
				
				wsw_getExternalData(wsw_url_job_search, 'wsw_job_overview', $("#wsw_job_filter_form").serialize());

		}
	});

 });
// END INIT JQUERY



// signs user out using ajax with loader
// -------------------------------------------------------------------------------
function wsw_doSignout() {
	$("#wsw_my_menu").html('<div class="wsw_ajax_loader">'+wsw_msg_signing_out+'</div>');
	wsw_getExternalData(wsw_url_sign_out, 'wsw_my_menu', '');
}



/* get external data using jquery */
// -------------------------------------------------------------------------------
function wsw_getExternalData(jURL, jField, jSendData) {
	$.ajax({
		type: "POST",
		url: jURL,
		dataType:"html",
		data: jSendData,
		timeout: 10000,
		cache: false,
		success: function(html){ if (html=='wsw_reload') { window.location.reload(); } else { $("#"+jField).html(html); } },
        error: function (xhr, ajaxOptions, thrownError){ if (thrownError==undefined) { $("#"+jField).html('<div class="wsw_ajax_error">'+wsw_msg_loading_error+'</div>'); } } 
	 });
}

