var moduleCalendar = function(me,id,type,baseURL) {
   this.self = me;
   this.tableID = id;
   this.calType = type;
   this.baseURL = baseURL;
   this.currentDate = new Date();
   this.month = this.currentDate.getMonth() + 1;
   this.day = this.currentDate.getDate();
   this.year = this.currentDate.getFullYear();
   this.listyear = this.year - 3;
   this.arrReq = new Array();
   if (this.calType == 'FULL') {
      this.monthText = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
      this.arrHeader = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
   }else{
      this.monthText = new Array('Ja','Fe','Mr','Ap','My','Jn','Jl','Au','Se','Oc','No','De');
      this.arrHeader = new Array('S','M','T','W','T','F','S');
   }
}
moduleCalendar.prototype.setDate = function(m,y) {
   this.month = m;
   this.year = y;
}
moduleCalendar.prototype.prevMonth = function () {
   if (!runsAjax) return false;

   this.month--;
   if (this.month < 1) {
      this.month = 12;
      this.year--;
   }
   this.sendRequest();
}
moduleCalendar.prototype.nextMonth = function () {
   if (!runsAjax) return false;

   this.month++;
   if (this.month > 12) {
      this.month = 1;
      this.year++;
   }
   this.sendRequest();
}
moduleCalendar.prototype.sendRequest = function () {
   var seed = Math.random();
   this.arrReq['req'+seed] = getXmlHttpRequestObject();
   var getvars = '?m='+this.month+'&y='+this.year;  
   var url = this.baseURL+'/ajax/calXML.aro'+getvars;

   this.arrReq['req'+seed].onreadystatechange = (function (func,seed) { return function() {
      func.processNewCalendar(seed);
   }})(this,seed);
   this.arrReq['req'+seed].open('GET',url,true);
   this.arrReq['req'+seed].send('');
   return true;
}
moduleCalendar.prototype.toggleVisibilityMonth = function (month) {
   var div = document.getElementById('cal'+this.tableID+'MonthPicker');
   if (div.style.visibility == '') {
      div.style.visibility = 'hidden';
   }else{
      div.style.visibility = '';
   }
}
moduleCalendar.prototype.setMonth = function (month) {
   if (!runsAjax) return false;
   this.month = month;
   this.sendRequest();
}
moduleCalendar.prototype.toggleVisibilityYear = function (year) {
   if (this.cancelToggle) {
      this.cancelToggle = false;
      return;
   }
   var div = document.getElementById('cal'+this.tableID+'YearPicker');
   if (div.style.visibility == '') {
      div.style.visibility = 'hidden';
   }else{
      this.listyear = this.year - 3;
      var list = document.getElementById('cal'+this.tableID+'YearList');
      while(list.childNodes[0]) list.removeChild(list.childNodes[0]);
      for (var i = 0; i < 7;i++) {
         var li = document.createElement('li');
         if (this.listyear + i == this.year) {
            li.style.fontWeight = '700';
         }
         li.appendChild(document.createTextNode(this.listyear + i));
         li.onclick = (function(func,year) { return function() {
            func.setYear(year);
         }})(this,this.listyear + i);
         list.appendChild(li);
      }
      div.style.visibility = '';
   }
}
moduleCalendar.prototype.setYear = function (year) {
   if (!runsAjax) return false;
   this.year = year;
   this.sendRequest();
}
moduleCalendar.prototype.incYear = function (act,delay) {
   this.cancelToggle = true;
   if (typeof(delay) == 'undefined') delay = 1000;
   if (act == 'start') {
      this.listyear++;
      var list = document.getElementById('cal'+this.tableID+'YearList');
      var li = document.createElement('li');
      if (this.listyear + 6 == this.year) {
         li.style.fontWeight = '700';
      }
      li.appendChild(document.createTextNode(this.listyear + 6));
      li.onclick = (function(func,year) { return function() {
         func.setYear(year +6);
      }})(this,this.listyear);
      var items = list.getElementsByTagName('li');
      list.removeChild(items.item(0));
      list.appendChild(li);
      this.timerInc = setTimeout (this.self+".incYear('start',80)",delay);
   }else{
      clearTimeout(this.timerInc);
   }
}
moduleCalendar.prototype.dincYear = function (act,delay) {
   this.cancelToggle = true;
   if (typeof(delay) == 'undefined') delay = 1000;
   if (act == 'start') {
      this.listyear--;
      var list = document.getElementById('cal'+this.tableID+'YearList');
      var li = document.createElement('li');
      if (this.listyear == this.year) {
         li.style.fontWeight = '700';
      }
      li.appendChild(document.createTextNode(this.listyear));
      li.onclick = (function(func,year) { return function() {
         func.setYear(year);
      }})(this,this.listyear);
      var items = list.getElementsByTagName('li');
      list.removeChild(items.item(items.length - 1));
      list.insertBefore(li,items.item(0));
      this.timerInc = setTimeout (this.self+".dincYear('start',80)",delay);
   }else{
      clearTimeout(this.timerInc);
   }
}


