$(document).ready(function(){
var $discount;
var $adiscount;
var $attendees;
var $note;
var discount_vis = false;
var last_att = 1;
var open_term = false;
var open_term_last = false;
$discount = $('#discount');
$adiscount = $('#a-discount');
$attendees = $('#attendees');
$note = $('#note');
var att = 1;
var notes = [
'Každé 4. místo je zdarma',
'Jedno místo zdarma',
'Dvě místa zdarma',
'Tři místa zdarma',
'Z toho zdarma: '
];
var apply_discount = function() {
att = parseInt($attendees.val());
if (isNaN(att)) $attendees.val(1);
if (att < 1) $attendees.val(1);
if (att > 12) $attendees.val(12);
att = parseInt($attendees.val());
var price = parseInt($('#init_cp').val());
var free = Math.floor(att / 4);
open_term = $(':radio[name="skoleni"]:checked').attr('id').indexOf('otevreny') !== -1;
$('#price-td').parents('tr:eq(0)').toggle(!open_term);
$('span#note').toggle(!open_term);
if ((att > 3 && last_att < 4) || open_term) {
// Slevy zakazat
discount_vis = 'none' != $discount.css('display');
$adiscount.hide();
$discount.hide();
}
else if ((att < 4 && last_att > 3) || (!open_term && free < 1)) {
// Slevy povolit
$adiscount.show();
if (discount_vis) $discount.show();
}
if (free === 0) {
switch ($(':radio[name="discount"]:checked').val()) {
case 'regular': price *= 0.9; break;
case 'students': price *= 0.5; break;
}
}
price *= att - free;
price = Math.ceil(price);
$('#price').val(price);
$('#price-td').html(price.toString().replace(/(.+)(...)/, '$1&nbsp;$2'));
$('#price-td-vat').html(Math.ceil(price*$.vat).toString().replace(/(.+)(...)/, '$1&nbsp;$2'));
last_att = att;
if (0 === free) {
$note.removeClass('free-places');
$note.html(notes[free]);
}
else if (free > 3) {
$note.addClass('free-places');
$note.html(notes[4] + free);
}
else {
$note.addClass('free-places');
$note.html(notes[free]);
}
return true;
}
var modify_attendees = function(ev) {
var value = parseInt($attendees.val());
if (isNaN(value)) value = 1;
value += parseInt(ev.data.modification);
$attendees.val(value);
apply_discount();
return false;
}
if ('none' === $('input[name="discount"]:checked').val()) $discount.hide();
$discount.find(':radio').change(apply_discount);
$attendees.change(apply_discount);
$(':radio[name="skoleni"]').change(apply_discount);
$('#price-toggle').click(function(){
if ('none' == $discount.css('display')) {
$discount.show();
}
else if ('none' == $(':radio[name="discount"]:checked').val()) {
$discount.hide();
}
return false;
});
$('#attendees-add').bind('click', {modification: +1}, modify_attendees);
$('#attendees-subtract').bind('click', {modification: -1}, modify_attendees);
apply_discount();
});
