//Формирование тэгов
function insert_tags(value, id)
{	
	if(document.getElementById(id).value.indexOf(value, 0)!=-1)
	{
		s = value +";";
		document.getElementById(id).value = document.getElementById(id).value.replace(s, '');
	}
	else
		document.getElementById(id).value = document.getElementById(id).value + value + ";";
}

function insert_tags_div(value, id)
{	
	mas = document.getElementById(id).innerHTML.substring(0, document.getElementById(id).innerHTML.length-1);
	mas = mas.split(";");

	index = 0;
	
	for(i=0;i<=mas.length;i++)
	{
		if(mas[i]==value)
			index = 1;
	}

	if(index==1)
	{
		str_html = "";
		for(i=0;i<=mas.length;i++)
		{
			if(mas[i]!=value && mas[i]!=null)
				str_html = str_html + mas[i] + ";";
		}
		
		document.getElementById(id).innerHTML = str_html;
	}
	else
		document.getElementById(id).innerHTML = document.getElementById(id).innerHTML + value + ";";
}

$.fn.treatmentObj = function(options)
{
	var settings = {obj: "", id: "", count: 3};
	settings = $.extend(settings, options);
	
	$(settings.obj).each(function (){
		$(this).unbind();
		$(this).click(function(){
			str_control = $("#" + settings.id).val();
			str_control = str_control.substring(0, str_control.length-1);
			
			if(str_control!="")
			{
				mas_control = str_control.split(";");
				mas_control_length = mas_control.length;
			}
			else
				mas_control_length = 0;

			if((settings.id=="text_tag_branch" && mas_control_length>=settings.count && $(this).attr('class')=="unchecked") || (settings.id=="text_geography" && $(this).attr('class')=="unchecked" && ((mas_control_length>=settings.count && $('#text_geography_user').val()=="") || (mas_control_length>=(settings.count-1) && $('#text_geography_user').val()!=""))) || (settings.id.search(/text_geography_office/)!=-1 && str_control!="" && $(this).attr('class')=="unchecked"))
				return false;
			else
			{
				if($(this).attr('class')=="checked")
				{
					$(this).removeClass("checked");
					$(this).addClass("unchecked");
				}
				else if($(this).attr('class')=="checked hover")
				{
					$(this).removeClass("checked hover");
					$(this).addClass("unchecked");
				}
				else if($(this).attr('class')=="unchecked")
				{
					$(this).removeClass("unchecked");
					$(this).addClass("checked");
				}
				else if($(this).attr('class')=="unchecked hover")
				{
					$(this).removeClass("unchecked hover");
					$(this).addClass("checked");
				}
	
				if(window.location.href.match(/.+secId.+/))
					insert_tags_div($(this).text(), settings.id);
				else
					insert_tags($(this).text(), settings.id);
				
				if(settings.id=="text_tag_branch")
				{
					this_tags = $("#text_tag_common").val();
					
					var out = new String();
					
					$("#branch_tree a.checked").each(function() {
						out = out + $(this).attr("tag_id") + "|";
					});
						
					out = out.substr(0, out.length-1);
				
					$("#place_tag").load("/parse/", {str_tag_id: out, str_this_tag_id: this_tags, count: $("#place_tag").attr('count')});
				}
				
				return false;
			}
		});
	});
}

//Подзрузка географии, изменение стиля пункта списка
function getGeogrCh(this_class, id, did, text_pref)
{
	if(this_class=='close')
	{
		if($("#geography_tree").attr('count')==null)
			geogr_count = 10000;
		else
			geogr_count = $("#geography_tree").attr('count');
		
		$('#pgeogr' + did + id).load('/pgeogr/', {geography_owner:id, owner_null:0, did:did, text_pref:text_pref}, function() {
			$().treatmentObj({obj:"#geography_tree" + did + " a:odd", id:"text_geography" + text_pref, count:geogr_count}); 
			$().restStyle({text_id:"#text_geography" + text_pref, obj:"#geography_tree" + did + " a:odd"}); 
			aFalse("#geography_tree" + did + " a:even");
		});
		document.getElementById('geogr' + did + id).className = 'open';
	}
	else if(this_class=='open')
	{
		$('#pgeogr' + did + id).load('/pgeogr/', {geography_owner:0, owner_null:0, did:did, text_pref:text_pref});
		document.getElementById('geogr' + did + id).className = 'close';
	}
}

//Подзрузка отраслей, изменение стиля пункта списка
function getTagCh(this_class, id)
{
	if(this_class=='close')
	{
		if($("#branch_tree").attr('count')==null)
			branch_count = 3;
		else
			branch_count = $("#branch_tree").attr('count');
		
		$('#pbranch' + id).load('/pbranch/', {branch_owner:id, owner_null:0}, function() {
			$().treatmentObj({obj:"#branch_tree a:odd", id:"text_tag_branch", count:branch_count}); 
			$().restStyle({text_id:"#text_tag_branch", obj:"#branch_tree a:odd"}); 
			aFalse("#branch_tree a:even");
		});
		document.getElementById('branch' + id).className = 'open';
	}
	else if(this_class=='open')
	{
		$('#pbranch' + id).load('/pbranch/', {branch_owner:0, owner_null:0});
		document.getElementById('branch' + id).className = 'close';
	}
}

