function disable(obj){
		obj.attr("disabled", true);
/*		obj.addClass("disabled");*/
}

function enable(obj){
		obj.attr("disabled", false);
/*		obj.removeClass("disabled");*/
}

function vote(num){
	disable($("button"))
	$("#test_loader").show()
	$("#test_question").hide()
	$.post("/json/test/", {id: num},
		function (data, textStatus) {
			enable($("button"))
			if (data.error) {
				alert(data.error);
			} else {
				if (data.goto){
					document.location.href = data.goto
				}
				else {
					$("#test_question_content").html(data.content);
						$("#test_loader").hide()
						$("#test_question").show()
				}
			}

	}, "json");return false;}


function del_div(div){
	$(div).remove();
	return false;
}


/* Form errors */

function show_form_errors(form, error_json) 
	{
	if (error_json['massage']){
		alert(error_json['massage'])
	} else {
	    $(".magic_separator").removeClass("create_form_error");
	    form.find("em.errors").remove();
	    for (name in error_json['details']) {
		var elem = $("#"+name);
		if((error_json['details'][name]['errors']) && (error_json['details'][name]['errors'] != "")){
			elem.parent().prepend($('<em class="errors">' + error_json['details'][name]['errors'] + '</em>'));
			elem.parent().addClass("create_form_error");
		}
	    }
	    $.scrollTo($('.errors:first'),0)
	}
}

