// JavaScript Document
var Calendar = Class.create();
Calendar.prototype = {
  initialize: function(){
    this.calTable = $('smallCalendar');
    this.tBody;
    this.actualMonth = $('actualMonth');
    this.prevMonth = $('prevMonth');
    this.nextMonth = $('nextMonth');
    this.myDate = new Date();
    this.myDate.setDate(1);
    this.days = ['Po', 'Út', 'St', 'Čt', 'Pá', 'So', 'Ne'];
    this.months = ["leden","únor","březen","duben","květen","červen","červenec","srpen","září","říjen","listopad","prosinec"];    
  },
  
  createCalHead: function()
  {
    var daysName = document.createElement('tr');
    daysName.setAttribute('align', 'center');
    daysName.setAttribute('valign', 'middle');
    //this.tBody.appendChild(daysName);
    
    var th;
    for(i=0;i<7;i++)
    {
      th = document.createElement('th');   
      th.appendChild(document.createTextNode(this.days[i]));
      daysName.appendChild(th);
    }
  },

  getUrl: function()
  {
    var adresa = location.href;
    var sectionUrl = "";
    
    adresa = adresa.split('?');
    adresa = adresa[0];
    adresa = adresa.split('/');
    adresaSize = adresa.length
    for(i=3;i<adresaSize;i++)
    {
      sectionUrl += adresa[i]+'/';
    }
    sectionUrl = sectionUrl.replace(/\/$/,"");
    
    return sectionUrl;
  },
  
  getActionDays: function()
  {
    var self = this;
    var month = parseInt(this.myDate.getMonth()) + 1;
    newAjax = new mujAjax();
    
    newAjax.InitializeRequest('get','../ajax/calendar.php?month='+month);
    
    newAjax.Commit('');
    newAjax.OnSuccess = function() {self.createDays(newAjax.GetResponseText())}; 
  },
  
  createDays: function(actionDays)
  {          
    var url = this.getUrl();
    var actionDaysArr = new Array(); 
    actionDaysArr = actionDays.split(',');
    var helpDate = new Date();
    var dayNumber = 1;
    var loop = 1;
    var row, col, a;    
    helpDate.setFullYear(this.myDate.getFullYear(),this.myDate.getMonth(),1);
    do
    {
      if (loop % 7 == 1)
      {
        row = document.createElement('tr');
        //this.tBody.appendChild(row);
      }
         
      if (dayNumber == 1)
      {
        for (j=1;j<helpDate.getDay();j++)
        {
          col = document.createElement('td');
          row.appendChild(col);
          loop++;
        }
      }
      a = document.createElement('a');
      a.setAttribute('href', '/'+url+'?year='+helpDate.getFullYear()+'&month='+helpDate.getMonth()+'&day='+helpDate.getDate());
      if (actionDaysArr.find(helpDate.getDate()))
      {
        a.setAttribute('class', 'actionDay');
        a.setAttribute('className', 'actionDay');
      }
        a.appendChild(document.createTextNode(helpDate.getDate()));
      col = document.createElement('td');
      col.appendChild(a);
      row.appendChild(col);
      dayNumber++;loop++;
      helpDate.setDate(dayNumber);
      dayNumber = helpDate.getDate();
    }
    while (dayNumber>1)
  },

  truncateTable: function()
  {
    
    //var parentEle = this.calTable.parentNode;
    var tBody;
    /*
    this.calTable.parentNode.removeChild(this.calTable);
    
    this.calTable = document.createElement('table');
    this.calTable.setAttribute('id', 'smallCalendar');
    this.calTable.setAttribute('class', 'calendar');
    this.calTable.setAttribute("className", "calendar");
    this.calTable.setAttribute('cellspacing', '0');
        
    this.tBody = document.createElement('tBody');
   
    this.calTable.appendChild(this.tBody);      
    parentEle.appendChild(this.calTable);
    */         
  },
  
  
  getMonthNumber: function(month)
  {
    if (month > 11)
    {
      return (month - 12)
    }      
    else if (month < 0)
      return (month + 12)
    else
      return month;     
  },
  
  setSmallCalValues: function(month,year)
  {
    var actMonth = this.months[month]+" "+year;
                
    if (month == 11)
    {
      var nextM = 0;
      var prevM = month - 1;
      var Nyear = year+1;
      var Pyear = year;
    }      
    else if (month == 0){
      var nextM = month + 1;
      var prevM = 11;
      var Nyear = year;
      var Pyear = year-1;
    }else{
      var nextM = month + 1;
      var prevM = month - 1;
      var Nyear = year;
      var Pyear = year;
    }
      
    /*
    var nextM = this.getMonthNumber(month + 1);
    var prevM = this.getMonthNumber(month - 1);
    */
           //alert (month);
    /*this.nextMonth.href="javascript:cal.setSmallCal("+nextM+",'next')";
    this.prevMonth.href="javascript:cal.setSmallCal("+prevM+",'prev')";
    */
    
    this.nextMonth.href="javascript:ajaxpage2('/ajax.php?get=calendar&action=init2&month="+nextM+"&year="+Nyear+"','ajax',"+nextM+","+Nyear+");";
    this.prevMonth.href="javascript:ajaxpage2('/ajax.php?get=calendar&action=init2&month="+prevM+"&year="+Pyear+"','ajax',"+prevM+","+Pyear+");";
    
    this.actualMonth.innerHTML = actMonth;
       
  },
  
  setRightYear: function(month, direction)
  {
    if (direction == 'next' && month == 0)
      this.myDate.setFullYear(this.myDate.getFullYear() + 1);
    if (direction == 'prev' && month == 11)
      this.myDate.setFullYear(this.myDate.getFullYear() - 1);
  
  },
  
  setSmallCal: function(month, direction)
  { 
    
    this.setRightYear(month, direction);
    this.myDate.setMonth(month);    
    this.setSmallCalValues(month);
    this.myDate.setMonth(month);    
    this.buildCal(month);        
  },

  buildCal: function(month) {    
    this.myDate.setMonth(month);
    this.truncateTable();
    this.createCalHead();
    this.getActionDays();
  }
}