function aFalse(obj)
{
	$(obj).each(function() { 
		$(this).click(function() { 
			return false; 
		}); 
	});	
}

function getPageInfo(this_class, id, post_select, category_select, pay_from, pay_to)
{
	$("." + this_class + " a").each(function () {
		$(this).click(function () {
			$("#" + id).load($(this).attr('href'), {post_select:post_select, category_select:category_select, pay_from:pay_from, pay_to:pay_to}, function () { getPageInfo(this_class, id, post_select, category_select, pay_from, pay_to); });
			return false;
		});
	});	
}

function getGeogrDefault(checked)
{
	$("#text_geography").val("");
	
	if(checked==1)
	{
		value = $("#hidden_geogr_default").val();
		$("#text_geography").val(value);
	}
}

function loadTagsForIndex(filter, act, deact)
{
	$("#tags_catalog").load("/parse_tags_catalog/", {filter:filter}, function() {
		$("#" + act + " font").css('border-bottom','solid 1px #ff8400');
		$("#" + deact + " font").css('border','none');
		$().getChildrenTagsIndex();
	});
	$("#content_catalog").load("/parse_catalog/", {filter:filter}, function() {
		$().getChildrenTagsIndex()
	});

	$("#block").load("/block_show/", {filter:filter}, function() {
		$().getChildrenTagsIndex()
	});
	$("#block").hide();
	$(".hide_blok").hide();
}

$.fn.createTree = function(options) {
	var settings = {id: "tree_all"};
	settings = $.extend(settings, options);
	
	//Формирование дерева
	$("#" + settings.id).treeview({
		persist: "location",
		collapsed: true,
		unique: false
	});
}

$.fn.restStyle = function(options) {
	var settings = {text_id: "", obj: ""};
	settings = $.extend(settings, options);

	str = $(settings.text_id).val();
	str = str.substring(0, str.length-1);
	mas = new Array();
	mas = str.split(";");

	$(settings.obj).each(function() {							 
		a = $(this);
		$.each(mas, function(i, n) {
			if(a.text()==n)
			{
				if(a.attr('class')=="unchecked")
				{
					a.removeClass("unchecked");
					a.addClass("checked");
				}
			}
		});
	});
}

$.fn.getChildrenTagsIndex = function() {
	$(".parent a").each(function() {
		$(this).mouseover(function() {
			$(this).next("div").css({"display":"block"});
			//$(this).next("div").load("/pch_tags/", {parent_id:$(this).attr('tag_id')});
		});
		
		$(this).mouseout(function(e) {
			$(this).next("div").css({"display":"none"});
		});
	});
}

function clientsLogin()
{
	if($('#clients_login').val()!='')
	{
		$('#clients_send').attr('disabled', false);
		$('#clients_send').removeClass('button_deact');
		$('#clients_send').addClass('button');
	}
	else
	{
		$('#clients_send').attr('disabled', true)
		
		if($('#clients_send').attr('class')=='button')
		{
			$('#clients_send').removeClass('button');
			$('#clients_send').addClass('button_deact');	
		}
	}	
}

function getStyleForObj(obj, type)
{
	if(obj.attr('obj_start')!=null)
		obj_start = obj.attr('obj_start');
	else
		obj_start = '';
					  
	if(obj.attr('obj_end')!=null)
		obj_end = obj.attr('obj_end');
	else
		obj_end = '';
					  
	if(obj.attr('obj_class')!=null)
	{
		if(obj.attr('obj_style')=="background_top")
			$('.' + obj.attr('obj_class')).css('background', 'url(' + $('#mmenu_pointer_deact1').attr('path') + ') 0 ' + obj.val() +  'px no-repeat');
		else
			$('.' + obj.attr('obj_class')).css(obj.attr('obj_style'), obj_start + obj.val() + obj_end);	
	}
		
	if(obj.attr('obj_class')=="msite_mmenu" || obj.attr('obj_class')=="a" || obj.attr('obj_class')=="ants")
	{
		$(type).each(function() {
			if($(this).attr('name')+'de'==obj.attr('name'))
			{
				$(this).val(obj.val());
					
				if($(this).attr('obj_style')=="background_top")
					$('.msite_mmenus').css('background', 'url(' + $('#mmenu_pointer_act1').attr('path') + ') 0 ' + obj.val() +  'px no-repeat');
				else
				$('.' + obj.attr('obj_class') + 's').css(obj.attr('obj_style'), obj_start + obj.val() + obj_end);
			}
		});
	}
}

function radioNone(rid, fid, icname, cname, type)
{
	$('#' + rid).click(function() {
		if(type=="image")
		{
			$('.' + icname).css('display', 'none');
			$('.d' + icname).css('display', 'block');
		}
		else
		{
			$('.' + cname).css('background', 'none');
			$('.' + cname).css('padding', '0px');	
		}
		
		$('#' + fid).attr('disabled', true);
	});	
}

