$(document).ready(function(){
	
    //$('div.bar-big, div.bar-mini').pngFix();
	
    $('form.find span').toggle(
        function(){
            $(this).toggleText('↓', '↑');
            $('div.find-select').css('display', 'block');        	
        },
        function(){
            $(this).toggleText('↓', '↑');
            $('div.find-select').css('display', 'none');
        }
    ).hover(
        function(){
        	$(this).attr('class', 'hover');
        },
        function(){
            $(this).removeAttr('class');
        }
    );
    
    if ($.browser.msie && $.browser.version == '6.0'){
        $('ul.plan-link span').each(function(){
            $(this).width($(this).width() * 0.07 + 'em');
        });
    }
    
    $('div.fixed').each(function(){
        $(this).css('margin-top', -$(this).height() / 2);
    });

    $('img.fixed-close').click(function(){
        $('div.fixed').css('display', 'none');
    });
    
    $('div.plus-marker').click(function(){
        $(this)
        .toggleClass('plus-marker-reveal')
        .parent('div.plus')
        .find('div.plus-frame')
        .toggleClass('block');
    });
});

jQuery.fn.toggleText = function(a, b){
    return this.html(this.html().replace(new RegExp('('+ a +'|'+ b +')', 'g'), function(x){return(x == a) ? b : a;}));
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function fill_the_input(){

    alert('Заполните поле "'+this.title+'"')
    this.focus();

} // function fill_the_input


// счетчик форм
var forms_cnt = 0;
// ajax обертывалка
function set_form_submit( ajax_id ){ 
  
  var parent_selector = ajax_id ? '[id='+ajax_id+']' : '[id^=ajax_]';
  // обработка обычных ссылок 

  $(parent_selector+' a:not([onClick][href^=javascript][href^=http://])').not('[noajax]').each(
  function(){

      var current = this;

      $(current).unbind('click.ajax');
      $(current).bind('click.ajax',function(){

          // массив параметров
          var href_arr = new Array();

          // параметры в ссылке
          href = current.href.replace('http://','');          
          a_href = href.split('?');
          if ( a_href[1] )
              href_arr.push(a_href[1]);

          // внутренний идентификатор объекта
          href_arr.push('ajax_id='+$(current).parents(parent_selector).attr('id').substr(5));

          // параметры, измененные url_rewriter'ом
          href_arr.push('ajax_request_uri='+a_href[0].substr(href.search('/')));
          
          $.get('/ajax/ajax.php?'+href_arr.join('&'),'',function(resp){
              set_form_submit( $(current).parents(parent_selector).html(resp).attr('id') );
          });
          
          return false;
      });
      
  }
  );

  // обработка форм
  $((ajax_id?parent_selector:'')+' form:not([onSubmit]):not([action])').each(function(){

      var current_form = this;

      forms_cnt++;
      
      $(current_form).attr('id','ajaxform_'+forms_cnt);

      // если форма лежит в контейнере с зарезервированным id, то
      // дописать в нее скрытое поле с номером объекта 
      $(current_form).parents(parent_selector).each(
          function(){
              $('<input type="hidden" name="ajax_id">').val(this.id.substr(5)).appendTo(current_form);
          }
      );
      
      $(current_form).unbind('submit.ajax');
      // подключение обработчика отправки формы
      $(current_form).bind('submit.ajax',function(){

          if ( !$('#'+current_form.id+' :input[name^=_]:first').filter('[value=""]').each( fill_the_input ).size() ){
      
              var parameters = new Object();
              
              $('#'+current_form.id+' :input[name]').each(function(){
                  current = this;
                  if ( $(this).is('[name^=_]') ) 
                      $(this).attr('name',current.name.substr(1));
                  parameters[current.name] = current.value;
              });
              
              $.post('/ajax/ajax.php',parameters,function(response,ans_code){

                  var replace = $(current_form).attr('replace');
                  
                  switch( replace ){
                    case undefined:
                        if(response == 'captcha_error'){
                            alert ('captcha_error');
                        }
                        else {
                           set_form_submit( $(current_form).parents(parent_selector).html(response).attr('id') );
                        }
                        break;
                    case '':
                        break;
                    default:
                        if(response == 'captcha_error'){
                            alert ('captcha_error');
                        }
                        else {
                            $('#'+replace).html(response);
                        }
                        break;
                  } // switch

              });
      
          } // if
        
          return false;

      });

  });

} // function set_form_submit

$( function(){set_form_submit();} );


$(function(){   
  $('.cat_table_hide div').not('[title]').each(function(){
    $(this).attr('title',$(this).text())  
  })
});


  
function sizer () {
  var height_c = $('.page .content').height();
  var height_n = $('.page .navigation').height();
  var height_m = Math.max(height_n,height_c);
  $('.page .content .main').height(height_m); 
  $('.page .navigation .main').height(height_m);
}
$(function() {
    sizer();
    $('.wrapper img').load(sizer); 
})
