// JavaScript Document
if(!init){
	var init=new Object();
}




init['module-search-property']=function(){
	
	/////////////////////////////////////////////////////////////////////////
	// Наведение на кнопку поиска
	$('input.main-search').mouseover(
		function(type){
			if(!$(this).hasClass('hover')){
				$(this).addClass('hover');
			}
		}
	);
	$('input.main-search').mouseout(
		function(type){
			if($(this).hasClass('hover')){
				$(this).removeClass('hover');
			}
		}
	);
	/////////////////////////////////////////////////////////////////////////
	
	// расширенный поиск
	$('#module-search-property-advanced-1').click(function(){
		$('#module-search-property-advanced').click();
		return false;
	});
	$('#module-search-property-advanced').bind('click',
		function(){
			if(this.isshow){
				$('#module-search-property-advanced-search').animate({height:'hide',opacity:'hide'},300);
				$(this).text('расширенный поиск');
				$('#module-search-property-advanced-1').text('расширенный поиск');
				this.isshow=false;
			}else{
				$('#module-search-property-advanced-search').animate({height:'show',opacity:'show'},300);
				$(this).text('скрыть расширенный поиск');
				$('#module-search-property-advanced-1').text('скрыть расширенный поиск');
				this.isshow=true;
				
			}
			return false;
		}
	);
	
	$('#collapse-advanced-search').click(
		function(){
			$('#module-search-property-advanced').click();
			return false;
		}
	);
	
/*
	$('#module-search-result-property .module-search-property-result-detail').bind('click',
		function(){
			if(this.isshow){
				$('+div',this).animate({height:'hide'},300);
				this.isshow=false;
			}else{
				$('+div',this).animate({height:'show'},300);
				this.isshow=true;
			}
			return false;
		}
	);
	$('#module-search-property-city-id').bind('change',function(){
		if(this.value!='null'){
			$.get('index.php',
				{
					page:'module.ajax.search.ctar.id',
					'module_search_property[city_id]':this.value},
				function(data){
					$('#module-search-property-ctar-id').html(data);
					$('#module-search-property-ctar-id')[0].disabled=false;
					if($('#module-search-property-ctar-id')[0].value!='null'){
						$.get('index.php',
							{page:'module.ajax.search.strt.id'},
							function(data){
								$('#module-search-property-strt-id').html(data);
								$('#module-search-property-strt-id')[0].disabled=false;
							});
					}else{
						$('#module-search-property-strt-id')[0].disabled=true;
						$('#module-search-property-strt-id').html('<option>не выбран район</option>');
					}
				});
			// Добавляем станции метрополитена
			$.get('index.php',
				{
					page:'module.ajax.search.metro.stations.id',
					'module_search_property[city_id]':this.value
				},
				function(data){
					$('#module-search-property-metros').html(data);
					$('#module-search-property-metros')[0].disabled=false;
				}
			);
		}else{
			$('#module-search-property-ctar-id')[0].disabled=true;
			$('#module-search-property-ctar-id').html('<option>не выбран город</option>');
			$('#module-search-property-strt-id')[0].disabled=true;
			$('#module-search-property-strt-id').html('<option>не выбран район</option>');
			$('#module-search-property-metros')[0].disabled=true;
			$('#module-search-property-metros').html('<option>не выбран город</option>');
		}
	});
	
	$('#module-search-property-ctar-id').bind('change',function(){
		if(this.value!='null'){
			$.get('index.php',
				{
					page:'module.ajax.search.strt.id',
					'module_search_property[ctar_id]':this.value},
				function(data){
					$('#module-search-property-strt-id').html(data);
					$('#module-search-property-strt-id')[0].disabled=false;
				});
		}else{
			$('#module-search-property-strt-id')[0].disabled=true;
			$('#module-search-property-strt-id').html('<option>не выбран район</option>');
		}
	});
	
	if($('#module-search-property-city-id')[0].value!='null'){
		$.get('index.php',
			{
				page:'module.ajax.search.ctar.id'},
			function(data){
				$('#module-search-property-ctar-id').html(data);
				$('#module-search-property-ctar-id')[0].disabled=false;
				if($('#module-search-property-ctar-id')[0].value!='null'){
					$.get('index.php',
						{page:'module.ajax.search.strt.id'},
						function(data){
							$('#module-search-property-strt-id').html(data);
							$('#module-search-property-strt-id')[0].disabled=false;
						});
				}
			});
		// Добавляем станции метрополитена
		$.get('index.php',
			{
				page:'module.ajax.search.metro.stations.id',
				'module_search_property[city_id]':$('#module-search-property-city-id')[0].value
			},
			function(data){
				$('#module-search-property-metros').html(data);
				$('#module-search-property-metros')[0].disabled=false;
			}
		);
	}
	
	$('#module-search-property').each(
		function(){
			// Для картинок
			hs.graphicsDir='/highslide/graphics/';
			hs.align='center';
			hs.transitions=['expand','crossfade'];
			hs.outlineType='rounded-white';
			hs.fadeInOut=true;
			//hs.dimmingOpacity=0.75;
		}
	);
	
*/	
	// Раскрытие блоков поиска
	$('.search-block').click(
		function(){
			var el=$('#'+this.getAttribute('for'));
			var action=this.getAttribute('action');
			if(this.show){
				el.animate({height:'hide',opacity:'hide'},300);
				if(action){$('#'+action).animate({opacity:'hide'},300);}
				this.show=false;
			}else{
				el.animate({height:'show',opacity:'show'},300);
				if(action){$('#'+action).animate({opacity:'show'},300);}
				this.show=true;
			}
			$(this).blur();
			return false;
		}
	);
	
	// Всплывающие подсказки
	$('.system-help-link').click(
		function(){
			var help_block=$('#'+this.getAttribute('for'));
			if(this.show){
				help_block.animate({height:'hide',opacity:'hide'},300);
				this.show=false;
			}else{
				help_block.animate({height:'show',opacity:'show'},300);
				this.show=true;
			}
			return false;
		}
	);
	
	
	/**
	 * Ссылка "подробно" в результатах поиска.
	 */
	$('.result-showall').click(function(){
		var els=new Array();
		var pos=0;
		$('.searchresult a.search-block').each(function(){
			els[pos++]='#'+this.getAttribute('for');
			this.show=true;
		});
		$(els.join(',')).animate({height:'show',opacity:'show'},300);
		return false;
	});
	
	/**
	 * Ссылка "кратко" в результатах поиска.
	 */
	$('.result-hideall').click(function(){
		var els=new Array();
		var pos=0;
		$('.searchresult a.search-block').each(function(){
			els[pos++]='#'+this.getAttribute('for');
			this.show=false;
		});
		$(els.join(',')).animate({height:'hide',opacity:'hide'},300);
		return false;
	});
	
	/**
	 * Ссылка "добавить предложение в избранное" в результатах поиска
	 */
	$('.lot_favorite').click(
		function(){
			var lot_id=this.getAttribute('lotid');
			var dop=this.getAttribute('dop');
			var self=this;
			var get=new Object();
			get['favorite']=lot_id;
			if(self.request==1||self.request==0){
				if(self.request==1)get['del']='';
			}else if(dop=='1'){
				get['del']='';
			}
			$.ajax({url:'favorite.php',dataType:'json',data:get,success:function(json){
				switch(json.status){
					case 1:
						// alert('Предложение добавлено в избранное!');
						$(self).text('удалить предложение из избранного');
						$('#lotfirst'+lot_id+' a.choose').removeClass('choose').addClass('choose1');
						break;
					case 0:
						// alert('Предложение удалено из избранного!');
						$(self).text('добавить предложение в избранное');
						$('#lotfirst'+lot_id+' a.choose1').removeClass('choose1').addClass('choose');
						break;
				}
				self.request=json.status;
				$('#favorite_counter span').text(json.count);
				switch(json.count){
					case 0:
							$('#favorite_counter').addClass('galka3').removeClass('galka2');
						break;
					default:
							$('#favorite_counter').addClass('galka2').removeClass('galka3');
						break;
				}
			}});
			
			return false;
		}
	);
	
	/**
	 * Галочка "добавить предложение в избранное" в результатах поиска
	 */
	$('.lotitem').each(
		function(){
			var lot_id=this.getAttribute('lotid');
			$('a.choose1,a.choose',this).click(
				function(){
					$('#lot-fav-link-'+lot_id).click();
					return false;
				}
			);
		}
	);
	
	/**
	 * Выпадающий список "записей на странице" в результатах поиска
	 */
	$('.itl').change(
		function(){
			window.location=this.value;
		}
	);
	
	var cityareaschk=new Object();
	var cityin='';
	
						
	/////////////////////////////////////////////////
	// Сохранённые чекбоксы улиц
	var str_savechk=Array();
	$('#seach-block-streets input[type="hidden"]').each(
		function(){str_savechk=this.value.split(',');}
	);
	/////////////////////////////////////////////////
						
	// Изменение города
	$('#gor').change(
		function(){
			var self=this;
			if(this.value!='null'){
				// Меняем характеристики города
				//////////////////////////////////////////////////////////////////////
				// Обнуляем улицы
				$('#seach-block-streets div.inblock').
				animate({height:'hide',opacity:'hide'},300,
					function(){
						$(this).html('Не выбран район(округ) города!');
						$(this).animate({height:'show',opacity:'show'},300);
					}
				);
				cityareaschk=new Object();
				cityin='';
				// Районы города
				$('#seach-block-carea div.inblock').animate({height:'hide',opacity:'hide'},300,
					function(){
						var block=this;
						
						/////////////////////////////////////////////////
						// Сохранённые чекбоксы
						var savechk=Array();
						$('input[type="hidden"]',this).each(
							function(){
								savechk=this.value.split(',');
							}
						);
						/////////////////////////////////////////////////
						
						$(this).html('<div class="preload">Подождите, идёт загрузка.</div>').
						animate({height:'show',opacity:'show'},300,
							function(){
								$.get('index.php',
								// Загружаем районы городов
								{page:'module.ajax.search.city.areas','id':self.value},
									function(data){
										$(block).animate({height:'hide',opacity:'hide'},300,
											function(){
												$(this).html(data).
												animate({height:'show',opacity:'show'},300);
												// Описываем событи загруженых полей
												$('input',this).each(
													function(){
														cityareaschk[this.getAttribute('name')]=this.checked;
														$(this).bind('change focus click',
															function(){
																$('input',block).each(
																	function(){
																		cityareaschk[this.getAttribute('name')]=this.checked;
																	}
																);
																var tempin=new Array();
																var arrpos=0;
																for(var name in cityareaschk){
																	if(cityareaschk[name]){
																		tempin[arrpos++]=
																		name.replace('a','');
																	}
																}
																// Если меняется содержимое чекбоксов
																if(cityin!=tempin.join(',')){
																	cityin=tempin.join(',');
																	if(cityin==''){
																		$('#seach-block-streets div.inblock').
																		animate({height:'hide',opacity:'hide'},300,
																			function(){
																				$(this).html('Не выбран район(округ) города!');
																				$(this).animate({height:'show',opacity:'show'},300);
																			}
																		);
																	}else{
																		$('#seach-block-streets div.inblock').
																		html('<div class="preload">Подождите, идёт загрузка.</div>');
																		// Подгружаем улицы
																		var cityinold=cityin;
																		$.get('index.php',
																			{page:'module.ajax.search.streets','id':cityin},
																			function(data){
																				if(cityinold==cityin)
																				$('#seach-block-streets div.inblock').
																				animate({height:'hide',opacity:'hide'},300,
																					function(){
																						if(cityinold==cityin)
																						$(this).html(data);
																						
																						/////////////////////////////////////////////
																						// Расставляем сохранённые чекбоксы
																						for(var i in str_savechk){
																							$('input#s'+str_savechk[i],this).click();
																						}
																						//alert(str_savechk);
																						str_savechk=Array();
																						
																						/////////////////////////////////////////////
																						
																						$(this).animate({height:'show',opacity:'show'},300);
																					}
																				);
																			}
																		);
																	}
																}
															}
														);
													}
												);
												
												/////////////////////////////////////////////
												// Расставляем сохранённые чекбоксы
												for(var i in savechk){
													$('input#a'+savechk[i],this).click();
												}
												/////////////////////////////////////////////
												
											}
										);
									}
								);
							}
						);
					}
				);
				//////////////////////////////////////////////////////////////////////
				// Станции метрополитена
				$('#seach-block-metro div.inblock').animate({height:'hide',opacity:'hide'},300,
					function(){
						var block=this;
						
						/////////////////////////////////////////////////
						// Сохранённые чекбоксы
						var savechk=Array();
						$('input[type="hidden"]',this).each(
							function(){
								savechk=this.value.split(',');
							}
						);
						/////////////////////////////////////////////////
						
						$(this).html('<div class="preload">Подождите, идёт загрузка.</div>').
						animate({height:'show',opacity:'show'},300,
							function(){
								$.get('index.php',
								// Загружаем станции метро
								{page:'module.ajax.search.metro','id':self.value},
									function(data){
										$(block).animate({height:'hide',opacity:'hide'},300,
											function(){
												$(this).html(data);
												
												/////////////////////////////////////////////
												// Расставляем сохранённые чекбоксы
												for(var i in savechk){
													$('input#m'+savechk[i],this).click();
												}
												/////////////////////////////////////////////
												
												$(this).animate({height:'show',opacity:'show'},300);
											}
										);
									}
								);
							}
						);
					}
				);
			}else{
				// Забиваем всё
				$('#seach-block-carea div.inblock,#seach-block-metro div.inblock').
				animate({height:'hide',opacity:'hide'},300,
					function(){
						$(this).html('Не выбран город!');
						$(this).animate({height:'show',opacity:'show'},300);
					}
				);
				$('#seach-block-streets div.inblock').
				animate({height:'hide',opacity:'hide'},300,
					function(){
						$(this).html('Не выбран район(округ) города!');
						$(this).animate({height:'show',opacity:'show'},300);
					}
				);
			}
		}
	);
	//////////////////////////////////////////////////////////////////////////////////
	// Если выбран город
	if($('#gor').size()>0)
	if($('#gor')[0].value!='null'){
		$('#gor').change();
	}
	//////////////////////////////////////////////////////////////////////////////////
	
	
	
	//////////////////////////////////////////////////////////////////////////////////
	// Раскрывание выделенных блоков
	$('fieldset').each(function(){
		var self=this;
		$(this).children('legend').find('a.search-block').each(
			function(){
				if($('input:checked',self).length>0){
					$(this).click();
				}else{
					if($('input[type="hidden"]',self).length>0)
					if($('input[type="hidden"]',self)[0].value!=''){
						$(this).click();
					}
				}
			}
		);
	});
	// Раскрытие расширенного поиска
	if($('#module-search-property-advanced-search input:checked').length>0)
	$('a#module-search-property-advanced').click();
	// Раскрытие Район(округ)/Улица/Станции метрополитена
	
	//////////////////////////////////////////////////////////////////////////////////
	
	
	
	// отметить всё
	$('.chkall').click(function(){
		var size=$('#'+this.getAttribute('for')+' input').size();
		var pos=0;
		$('#'+this.getAttribute('for')+' input').each(
			function(){
				this.checked=true;pos++;
				if(pos==size){$(this).focus();}
			}
		);
		return false;
	});
	// снять отметку со всех
	$('.nochkall').click(function(){
		var size=$('#'+this.getAttribute('for')+' input').size();
		var pos=0;
		$('#'+this.getAttribute('for')+' input').each(
			function(){
				this.checked=false;pos++;
				if(pos==size){$(this).focus();}
			}
		);
		return false;
	});
	
	// Делаем резет всех полей
	$('#main-search').each(function(){this.reset();});
	
	$('table.searchresult').each(
		function(){
			// Для картинок
			hs.graphicsDir='/highslide/graphics/';
			hs.align='center';
			hs.outlineType='rounded-white';
			hs.fadeInOut=true;
			hs.dimmingOpacity = 0.75;
			hs.useControls=true;
			$('.photogroup a').click(
				function(){
					return hs.expand(
						this,{
							slideshowGroup:this.parentNode.getAttribute('group'), 
							captionText:this.parentNode.getAttribute('caption'),
							interval: 3000,
							repeat: false,
							useControls: true,
							overlayOptions: {
								opacity: 0.75,
								position: 'bottom center',
								hideOnMouseOut: true
							}
						}
					)
				}
			);
		}
	);
	
	
	
	// Автозаполнение удалённости от метро
	$('.mtrdist').each(
		function(){
			var self=this;
			$('input',this).click(
				function(){
					var pos=this.getAttribute('pos');
					if(this.checked)
					$('input',self).each(
						function(){
							if(pos>this.getAttribute('pos'))
							this.checked=true;
						}
					);
				}
			)
		}
	);
	
	
	// провайдеры услуг
	$('.providers').each(
		function(){
			var href=this.getAttribute('href');
			if(href){
				var title=this.getAttribute('title');
				title=title?title:'';
				$(this).replaceWith($(this).html()+
				'<a target="_blank" title="Домашняя страница оператора. '+title+
				'" class="provider" href="'+href+'">[?]</a> ')
			}
		}
	);
	
}