function radioImage(rid, fid, icname, cname, type, path)
{
	$('#' + rid).click(function() {	
		if(type=="image")
		{
			$('.' + icname).css('display', 'block');
			$('.d' + icname).css('display', 'none');
		}
		else
		{
			if(cname=="msite_mmenu" || cname=="msite_mmenus")
				mmenu_top = $('#mmenu_top_actde').val() + "px";
			else
				mmenu_top = '0';
			
			$('.' + cname).css('background', 'url(' + path + ') 0 ' + mmenu_top + ' no-repeat');
			$('.' + cname).css('padding-left', $(this).attr('padd') + 'px');
		}
		
		$('#' + fid).attr('disabled', false);
	});
}

$(document).ready(function(){	

/*
//left menu, contacts
var size_td = $(".msite_contact_under").height();
var size_menu = $(".msite_mmenu_bg").height();
var size_contacts = $(".msite_contact_name").height();
var size_contact_bg = $(".msite_contact_bg").height();

var size_cont = size_td -size_menu- (size_contacts + size_contact_bg) - 40;
//"top", "-"+length+"px"
$(".msite_cont").css("margin-top",""+size_cont+"px");
*/

	//submit в личный кабинет
	$('#a_lk').click(function() {
		$('#form_lk').submit();
		return false;
	});
		
	//submit по темам вопросов
	$('#search_faq_themes').click(function() {
		$('#form_sft').submit();
		return false;
	});
		   
	//Подключение всплывающих ведомых тэгов
	$().getChildrenTagsIndex();
		
	$('#text_geography_user').click(function() {
		str_control = $("#text_geography").val();
		
		if(str_control!="")
		{
			str_control = str_control.substring(0, str_control.length-1);
			mas_control = str_control.split(";");
			mas_control_length = mas_control.length;
			
			if(mas_control_length>=$('#geography_tree').attr('count'))
				$(this).blur();
		}											 
	});
		
	$('#text_geography_user').blur(function() {
		value_length = document.getElementById('text_geography_user').value.length - 1;
		if(document.getElementById('text_geography_user').value.lastIndexOf(';')!=value_length)
			$('#text_geography_user').val($('#text_geography_user').val() + ';');
	});

	if($("#only_sfo").val()!=null)
	{
		if($("#geography_tree").attr('count')==null)
			geogr_count = 10000;
		else
			geogr_count = $("#geography_tree").attr('count');

		//Дерево Географии
		if($("#only_sfo").attr("checked")==true)
			$("#geography_tree").load('/pgeogr/', {geography_owner:2, owner_null:0}, function() {
				$().treatmentObj({obj:"#geography_tree a:odd", id:"text_geography", count:geogr_count}); 
				$().restStyle({text_id:"#text_geography", obj:"#geography_tree a:odd"}); 
				aFalse("#geography_tree a:even");
			});
		else
			$("#geography_tree").load('/pgeogr/', {geography_owner:0, owner_null:1}, function() { 
				$().treatmentObj({obj:"#geography_tree a:odd", id:"text_geography", count:geogr_count}); 
				$().restStyle({text_id:"#text_geography", obj:"#geography_tree a:odd"}); 
				aFalse("#geography_tree a:even");
			});
		
		$("#only_sfo").click(function() {
			if($("#geography_tree").attr('count')==null)
				geogr_count = 10000;
			else
				geogr_count = $("#geography_tree").attr('count');
									  
			if($(this).attr("checked")==true)
			{	
				$("#geography_tree").load('/pgeogr/', {geography_owner:2, owner_null:0}, function() {
					$().treatmentObj({obj:"#geography_tree a:odd", id:"text_geography", count:geogr_count}); 
					$().restStyle({text_id:"#text_geography", obj:"#geography_tree a:odd"}); 
					aFalse("#geography_tree a:even");
				});	
			}
			else
			{
				$("#geography_tree").load('/pgeogr/', {geography_owner:0, owner_null:1}, function() {
					$().treatmentObj({obj:"#geography_tree a:odd", id:"text_geography", count:geogr_count}); 
					$().restStyle({text_id:"#text_geography", obj:"#geography_tree a:odd"}); 
					aFalse("#geography_tree a:even");
				});
			}
		});
		
		if($("#geogr_default").attr("checked")==true)
			getGeogrDefault(1);
			
		$("#geogr_default").click(function() {
			if($(this).attr("checked")==true)
				getGeogrDefault(1);
			else
				getGeogrDefault(0);
		});
	}
		
	if($("#text_tag_branch").val()!=null)
	{
		if($("#branch_tree").attr('count')==null)
			branch_count = 3;
		else
			branch_count = $("#branch_tree").attr('count');
		
		//Дерево Отраслей
		$("#branch_tree").load('/pbranch/', {branch_owner:0, owner_null:1}, function() {
			$().treatmentObj({obj:"#branch_tree a:odd", id:"text_tag_branch", count:branch_count}); 
			$().restStyle({text_id:"#text_tag_branch", obj:"#branch_tree a:odd"}); 
			aFalse("#branch_tree a:even");
		});
	}
		
	if($("#text_geography_office1").val()!=null)
	{
		//Дерево Географии для Контактов
		$(".org").each(function(i) {
			id = i + 1;

			if($("#only_sfo" + id).attr("checked")==true)
				$("#geography_tree" + id).load('/pgeogr/', {geography_owner:2, owner_null:0, did:id, text_pref:'_office' + id}, function() {
id = i + 1; 
$().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); 
$().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); 
aFalse("#geography_tree" + id + " a:even"); 
});
			else
				$("#geography_tree" + id).load('/pgeogr/', {geography_owner:0, owner_null:1, did:id, text_pref:'_office' + id}, function() { 
id = i + 1; 
$().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); 
$().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); 
aFalse("#geography_tree" + id + " a:even"); });
			
			$("#only_sfo" + id).click(function() {
				id = i + 1;

				if($(this).attr("checked")==true)
				{	
					$("#geography_tree" + id).load('/pgeogr/', {geography_owner:2, owner_null:0, did:id, text_pref:'_office' + id}, function() { $().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); $().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); aFalse("#geography_tree" + id + " a:even"); });	
				}
				else
				{
					$("#geography_tree" + id).load('/pgeogr/', {geography_owner:0, owner_null:1, did:id, text_pref:'_office' + id}, function() { $().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); $().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); aFalse("#geography_tree" + id + " a:even"); });
				}
			});
		});
	}

	//Добавление блока контактов
	$("#add").click(function(){
		$("#orgs .org:first-child").clone().appendTo($("#orgs"));
		id = $("#orgs .org").size();
	
		$("#orgs .org:last-child #text_geography_office1").val("");
		$("#orgs .org:last-child #text_geography_office1").attr("name","text_geography_office[" + id + "]");
		$("#orgs .org:last-child #text_geography_office1").attr("id","text_geography_office" + id);
		$("#orgs .org:last-child #text_geography_user_office1").val("");
		$("#orgs .org:last-child #text_geography_user_office1").attr("name","text_geography_user_office[" + id + "]");
		$("#orgs .org:last-child #text_geography_user_office1").attr("id","text_geography_user_office" + id);
		
		$("#orgs .org:last-child #only_sfo1").attr("checked","checked");
		$("#orgs .org:last-child #only_sfo1").attr("name","only_sfo[" + id + "]");
		$("#orgs .org:last-child #only_sfo1").attr("id","only_sfo" + id);
		
		$("#orgs .org:last-child #1").val("");
		$("#orgs .org:last-child #1").attr("name","organization_contacts[" + id +"]");
		$("#orgs .org:last-child #1").attr("id",id);

		$("#orgs .org:last-child #div_contacts1").html("осталось <b>500</b> символов");
		$("#orgs .org:last-child #div_contacts1").attr("id","div_contacts"+id);

		$("#orgs .org:last-child #geography_tree1").text("");
		$("#orgs .org:last-child #geography_tree1").attr("id","geography_tree" + id);

		$("#geography_tree" + id).load('/pgeogr/', {geography_owner:2, owner_null:0, did:id, text_pref:'_office' + id}, function() { $().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); $().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); aFalse("#geography_tree" + id + " a:even"); });

		//Дерево Географии для Контактов
		$("#only_sfo" + id).click(function() {
			id = $("#orgs .org").size();

			if($(this).attr("checked")==true)
			{	
				$("#geography_tree" + id).load('/pgeogr/', {geography_owner:2, owner_null:0, did:id, text_pref:'_office' + id}, function() { $().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); $().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); aFalse("#geography_tree" + id + " a:even"); });	
			}
			else
			{
				$("#geography_tree" + id).load('/pgeogr/', {geography_owner:0, owner_null:1, did:id, text_pref:'_office' + id}, function() { $().treatmentObj({obj:"#geography_tree" + id + " a:odd", id:"text_geography_office" + id}); $().restStyle({text_id:"#text_geography_office" + id, obj:"#geography_tree" + id + " a:odd"}); aFalse("#geography_tree" + id + " a:even"); });
			}
		});
		
		$("#text_geography_user_office"+id).focus(function (){
			if(document.getElementById('text_geography_office'+id).value!='')
				$(this).blur();
		});
	});
		
	//Удаление блока контактов
	$("#del").click(function(){
		if ($("#orgs .org:last-child select").attr("name") != "select_geography_contacts[1]")
			$("#orgs .org:last-child").remove();
	});
		
	//Загрузка Каталога/Интернет-каталога в зависимости от адреса страницы
	if(location.href.match("catalog_organization"))
	{
		if(location.href.match("usual"))
			loadTagsForIndex('0', 'cat1', 'cat2');
		else if(location.href.match("internet"))
			loadTagsForIndex('ok', 'cat2', 'cat1');
		else
			loadTagsForIndex('0', 'cat1', 'cat2');
	}
	else
	{
		if($("#tags_catalog").size()!=0)
			loadTagsForIndex('0', 'cat1', 'cat2');
	}
		
	//Подгрузка блока с данными "Каталог организаций"
	$("#cat1").click(function () {
		loadTagsForIndex('0', 'cat1', 'cat2');
	});
	
	$("#img_cat1").click(function () {
		loadTagsForIndex('0', 'cat1', 'cat2');
	});
		
	//Подгрузка блока с данными "Интернет каталог"
	$("#cat2").click(function () {
		loadTagsForIndex('ok', 'cat2', 'cat1');
	});
	
	$("#img_cat2").click(function () {
		loadTagsForIndex('ok', 'cat2', 'cat1');
	});
	
	//Регистрация организации
	$("#reg1").click(function () {
		$("#reg1 font").css('border-bottom','solid 1px #ff8400');
		$("#reg2 font").css('border','none');
		
		$("#div_reg1").css('display','block');
		$("#div_reg2").css('display','none');
	});
		
	//Регистрация частного пользователя
	$("#reg2").click(function () {
		$("#reg2 font").css('border-bottom','solid 1px #ff8400');
		$("#reg1 font").css('border','none');
		
		$("#div_reg2").css('display','block');
		$("#div_reg1").css('display','none');
	});
		
	//Проверка уникальности логина
	$("#login_org").blur(function() {
		$("#div_login_org").load("/islogin/", {login: $("#login_org").val()});
	});
	
	$("#login_user").blur(function() {
		$("#div_login_user").load("/islogin/", {login: $("#login_user").val()});
	});
	
	if($("#div_events").size()!=0)
	{
		//Подгрузка событий
		$("#div_events").load("/parse_events/", {date_id: "today", type_ev_id: "all", items:$("#div_events").attr('items')});	
	}
		
	if($("#div_date_events").size()!=0)
	{
		//Стили для дат событий
		$("#div_date_events a").each(function (){		
			$(this).click(function(){
				$("#div_date_events a").each(function (){
					if($(this).attr("class")=="check_ev")
					{
						$(this).removeClass("check_ev");
						$(this).addClass("uncheck_ev");
					}
				});
			
				if($(this).attr("class")=="uncheck_ev")
				{
					$(this).removeClass("uncheck_ev");
					$(this).addClass("check_ev");
				}
				
				var bbb = $(this).attr("val");
			
				$("#div_type_events a").each(function (){
					if($(this).attr("class")=="check_ev")
						$("#div_events").load("/parse_events/", {date_id: bbb, type_ev_id: $(this).attr("val"), items:$("#div_events").attr('items')});
				});
			});
		});
	}
	
	if($("#div_type_events").size()!=0)
	{
		//Стили для категорий событий
		$("#div_type_events a").each(function (){		
			$(this).click(function(){
				$("#div_type_events a").each(function (){
					if($(this).attr("class")=="check_ev")
					{
						$(this).removeClass("check_ev");
						$(this).addClass("uncheck_ev");
					}
				});
			
				if($(this).attr("class")=="uncheck_ev")
				{
					$(this).removeClass("uncheck_ev");
					$(this).addClass("check_ev");
				}
				
				var bbb = $(this).attr("val");
			
				$("#div_date_events a").each(function (){
					if($(this).attr("class")=="check_ev")
						$("#div_events").load("/parse_events/", {date_id: $(this).attr("val"), type_ev_id: bbb, items:$("#div_events").attr('items')});
				});
			});
		});
	}
		
	if($("#div_news").size()!=0)
	{
		//Подгрузка новостей по умолчанию (Все)
		$("#div_news").load("/parse_news/", {type_news_id: "all", items:$("#div_news").attr('items')});
	}
	
	if($("#div_type_news").size()!=0)
	{
		//Стили для категорий новостей
		$("#div_type_news a").each(function (){		
			$(this).click(function(){
				$("#div_type_news a").each(function (){
					if($(this).attr("class")=="check_ev")
					{
						$(this).removeClass("check_ev");
						$(this).addClass("uncheck_ev");
					}
				});
			
				if($(this).attr("class")=="uncheck_ev")
				{
					$(this).removeClass("uncheck_ev");
					$(this).addClass("check_ev");
				}
				
				var bbb = $(this).attr("val");
			
				$("#div_type_news a").each(function (){
					if($(this).attr("class")=="check_ev")
						$("#div_news").load("/parse_news/", {type_news_id: bbb, items:$("#div_news").attr('items')});
				});
			});
		});
	}
				
	//Формирование тэгов относительно отраслей, подгрузка тэгов
	$("#sprav").click(function(){ 
		var this_tags = document.getElementById("text_tag_common").value;
	
		var out = new String();
				
		$("#branch_tree a.checked").each(function() {
			out = out + $(this).attr("tag_id") + "|";
		});

		out = out.substr(0, out.length-1);
		$("#place_tag").load("/parse/", {str_tag_id: out, str_this_tag_id: this_tags, count: $("#place_tag").attr('count')});

		if($('#tags_container').css('display')=="none") 
			$('#tags_container').css('display','block'); 
		else
			$('#tags_container').css('display','none');
	});
							
	$("#div_tags2 a").each(function (){
		$(this).click(function(){
			return false;
		});
	});
	
	$("#tags_from_branch a").each(function (){
		$(this).click(function(){
			return false;
		});
	});
	
	$("#div_date_events a").each(function (){
		$(this).click(function(){
			return false;
		});
	});
	
	$("#div_type_events a").each(function (){
		$(this).click(function(){
			return false;
		});
	});
	
	$("#div_type_news a").each(function (){
		$(this).click(function(){
			return false;
		});
	});
		
	//Разворачивание полного описание тэга/географии
	$('#all_body_tag').click(function () {
		$(this).css('display','none');
		$('#small_body_tag').css('display','none');
		$('#div_all_body_tag').css('display','block');
		
		return false;
	});
	
	$('#clients_login').keyup(function() {
		clientsLogin()
	});
	
	$('#clients_login').blur(function() {
		clientsLogin()
	});

	//Работа с блоком география на главной странице
	$(".show_blok").click(function () {
		$("#block").show();
		$(".hide_blok").show();
	});
	
	$(".hide_blok").click(function () {
		$("#block").hide();
		$(".hide_blok").hide();
	});
	
	//====================================================================
	// Работа с резюме и вакансиями
	//====================================================================
	//--------------------Автозаполнение должностей-----------------------
	if($('#vacancy_post').size()!=0)
	{
		$("#vacancy_post").autocomplete("/parse_post/", {
			delay:10,
			minChars:1,
			matchSubset:1,
			autoFill:true,
			matchContains:1,
			cacheLength:10,
			selectFirst:true,
			maxItemsToShow:10
		});
	}
	//--------------------------------------------------------------------
	
	//******** Блок Вакансии ********//
	// начальная загрузка		
	if($(".vacansy1").size()!=0)
	{
		//загрузка Кадровые агенства
		$("#catalog_agent").load("/catalog_select/");
		//загрузка вакансий
		$("#vacansy_tag").load("/vacancys/");
		//загрузка 
		$("#vacancys_post").load("/vacancys_right/");
	}

	if($(".vacansy2").size()!=0)
	{
		//загрузка Кадровые агенства
		$("#catalog_agent").load("/catalog_select/");
		//загрузка вакансий
		var vid = $(".vacansy2").attr("id");
		$("#vacansy_tag").load("/vacancys/", {id_vac:vid});
		//загрузка 
		$("#vacancys_post").load("/vacancys_right/");
	}

