$(document).ready(function() {
  
   /* calendar */
  var now = new Date();
  var m = now.getMonth()+1;
  var y = now.getFullYear();  
  	
	$('#calendar').dialog({
		autoOpen: false,
		width: 625,
		modal: true,
		title: 'Kalendář',
		position: 'top',
		open: function(event, ui) {
        $.get(url+'inc/ajax/acalendar.php?month='+m+'&year='+y, function(data) {
          $('#calendar').html(data);
          load_calender();
        });  
    }
	});
  $('#choose_date').click(function() {
     $('#calendar').dialog('open'); 
  }); 
      
});

var load_calender = function($blah) {
  $('#calendar td.dayBox.active').click(function() {
      $('.dayBox').removeClass('act');
      $(this).addClass('act');
      var week = $(this).parent().attr("id");
      week = week.replace('daybox','timebox');
      $('.timefield').hide();
      $('#'+week).show();
      
      var ship1_price = $('#calendar #ship1_price').html();
      var ship1_title = $('#calendar #ship1_title').html();
      var ship2_price = $('#calendar #ship2_price').html();
      var ship2_title = $('#calendar #ship2_title').html();
       
      $('#calendar .timebox').addClass('active');
      $('#calendar .timebox').removeClass('deactive');
     // $('#calendar .timebox .title').html(ship2_title);
     // $('#calendar .timebox .price').html(ship2_price+' Kč');
      if( $(this).hasClass('today') ){
        var currentTime = new Date();
        var h = currentTime.getHours();
        for( i=8; i<= h+1; i++){
          $('#calendar #timebox_'+i).addClass('deactive');
          $('#calendar #timebox_'+i).removeClass('active');
         // $('#calendar #timebox_'+i+' .title').html('nelze');
         // $('#calendar #timebox_'+i+' .price').html('--');
        }
        //$('#calendar #timebox_'+(++h)+' .title').html(ship1_title);
        //$('#calendar #timebox_'+(h)+' .price').html(ship1_price+' Kč');
        //$('#calendar #timebox_'+(++h)+' .title').html(ship1_title);
        //$('#calendar #timebox_'+(h)+' .price').html(ship1_price+' Kč');
      }
            
      $("#start_date").attr("value", $(this).attr("id") );
  });

  $('#calendar .timebox.active').click(function() {
     if( $(this).hasClass('deactive') ){
        return;
     }     
     var hour = $(this).attr("id");
     hour = hour.replace('timebox_', '');
     $("#start_time").attr("value", hour+":00" );
     
     $("#calendar").dialog('close');
  });
   
};


function change_month(month, year) { 
    var product_id = $('#product_id').attr("value");
    var future = $("input[name='daycount']:checked").val();
    $.get(url+'inc/ajax/acalendar.php?month='+month+'&year='+year+'&id='+product_id+'&future='+future, function(data) {
        $('#calendar').html(data);
        load_calender();
    }); 
         
};
