//Cufon.now();

// 1. Change images on hover
$$('.hover').each(function(e) {
  Event.observe(e, 'mouseover', function() {
    // get and update src
    var src = e.readAttribute('src').replace(/1.png/i, '1-over.png');
    // set src
    e.writeAttribute('src', src);
  });

  Event.observe(e, 'mouseout', function() {
    // get and update src
    var src = e.readAttribute('src').replace(/1-over.png/i, '1.png');
    // set src
    e.writeAttribute('src', src);
  });

  Event.observe(e, 'focus', function() {
    // get and update src
    var src = e.readAttribute('src').replace(/1.png/i, '1-over.png');
    // set src
    e.writeAttribute('src', src);
  });

  Event.observe(e, 'blur', function() {
    // get and update src
    var src = e.readAttribute('src').replace(/1-over.png/i, '1.png');
    // set src
    e.writeAttribute('src', src);
  });

  Event.observe(e, 'click', function() {
    // get and update src
    var src = e.readAttribute('src').replace(/1.png/i, '2.png');
    // set src
    e.writeAttribute('src', src);
  });
});

// 2. Replace preview image on change
$$('select[name="produkt_id"]').each(function(e) {
  Event.observe(e, 'change', function() {
    var node;
    var index = e.selectedIndex;
    var src = e.options[index].getAttribute('img');
    var price = parseFloat(e.options[index].getAttribute('price'));
    var sale = e.options[index].getAttribute('sale');
    var id = e.options[index].getAttribute('value');
    var p = e.options[index].getAttribute('p');
    
    var img = e.up('li').down('img');
    img.writeAttribute('src', src.replace(/{size}/i, 'medium'));
    
    //var link = img.up('a');
    //link.writeAttribute('href', src.replace(/{size}/i, 'large'));
    
    //var link2 = e.up('li').down('.buttony a');
    //link2.writeAttribute('href', src.replace(/{size}/i, 'large'));
    
    node = e.up('li').down('h4.case');
    if (p > 0) {
      if (node == undefined) {
        e.up('li').down('h3').insert({after: '<h4 class="case">vč. pouzdra</h4>'});
      }
    }
    else {
      if (node != undefined){
        node.remove();
      }
    }

    node = e.up('li').down('h4.id');
    node.innerHTML = 'kód ' + id.substr(0,3) + ' ' + id.substr(3);

    if (u == true) {
      node = e.up('li').down('.rabat');
      if (sale == 'true' || parseInt(sale) > 0) {
        if (sale == 'true') {
          node.innerHTML = 'Cena po slevě';
        }
        else {
          node.innerHTML = 'Cena &minus;' + parseInt(sale) + '&nbsp;%';
        }
      }
      else {
        if (node != undefined){
          node.remove();
        }
      }

      node = e.up('li').down('h3 strong');
      if (parseInt(sale) > 0) {
        price = price * (1 - parseInt(sale)/100);
      }
      node.innerHTML = price.toFixed(2).replace('.', ',');
    }
  });
});

// 3. Update postage in order form
Event.observe('zpusob_dodani', 'change', function() {
  var glasses_quantity_total = parseInt($F('glasses_quantity_total'));
  
  if (glasses_quantity_total > 10) {return;} // quantity is sky-high, postal service is free
  
  // get selected option
  var select = $('zpusob_dodani');
  var index = select.selectedIndex;
  var price = parseFloat(select.options[index].getAttribute('price'));
  var price_vat = price * 1.2;
  var postal = parseFloat($('postal').innerHTML.replace(',', '.'));
  var price_total = parseFloat($('price_total').innerHTML.replace(',', '.'));
  var price_vat_total = parseFloat($('price_vat_total').innerHTML.replace(',', '.'));

  // none selected, substract current postage
  if (index == 0) {
    price = 0;
    price_vat = 0;
  }

  $('postal').update(price.toFixed(2).replace('.', ','));
  $('price_total').update((price_total - postal + price).toFixed(2).replace('.', ','));
  $('price_vat_total').update((price_vat_total - (postal * 1.2) + price_vat).toFixed(2).replace('.', ','));
});

// 4. Show/hide custom address field in order form
if ($('dodaci_adresa_vlastni') != null) {
  $('dodaci_adresa_vlastni').hide();
  Event.observe('dodaci_adresa', 'change', function() {
    if ($F('dodaci_adresa') == 'input') {
      $('dodaci_adresa_vlastni').show();
    }
    else {
      $('dodaci_adresa_vlastni').hide();
    }
  });
}