//Подгрузка блока с данными "Вакансии" (сверху поиск, внизу список из *ти последних)
$(".vacansy1").click(function () {
	$("#vacansy_tag").load("/vacancys/", {}, function () { getPageInfo("din_vac", "vacancys", 0, 0, 0, 0 ); });
	$("#vacancys_post").load("/vacancys_right/");
	$("#search_vacansy1").addClass("inset");
	$(".vacansy2").addClass("inset");
	$("#search_res2").removeClass("inset");
	$("#catalog_agent").load("/catalog_select/", {region_select:0});
});

//Подгрузка блока с данными "Вакансии" (сверху поиск, внизу список из *ти последних)
$(".vacansy2").click(function () {
	$("#vacansy_tag").load("/vacancys/", {}, function () { getPageInfo("din_vac", "vacancys", 0, 0, 0, 0 ); });
	$("#vacancys_post").load("/vacancys_right/");
	$("#search_vacansy1").addClass("inset");
	$(".vacansy2").addClass("inset");
	$("#search_res2").removeClass("inset");
	$("#catalog_agent").load("/catalog_select/", {region_select:0});
});

//Вывод одной вакансии
$(".vac").livequery('click', function () {
	var id = $(this).attr("id");
	$("#vacansy_tag").load("/vacancys/", {id_vac:id});
	$("#vacancys_post").load("/vacancys_right/");
	$("#search_vacansy1").addClass("inset");
	$(".vacansy2").addClass("inset");
	$("#search_res2").removeClass("inset");
	$("#catalog_agent").load("/catalog_select/", {region_select:0});
});

