$(document).ready(function()  {  

  searchFilter = {};
  /* Текстбоксы поиска по цене */              
  $('#price_start').blur(function() {
    /*var max = $('#slider-range').slider('values', '1');
    var slide_to = Math.min(max, parseInt($(this).val()));
    slide_to = Math.max(slide_to, $('#slider-range').slider('option', 'min'));
    $(this).val(slide_to);
    $('#slider-range').slider('values', '0', slide_to);*/
  });
  
  $('#price_end').blur(function() {
    /*var min = $('#slider-range').slider('values', '0');
    var slide_to = Math.max(min, parseInt($(this).val()));
    slide_to = Math.min(slide_to, $('#slider-range').slider('option', 'max'));
    $(this).val(slide_to);
    $('#slider-range').slider('values', '1', slide_to);*/
  });
  
  searchFilter.is_ajax = true;
  
  searchFilter.sortPhone = function() {
    if (!searchFilter.is_ajax) {     
      document.location.href = $(this).attr('href');
    } else {      
      //$('#phone-list-conteiner').fadeOut(1000);           
      $.ajax({
        url: $(this).attr('href'),
        success: function(data) {
          //$('#phone-list-conteiner').fadeIn(1000);     
          $('#phone-list-conteiner').html(data);          
          searchFilter.init_phone(); 
        }
      });
    }        
    return false;
  }
  
  searchFilter.sortBriefcase = function() {
    if (!searchFilter.is_ajax)  {
      document.location.href = $(this).attr('href');
    } else  {
      $.ajax({
        url: $(this).attr('href'),
        success: function(data) {
          $('#briefcase-list-container').html(data);
          searchFilter.init_briefcase();
        }
      });
    }
    return false;
  }

  searchFilter.init_phone = function() {
    $('a#sort1, a#sort2').click(searchFilter.sortPhone);
  }
  
  searchFilter.init_briefcase = function()  {
    $('a#sort_briefcase1, a#sort_briefcase2').click(searchFilter.sortBriefcase);
  }
  
  $('#group').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57) || (
        key == 8 || 
        key == 9 ||
        key == 46
    );     
    
    return its_num;  
  });
    
  $('#reset_mask').click(function() {
    for(var i=1; i<=7; i++) {
      $('#mask_'+i).val('');
    }
    $(this).hide();
  });
  
  $('#mask_1').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_2').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  $('#mask_2').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_3').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#mask_3').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_4').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#mask_4').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_5').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#mask_5').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_6').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#mask_6').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      $('#mask_7').focus();
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#mask_7').keypress(function(e)  {
    var key = e.charCode || e.keyCode || 0;
    var its_num = (key >= 48 && key <= 57);
    if (its_num) {
      if (($.browser.msie)||($.browser.opera)) { $(this).val(String.fromCharCode(e.keyCode)); }
      else { $(this).val(String.fromCharCode(e.charCode)); }
      $('#reset_mask').show();
      return false;
    }
    its_num = its_num || 
    (
        key == 8 || 
        key == 9 ||
        key == 46);       
    
    return its_num;  
  });
  
  $('#search_mask_form').submit(function()  {
    if ($('#reset_mask').is(':visible')) {
      $('#mask_error').hide();
      return true;
    }
    else  {
      $('#mask_error').show();
      return false;
    }
  });
  
  $('#search_group_form').submit(function() {
    if ($('#group').val() != '')  {
      $('#group_error').hide();
      return true;
    }
    else  {
      $('#group_error').show();
      return false;
    }
  });
});
