var $j = jQuery.noConflict();
$j(document).ready(function($)
{ 
  
  var hover = 0;
  var hov = 0;
  var q;
  $('.econom').hover(
    function() {
      q = $('.calc_table').height() - 9;
      $('.item_bg_center').css('height', q);
      $('.econom div').children('.item_bg').css('display', 'block');
    },
    function() {
      if(hover != 1) {
        $('.econom div').children('.item_bg').css('display', 'none');
      }
    }
  );
  $('.ideal').hover(
    function() {
      q = $('.calc_table').height() - 9;
      $('.item_bg_center').css('height', q);
      $('.ideal div').children('.item_bg').css('display', 'block');
    },
    function() {
      if(hover != 2) {
        $('.ideal div').children('.item_bg').css('display', 'none');
      }
    }
  );
  $('.exclus').hover(
    function() {
      q = $('.calc_table').height() - 9;
      $('.item_bg_center').css('height', q);
      $('.exclus div').children('.item_bg').css('display', 'block');
    },
    function() {
      if(hover != 3) {
        $('.exclus div').children('.item_bg').css('display', 'none');
      }
    }
  );
  

  
  $('.calc_class_title sup').hover(
    function() {
      $(this).parent().parent().children('.hint').fadeIn(200, function() {
        $(this).children('.hint_middle').slideDown(200, function() {});
      });      
    },
    function() {
      $(this).parent().parent().children('.hint').children('.hint_middle').slideUp(200, function() {
        $(this).parent().fadeOut(200, function() {});
      });                 
    }
  );
  
  $('.calc_table_legend sup').hover(
    function() {
      $(this).children('.hint').fadeIn(200, function() {
        $(this).children('.hint_middle').slideDown(200, function() {});
      });      
    },
    function() {
      $(this).children('.hint').children('.hint_middle').slideUp(200, function() {
        $(this).parent().fadeOut(200, function() {});
      });                 
    }
  );
  
  
  var re = /^[0-9]*$/;
  var error = 0;
  var val;
  
  var proj;
  var light;
  var equipment;
  var install;
  var start;      
  var sum_econom;
  var sum_ideal;
  var sum_exclus;
  
  var outprice = function (price) {
    price = price.toFixed(0);
    price = price + '';
    price = price.substr(0,price.length-3) + ' ' + price.substr(price.length-3,3) + ' руб.';
    return price;
  }
  
  var prices = function () {
    if(!re.test($('#area').val())) {
      $('.area_error').html('Введите целое число');
      error = 1;
    } else {             
      if($('#area').val() > 2500) {
        $('.area_error').html('Максимальная площадь: 2500 м<sup>2</sup>');
        error = 1;
      }
      else {
        $('.area_error').html('');
        error = 0;
      }             
    }
    
    if(error == 0) {
      val = $('#area').val();
      
      if(val < 100) {
        proj = 100 * 300;
        light = 100 * 251.85;
      }
      else {
        if(val < 301) {
          proj = val * 300;
          light = val * 251.85;
        }
        else {
          if(val < 701) {
            proj = val * 275;
            light = val * 209.88;
          }
          else {
            if(val < 1501) {
              proj = val * 250;
              light = val * 167.90;
            }
            else {
              proj = val * 200;
              light = val * 41.98;
            }
          }
        }
      }
      
      equipment = new Array(val * 2250, val * 6300, val * 8500);
      install = new Array(val * 405, val * 567, val * 765);
      start = new Array(val * 810, val * 1134, val * 1530);
      
      sum_econom = proj + light + equipment[0] + install[0] + start[0];
      sum_ideal = proj + light + equipment[1] + install[1] + start[1];
      sum_exclus = proj + light + equipment[2] + install[2] + start[2];
      
     
      $('.sum_econom').html(outprice(sum_econom));
      $('.sum_ideal').html(outprice(sum_ideal));
      $('.sum_exclus').html(outprice(sum_exclus));
      $('.project').html(outprice(proj));
      $('.light').html(outprice(light));
      $('.econom_equipment').html(outprice(equipment[0]));
      $('.ideal_equipment').html(outprice(equipment[1]));
      $('.exclus_equipment').html(outprice(equipment[2]));
      $('.econom_install').html(outprice(install[0]));
      $('.ideal_install').html(outprice(install[1]));
      $('.exclus_install').html(outprice(install[2]));
      $('.econom_start').html(outprice(start[0]));
      $('.ideal_start').html(outprice(start[1]));
      $('.exclus_start').html(outprice(start[2]));
    }
  
  }
  prices();
  
  $('#area_form').submit(function() {    
    
    prices();
    
    return false;
    
  });
 
  var w;
 w = ($('.calc_table').innerWidth() - 705) / 3;
  $('.blank_td').css('width', w); 
 
}); 