//Поиск вакансий (сверху поиск, внизу список из *10*ти)
$("#find_vacancy").livequery('click', function () {
	$("#vacansy_tag").load("/vacancys/", {pager_str:1, post_select:$("#post_vacancy").val(), 
		category_select:$("#category_vacancy").val(), 
		//region_select:$("#region_vacancy").val(), town_select:$("#category_vacancy").val(), 
		pay_from:$("#pay_from_vacancy").val(), pay_to:$("#pay_to_vacancy").val(), 
		region_select:$("#region_vacancy").val(), town_select:$("#town_vacancy").val(), 
		town_select1:$("#town_vacancy1").val()}, 
		function () { getPageInfo("din_vac", "vacancys", $("#post_vacancy").val(), $("#category_vacancy").val(), 
				$("#pay_from_vacancy").val(), $("#pay_to_vacancy").val()); });
	$("#catalog_agent").load("/catalog_select/", {region_select:$("#region_vacancy").val()});
});


// Вывод всех вакансий (без поиска, список из *10*ти)
$(".all_vac").livequery('click', function () {
	var id = $(this).attr("id");
	var pager_id = id.substr(1);
	$("#vacansy_tag").load("/vacancys/", {pager_str:1, start_page:pager_id});
	$("#vacancys_post").load("/vacancys_right/");
	$("#search_vacansy1").addClass("inset");
	$(".vacansy2").addClass("inset");
	$("#search_res2").removeClass("inset");
	$("#catalog_agent").load("/catalog_select/", {region_select:0});
});

