$(document).ready(function() {
	if($("#button").length>0) {
		$("#button img").hover(function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/but_search_hover.png');
		}, function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/but_search.png');
		});
	}
	
	if($(".sendCv").length>0) {
		$(".sendCv img").hover(function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/jobs/send_cv_hover.'+_var_lang_id_+'.png');
		}, function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/jobs/send_cv.'+_var_lang_id_+'.png');
		});
	}
		
	if($("#publish_but").length>0) {
		$("#publish_but").hover(function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/publish_new_job_hover.png');
		}, function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/publish_new_job.png');
		});
	}
		
	if($("#jobButton").length>0) {
		$("#jobButton").hover(function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/update_hover.png');
		}, function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/update.png');
		});
	}
	
	if($("#sendBut").length>0) {
		$("#sendBut").hover(function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/send_hover.png');
		}, function() {
			$(this).attr('src', '/sites/wannajob/_static_media/images/buttons'+_var_lang_id_+'/send.png');
		});		
	}
	
	// Send friend fancybox code
	$('.iframe', $('.sendTo')).fancybox({
		'hideOnContentClick': false,
		'zoomSpeedIn': 300,
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'centerOnScroll': true,
		'height': 500,
		'width': 305,
		'overlayColor': '#666',
		'scrolling': 'no',
		'overlayOpacity': 0.9
	});
	
	// Print fancybox code
	$('.iframe', $('.printLink')).fancybox({
		'hideOnContentClick': false,
		'zoomSpeedIn': 300,
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'centerOnScroll': true,
		'height': 600,
		'width': 635,
		'overlayColor': '#666',
		'overlayOpacity': 0.9
	});
	
	// company_profile fancybox code
	$('.iframe', $('.logo_details')).fancybox({
		'hideOnContentClick': false,
		'zoomSpeedIn': 300,
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'centerOnScroll': true,
		'height': 600,
		'width': 660,
		'overlayColor': '#666',
		'overlayOpacity': 0.9
	});
	
	$('.show_more').click(function(){
		$(this).parent().hide('fast');
		$('.long_desc', $(this).parent().parent()).show('fast');
	});
	
	$('.open_job_search').click(function(){
		$(this).hide('fast');
		$('.job_search').slideDown('slow');
	});
	
	$('.option, .optionSelected').click(function(){
		var check = $('input', $(this)).attr('checked')==true
		$(this).attr('class', (check ? 'optionSelected' : 'option'));
	});

	$('#cancelSearch').click(function(){
		//$('#jobs_search_exec').val("cancel_search");
		//$('#job_search_form').submit();
		$('#job_search_form').clearForm();
  		$('.optionSelected').attr('class', 'option');
  		$('input', $('#position')).parent().hide();
	});
	
	$.fn.clearForm = function() {
	  	return this.each(function() {
	    	var type = this.type, tag = this.tagName.toLowerCase();
	    	if (tag == 'form')
	      		return $(':input',this).clearForm();
	    	if (type == 'text' || type == 'password' || tag == 'textarea')
	      		this.value = '';
	    	else if (type == 'checkbox' || type == 'radio')
	      		this.checked = false;
	    	else if (tag == 'select')
	      		this.selectedIndex = 0;
	  	});
	};	

	$('.print').click(function(){
		var url;
		if($(this).attr('class')=='printLink'){
			url = $(this).parent().parent().parent().attr('id').split('job_');
			url = url[1].split('Details');
			
			url = '/print_jobs/'+url[0]+'/nishot/';
		}
		else{
			if($(this).css('cursor')!='pointer'){
				return
			}
			
			var job_ids= new Array();
			var i=0;
			$('.job-sign-chk:checked').each(function(){
	        	job_ids[i++] = $(this).val();
			})
			url = '/print_jobs/'+job_ids.join(",")+'/nishot/';
		}
		
		// fancybox code
		$.fancybox({
			href					: url,
			type 					: 'iframe',
			hideOnContentClick: false,
			hideOnOverlayClick: false,
			zoomSpeedIn			: 300,
			zoomSpeedOut		: 300, 
			overlayShow			: true,
			centerOnScroll		: true,
			height				: 700,
			width					: 700,
			overlayColor		: '#666',
			overlayOpacity		: 0.9
		});
	});

	$('.sendCv, .send').click(function(){
		var url;
		if($(this).attr('class')=='sendCv'){
			url = $(this).parent().parent().parent().parent().attr('id').split('job_');
			url = url[1].split('Details');
			url = '/cv_send/'+url[0]+'/nishot/';
		}
		else{
			if($(this).css('cursor')!='pointer'){
				return;
			}
			
			var job_ids= new Array();
			var i=0;
			$('.job-sign-chk:checked').each(function(){
	        	job_ids[i++] = $(this).val();
			})
			url = '/cv_send/'+job_ids.join(",")+'/nishot/';
		}
		
		// fancybox code
		$.fancybox({
			href					: url,
			type 					: 'iframe',
			hideOnContentClick: false,
			hideOnOverlayClick: false,
			zoomSpeedIn			: 300,
			zoomSpeedOut		: 300, 
			overlayShow			: true,
			centerOnScroll		: true,
			height				: 700,
			width					: 840,
			overlayColor		: '#666',
			overlayOpacity		: 0.9
		});
	});
	
	$('#button, #buttonSmall').click(function(){
		$('#jobs_search_exec').val("on");
		$('#job_search_form').submit();
	});
	
	$('#professionSel').change(function(){
			$('input', $('#position')).parent().hide();
			$('input', $('#position')).attr('checked', false);
			$('#all_exp_check0').parent().attr('class', 'option');
			$('input', $('#position')).parent().attr('class', 'option');
			check_one_cat($(this).val());
			$('#all_exp_check0').parent().show();
	});
	
	$('#profession input').click(function(){
		if($(this).attr('selected')==true){
			check_one_cat($(this).val());
		}
		else{
			$('input', $('#position')).parent().hide();
			
			$('input', $('#profession')).each(function(){
				if($(this).attr('checked')==true){
					check_one_cat($(this).val());
				}
			});			

			$('#all_exp_check0').parent().show();
		}
	});

	var total_position = 0;
	function check_one_cat(one_cat){
		$('input', $('#position')).parent().each(function(){
			var cat_arr = $(this).attr('cat_id')+",";
			if(cat_arr.indexOf(","+one_cat+",")!=-1){
				$(this).show();
				if($('#all_exp_check0').attr('checked')==true){
					$('#all_exp_check0').attr('checked', false);
					$('#all_exp_check0').parent().attr('class', 'option');
				}
				if($('input', $(this)).attr('checked')==true){
					total_position++;
				}
			}
		});
	}

	// Check uncheck experties checkbox
	$('#all_exp_check0').click(function(){
		var checked = ($(this).attr('checked') ? true : false);
		$('input', $('#position')).each(function(){
			if($(this).parent().css('display')=="block"){
				$(this).attr('checked', checked);
				$(this).parent().attr('class', (checked ? 'optionSelected' : 'option'));	
			}
		});

	});
	
	$('[id^=exp_check]', $('#position')).click(function(){
		if($(this).attr('checked')==true){
			total_position
		}
		else{
			$('#all_exp_check0').attr('checked', false);
			$('#all_exp_check0').parent().attr('class', 'option');
		}
	});
	
	// Check uncheck areas checkbox
	total_area = 7;
	$('#all_area_check0').click(function(){
		var checked = ($(this).attr('checked') ? true : false);
		$('input', $('#area')).each(function(){
			if($(this).parent().css('display')=="block"){
				$(this).attr('checked', checked);
				$(this).parent().attr('class', (checked ? 'optionSelected' : 'option'));	
			}
		});
	});
	
	$('[id^=area_check]', $('#area')).click(function(){
		if($(this).attr('checked')==true){
			if($('[id^=area_check]:checked').size()==total_area){
				$('#all_area_check0').attr('checked', true);
				$('#all_area_check0').parent().attr('class', 'optionSelected');
			}
		}
		else{
			$('#all_area_check0').attr('checked', false);
			$('#all_area_check0').parent().attr('class', 'option');
		}
	});

	// Check uncheck types checkbox
	total_type = 6;
	$('#all_type_check0').click(function(){
		var checked = ($(this).attr('checked') ? true : false);
		$('input', $('#type')).each(function(){
			if($(this).parent().css('display')=="block"){
				$(this).attr('checked', checked);
				$(this).parent().attr('class', (checked ? 'optionSelected' : 'option'));	
			}
		});
	});
	
	$('[id^=type_check]', $('#type')).click(function(){
		if($(this).attr('checked')==true){
			if($('[id^=type_check]:checked').size()==total_type){
				$('#all_type_check0').attr('checked', true);
				$('#all_type_check0').parent().attr('class', 'optionSelected');
			}
		}
		else{
			$('#all_type_check0').attr('checked', false);
			$('#all_type_check0').parent().attr('class', 'option');
		}
	});
	
	$('.close').click(function(){
		var objDetail = $('#'+$(this).parent().parent().attr('id'));
		var my_row = $(this).parent().parent().attr('id').split('Details')
		var objTitle = $('#'+my_row[0]);

		if (objDetail.css('display') == 'block') {
			objDetail.css('display', 'none');
			objTitle.removeClass('open');
		} else {
			objDetail.css('display', 'block');
			objTitle.addClass('open');
		}
	});
	
	var total_chk = $('.job-sign-chk').size();
	$('.job-sign-chk').click(function(){
		var chk_sign = $('.job-sign-chk:checked').size();
		if(chk_sign){
			$('.send').css({cursor:'pointer', color:'#F12E8E'});
			$('.print').css({cursor:'pointer', color:'#F12E8E'});
		}
		else{
			$('.send').css({cursor:'default', color:'gray'});
			$('.print').css({cursor:'default', color:'gray'});			
		}
		
		if (chk_sign==total_chk){
			$('#check-all').attr('checked', true);
		}
		else{
			$('#check-all').attr('checked', false);			
		}
	});	
});