$(document).ready(function(){ 
	$('.now_curr').click(function(){if ($('.curr-list').css('display')=='none'){$('.curr-list').show(400);}else{$('.curr-list').hide(400);}});
	$('body').click(function(event){
		if (event.target.className!='now_curr')
		$('.curr-list').hide(400);
	});
	$('.curr-list a').click(function(){
if ($('#admin').text()=='1'){
    var wlist='';
		$('.description_table .week_ckidka').each(function(){
			(wlist!='')?wlist+=',':'';
			wlist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		var dlist='';
		$('.description_table .day_ckidka').each(function(){
			(dlist!='')?dlist+=',':'';
			dlist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		var clist='';
		$('.description_table .cabbr').each(function(){
			(clist!='')?dlist+=',':'';
			clist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		$.post('/vhost/'+location.hostname+'/site_ajax/curr.php',{id:$(this).attr('id'),obj:$('.convertor').attr('id'),wlist:wlist,clist:clist,dlist:dlist},function(r){
			var cnt=0;
			$('.description_table .week_ckidka').each(function(){
                $(this).html(r.wlist[cnt]);
				cnt++;
			});
			var cnt=0;
			$('.description_table .day_ckidka').each(function(){
				$(this).html(r.dlist[cnt]);
				cnt++;
			});
			var cnt=0;
			$('.description_table .week_ckidka_b').each(function(){
				$(this).text(r.week_ckidka_b[cnt]);
				cnt++;
			});   
            var cnt=0;
			$('.description_table .day_ckidka_b').each(function(){
				$(this).text(r.day_ckidka_b[cnt]);
				cnt++;
			}); 
			var cnt=0;
			$('.description_table .cabbr').each(function(){
				$(this).html(r.clist[cnt]);
				cnt++;
			});
			$('.now_curr').html(r.cur);
		},'json');

}
else {
    var wlist='';
		$('.description_table .week').each(function(){
			(wlist!='')?wlist+=',':'';
			wlist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		var dlist='';
		$('.description_table .day').each(function(){
			(dlist!='')?dlist+=',':'';
			dlist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		var clist='';
		$('.description_table .cabbr').each(function(){
			(clist!='')?dlist+=',':'';
			clist+=$(this).html();
			$(this).html('<img src="/images/site/103.gif"/>');
		});
		$.post('/vhost/'+location.hostname+'/site_ajax/curr.php',{id:$(this).attr('id'),obj:$('.convertor').attr('id'),wlist:wlist,clist:clist,dlist:dlist},function(r){
			var cnt=0;
			$('.description_table .week').each(function(){
                $(this).html(r.wlist[cnt]);
				cnt++;
			});
			var cnt=0;
			$('.description_table .day').each(function(){
				$(this).html(r.dlist[cnt]);
				cnt++;
			});
			var cnt=0;
			$('.description_table .cabbr').each(function(){
				$(this).html(r.clist[cnt]);
				cnt++;
			});
			$('.now_curr').html(r.cur);
		},'json');
}
	});
	$('#new_question').dialog({autoOpen: false,minHeight: 360,minWidth: 480,position: 'center',resizable:false});
	$('.ask_a_qst').click(function(){ask();});
	$('#rt').change();
	$('.question-answer .link').click(function(){/* $('.myCaptcha').captcha('/vhost/'+location.hostname+'/site_ajax/captcha.process.php',true); */$('#post').val('');$('#new_question textarea').val('');$('.captchaRefresh').click();$('#new_question').dialog('open');return false;});
	$('.mail_to').click(function(){$('.question-answer .link').click();});
	//galery
	var link = $(".charecteristics_block li a");
	$(link).click(function(){
		var adress = $(this).attr("href");
		$(".big-house_screen").attr({src: adress});
		$(link).removeClass("active");
		$(this).addClass("active");
		return false;
	});
	
	$('#ct').change(function(){
		var c=$('#ct').val();
		$('body').css('cursor','wait');
		$('#ar').fadeOut();
		$.post('/vhost/'+location.hostname+'/site_ajax/search.php',{o:'sel_c',c:c},function(r){$('#ar').html(r);$('body').css('cursor','default');$('#ar').fadeIn();if($('#ar_val').get(0)){$('#ar').val($('#ar_val').val());}});
	});
	$('#ct').change();
	var el=$('#shortrent');
	$('#sale,#rent,#shortrent').each(function(){
		if ($(this).css('display')!='none')
		el=$(this);
	});
	var txt=$(el).find('option:selected').text();
	if(txt=='выбрать свой диапазон')
	{
		$('#pfr').removeClass('hidden');
		$('#pfr').prev('span').removeClass('hidden');
		$('#pto').prev('span').removeClass('hidden');
		$('#pto').removeClass('hidden');
	}
	else
	{
		$('#pfr').addClass('hidden');
		$('#pfr').prev('span').addClass('hidden');
		$('#pto').prev('span').addClass('hidden');
		$('#pto').addClass('hidden');
	}
	
	$('.find').click(function(){
		var el=$('#shortrent');
		var pers=false;
		$('#sale,#rent,#shortrent').each(function(){
			if ($(this).css('display')!='none')
			el=$(this);
		});
		var txt=$(el).find('option:selected').text();
		if(txt=='выбрать свой диапазон')
		{
			
			$('#pfr').removeClass('hidden');
			$('#pfr').prev('span').removeClass('hidden');
			$('#pto').prev('span').removeClass('hidden');
			$('#pto').removeClass('hidden');
			pers=true;
		}
		else
		{
			$('#pfr').addClass('hidden');
			$('#pfr').prev('span').addClass('hidden');
			$('#pto').prev('span').addClass('hidden');
			$('#pto').addClass('hidden');
		}
		
		var n=$('.input_number').val();
		(n=='')?n='any':n=n;
		var rt=$('#rt').val();
		(rt=='')?rt='any':rt=rt;
		var tp=$('#tp').val();
		(tp=='')?tp='any':tp=tp;
		var ct=$('#ct').val();
		(ct=='')?ct='any':ct=ct;
		var sq=$('#sq').val();
		(sq=='')?sq='any':sq=sq;
		var ar=$('#ar').val();
		(ar=='')?ar='any':ar=ar;
		var pfr=parseInt($('#pfr').val());
		(isNaN(pfr))?pfr='any':pfr=pfr;
		var pto=parseInt($('#pto').val());
		(isNaN(pto))?pto='any':pto=pto;
		var curr=$('#curr').val();
		(curr=='')?curr='6':curr=curr;
		document.location.href='http://'+location.hostname+'/search/?object_id='+n+'&rt='+rt+'&tp='+tp+'&ct='+ct+'&ar='+ar+'&pfr='+pfr+'&pto='+pto+'&curr='+curr+'&sq='+sq+'&pers='+pers;
	});

	$('.input_number').keypress(function (event) {if (event.keyCode == '13') {$('.find').click();}});
	$('.overflow input:button').click(function(){search_head();});
	//$('.overflow input:text').keypress(function (event) {if (event.keyCode == '13') {search_head();}});
	$('#search').submit(function(){search_head();});
	$('.more3').click(function(){sub_basket($(this));});
	
	$('.cityo').hover(function(){
		$('.city-list').removeClass('hidden');
	});
	$('.city-list').hover(function(){
		$('#cityo').addClass('selected');
	},function(){
		$('#cityo').removeClass('selected');
	});
	$('.typeo').hover(function(){
		$('.type-list').removeClass('hidden');
	});
	$('.type-list').hover(function(){
		$('#typeo').addClass('selected');
	},function(){
		$('#typeo').removeClass('selected');
	});
	$(document).mousemove(function(event){
			if (event.target.className!='cty'&&event.target.className!='cityo')
			$('.city-list').addClass('hidden');
			if (event.target.className!='tty'&&event.target.className!='typeo')
			$('.type-list').addClass('hidden');
	});
});

function sub_basket(el){
	if(confirm('Удалить данный объект из избранного ?'))
	$.post('/vhost/'+location.hostname+'/site_ajax/basket.php',{o:'del',id:$(el).attr('id')},function(){alert('Объект успешно удален');document.location.reload();});
}

function ask(){
	var m=$('#post').val();
	var q=$('#new_question textarea').val();
	/* if ($('.targetWrapper').hasClass('captchaSuccess'))
	{ */
		if (m=='')
		{
			alert('Введите Ваш электронный адрес.');
			$('#post').focus();
			return false;
		}
		if(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(m) == false) 
		{
			alert('Введите корректный электронный адрес.');
			$('#post').focus();
			return false;
		}
		if (q=='')
		{
			alert('Введите вопрос.');
			$('#new_question textarea').focus();
			return false;
		}
		$.post('/vhost/'+location.hostname+'/site_ajax/ask_a_question.php',{m:m,q:q},function(){alert('Вопрос успешно добавлен. Ответ будет выслан на указанный Вами электронный адрес.');$('.ui-dialog .ui-icon-closethick').click();});
	/* }
	else
	{
		alert('Вы не прошли проверку.');
		$('.captchaRefresh').click();
	} */
}

function search_head(){
	var n=$('.overflow input:text').val();	
	if (n==''||n.length<3){alert('Поле поиска должно содержать более 3х символов');$('.overflow input:text').focus();return false;}
	if (n=='Поиск по сайту'){alert('Задайте параметр поиска');$('.overflow input:text').focus();return false;}
	document.location.href='http://'+location.hostname+'/search/?var='+n;
}

$(document).ready(
	function()
	{
		$("i").each(function(){
			$(this).replaceWith('<a class="' + $(this).attr("class") +'"' +' href="' + $(this).attr("title") + '">' +$(this).text() + "</a>");
		});
	}
);