//по фильтру правого блока
//отображение вакансий по категории
$(".find_vacancy_category").livequery('click', function () {
	var id = $(this).attr("id");
	var category_id = id.substr(1);
	$("#vacansy_tag").load("/vacancys/", {pager_str:1, category:" : ",  category_select:category_id}, function () { getPageInfo("din_vac", "vacancys", 0, category_id, 0, 0); });
});

//по городам	
$(".find_vacancy_towns").livequery('click', function () {
	var id = $(this).attr("id");
	var town_id = id.substr(1);
	$("#vacansy_tag").load("/vacancys/", {pager_str:1, town:" : ", town_select:town_id}, 
			function () { getPageInfo("din_vac", "vacancys", 0, 0, 0,0, town_id); });
});

//навигация постраничная
$(".vac_pager_ajax a").livequery('click', function () {
	var id = $(this).attr("id");
	var pager_id = id.substr(1);
	//alert($("#town_vacancy1").val());
	$("#vacansy_tag").load("/vacancys/", {pager_str:1, start_page:pager_id, post_select:$("#post_vacancy").val(), 
		category_select:$("#category_vacancy").val(), pay_from:$("#pay_from_vacancy").val(), pay_to:$("#pay_to_vacancy").val(),
		region_select:$("#region_vacancy").val(), town_select:$("#town_vacancy").val(), 
		town_select1:$("#town_vacancy1").val()});
	return false;
});
;