$(document).ready(function(){

var pass_step = 0;
var delay = 200;
if ($.browser.msie) { delay = 5}

/* Loaders */   
$(".michael_jackson_loader")
    .ajaxStart(function(){$(this).show()})
    .ajaxStop(function(){$(this).hide()});
    
/* Buttons */

 $("button")
 	.ajaxStart(function(){disable($(this))})
 	.ajaxStop(function(){enable($(this))});
    


/*     */

	$(".del").click(function(){
		if (confirm("Вы уверенны?")) {
			$(this).prepend("<form id='delform' method='POST' action='"+$(this).attr('href')+"'><input type='hidden' name='referer' value='"+$("#referer").attr('href')+"'></form>");
			$('#delform').submit(); 			
			return false
		} else {
			return false}
	});

/* Test */

$("#starttest").click(function(){$("#test_begin").hide();$("#test_instruction").show(); return false;})
$("#starttestnow").click(function(){
	$.post("/json/test/", {id: ""},
		function (data, textStatus) {
			if (data.error) {
				alert(data.error);
			} else {
					$("#test_question").show(); 	
					$("#test_instruction").hide(); 
					$("#test_question_content").html(data.content);
			}

	}, "json");
	return false;
	})


$(".close_test").click(function(){
		$(".tests_win").hide();
		return false;
	});

/* Login screen */

	$("#link_login").click(function(){
		$("#enter_form").fadeIn(delay);
		return false;
	});


	$(".login_close").click(function(){
		$("#enter_form").hide();
		return false;
	});


/* Extended textarea */

	if ($("#count_skills").length){
		for (i = 1; i <= $("#count_skills").val(); i++) {
			$("#id_form-"+i+"-description").maxlength({
			    'feedback' : "#c_"+i // note: looks within the current form
			});
		}
	}

	if ($("#id_description").length){
	$('.d_description').maxlength({
		    'feedback' : '#c_description' // note: looks within the current form
		});}

	if ($("#id_responsibility").length){
	$('.d_responsibility').maxlength({
		    'feedback' : '#c_responsibility' // note: looks within the current form
		});}

	if ($("#id_conditions").length){
	$('.d_conditions').maxlength({
		    'feedback' : '#c_conditions' // note: looks within the current form
		});}

/* Formsets */



$(".skillmaster").change().click(function(){
	if ($(this).attr('checked')){
		$(this).parent().css('font-weight','bold');
		$(this).next().show()
		$(this).parent().next().show()
	}
	else {
		$(this).parent().css('font-weight','normal');
		$(this).next().hide();
		$(this).parent().next().hide();
	}
})

$(".scalemaster").change().click(function(){
	if ($(this).attr('checked')){
		$(this).next().next().show()
		$(this).next().next().next().show()
	}
	else {
		$(this).next().next().hide();
		$(this).next().next().next().hide()
	}
})


	$(".close_form").click(function(){
		$(this).parent().parent().remove();
		return false;
	});

/* JSON Forms */


$("#add_edu").click(function(){
	edu_count = $("#id_edu_form-TOTAL_FORMS").val()*1 + 1;
	form_number = edu_count - 1;
	$.post("/json/form_edu/"+form_number+".html",{1:1},
		function (data, textStatus) {
			$("#id_edu_form-TOTAL_FORMS").val(edu_count);
			$(data.form).appendTo("#edus");
	}, "json");
	return false;
})

$("#add_job").click(function(){
	job_count = $("#id_job_form-TOTAL_FORMS").val()*1 + 1;
	form_number = job_count - 1;
	$.post("/json/form_job/"+form_number+".html",{1:1},
		function (data, textStatus) {
			$("#id_job_form-TOTAL_FORMS").val(job_count);
			$(data.form).appendTo("#jobs");

	
	}, "json");
	return false;
})


$("#loginform").submit(function(){
	$.post("/json/login/", $("#loginform").formSerialize(),
		function (data, textStatus) {
			if (data.error) {
				$("#loginform_email_label").html("<i>E-mail</i>");
			} else { window.location.reload();}
	}, "json");
	return false;
})

$("#loginform_2").submit(function(){
	$.post("/json/login/", $("#loginform_2").formSerialize(),
		function (data, textStatus) {
			if (data.error) {
			} else { window.location.reload();}
	}, "json");
	return false;
})


$("#v_step_2_back").click(function(){
	$.cookie('v_step',1,{expires: 7});
	$("#v_step_1_forward").show();
	$(".create_step").hide();
	$("#v_step_1").show();
	return false;});

$("#v_step_3_back").click(function(){
	$.cookie('v_step',2,{expires: 7});
	$(".create_step").hide();
	if (pass_step){
		$("#v_step_1_forward").show();
		$("#v_step_1").show();}
	else {
		$("#v_step_2_forward").show();
		$("#v_step_2").show();}
	return false;});

$("#v_step_4_back").click(function(){
	$.cookie('v_step',3,{expires: 7});
	$(".create_step").hide();
	$("#v_step_3_forward").show();
	$("#v_step_3").show();
	return false;});

$("#v_step_5_back").click(function(){
	$.cookie('v_step',4,{expires: 7});
	$(".create_step").hide();
	$("#v_step_4_forward").show();
	$("#v_step_4").show();
	return false;});

$("#v_step_6_back").click(function(){
	$.cookie('v_step',5,{expires: 7});
	$(".create_step").hide();
	$("#v_step_5_forward").show();
	$("#v_step_5").show();
	return false;});

$("#v_step_1_forward").click(function(){
	$("#v_form_1").submit();
	return false;});

$("#v_step_2_forward").click(function(){
	$("#v_form_2").submit();
	return false;});

$("#v_step_3_forward").click(function(){
	$("#v_form_3").submit();
	return false;});

$("#v_step_4_forward").click(function(){
	$("#v_form_4").submit();
	return false;});

$("#v_step_5_forward").click(function(){
	$("#v_form_5").submit();
	return false;});

if ($("#pass_step").length){	
	pass_step = 1;}



$("#v_form_1").submit(function(){
	$.post("/addvacancy/", $("#v_form_1").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_form_1"),data)}
			else {
				$.cookie('v_step',2,{expires: 7});
				$("#v_step_1_forward").show();
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".hidden_id").val(data.id);
				if (data.pass_step){
					pass_step = 1;
					$(".create_step").hide();
					$("#v_step_3").show(); }
				else {
					$(".create_step").hide();
					$("#v_step_2").show();
				}
				$.scrollTo(0,0)
			}
		}, "json");
	return false;
});


$("#v_form_2").submit(function(){
	$.post("/addvacancy/", $("#v_form_2").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_form_2"),data)}
			else {
				$.cookie('v_step',3,{expires: 7});
				$("#v_step_2_forward").show();
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".create_step").hide();
				$("#v_step_3").show();
				$.scrollTo(0,0)
			}
		}, "json");
	return false;
});