var cal;


function mujAjax()
{
   //---------------------
   // Private Declarations
   //---------------------
   var _request = null;
   var _this = null;

   //--------------------
   // Public Declarations
   //--------------------
   this.GetResponseXML = function()
   {
      return (_request) ? _request.responseXML : null;
   }

   this.GetResponseText = function()
   {
      return (_request) ? _request.responseText : null;
   }

   this.GetRequestObject = function()
   {
      return _request;
   }

   this.InitializeRequest = function(Method, Uri)
   {
      _InitializeRequest();
      _this = this;

      switch (arguments.length)
      {
         case 2:
            _request.open(Method, Uri);
            break;

         case 3:
            _request.open(Method, Uri, arguments[2]);
            break;
      }

      if (arguments.length >= 4) _request.open(Method, Uri, arguments[2], arguments[3]);
      this.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   }

   this.SetRequestHeader = function(Field, Value)
   {
      if (_request) _request.setRequestHeader(Field, Value);
   }

   this.Commit = function(Data)
   {
      if (_request) _request.send(Data);
   }

   this.Close = function()
   {
      if (_request) _request.abort();
   }

   //---------------------------
   // Public Event Declarations.
   //---------------------------
   this.OnUninitialize = function() { };
   this.OnLoading = function() { };
   this.OnLoaded = function() { };
   this.OnInteractive = function() { };
   this.OnSuccess = function() { };
   this.OnFailure = function() { };

   //---------------------------
   // Private Event Declarations
   //---------------------------
   function _OnUninitialize() { _this.OnUninitialize(); };
   function _OnLoading() { _this.OnLoading(); };
   function _OnLoaded() { _this.OnLoaded(); };
   function _OnInteractive() { _this.OnInteractive(); };
   function _OnSuccess() { _this.OnSuccess(); };
   function _OnFailure() { _this.OnFailure(); };

   //------------------
   // Private Functions
   //------------------
   function _InitializeRequest()
   {
      _request = _GetRequest();
      _request.onreadystatechange = _StateHandler;
   }

   function _StateHandler()
   {
      switch (_request.readyState)
      {
         case 0:
            window.setTimeout("void(0)", 100);
            _OnUninitialize();
            break;

         case 1:
            window.setTimeout("void(0)", 100);
            _OnLoading();
            break;

         case 2:
            window.setTimeout("void(0)", 100);
            _OnLoaded();
            break;

         case 3:
            window.setTimeout("void(0)", 100);
            _OnInteractive();
            break;

         case 4:
            if (_request.status == 200)
               _OnSuccess();
            else
               _OnFailure();

            return;
            break;
      }
   }

   function _GetRequest()
   {
      var obj;

      try
      {
         obj = new XMLHttpRequest();
      }
      catch (error)
      {
         try
         {
            obj = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (error)
         {
            return null;
         }
      }

      return obj;
   }
}

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]==searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}
