var manWomanChecked = function(){
	var man = $("#people-search-field-man");
	var woman = $("#people-search-field-woman");

	if(man.attr("checked") && !woman.attr("checked")) {
		man.attr("disabled", true);
		$("#people-search-field-gender").val(1);
	}
	else if(!man.attr("checked") && woman.attr("checked")) {
		$("#people-search-field-gender").val(0);
		woman.attr("disabled", true);
	} else {
		$("#people-search-field-gender").val(2);
		man.removeAttr("disabled");
		woman.removeAttr("disabled");
	}
}

$("#people-search-field-man").bind("click", manWomanChecked);
$("#people-search-field-woman").bind("click", manWomanChecked);
$(document).ready(manWomanChecked);

$("#search-details-link").bind("click", function(){
	var details = $("#people-search-form div.details");
	if(details.is(":visible")){
		details.hide("fast");
		this.className = "expand-icon";
	} else {
		details.show("fast");
		this.className = "collapse-icon";
	}
});

$("#people-search-form input[name=q]").bind("focus", function(){
	var defaultvalue = "имя, интересы";
	this.value == defaultvalue ? this.value= "" : true;
	$(this).addClass("focused");
}).bind("blur", function(){
	var defaultvalue = "имя, интересы";
	this.value == "" ? this.value = defaultvalue : true;
	if(this.value == defaultvalue) $(this).removeClass("focused");
});

var removeDefaultValue = function() {
	var defaultvalue = "имя, интересы";
	$("#people-search-form input[name=q]").val() == defaultvalue ? $("#people-search-form input[name=q]").val("") : true;
}

$("#people-search-form").bind("submit", function() {
	removeDefaultValue();
});

$("div.input-hint span.dashedlink, div.fastsearch span.link-dashed, div.keywords span.link-dashed").bind("click", function(){
	removeDefaultValue();
	var tag = $(this).text();
	var value = $("#people-search-form input[name=q]").val();
	if (!$("#people-search-form input[name=q]").hasClass("focused")) {
		$("#people-search-form input[name=q]").addClass("focused");
	}
	if(value != "") tag = " " + tag;
	$("#people-search-form input[name=q]").val(value + tag);
	$("#people-search-form input[name=q]").focus();
});

$("#people-search-field-age-from").numeric();
$("#people-search-field-age-to").numeric();
$("#people-search-field-star-from").numeric();
$("#people-search-field-star-to").numeric();
$("#people-search-field-city").alphanumeric({allow: " -"});

