var mod_job = {

	setView: function(obj, arrobj)
	{
		if(obj != null)
			document.getElementById(obj).style.display = '';

		for(var i = 0; i < arrobj.length; i++)
			if(arrobj[i] != null)
				document.getElementById(arrobj[i]).style.display = 'none';
	},

	change_city: function(obj, toidobj, toidobj2, default_txt)
	{
	    obj = $(obj);
	    toobj = $('#'+toidobj);
	    toobj.get(0).disabled = true;

		if (toidobj2 != null)
		{
			obj2 = $('#'+toidobj2).get(0);
		    if($(obj).val() > 0)
		        obj2.disabled = true;
		    else
		        obj2.disabled = false;
		}

	    theme_source.get_data({
	        source:'db.location',
	        params: {type: 'area', city: obj.val()},
	        success: function(data){this.load_success(toidobj, data, default_txt);},
	        failed: function(data){this.load_failed(toidobj);},
	        scope: this});
	},

	load_success: function(to, data, default_txt)
	{
	    obj = $('#'+to).get(0);

	    obj.options.length = 0;
	    obj.options[0] = new Option((default_txt != null)?default_txt:'не задано', 0, false, false);
		for(var i in data)
			obj.options[obj.options.length] = new Option(data[i].name, Number(i), false, false);
		obj.selectedIndex = 0;
		if (obj.options.length > 0)
			obj.disabled = false;
	},

	load_failed: function(to)
	{
	    obj = $('#'+to);
		if (obj.options.length > 0)
		    obj.get(0).disabled = true;
	},
	
	toFavorites: function(id, type) {                   
		
		$.ajax({
            type: 'POST',
            url: '.',
            data: {'FavoriteID': id, 'action': 'toFavorites', 'type': type},
            dataType: 'json',
            error: function(data) { 
            
            	mod_job.favorites_add_failed(data); 
            },
            success: function(data) { 
            	if (data.success  && data.success === true)
            		mod_job.favorites_add_success(data); 
            	else
            		mod_job.favorites_add_failed(data); 
            }
        });
	},
	
	favorites_add_success: function(data)
	{
		if ( data.FavoriteID > 0) 
			$('#ln'+data.FavoriteID).get(0).style.display = 'none';
		
		if ( data.type && data.type == 'resume' ) {
			if ( !data.urlToFavorites )	
				alert("Резюме успешно добавлено в избранные резюме!");
			else if ( confirm("Резюме успешно добавлено в избранные резюме!\nПерейти к списку избранных резюме?") ) 
				window.location.href = data.urlToFavorites;
		} else if ( data.type && data.type == 'vacancy' ) {
			if ( !data.urlToFavorites )	
				alert("Вакансия успешно добавлена в избранные вакансии!");
			else if ( confirm("Вакансия успешно добавлена в избранные вакансии!\nПерейти к списку избранных вакансии?") ) 
				window.location.href = data.urlToFavorites;
		}
	},

	favorites_add_failed: function(data)
	{
		if ( data.type && data.type == 'resume' ) {
			alert('Не удалось добавить резюме');
		} else if ( data.type && data.type == 'vacancy' ) {
			alert('Не удалось добавить вакансию');
		}
		
	},

	show_details: function($type,$id)
	{
		if ( $('#'+$type+$id+'content').css('display') == 'none' )
		{
			if ( $('#'+$type+$id+'content').html() == '' )
			{
				$('#'+$type+$id+'loader').html('<img src="/_img/themes/frameworks/jquery/ajax/loader-big.gif" border="0" width="32" height="32"/>');
				$('#'+$type+$id+'loader').slideDown(
					'normal',
					function()
					{
						//mod_job.visible_details = $type+$id;
						$.ajax({
							url: '.',
							type: 'POST',
							dataType: 'json',
							data: {
								action: 'get_details',
								type: $type,
								id: $id
							},
							success: function (data)
							{
								$('#'+$type+$id+'loader').css('display','none');
								$('#'+$type+$id+'content').html(data.html);
								$('#'+$type+$id+'content').slideDown('slow');
							},
							failed: function()
							{
								$('#'+$type+$id+'loader').slideUp('normal');
							}
						});
					}
				);
			} else {
				$('#'+$type+$id+'content').slideDown('slow');
			}
		} else {
			$('#'+$type+$id+'content').slideUp('normal');
		}
	},

	selAll: function (name,obj)
	{
		l = document.getElementsByName(name+'[]');
		for (i=0; i<l.length; i++)
			l[i].checked = obj.checked;
	}

};

