$(document).ready(function(){
  var curr_coef = 1;
    
  if(!$('#curr_id1').val()){
    $('#curr_id1').val( $('#weget_curr_list li').eq(0).attr('currid') );
    $('#weget_label').html( $('#weget_curr_list li').eq(0).html() );
  }
  else
    $('#weget_label').html( $('#weget_curr_list li[currid='+$('#curr_id1').val()+']').html() );
  
  if(!$('#curr_id2').val()){
    $('#youget_label').html( $('#youget_curr_list li').eq(1).html() );
    $('#curr_id2').val( $('#youget_curr_list li').eq(1).attr('currid') );
  }
  else
    $('#youget_label').html( $('#youget_curr_list li[currid='+$('#curr_id2').val()+']').html() );
  
  set_curr_coef();
  calculate();
  
  $('#calc span').mouseover(function(){
    var thisid = $(this).attr('id').replace(/_label/,'');
    var thislayer = $('#'+thisid+'_curr_list');
    
    // reset opened
    $('#calc ul').hide();
    $('#calc ul li').unbind();
    
    // open selected
    var thisoffset = $(this).position();
    //alert(thisoffset.left+' - '+thisoffset.top);
    thislayer.css({'top':thisoffset.top,'left':thisoffset.left});
    thislayer.fadeIn(100);
    //alert(thisoffset.left+' - '+thisoffset.top);
    
    // close if leave
    thislayer.mouseleave(function(){
      thislayer.fadeOut(200);
      $('#'+thisid+'_curr_list li').unbind();
    });
    
    // check if click
    $('#'+thisid+'_curr_list li').click(function(){
      $('#'+thisid+'_label').html($(this).html());
      $('#'+thisid+'_label').prev().val( $(this).attr('currid') );
      thislayer.fadeOut(200, function(){set_curr_coef();calculate();});
    });
    
    $('#'+thisid+'_curr_list li').mouseenter(function(){
      //$(this).css('background','#dde');
      $(this).css('background-color','#dde');
    });
    $('#'+thisid+'_curr_list li').mouseleave(function(){
      //$(this).css('background','url(/css/images/bg_li.gif) no-repeat left top');
      $(this).css('background-color','#fff');
    });
  });
  
  $('#weget').keyup(function(){ calculate(); });
  $('#youget').keyup(function(){ calculate(true); });
  
  
  
  function set_curr_coef(){
    var curr_id1 = $('#curr_id1').val();
    var curr_id2 = $('#curr_id2').val();
    //alert(curr_id1+' - '+curr_id2);
    if(!curr_coefs[curr_id1])
      curr_coef = 0;
    else if(!curr_coefs[curr_id1][curr_id2])
      curr_coef = 0;
    else
      curr_coef = curr_coefs[curr_id1][curr_id2];
    //alert(curr_coef);
  }
  
  function calculate(back){
    var weget_field = $('#weget');
    var youget_field = $('#youget');
    
    // clean fields
    var replace_exp = new RegExp(/([^0-9\.]+)|([\.][0-9]*[\.])/);
    
    weget_field.val(weget_field.val().replace(/,/,'.'));
    weget_field.val(weget_field.val().replace(replace_exp,''));
    youget_field.val(youget_field.val().replace(/,/,'.'));
    youget_field.val(youget_field.val().replace(replace_exp,''));
    
    //
    if(back) {
      if(youget_field.val()>0 && curr_coef>0)
        weget_field.val( Math.round(youget_field.val()/curr_coef*100)/100 );
      else
        weget_field.val('направление закрыто');
    }
    else{
      if(weget_field.val()>0 && curr_coef>0)
        youget_field.val( Math.round(weget_field.val()*curr_coef*100)/100 );
      else
        youget_field.val('направление закрыто');
    }
  }
});