moduleCalendar.prototype.processNewCalendar = function (seed) {
   var ajaxReq = this.arrReq['req'+seed];
   if (ajaxReq.readyState == 4) {
      try{ajaxReq.status}catch(e){return false;}
      if (ajaxReq.status == 200) {
         var xmlDoc = ajaxReq.responseXML;
         var days = xmlDoc.getElementsByTagName('response').item(0).getElementsByTagName('day');

         var tbody = document.createElement('tbody');
         var tr = document.createElement('tr');
         for ( key in this.arrHeader) {
            var th = document.createElement('th');
            th.appendChild(document.createTextNode(this.arrHeader[key]));
            tr.appendChild(th);
         }
         tbody.appendChild(tr);
         var tr = document.createElement('tr');
         for (var iNode = 0; iNode < days.length; iNode++) {
            var node = days.item(iNode);
            var className = node.getElementsByTagName('type').item(0).firstChild.nodeValue;
            var dayNumber = node.getElementsByTagName('number').item(0).firstChild.nodeValue;
            var calEvents  = node.getElementsByTagName('events').item(0);
            if (node.getElementsByTagName('events').item(0)) {
               var calEvents = node.getElementsByTagName('events').item(0).getElementsByTagName('event');
            }
            var td = document.createElement('td');
            td.className = className;
            if (this.calType == 'FULL') 
               td.appendChild(document.createElement('br'));
            if (calEvents && calEvents.length) {
               var span = document.createElement('span');
               span.appendChild(document.createTextNode(dayNumber));
               if (this.calType == 'FULL') 
                  span.appendChild(document.createElement('br'));
               if (calEvents.length > 1) {
                  if (this.calType == 'FULL') 
                     span.appendChild(document.createTextNode(calEvents.length+' events'));
                  var text = '';
                  for(var key = 0;key < calEvents.length; key++ ){
                     text = text ? text+'<br/><br/>'+calEvents.item(key).getElementsByTagName('title').item(0).firstChild.nodeValue : calEvents.item(key).getElementsByTagName('title').item(0).firstChild.nodeValue;
                  }
                  td.onmouseover = (function(text) { return function() {
                     Tip(text,TITLE,'Events for this day: ');
                  }})(text);
               }else{
                  if (this.calType == 'FULL') 
                     span.appendChild(document.createTextNode('1 event'));
                  td.onmouseover = (function(text) { return function() {
                     Tip(text,TITLE,'Events for this day: ');
                  }})(calEvents.item(0).getElementsByTagName('title').item(0).firstChild.nodeValue);
               }
               span.className = 'fakelink';
               span.onclick = (function (baseURL,m,y,d) { return function() {
                  tmp = window.open(baseURL+'/event.aro?m='+m+'&y='+y+'&d='+d,'eventday','width=780,height=600,resizable=1,scrollbars=yes');
                  tmp.opener = window;
               }})(this.baseURL,this.month,this.year,dayNumber);

               td.appendChild(span);
            }else {
               td.appendChild(document.createTextNode(dayNumber));
               if (this.calType == 'FULL') {
                  td.appendChild(document.createElement('br'));
                  td.appendChild(document.createTextNode('\u00a0'));
               }
            }
            tr.appendChild(td);
            if ((iNode + 1) % 7 == 0 && iNode > 0) {
               tbody.appendChild(tr);
               tr = document.createElement('tr');
            }
         }
         var table = document.getElementById('table'+this.tableID+'table');
         while(table.childNodes[0]) table.removeChild(table.childNodes[0]);
         table.appendChild(tbody);
      }
   }
   document.getElementById('cal'+this.tableID+'ThisYear').innerHTML = this.year;
   document.getElementById('cal'+this.tableID+'ThisMonth').innerHTML = this.monthText[this.month - 1];
}

