// kalendar

$(document).ready(function(){
   
   $( "#datepicker" ).datepicker({
	   inline: true,
      dateFormat: 'dd.mm.yy',
      dayNames: ['Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota'],
      dayNamesMin: ['Ne', 'Po', 'Út', 'St', 'Čt', 'Pá', 'So'],
      firstDay: 1,
      monthNames: ['Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'],
      monthNamesShort: ['Led', 'Úno', 'Bře', 'Dub', 'Kvě', 'Čer', 'Červ', 'Srp', 'Zář', 'Říj', 'Lis', 'Pro'],
      navigationAsDateFormat: true,
      nextText: 'mm/yy',
      prevText: 'mm/yy',
      onChangeMonthYear: function(year, month, inst) {
         cal_events_highlight(year, month);
      },
      onSelect: function(dateText, inst) {
         cal_get_event(dateText);
      }
	});
   
   cal_events_highlight('', '');
   
});

function cal_events_highlight(year, month){
   
   $.ajax({
      type: "POST",
      url: "/ajax/get-events-days",
      data: {
         year: year,
         month: month
      },
      success: function(msg){
         if(msg){
            days = msg.split('|');
            for(var i in days){
               $("#cal_"+days[i]).addClass('ui-state-active-my');
            }
         }
      }
   });
   
}

function cal_get_event(date){
   
   $.ajax({
      type: "POST",
      url: "/ajax/get-event",
      data: {
         date: date
      },
      success: function(msg){
         if(msg){
            $("#events_content").html(msg);
         }
      }
   });
   
}

// anketa

function survey_vote(ans){
   if(ans){
      id = document.anketa.id.value;
      
      $.ajax({
         type: "POST",
         url: "/ajax/survey-vote",
         data: {
           id: id,
           ans: ans
         },
         success: function(msg){
           survey_result(msg);
         }
      });
      
   }
}

function survey_result(resp){
   if(resp == 'error_01'){
      $.slideNotice('V každé anketě můžete hlasovat pouze jednou.', { type: 'bad' });
   }
   else{
      pole = resp.split('##|##');
      $("#survey_voting_count").fadeTo(300, 0.01); setTimeout(function(){ $("#survey_voting_count").html(pole[0]); }, 350); setTimeout(function(){ $("#survey_voting_count").fadeTo(300, 1); }, 400);
      $("#survey_content").fadeTo(300, 0.01); setTimeout(function(){ $("#survey_content").html(pole[1]); }, 350); setTimeout(function(){ $("#survey_content").fadeTo(300, 1); }, 400);
      $.slideNotice('Váš hlas byl započítán. Děkujeme.', { type: 'good' });
   }
}

// kontaktni formular

function contact_form_submit(){
   
   $.ajax({
      type: "POST",
      url: "/ajax/contact-send",
      data: {
        name: $("#c_name").val(),
        mail: $("#c_mail").val(),
        phone: $("#c_phone").val(),
        text: $("#c_text").val()
      },
      success: function(msg){
        contact_form_result(msg);
      }
   });
   
}

function contact_form_result(resp){
   if(resp == 'error_01'){
      $.slideNotice('Vyplňte povinné údaje - jméno, e-mail a text.', { type: 'bad' });
   }
   else if(resp == 'error_02'){
      $.slideNotice('Neplatný e-mail.', { type: 'bad' });
   }
   else if(resp == 'ok'){
      $.slideNotice('Váš dotaz byl odeslán. Děkujeme.', { type: 'good' });
      document.forms['contact_form'].reset();
   }
}

// lboxy

function lbox_show(id){
   $("#my_lbox_overlay").show(1);
   $("#lbox_"+id).show(1);
}

function lbox_hide(id){
   $("#lbox_"+id).hide(1);
   $("#my_lbox_overlay").hide(1);
}

// objednavkovy form

$(document).ready(function(){
   
   order_changing_handler_init();
   
   order_load_items();
   
});

function order_changing_handler_init(){
   $("#order_form").find("input,select,textarea").unbind().change(function(){
      order_save();
   });
}

function order_save(){
   
   var form = document.forms['order_form'];
   var data = $(form).serialize();
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-save",
      data: data,
      success: function(msg){
         
      }
   });
   
}

function order_load_items(){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-load-items",
      success: function(msg){
         if(msg) $("#order_products_content").html(msg);
         order_changing_handler_init();
      }
   });
   
}

function order_load_item_parameters(key){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-load-item-parameters",
      data: { key: key, id_product: $("#item_product_"+key).val() },
      success: function(msg){
         if(msg) $("#parameters_"+key).html(msg);
         order_changing_handler_init();
      }
   });
   
}

function order_add_item(id){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-add-item",
      data: { id: id },
      success: function(msg){
         order_load_items();
      }
   });
   
}

function order_remove_item(key){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-remove-item",
      data: { key: key },
      success: function(msg){
         order_load_items();
      }
   });
   
}

function order_clear(){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-clear",
      success: function(msg){
         order_load_items();
      }
   });
   
}

function order_send(){
   
   $.ajax({
      type: "POST",
      url: "/ajax/order-send",
      success: function(msg){
         if(msg == 'error_01'){
            $.slideNotice('Vyplňte povinné údaje - jméno, e-mail a adresu.', { type: 'bad' });
         }
         else if(msg == 'error_02'){
            $.slideNotice('Špatný tvar emailové adresy.', { type: 'bad' });
         }
         else if(msg == 'ok'){
            $.slideNotice('Vaše objednávka byla odeslána.', { type: 'good' });
            order_clear();
            lbox_hide('form');
            $("#order_form").find("input,select,textarea").val('');
         }
      }
   });
   
}
