(function() {
  var disabledDays = [];
  var methods_table = $("#methods_table, #offices_table");

  if ( methods_table.length>0 ) {
    $("tr").hover(
      function () {
        $(this).addClass("hover");
      }, 
      function () {
        $(this).removeClass("hover");
      }
      );

    $("tr").click(function() {
      $("input", this).attr("checked", true);
    });
  }

})(jQuery)


// kustuta büroo
$(".delete_office_button").click(function() {
  $("#dialog_delete_office").dialog({
    bgiframe: true,
    resizable: false,
    height:140,
    modal: true,
    overlay: {
      backgroundColor: '#000',
      opacity: 0.5
    },
    buttons: {
      'Kustuta valitud büroo': function() {
        $(this).dialog('close');
      },
      "Katkesta": function() {
        $(this).dialog('close');
      }
    }
  });
});

// kustuta meetod
$(".delete_method_button").click(function() {
  $("#dialog_delete_method").dialog({
    bgiframe: true,
    resizable: false,
    height:140,
    modal: true,
    overlay: {
      backgroundColor: '#000',
      opacity: 0.5
    },
    buttons: {
      'Kustuta valitud meetod': function() {
        $(this).dialog('close');
      },
      "Katkesta": function() {
        $(this).dialog('close');
      }
    }
  });
});

