$(document).ready(function(){

   if ($("#content_bl").height() > $("#right_bl").height()) {$("#right_bl").height($("#content_bl").height());}
   else {$("#content_bl").height($("#right_bl").height());$("#content_bl .cor_rb").height($("#right_bl").height());$("#content_bl .cor_lb").height($("#right_bl").height());}
   

//Сортировка по городу
$("a.csort").livequery('click', function(event){

   $(window).scrollTop('0px');

   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'act=city&city_id='+$(this).attr('city'),
      beforeSend: function(){
         $("#content_in").html("");
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         contentUpdate();
      }
   }); return false;

});


//Сортировка по дате/цене
$("#sort a").livequery('click', function(event){

   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'act=sort&sort='+$(this).attr('sort'),
      beforeSend: function(){
         $("#content_in").html("");
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         contentUpdate();
      }
   }); return false;

});


//Вывод всех объявлений
$("a#show_all").click(function(){

   $(window).scrollTop('0px');

   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'act=all',
      beforeSend: function(){
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         contentUpdate();
      }
   }); return false;

});


//Поиск объявлений
$("a#search_button").click(function(){

   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'me='+$('#me').val()+'&sfor='+$('#sfor').val()+'&home='+$('#home').val()+'&price_from='+$('#price_from').val()+'&price_to='+$('#price_to').val()+'&city='+$('#city').val()+'&district='+$('#district').val(),
      beforeSend: function(){
         $("#content_in").html("");
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         $("#content_in").html(output);
      }
   });// return false;

});


//Смена языка
$("#langs a").click(function(){

   var sURL = unescape(window.location.pathname),
       LANG = $(this).attr('id');

   $.ajax({
      url: '/index.php?ajax=lang',
      data: 'lang='+LANG,
      beforeSend: function(){
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         window.location.href = sURL + LANG + '/';
      }
   }); return false;

});


//Переключение картинок
$("#photos_full a").click(function(){

	var largePath = $(this).attr("href");
	$("#large_img").attr("src", largePath); return false;

});

//Загрузка картинок
$('#upload_1').upload({name: 'file',	method: 'post',	enctype: 'multipart/form-data',	action: '/index.php?ajax=upload',	params: {num: '1'}, onSubmit: function() {$('#upload_1').text(LANGS_LOADING);},	onComplete: function(output) {$("#newimg_1").attr("src", '/uploads/tmp/'+output+'_1_sm.jpg');$("#newimg_1").fadeIn();}});
$('#upload_2').upload({name: 'file',	method: 'post',	enctype: 'multipart/form-data',	action: '/index.php?ajax=upload',	params: {num: '2'}, onSubmit: function() {$('#upload_2').text(LANGS_LOADING);},	onComplete: function(output) {$("#newimg_2").attr("src", '/uploads/tmp/'+output+'_2_sm.jpg');$("#newimg_2").fadeIn();}});
$('#upload_3').upload({name: 'file',	method: 'post',	enctype: 'multipart/form-data',	action: '/index.php?ajax=upload',	params: {num: '3'}, onSubmit: function() {$('#upload_3').text(LANGS_LOADING);},	onComplete: function(output) {$("#newimg_3").attr("src", '/uploads/tmp/'+output+'_3_sm.jpg');$("#newimg_3").fadeIn();}});
$('#upload_4').upload({name: 'file',	method: 'post',	enctype: 'multipart/form-data',	action: '/index.php?ajax=upload',	params: {num: '4'}, onSubmit: function() {$('#upload_4').text(LANGS_LOADING);},	onComplete: function(output) {$("#newimg_4").attr("src", '/uploads/tmp/'+output+'_4_sm.jpg');$("#newimg_4").fadeIn();}});
$('#upload_5').upload({name: 'file',	method: 'post',	enctype: 'multipart/form-data',	action: '/index.php?ajax=upload',	params: {num: '5'}, onSubmit: function() {$('#upload_5').text(LANGS_LOADING);},	onComplete: function(output) {$("#newimg_5").attr("src", '/uploads/tmp/'+output+'_5_sm.jpg');$("#newimg_5").fadeIn();}});


