function changeTitle()
{
	var $title = $("#title");
	var title = $.trim($title.html());
	$title.remove();
	document.title = title;
	
}

function processPages()
{
	$("#pages a").each(function()
	{
		$(this).click(function(){
			ajaxOpenContent(this);
			return false;
		});
	});
}

function ajaxOpenContent(el)
{
	$("#content").empty();
	$("#content").append("Загрузка...");
	$.post($(el).attr('href'), {}, function (rez){
		$("#content").empty();
		$("#content").append(rez);
		changeTitle();
		$(".post_image").fancybox({hideOnContentClick:true});
		processPages();
		
	});
	
	return false;	
}

function centerMenuClick()
{
	$('#center_menu a').click(
					function ()
					{
						$('#center_menu a').removeClass('selected');
						$(this).addClass('selected');
						return	ajaxOpenContent(this);				
					}
				);
}

function ajaxOpenMenu(el)
{
	$('#main_menu a.selchild').removeClass('selchild');
	$(el).addClass('selchild');
	
	$("#center_menu").hide(500);
	$("#center_menu").empty();
	
	/* $("#center_menu").show();
	$("#center_menu").append("Загрузка..."); */
	$("#center_menu").load(
		$(el).attr('href'),{},
			function()
			{
				changeTitle();
				centerMenuClick();
				$("#center_menu").show(500);
			}
		);
	return false;	
}




$(document).ready(
function()
{
	
	$("#search input.text").focus(
	function()
	{
		var el = $(this);
		if( el.is('.untouched')) 
		{
			el.removeClass('untouched');
			el.val('');			
		}
	});
	$("#search input.text").blur(
	function()
	{
		var el = $(this);
		if( el.val()=='') 
		{
			el.addClass('untouched');
			el.val('Поиск');			
		}
	});
	
	
	$('#rem_highlight').click(function(){  $('.highlight').removeClass('highlight'); $(this).remove(); return false; });
	
	
	$(".post_image").fancybox({hideOnContentClick:true});	
	
	$("ul.search_rez a").each(function(id)
	{
		var url = this.href;
		$(this).fancybox({
			'frameWidth':455,
			'itemLoadCallback': function(opts)
			{
				opts.itemArray.push({url:url+'/shortview/true'});
			}
		});
	});
	

	
	$('a.image_preview').each(function(i){
			  $(this).hover(
			  	function()
			  	{
			  		$(this).prepend('<img class="inline" src="'+this.href+'" alt="">');
			  	},
			  	function()
			  	{
			  		$('img.inline').remove();
			  	}			  
			  );
		});
	$('#main_menu').accordion(
	{
		 header: 'a.menu1', 
		 autoheight: false,
		 
		 active: false, 
		 alwaysOpen: false
		
	});
	

	$('#main_menu a.menu3, #main_menu a.menu').click(
					function ()
					{
						$('#main_menu a.selchild').removeClass('selchild');
						$(this).addClass('selchild');
						$('#center_menu').hide();
						ajaxOpenContent(this);
						return false;				
					}
				);



	$("#main_menu a.menu2").click(
	  	function()
		{
			return ajaxOpenMenu(this);
		}
	  );
	
	 centerMenuClick();

	 
	 processPages();


	/*  Редактирование секций */
	
	$("form.main fieldset *:not(legend)").hide();
	$("form.main fieldset legend").click(
		function(){
			$("*:not(legend)", this.parentNode).toggle();
		}
	);
	

}
);