var t;
window.onload = function () {
  initCalendar();
}
function initCalendar() {
  hideCalendar();
}
function showInfo(event, ids) {
  infoObj=getDomObject('day_info');
  x=event.clientX;
  y=event.clientY;
  infoObj.style.left=(x+10)+'px';
  infoObj.style.top=(y+20)+'px';
  url='/getCalendarDayInfo_XML?ids='+ids;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateInfo);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}
function updateInfo() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html=root.getElementsByTagName('html_contents')[0].firstChild.nodeValue;
    infoObj=getDomObject('day_info');
    infoObj.innerHTML=html;
    changeClassEl(infoObj, 'day_info show');
  }
}
function hideInfo() {
  infoId='day_info';
  infoObj=getDomObject(infoId);
  changeClassEl(infoObj, 'day_info hide');
}

function calendarInit() {
  prevBut=getDomObject('prev_month');
  nextBut=getDomObject('next_month');
  cal=getDomObject('calendar');

  if (prevBut.addEventListener) prevBut.addEventListener('click',getPreviousMonth,false);
  else if (prevBut.attachEvent) prevBut.attachEvent('onclick',getPreviousMonth,false);
  if (nextBut.addEventListener) nextBut.addEventListener('click',getNextMonth,false);
  else if (nextBut.attachEvent) nextBut.attachEvent('onclick',getNextMonth,false);

  if (cal.addEventListener) cal.addEventListener('mouseover',showCalendar,false);
  else if (cal.attachEvent) cal.attachEvent('onmouseover',showCalendar,false);

  if (cal.addEventListener) cal.addEventListener('mouseout',hideCalendar,true);
  else if (cal.attachEvent) cal.attachEvent('onmouseout',hideCalendar,true);

}

function showCalendar(e) {
  cal_cont=getDomObject('calendar_container');
  newClass='visible';
  changeClassEl(cal_cont, newClass);
}
function hideCalendarDelay(e) {
  alert(e.type);
  t=setTimeout('hideCalendar()', 1000);
}
function hideCalendar() {
  cal_cont=getDomObject('calendar_container');
  newClass='invisible';
  changeClassEl(cal_cont, newClass);
  infoObj=getDomObject('day_info');
  changeClassEl(infoObj, 'day_info hide');
  clearTimeout(t);
}

function getPreviousMonth(e) {
  if (currentCalendarDate) {
    date=currentCalendarDate.split('-');
  }
  else {
    now=Date();
    date[0]=now.getFullYear();
    date[1]=now.getMonth()+1;
  }
  year=parseInt(date[0]);
  if (date[1].charAt(0)=='0') month=parseInt(date[1].substr(1))-1;
  else month=parseInt(date[1])-1;
  if (month==0) {
    month='12';
    year=parseInt(date[0])-1;
  }
  else if (month<10) month='0'+''+month;

  currentCalendarDate=year+'-'+month;
  url='/getCalendarMonth_XML?date='+currentCalendarDate;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateCalendar);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}

function getNextMonth(e) {
  if (currentCalendarDate) {
    date=currentCalendarDate.split('-');
  }
  else {
    now=Date();
    date[0]=now.getFullYear();
    date[1]=now.getMonth()+1;
  }
  year=parseInt(date[0]);
  if (date[1].charAt(0)=='0') month=parseInt(date[1].substr(1))+1;
  else month=parseInt(date[1])+1;
  if (month<10) month='0'+''+month;
  else if (month==13) {
    month='01';
    year=parseInt(date[0])+1;
  }
  currentCalendarDate=year+'-'+month;
  url='/getCalendarMonth_XML?date='+currentCalendarDate;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateCalendar);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}

function updateCalendar() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html=root.getElementsByTagName('html_contents')[0].firstChild.nodeValue;

    cal=getDomObject('calendar_container');
    cal.innerHTML=html;
    calendarInit();
  }
}

function getDomObject(id) {
  var obj;
  if (document.layers) {
    obj = document.layers[id];
  }
  else if (document.getElementById){
    obj = document.getElementById(id);
  }
  else if (document.all) {
    obj = document.all[id];
  }
  return obj;
}

function changeClassEl(el, newClass) {
  if (navigator.userAgent.indexOf("MSIE")>=0){
    classHandler='className';
  }
  else {
    classHandler='class';
  }
  el.setAttribute(classHandler, newClass);
  return 1;
}