//Открытие списка районов
$("#district_add").click(function(){
   if ($(this).attr('href')) $("#districts_list").slideDown();
});
   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'act=show_districts&lang='+$('#site_lang').val(),
      beforeSend: function(){
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         $("#districts_list").html(output);
//   $("#districts_list").slideDown();
      }
   });

$("#add_city").change(function(){
   if ($(this).val() == 57) {$('#district_add').attr('href','javascript://');$('#district_add').removeClass('district_dis');}
   else {$('#district_add').removeAttr('href');$('#district_add').addClass('district_dis');}
});

$("#city").change(function(){
   if ($(this).val() == 57) $('#disctrict_f').css('visibility','visible');
   else $('#disctrict_f').css('visibility','hidden');
});


//Закрыть список районов
$("#district_list_close").livequery('click', function(event){
   $("#districts_list").slideUp(); return false;
});


//Выбор района
$(".district_select").livequery('click', function(event){

   $('.district_select').attr('disabled', '');
   ID = $(this).val(),
   SEP = '',
   DISTRICT = $(this).parent().children('span').text(),
   LENGTH = $(".district_select:checked").length;
   if ($(this).attr('checked') == true) {
      //if (LENGTH != 5) SEP = ', ';
      if (LENGTH <= 5) {
         $("#districts_sel").append('<span id="new_district_'+ID+'">'+DISTRICT+SEP+', </span>');
         $("#selected_districts").val($("#selected_districts").val()+','+ID)
      }
   }
   else {$("#districts_sel span#new_district_"+ID).remove();}
   if (LENGTH >= 5) $('.district_select').not('.district_select:checked').attr('disabled', 'disabled');

});



//Отправка формы
$("#submit_form").click(function(){
   $('#add_price').parent().parent().css('background', '');
   $('#add_descr').parent().parent().css('background', '');
   $('#add_contacts').parent().parent().css('background', '');
   $('#add_name').parent().parent().css('background', '');
   $('#add_city_bg').css('background', '');
   $('#add_email').parent().parent().css('background', '');
   $('#accept_terms').parent().css('background', '');
   ERROR = 0;

   if ($('#add_price').val() == '' || $('#add_price').val() == 0) {$('#add_price').parent().parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_descr').val().length < 15 || $('#add_descr').val().length > 4000) {$('#add_descr').parent().parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_contacts').val() == '' || $('#add_contacts').val() == 0) {$('#add_contacts').parent().parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_name').val() == '' || $('#add_name').val() == 0) {$('#add_name').parent().parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_city').val() == '' || $('#add_city').val() == 0) {$('#add_city_bg').css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_city').val() == 57 & $('#selected_districts').val().length < 2) {$('#selected_districts').parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#add_email').val() == '' || $('#add_email').val() == 0) {$('#add_email').parent().parent().css('background', '#c1f0b1'); ERROR = 1;}
   if ($('#accept_terms').attr('checked') == false) {$('#accept_terms').parent().css('background', '#c1f0b1'); ERROR = 1;}
   if (ERROR == 0) $("#signup_form").submit();
   return false;
});




//MAIN!
});


//Переключение страниц
function changePage(FROM) {

   $(window).scrollTop('0px');

   $.ajax({
      url: '/index.php?ajax=sort',
      data: 'act=set_from&from='+FROM,
      beforeSend: function(){
         $("#content_in").html("");
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         contentUpdate();
      }
   });

}


//Обновление содержимого
function contentUpdate() {

   $.ajax({
      url: '/index.php?ajax=sort',
      data: '',
      beforeSend: function(){
         $("#content_in").html("");
         $("#loading").show();
      },
      complete: function(){
         $("#loading").hide();
      },
      success: function (output) {
         $("#content_in").html(output);
      }
   });

}