(function() {
  var process = false;
  var imb_reg1 = $("#imb_reg1");
  var imb_reg2 = $("#imb_reg2");
  var imb_reg3 = $("#imb_reg3");

  var imb_reg1_help = $("#imb_reg1_help");
  var imb_reg2_help = $("#imb_reg2_help");
  var imb_reg3_help = $("#imb_reg3_help");
  var imb_reg4_help = $("#imb_reg4_help");
  var imb_reg5_help = $("#imb_reg5_help");

  var greatest_help_height = 0;
  if ( imb_reg1_help.height() > greatest_help_height ) greatest_help_height = imb_reg1_help.height();
  if ( imb_reg2_help.height() > greatest_help_height ) greatest_help_height = imb_reg2_help.height();
  if ( imb_reg3_help.height() > greatest_help_height ) greatest_help_height = imb_reg3_help.height();
  if ( imb_reg4_help.height() > greatest_help_height ) greatest_help_height = imb_reg4_help.height();
  if ( imb_reg5_help.height() > greatest_help_height ) greatest_help_height = imb_reg5_help.height();

  $("#imb_reg_container").css("min-height", greatest_help_height+15+"px");

  imb_reg2_help.addClass("hidden");
  imb_reg3_help.addClass("hidden");
  imb_reg4_help.addClass("hidden");
  imb_reg5_help.addClass("hidden");

  $("#imb_reg1 p > a").click(function() {
    if (process) return false;
    process = true;
  	
    $("#imb_reg1 p > a").removeClass('active');
    $(this).addClass('active');
  	
    $('body').data('support', $(this).attr('id').replace('support', ""));
    hide([imb_reg3, $('.imb_reg4'), imb_reg1_help,imb_reg2_help,imb_reg3_help, imb_reg4_help, imb_reg5_help]);
    $.getJSON('json.php', {
      type: 'getoffices', 
      support: $('body').data('support')
    }, function(json) {
			
      process = false;
			
      $("#imb_reg2").remove();
      if (json){
        $('#imb_reg1').after(json.html);
      }
      imb_reg2 = $("#imb_reg2");
      imb_reg2.removeClass("hidden");
      imb_reg2_help.removeClass("hidden");

      $("#imb_reg2 p > a").click(function() {
        if (process) return false;
        process = true;
				
        $("#imb_reg2 p > a").removeClass('active');
        $(this).addClass('active');		  	
		  	
        $('body').data('office', $(this).attr('id').replace('office', ""));
        hide([$('.imb_reg4'), imb_reg2_help,imb_reg3_help, imb_reg4_help, imb_reg5_help]);
        imb_reg3.removeClass("hidden");
        imb_reg3_help.removeClass("hidden");
		    
		    
        $.getJSON('json.php', {
          type: 'disabled_days', 
          office: $('body').data('office'), 
          support: $('body').data('support')
        }, function(json) {
		    	
          process = false;
		    	
          var disabled_days = [];
          if (json) {
            disabled_days = json.days;
          }
          /*if (json.disabled == true) {
		    		
            hide([$('.imb_reg4'), imb_reg2_help,imb_reg3_help, imb_reg4_help, imb_reg5_help]);
            imb_reg5_help.html(json.html);
            imb_reg5_help.removeClass("hidden");
          }*/
          // init calendar
          if ( $('#datepicker').length > 0 ) {
            function isDisabledDay(date) {		
              var m = date.getMonth();
              var d = date.getDate();
              var y = date.getFullYear();
              for (i = 0; i < disabled_days.length; i++) {
                if ((m == disabled_days[i][0] - 1) && (d == disabled_days[i][1]) && (y == disabled_days[i][2]))
                {
                  var css = '';

                  if(disabled_days[i][3] == 'bronned') {
                    css = 'datepicker-redday';
                    return [true, css];
                  } else {
                    return [false];
                  }
                }
              }
              return [true];
            }
            $('#datepicker').datepicker('destroy');
            $('#datepicker').datepicker({
              changeMonth: true,
              changeYear: true,
              showButtonPanel: true,
              dayNamesMin: ['P', 'E', 'T', 'K', 'N', 'R', 'L'],
              dayNamesShort: ['Pü', 'Es', 'Te', 'Ko', 'Ne', 'Re', 'La'],
              currentText: 'Käesolev kuu',
              firstDay: 1,
              hideIfNoPrevNext: true,
              beforeShowDay: isDisabledDay,
              dateFormat: 'yy-mm-dd',
              minDate: new Date(),
              maxDate: '+6m',
              monthNames: ['Jaanuar','Veebruar','Märts','Aprill','Mai','Juuni','Juuli','August','September','Oktoober','November','Detsember'],
              monthNamesShort: ['Jaan','Veeb','Märts','Apr','Mai','Juuni','Juuli','Aug','Sep','Okt','Nov','Dets'],
              onSelect: function(dateText, inst) {
                if (process) return false;
                process = true;
                var ddate = new Date(dateText);    
                var isDisabled = isDisabledDay(ddate);
                var html = json.html;
                hide([imb_reg1_help,imb_reg2_help,imb_reg3_help, imb_reg4_help, imb_reg5_help]);
					    	
                $('body').data('date', dateText);
                $(".imb_reg4").addClass("hidden");
                $.getJSON('json.php', {
                  type: 'gettimes', 
                  support: $('body').data('support'), 
                  office: $('body').data('office'), 
                  date: dateText
                }, function(json) {
                  process = false;
                  if (json) {
                    $('#imb_reg3').after(json.html);
                    if (!isDisabled[0] || typeof(isDisabled[1]) != 'undefined') {
                      hide([imb_reg2_help,imb_reg3_help, imb_reg4_help, imb_reg5_help]);
                      imb_reg1_help.html(html);
                      imb_reg1_help.removeClass("hidden");
                    } else {
                      $("#imb_reg3_help").addClass("hidden");
      					      
                      $("#imb_reg4_help").removeClass("hidden");
                
                
                      $(".imb_reg4 td > a").click(function() {
                        $(this).html("Valitud");
                        $(this).parent().addClass('bronned');
                        window.location="?controller=default&action=reservation&reservationdate=" + $('body').data('date') + "&office=" + $('body').data('office')  + "&support=" + $('body').data('support') + "&time=" + $(this).parent().prev().text();
                        return false;
                      });  
                    }
                  }
                });
              }
            });
          }	
        })	    
		    
        return false;
      });    	
    })    
    return false;
  });

  
  function hide(o) {
    for(k in o) {
      o[k].addClass("hidden");
    }
  }
 
})(jQuery)
