function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

jQuery.fn.center = function() 
{
  	myWidth = f_clientWidth() + f_scrollLeft();
  	myHeight = f_clientHeight() + f_scrollTop();  	  	
	$(this).css({
		"z-index":"5000",
		"position" : "absolute",
		"left" : (Math.round(myWidth / 2 - $(this).width() / 2)) + "px",
		"top" : (Math.round(myHeight /2 - $(this).height() / 2)) + "px"
	});	
	return this;
}
/**
 * when dom-document is ready
 */
$(document).ready(function() {
	if ($('#hasLoginMsg').val() == 1 && $('#isLogged').val() == 0) {		
		loginWindowOpen();
	}
	searchResultsHighlight();		
	$.ajaxSetup({
		global: true,
  		beforeSend: function () {
  			$('#ajaxSpinnerImg').center();
  			$('#ajaxSpinnerImg').show();  			
  		},
  		complete: function() {
  			$('#ajaxSpinnerImg').hide();
  		}
	});
}
);
/**
 * opens modal login window
 */
function loginWindowOpen() {
	$('#loginWindow').modal(
		{
		  overlayCss: {
		    backgroundColor: '#000',
		    cursor: 'default'
		  },
		  containerCss: {
		    height: '200px',
		    width: '300px',
		    backgroundColor: '#fff',
		    border: '3px solid #ccc'
		  }
		}
	);
	$('#loginWindowContainer').show();
	$('#loginWindowContainer2').hide();
}
/**
 * close modal login window
 */
function loginWindowClose() {	
}
/**
 * chack if user is logged
 */
function isLoggedCheck() {
	if ($('#isLogged').val() == 1) {
		return true;
	} else {
		$('#loginWindow').modal();
		return false;
	}
}
/**
 * after search highlight search results
 */
function searchResultsHighlight() {
	if ($('#searchKeys') && $.trim($('#searchKeys').val()) != '') {
		var keys = new String($('#searchKeys').val());
		var keysArr = keys.split(",");
		//var containerHTML = new String($('#searchContainer').html());
		var containerHTML = $('#searchContainer').html();				
		if (keysArr.length > 0) {			
			for(var i=0; i<keysArr.length; i++) {				
				var r = new RegExp(keysArr[i], 'gi');				
				containerHTML = containerHTML.replace(r, "<span class=\"searchHeighlited\">"+keysArr[i]+"</span>");
			}
		}
		//$('#searchContainer').html('<b>hello</b>');
		$('#searchContainer').html(containerHTML);
	}
}
/**
 * password recovery
 */
function passRecovery(step) {
	switch (step) {
		case 1 :
			var ajax_url = '/passrecovery/getemail';
			$.ajax({
		  		url: ajax_url,
		  		cache: false,
		  		success: function(html){			    		
		    		$('#loginWindowContainer').hide();
		    		$('#loginWindowContainer2').html(html);
		    		$('#loginWindowContainer2').show();
		  		}
			});
		break;
		case 2 :
			if ($.trim($('#emailForRecovery').val()) != '') {
				var ajax_url = '/passrecovery/searchuser';
				$.ajax({
			  		url: ajax_url,
			  		cache: false,
			  		type: "POST",
			  		data: "email="+$('#emailForRecovery').val(),
			  		success: function(html){		  			
			    		$('#loginWindowContainer').hide();
			    		$('#loginWindowContainer2').html(html);
			    		$('#loginWindowContainer2').show();
			  		}
				});
			} else {
				alert ('Введите логин или email.');
			}
		break;		
	}
}
/**
 * back step history for recovery cards(forms)
 */
function passRecoveryBack(step) {
	switch (step) {
		case 1 :    		
		    $('#loginWindowContainer').show();
		    $('#loginWindowContainer2').hide();		    
		break;
		case 2 :			
			var ajax_url = '/passrecovery/getemail';
			$.ajax({
		  		url: ajax_url,
		  		cache: false,
		  		success: function(html){			    		
		    		$('#loginWindowContainer').hide();
		    		$('#loginWindowContainer2').html(html);
		    		$('#loginWindowContainer2').show();
		  		}
			});
		break;		
	}
}
/**
 * is used on add vacancy form
 */
function selectWDType(li, parentClass) {	
	if (li) {
		$("ul."+parentClass+" li").removeClass("sel");
		$(li).addClass("sel");
	}
}