//постраничная навигация
$(".res_pager_ajax a").livequery('click', function () {
	var id = $(this).attr("id");
	var pager_id = id.substr(1);
	$("#vacansy_tag").load("/resumes/", {pager_str:1, start_page:pager_id, post_select:$("#post_resume").val(), 
		category_select:$("#category_resume").val(), pay_from:$("#pay_from_resume").val(), pay_to:$("#pay_to_resume").val(),
		region_select:$("#region_resume").val(), town_select:$("#town_vacancy").val(), town_select1:$("#town_resume1").val()});
	return false;
});
/*$(".res_pager_ajax a").livequery('click', function () {
	var id = $(this).attr("id");
	var pager_id = id.substr(1);
	$("#vacansy_tag").load("/resumes/", {pager_str:1, start_page:pager_id, post_select:$("#post_resume").val(), 
		category_select:$("#category_resume").val(), pay_from:$("#pay_from_resume").val(), pay_to:$("#pay_to_resume").val()});
	return false;
});*/

//подгрузка списка городов, согласно заданному региону
$("#region_vacancy").livequery('change', function () {
		
		var region_id = $("#region_vacancy").val();
		$("#town_vacancy1").remove();
		$("#towns_load").load("/towns_select/", {region_select:region_id});
});


//******** Блок Резюме ********//
//Подгрузка блока с данными "Резюме"
$(".resume2").click(function () {
$("#vacansy_tag").load("/resumes/", {}, function () { getPageInfo("din_vac", "resumes", 0, 0, 0, 0 ); });
$("#vacancys_post").load("/resume_right/");
$("#search_res2").addClass("inset");
$("#search_vacansy1").removeClass("inset");
	$(".vacansy2").removeClass("inset");
$("#catalog_agent").load("/catalog_select/", {region_select:0});
});

// Поиск резюме (сверху поиск, внизу список из *ти)
	$("#find_resume").livequery('click', function () {
		$("#vacansy_tag").load("/resumes/", {pager_str:1, post_select:$("#post_resume").val(), 
			region_select:$("#region_resume").val(), town_select:$("#town_vacancy").val(), town_select1:$("#town_resume1").val(), 
			category_select:$("#category_resume").val(), pay_from:$("#pay_from_resume").val(), 
			pay_to:$("#pay_to_resume").val()}, function () { getPageInfo("din_vac", "resumes", 
					$("#post_resume").val(), $("#category_resume").val(), $("#pay_from_resume").val(), 
					$("#pay_to_resume").val()); });
		$("#catalog_agent").load("/catalog_select/", {region_select:$("#region_resume").val()});
	});
	
//Вывод резюме
	$(".res").livequery('click', function () {
		var id = $(this).attr("id");
		$("#vacansy_tag").load("/resumes/", {id_vac:id});
		$("#vacancys_post").load("/resume_right/");
		$("#search_res2").addClass("inset");
		$("#search_vacansy1").removeClass("inset");
		$(".vacansy2").removeClass("inset");
		$("#catalog_agent").load("/catalog_select/", {region_select:0});
	});
	
