$(document).ready(function(){
	
	$(document).pngFix();
	
	$('#menu #head').click(function(){
		$(this).next().slideToggle(500);
	});
	
	$('a.advSearch').click(function(){
		$('#advSearch').slideToggle(1000);
	});
	
	$('a.advSearch').toggle(
		function(){
			$(this).html('Скрыть расширенный <br /> поиск');
		},
		function(){
			$(this).html('Показать расширенный <br /> поиск');
		}
	);
	
	$('#searchByKeyword').keydown(function(e){
		if (e.keyCode == 13) {
			$('form#options select').each(function(){
				$(this).val('');
			});
			$('form#options').submit();
		}
	});
	
	$('#searchByNumber').keydown(function(e){
		if (e.keyCode == 13) {
			$('form#options select').each(function(){
				$(this).val('');
			});
			$('form#options').submit();
		}
	});
	
	$('#objectInfo #img a').click(function(e){
		$('#objectInfo #main img').hide().attr('src', $(this).attr('href')).load(function(){
			$(this).fadeIn(1000);
		});
		e.preventDefault();
	});
	
	$('#ukaz').change(function(e){
		$('#addObjectTable #price').replaceWith('<input id="price" name="price" type="text" />');
		$('#addObjectTable #price').next().html('AZN');
	});
	
	$('#dogovor').change(function(e){
		$('#addObjectTable #price').replaceWith('<input id="price" name="price" type="hidden" value="-1" />');
		$('#addObjectTable #price').next().html('');
	});
	
	$('#menuItem a').click(function(e){
		var department_id = $(this).attr('href');
		clearForm();
		$('#department_id option[value='+ department_id +']').attr('selected', 'selected');
		$.ajax({
			url: 'ajax/session_destroy.php'
		});
		e.preventDefault();
		$('form#search').submit();
	});
	
	$('#menu li').click(function(e){
		var href = $(this).find('a').attr('href').split('&');
		var aim_id = href[0];
		var type_id = href[1];
		var department_id = href[2];
		clearForm();
		$('#aim_id option[value='+ aim_id +']').attr('selected', 'selected');
		$('#type_id option[value='+ type_id +']').attr('selected', 'selected');
		$('#department_id option[value='+ department_id +']').attr('selected', 'selected');
		$.ajax({
			url: 'ajax/session_destroy.php'
		});
		e.preventDefault();
		$('form#search').submit();
	});
	
	function clearForm() {
		$('form#search input:not(:submit)').each(function(){
			$(this).val('');
		});
		$('form#search select').each(function(){
			$(this).val('');
		});
	};
	
	$('#pager #page').click(function(){
		$('input#page').attr('value', $(this).find('a').attr('href'));
		$('form#options').submit();
	});
	
	$('#pager #page a').click(function(e){
		e.preventDefault();
	});
	
	$('#pager_down #page_down').click(function(){
		$('input#page').attr('value', $(this).find('a').attr('href'));
		$('form#options').submit();
	});
	
	$('#pager_down #page_down a').click(function(e){
		e.preventDefault();
	});
	
	$('#options').change(function(){
		$(this).submit();
	});
	
	if ($('#objectPhotos div#img').size() > 0) {
		$('#btnAddObjectPhoto').attr('value', 'Добавить еще');
	}
	
/*	$('#slider').panelGallery({
		boxFadeDuration: 3000
	});*/
	
	/*$.ajax({
		type: 'POST',
		url: 'ajax/objectsList.php',
		async: false,
		success: function(data){
			$('#center').html(data);
		}
	});*/
	
	/*$('#addObjectForm').submit(function(e){
		ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email);
	});*/
	
	/*$.ajax({
		type: 'POST',
		url: 'ajax/objectsList.php',
		async: false,
		success: function(data){
			$('#center').html(data);
		}
	});*/
	
	
});