$("#v_form_3").submit(function(){
	$.post("/addvacancy/", $("#v_form_3").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_form_3"),data)}
			else {
				$.cookie('v_step',4,{expires: 7});
				$("#v_step_3_forward").show();
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".create_step").hide();
				$("#v_step_4").show();
				$.scrollTo(0,0)
			}
		}, "json");
	return false;
});

$("#v_form_4").submit(function(){
	$(".skillmaster:not(:checked)").parent().next().val('');
	$.post("/addvacancy/", $("#v_form_4").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_form_4"),data)}
			else {
				document.location.href = data.url
			}
		}, "json");
	return false;
});

$("#v_form_5").submit(function(){
	$.post("/addvacancy/", $("#v_form_5").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_form_5"),data)}
			else {
				document.location.href = data.url
			}

		}, "json");
	return false;
});

$("#v_mega_form").submit(function(){
	$.post(".", $("#v_mega_form").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#v_mega_form"),data)}
			else {
				document.location.href = data.url
			}}, "json");return false;});

$("#c_form").submit(function(){
	$.post(".", $("#c_form").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#c_form"),data)}
			else {
				document.location.href = data.url
			}}, "json");return false;});
$("#r_step_2_back").click(function(){
	$.cookie('r_step',1,{expires: 7});
	$(".create_step").hide();
	$("#r_step_1").show();
	$("#r_step_1_forward").show();
	return false;});

$("#r_step_3_back").click(function(){
	$.cookie('r_step',2,{expires: 7});
	$(".create_step").hide();
	$("#r_step_2_forward").show();
	$("#r_step_2").show();
	return false;});

$("#r_step_4_back").click(function(){
	$.cookie('r_step',3,{expires: 7});
	$(".create_step").hide();
	$("#r_step_3_forward").show();
	$("#r_step_3").show();
	return false;});

$("#r_step_5_back").click(function(){
	$.cookie('r_step',4,{expires: 7});
	$(".create_step").hide();
	$("#r_step_4_forward").show();
	$("#r_step_4").show();
	return false;});

$("#r_step_6_back").click(function(){
	$.cookie('r_step',5,{expires: 7});
	$(".create_step").hide();
	$("#r_step_5_forward").show();
	$("#r_step_5").show();
	return false;});

$("#r_step_1_forward").click(function(){
	$("#r_form_1").submit();
	return false;});

$("#r_step_2_forward").click(function(){
	$("#r_form_2").submit();
	return false;});

$("#r_step_3_forward").click(function(){
	$("#r_form_3").submit();
	return false;});

$("#r_step_4_forward").click(function(){
	$("#r_form_4").submit();
	return false;});

$("#r_step_5_forward").click(function(){
	$("#r_form_5").submit();
	return false;});




$("#r_form_1").submit(function(){
	$.post("/addresume/", $("#r_form_1").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_1"),data)}
			else {
				$.cookie('r_step',2,{expires: 7});
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".hidden_id").val(data.id);
				$(".create_step").hide();
				$("#r_step_2").show();
				$.scrollTo(0,0)
			}}, "json");return false;});

$("#r_form_2").submit(function(){
	$.post("/addresume/", $("#r_form_2").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_2"),data)}
			else {
				$.cookie('r_step',3,{expires: 7});
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".create_step").hide();
				$("#r_step_3").show();
				$.scrollTo(0,0)
			}}, "json");return false;});

$("#r_form_3").submit(function(){
	$.post("/addresume/", $("#r_form_3").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_3"),data)}
			else {
				$.cookie('r_step',4,{expires: 7});
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".create_step").hide();
				$("#r_step_4").show();
				$.scrollTo(0,0)
			}}, "json");return false;});

$("#r_form_4").submit(function(){
	$.post("/addresume/", $("#r_form_4").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_4"),data)}
			else {
				$.cookie('r_step',5,{expires: 7});
				$(".create_form_error").removeClass("create_form_error");
				$("em.errors").html("");
				$(".create_step").hide();
				$("#r_step_5").show();
				$.scrollTo(0,0)
			}}, "json");return false;});