// Вывод всех резюме
	$(".all_resume").livequery('click', function () {
		var id = $(this).attr("id");
		var pager_id = id.substr(1);
		$("#vacansy_tag").load("/resumes/", {pager_str:1, start_page:pager_id});
		$("#vacancys_post").load("/resume_right/");
		$("#search_res2").addClass("inset");
		$("#search_vacansy1").removeClass("inset");
		$(".vacansy2").removeClass("inset");
		$("#catalog_agent").load("/catalog_select/", {region_select:0});
	});

//по фильтру правого блока
	//отображение по категории		
	$(".find_resume_category").livequery('click', function () {
		var id = $(this).attr("id");
		var category_id = id.substr(1);
		$("#vacansy_tag").load("/resumes/", {pager_str:1,category:" : ", category_select:category_id}, 
				function () { getPageInfo("din_vac", "resumes", 0, category_id, 0, 0); });
	});
	
	//по городам	
	$(".find_resume_towns").livequery('click', function () {
		var id = $(this).attr("id");
		var town_id = id.substr(1);
		$("#vacansy_tag").load("/resumes/", {pager_str:1,town:" : ", town_select:town_id}, 
				function () { getPageInfo("din_vac", "resumes", 0, 0, 0,0, town_id); });
	});
	
//подгрузка списка городов, согласно заданному региону
	$("#region_resume").livequery('change', function () {
			
			var region_id = $("#region_resume").val();
			$("#town_resume1").remove();
			$("#towns_load").load("/resume_towns_select/", {region_select:region_id});
	});
	//====================================================================
	
	//====================================================================
	// Панель редактирования дизайна
	//====================================================================
	$('.msite_nmenu').each(function() {
		$(this).click(function() {	
			this_obj = $(this);

			$('#mess_for_user').text('');
			$('#error_for_user').text('');
					
			if($('.msite_nmenus').size()==0)
			{
				$(this).removeClass('msite_nmenu');
				$(this).addClass('msite_nmenus'); 
				$('#divm' + $(this).attr('divm')).animate({height: $(this).attr('divh') + 'px'}, 'slow');	
			}
			else
			{
				$('.msite_nmenus').each(function() {
					$(this).removeClass('msite_nmenus');
					$(this).addClass('msite_nmenu');
					
					this_obj.removeClass('msite_nmenu');
					this_obj.addClass('msite_nmenus'); 
					
					$('#divm' + $(this).attr('divm')).animate({height: '0px'}, 'slow', function() {
						$('#divm' + this_obj.attr('divm')).animate({height: this_obj.attr('divh') + 'px'}, 'slow');
					});
				});
			}
		});
	});
	
	if($('.msite_nmenu').size()!=0)
	{
		$('input').each(function() {
			$(this).blur(function() {
				getStyleForObj($(this), 'input');
			});
		});
		
		$('select').each(function() {
			$(this).change(function() {
				getStyleForObj($(this), 'select');
			});
		});
	}
	
	radioNone('mmenu_pointer_deact0', 'mmenu_image_pointer_deact', '', 'msite_mmenu', 'back');
	radioImage('mmenu_pointer_deact1', 'mmenu_image_pointer_deact', '', 'msite_mmenu', 'back', $('#mmenu_pointer_deact1').attr('path'));
	
	radioNone('mmenu_pointer_act0', 'mmenu_image_pointer_act', '', 'msite_mmenus', 'back');
	radioImage('mmenu_pointer_act1', 'mmenu_image_pointer_act', '', 'msite_mmenus', 'back', $('#mmenu_pointer_act1').attr('path'));
	
	radioNone('mmenu_line0', 'mmenu_image_line', 'image_mmenu_line', '', 'image');
	radioImage('mmenu_line1', 'mmenu_image_line', 'image_mmenu_line', '', 'image');
	
	radioNone('logo0', 'image_logo', 'image_logo', '', 'image');
	radioImage('logo1', 'image_logo', 'image_logo', '', 'image');
	
	radioNone('contact_bg0', 'image_contact_bg', '', 'msite_contact_bg', 'back');
	radioImage('contact_bg1', 'image_contact_bg', '', 'msite_contact_bg', 'back', $('#contact_bg1').attr('path'));
	//====================================================================
	
	//Подтверждение удаления объекта
	$('.adel').each(function() {
		$(this).click(function() {
			return confirm("Вы уверены, что хотите удалить данный объект? Все связанные с этим объектом данные будут также удалены!");
		});
	});
});

jQuery(document).ready(function(){

//left menu, contacts
var size_td = $(".msite_contact_under").height();
var size_menu = $(".msite_mmenu_bg").height();
var size_contacts = $(".msite_contact_name").height();
var size_contact_bg = $(".msite_contact_bg").height();

var size_cont = size_td -size_menu- (size_contacts + size_contact_bg) - 40;
//"top", "-"+length+"px"
$(".msite_cont").css("margin-top",""+size_cont+"px");

});