var mod_job_im_loader = {

	load: function(user_id, obj, type, title, url, reload)
	{	
		reload = ( !reload ) ? 0 : 1;
		
		mod_passport_im_loader.params.user_id	= user_id;
		mod_passport_im_loader.params.obj		= obj;
		mod_passport_im_loader.params.type		= type;
		mod_passport_im_loader.params.title		= title;
		mod_passport_im_loader.params.url		= url;
		mod_passport_im_loader.params.reload	= reload;
		
		$.ajax({
			url: '/job/resume/im_send.php',
			dataType: 'json',
			data: {
				to:user_id,
				m:obj, 
				type: type,
				url: url,
				title: title,
				rand:Math.random()},
			success: function(data){mod_job_im_loader.success_loaded(data, type, title, url, reload)},
			type: 'GET'});	
	},
		
	success_loaded: function(data, type, title, url, reload)
	{
		if(data.status == 'ok' || data.status == 'login')
		{
		    $.fn.nyroModalManual({
				modal: true,
				type: 'manual',
				content: '<div id="im_form_container" style="opacity: 0;filter: alpha(opacity=0);">' + data.data + '</div>',
				endShowContent: mod_passport_im_loader.rendered,
				hideContent: mod_passport_im_loader.hideContent,
				endRemove: function(){if(reload)location.href=location.href;}
		    });
			
			if ( data.status == 'ok' ) 
				$('.nyroModalSend').click(this.send);
			else
				$('.nyroModalSend').click(this.login);
		}
		else if(data.status == 'added')
		{
			$.nyroModalRemove();
			if(reload==1)
				location.href=location.href;
		}
		else if(data.status == 'error')
		{
			$('#imSendButton').attr('disabled', false);
			alert(data.data);
		}
	},
	
	send: function()
	{
		$('#imSendButton').attr('disabled', true);
		$.ajax({
			url: '/passport/im/ajax_new.php',
			dataType: 'json',
			data: {
				action:	'im_ajax_new',
				to:		$('#im_form_to').val(),
				text:	$('#im_form_text').val()+"\n\nСсылка на резюме: [URL="+$('#im_select').val()+"]"+$('#im_select option:selected').text()+"[/URL]",
				itype:	mod_passport_im_loader.params.type,
				ititle:	mod_passport_im_loader.params.title,
				iurl:	mod_passport_im_loader.params.url,
				rand:Math.random()
			},
			success: function(data){mod_passport_im_loader.success_loaded(data, mod_passport_im_loader.params.type, mod_passport_im_loader.params.url, mod_passport_im_loader.params.title, mod_passport_im_loader.params.reload)},
			type: 'POST'});	
	}
		
}

var mod_job_incorrect_obj = {	
	ready : true,
	incorrect_id: 0,
	type: null,
	maxlength: 200,
	temp_comment: "",
	
	refresh_captha: function()
	{
		var captcha_img = $('.incorrect_container .captcha_code_img');
		var captcha_code = $('.incorrect_container .captcha_code');
		if (captcha_img!=null && captcha_code!=null)
		{
			captcha_code.val('');
			captcha_img.attr("src", "");
			captcha_img.attr("src", "/service/captcha/?r="+Math.random());
		}
	},

	hide: function()
	{
		var container = $('#incorrect_adv_div');
		container.append($('#incorrect_form'));		
		$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
		
		var captcha_img = $('.incorrect_container .captcha_code_img');		
		if (captcha_img!=null)
			captcha_img.attr("src", "");
			
		$('.incorrect_container').hide();		
		this.incorrect_id = 0;
	},
	
	show: function(id, _type) 
	{		
		if (!this.ready)
		{
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.type = _type;
		
		if(this.incorrect_id == id)
		{
			this.hide();
			return;
		}	
		this.hide();		
		this.incorrect_id = id;
		
		var el = $('#incorrect_container_'+this.incorrect_id);		
		
		
		el.append($('#incorrect_form'));
		$('#incorrect_form').show();
		this.refresh_captha();
		el.show();		
		return false;
	},
	
	incorrect_adv_success: function(data, textStatus)
	{
		mod_job_incorrect_obj.ready = true;
		$('.incorrect_container .send_incorrect').removeAttr('disabled');
		if (data.status==0)
		{					
			alert(data.success);
			mod_job_incorrect_obj.hide();
			$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
		}
		else
		{
			alert(data.error);
			mod_job_incorrect_obj.refresh_captha();
		}
	},
	
	incorrect_adv_error: function(XMLHttpRequest, textStatus, errorThrown)
	{
		$('.incorrect_container .send_incorrect').removeAttr('disabled');
		alert("Ошибка");
		mod_job_incorrect_obj.ready = true;
		mod_job_incorrect_obj.refresh_captha();
	},
	
	incorrect_adv: function()
	{	
		if (!this.ready)
		{
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.ready = false;
		$('.incorrect_container .send_incorrect').attr('disabled', 'true');
		$.ajax({				
			type: 'POST',
			dataType: 'json',
			data: {
				action: 'incorrect',
				type: this.type,
				reasonType: $('.incorrect_container .incorrect_reason').val(),
				id: mod_job_incorrect_obj.incorrect_id,
				comment: $('.incorrect_container .incorrect_comments').val(),
				captcha_code: $('.incorrect_container .captcha_code').val()
			},
			success: this.incorrect_adv_success,
			error: this.incorrect_adv_error
		});
		
	},
	
	onchange_incorrect_reason: function()
	{		
		var incorrect_reason = $('.incorrect_container .incorrect_reason');
		if (incorrect_reason.val()=='other_marks')
			$('.incorrect_container .incorrect_comments').show();
		else
			$('.incorrect_container .incorrect_comments').hide();
	},
	
	textarea_maxlength: function()
	{
		var incorrect_comments = $('.incorrect_container .incorrect_comments');
		var comment = incorrect_comments.val();
		if(comment.length > this.maxlength) 
		{
			incorrect_comments.val(comment.substring(0, this.maxlength));
		}
	}
}