$("#r_form_5").submit(function(){
	$.post("/addresume/", $("#r_form_5").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_5"),data)}
			else {
				$.cookie('r_step',0,{expires: 0});
				document.location.href = data.url
			}}, "json");return false;});

$("#r_form_6").submit(function(){
	$.post("/addresume/", $("#r_form_6").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_form_6"),data)}
			else {
				$.cookie('r_step',0,{expires: 0});
				document.location.href = data.url
			}}, "json");return false;});

$("#r_mega_form").submit(function(){
	$.post(".", $("#r_mega_form").formSerialize() ,
		function (data, textStatus) {
			if (data.error){show_form_errors($("#r_mega_form"),data)}
			else {
				document.location.href = data.url
			}}, "json");return false;});


$(".stab,.htab").mouseover(function(){
	if (!$(this).hasClass("main_anchors_active")){
		$(this).addClass("anchors_hover");
	}
});

$(".stab,.htab").mouseout(function(){
	$(this).removeClass("anchors_hover");
});


$(".htab").click(
  function () {
    var content_id = "#c"+$(this).attr('id');
    $(".htab").removeClass("anchors_hover");
    $(".htab").removeClass("main_anchors_active");
    $(this).addClass("main_anchors_active");
    $(".hcon").hide();
    $(content_id).show();
    $.cookie('vacancy_tab',$(this).attr('id'),{expires: 7});
},function () {});

$(".stab").click(
  function () {
    var content_id = "#c"+$(this).attr('id');
    $(".stab").removeClass("anchors_hover");
    $(".stab").removeClass("main_anchors_active");
    $(this).addClass("main_anchors_active");
    $(".hcon").hide();
    $(content_id).show();
    $.cookie('resume_tab',$(this).attr('id'),{expires: 7});
},function () {});

$("#id_region,#id_profession,#id_skill_4_use,#id_skill_4_use_2,#id_skill_4_use_3,#id_schedule,#id_business_type").change(function(){
	$("#search_form").submit();
});

$(".start_form > select").change(function(){
	$(this).parent().submit();
});

$("#presentation > .close").click(function(){
	$("#presentation_containa").hide();
});

$("#id_description").mouseup();


        
$.cookie('session','1');
if ($.cookie('session')){
	if (!$("#binded").length){	
		$.post("/json/registermeplz/",function(data){},'json');}}
});


function favorite_vacancy(id){
	$.post("/json/favorite_vacancy/", {id: id},
		function (data, textStatus) {
			if (data.error) {

			} else {
				$("#favorite_vacancy_"+id).attr("src","/m/i/star"+data.img+".gif");
				$("#favorite_vacancys_sidebar").html(data.sidebar);
			}
	}, "json");
	return false;
}


function favorite_vacancy_body(id){
	$.post("/json/favorite_vacancy/", {id: id},
		function (data, textStatus) {
			if (data.error) {
			} else {
				$("#favorite_vacancy_sidebar").html(data.link);
			}
	}, "json");
	return false;
}

function favorite_resume_body(id){
	$.post("/json/favorite_resume/", {id: id},
		function (data, textStatus) {
			if (data.error) {
			} else {
				$("#favorite_resume_sidebar").html(data.link);
			}
	}, "json");
	return false;
}


function favorite_resume(id){
	$.post("/json/favorite_resume/", {id: id},
		function (data, textStatus) {
			if (data.error) {

			} else {
				$("#favorite_resume_"+id).attr("src","/m/i/star"+data.img+".gif");
				$("#favorite_resumes_sidebar").html(data.sidebar);
			}

	}, "json");
	return false;
}


var previos_field = "";
function more_field(field){
	$(".hidden_fields").hide();
	$("#hidden_"+field).show();
	$(".without_filter").removeClass('without_filter');		
	$(".current_filter").removeClass('current_filter');		
	$("#more_"+field).addClass("current_filter");
	previos_field = $("#id_"+field)
	return false;
}


