Effect.CalcBlindUp = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false, 
      scaleX: false, 
      restoreAfterFinish: true,
      afterFinish: function() {
        Element.remove(element);
      },
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping();
      } 
    }, arguments[1] || { })
  );
};

var bmiResults = {'18.5': ['podváha', 'vysoká', 1],
'25': ['norma', 'minimální', 0],
'30': ['nadváha', 'nízká až lehce vyšší', 0],
'35': ['obezita 1. stupně', 'zvýšená', 0],
'40': ['závažná obezita', 'vysoká', 1],
'end': ['těžká obezita', 'velmi vysoká', 1]};

var bmrConsts = [
  [66.473, 13.7516, 5.0033, 6.755],
  [655.0955, 9.5634, 1.8496, 4.6756]
];

var bmiWeight, bmiHeight, bmrHeight, bmrWeight;

function showBmiCalc(target)
{
  if ($('bmiCalc'))
  {
    Effect.CalcBlindUp('bmiCalc');
    return;
  }
  var target = target.parentNode.parentNode;
  var form = '<form method="post" action="#" id="bmiCalc" class="calc" style="display:none"><table><tr><th>Váha:</th><td><input type="text" name="bmi_weight" id="bmi_weight" value="" class="txt" /> kg</td></tr>'
    +'<tr><th>Výška:</th><td><input type="text" name="bmi_height" id="bmi_height" value="" class="txt" /> cm</td></tr>'
    +'<tr><th>&nbsp;</th><td><input onclick="countBMI();return false;" type="button" value="Spočítat" class="submit" /></td></tr></table></form>';
  Element.insert(target, form);
  
  bmiWeight = $('bmi_weight');
  bmiWeight.onchange = function() {if (bmrWeight) bmrWeight.value = this.value;}
  bmiHeight = $('bmi_height');
  bmiHeight.onchange = function() {if (bmrHeight) bmrHeight.value = this.value;}
  Effect.BlindDown('bmiCalc');
}

function showBmrCalc(target)
{
  if ($('bmrCalc'))
  {
    Effect.CalcBlindUp('bmrCalc');
    return;
  }
  var target = target.parentNode.parentNode;
  var form = '<form method="post" action="#" id="bmrCalc" class="calc" style="display:none"><table><tr><th>Váha:</th><td><input type="text" name="bmr_weight" id="bmr_weight" value="" class="txt" /> kg</td></tr>'
    +'<tr><th>Výška:</th><td><input type="text" name="bmr_height" id="bmr_height" value="" class="txt" /> cm</td></tr>'
    +'<tr><th>Věk:</th><td><input type="text" name="bmr_age" id="bmr_age" value="" class="txt" /> let</td></tr>'
    +'<tr><th>Pohlaví:</th><td><select name="bmr_gender" id="bmr_gender"><option value="0">Muž</option><option value="1">Žena</option></select></td></tr>'
    +'<tr><th>&nbsp;</th><td><input onclick="countBMR();return false;" type="button" value="Spočítat" class="submit" /></td></tr></table></form>';
  Element.insert(target, form);
  
  bmrWeight = $('bmr_weight');
  bmrWeight.onchange = function() {if (bmiWeight) bmiWeight.value = this.value;}
  bmrHeight = $('bmr_height');
  bmrHeight.onchange = function() {if (bmiHeight) bmiHeight.value = this.value;}
  Effect.BlindDown('bmrCalc');
}

function countBMI()
{
  closeBmiResult();
  var weight = bmiWeight.value;
  var height = bmiHeight.value;
  
  if (!weight || isNaN(weight)) alert('Prázdná nebo nesprávně vyplněná váha');
  else if (!height || isNaN(height)) alert('Prázdná nebo nesprávně vyplněná výška');
  else
  {
    height = height/100;
    var r = weight/(height*height);
    for (var cat in bmiResults) {
      if (cat == 'end' || r < cat) break;
    }
    var msg = 'Vaše hodnota BMI je <strong>'+(Math.round(r*100)/100)+'</strong>, což spadá do kategorie "<strong>'+bmiResults[cat][0]+'</strong>".<br />To představuje <strong'+(bmiResults[cat][2] ? ' class="warn"' : '')+'>'+bmiResults[cat][1]+' zdravotní rizika</strong>.';
    Element.insert(document.body, '<p id="bmiResult" class="calcResult">'+msg+'<a class="close" href="javascript:closeBmiResult();">Zavřít výsledek</a></p>');
    positionResult($('bmiResult'));
  }
}

function countBMR()
{
  closeBmrResult();
  var age = $('bmr_age').value;
  var weight = bmrWeight.value;
  var height = bmrHeight.value;
  var gender = $('bmr_gender').value;
  
  if (!age || isNaN(age)) alert('Prázdný nebo nesprávně vyplněný věk');
  else if (!weight || isNaN(weight)) alert('Prázdná nebo nesprávně vyplněná váha');
  else if (!height || isNaN(height)) alert('Prázdná nebo nesprávně vyplněná výška');
  else
  {
    var consts = bmrConsts[gender];
    var result = (consts[0]+(consts[1]*weight)+(consts[2]*height)-(consts[3]*age))*4.1868;
    var msg = 'Vaše doporučená denní spotřeba energie je <strong>'+Math.round(result)+'</strong> kJ.';
    Element.insert(document.body, '<p id="bmrResult" class="calcResult">'+msg+'<a class="close" href="javascript:closeBmrResult();">Zavřít výsledek</a></p>');
    positionResult($('bmrResult'));
  }
}

function positionResult(el)
{
  var wndSize = document.viewport.getDimensions();
  var offset = document.viewport.getScrollOffsets();
  el.style.left = (((wndSize.width-el.offsetWidth)/2)+offset.left)+'px';
  el.style.top = (((wndSize.height-el.offsetHeight)/2)+offset.top)+'px';
}

function closeBmiResult()
{
  if ($('bmiResult')) Element.remove('bmiResult');
}
function closeBmrResult()
{
  if ($('bmrResult')) Element.remove('bmrResult');
}