// {{{ Globals
var shifting=false;
var shiftto = "";
$(document).ready(function() {
  $('#datepicker').datepicker({
    dateFormat: '@',
    showOn: 'button',
    buttonImage: '/images/tiny_cal.png',
    buttonImageOnly: 'true',
    buttonText: 'Show Calendar',
    onSelect: function(dt) { selectDate(dt); }
  });
});
//}}}
function selectDate(dt) { //{{{
  var dte = new Date();
  var dtm = midnight(parseInt(dte.setTime(dt)/1000));
  //is today within our what is loaded in the scrolling calendar
  if((dtm >= parseInt(firsttime)) && (dtm <= parseInt(lasttime))) {
    shiftdays(dtm);
  } else {
    //reload scrolling calendar and show listing
    loadCalendar(dt/1000);
  }
}
//}}}
function goToday() { //{{{
  var ct = new Date();
  ct.setTime(curtime*1000);
  var dt = new Date();
  var dtm = midnight(parseInt(dt.getTime()/1000));
  //is today within our what is loaded in the scrolling calendar
  if((dtm >= parseInt(firsttime)) && (dtm <= parseInt(lasttime))) {
    //check to see if we are on today, if not move
    if(!((dt.getYear()==ct.getYear())&&(dt.getMonth()==ct.getMonth())&&(dt.getDate()==ct.getDate()))) {
      shiftdays(dtm);
    }
  } else {
    //reload scrolling calendar and show listing
    loadCalendar(dtm);
  }
} //}}}
function loadCalendar(dt) { //{{{
  var lowDt = addDay(dt,-3);
  var highDt = addDay(dt,3);
  var bldday = lowDt;
  var newBgRow = _$('bgRow').cloneNode(true); 
  var newTxtRow = _$('txtRow').cloneNode(true);
  newBgRow.innerHTML = "";
  newTxtRow.innerHTML = "";
  var bgNode = _$('bgRow').firstChild; 
  var txtNode = _$('txtRow').firstChild; 
  var dttm = new Date();
  firsttime = lowDt;
  lasttime = highDt;
  curtime = dt;
  while(lowDt<=highDt) {
    var newBgNode = bgNode.cloneNode(true);
    var newTxtNode = txtNode.cloneNode(true);
    dttm.setTime(bldday*1000);
    newTxtNode.getElementsByTagName('a')[0].setAttribute('onclick','shiftdays('+bldday+');');
    newTxtNode.getElementsByTagName('h1')[0].innerHTML=dateFormat(dttm,'ddd',true).toUpperCase();
    newTxtNode.getElementsByTagName('h2')[0].innerHTML=dateFormat(dttm,'mmm',true).toUpperCase();
    newTxtNode.getElementsByTagName('h3')[0].innerHTML=dateFormat(dttm,'dd',true);
    newTxtNode.setAttribute("id","day"+bldday);
    newBgRow.appendChild(newBgNode);
    newTxtRow.appendChild(newTxtNode);
    lowDt = addDay(lowDt,1);
    bldday = lowDt;
  }
  _$('bgRow').parentNode.replaceChild(newBgRow,_$('bgRow'));
  _$('txtRow').parentNode.replaceChild(newTxtRow,_$('txtRow'));
  _$S('dayslideBG').left='0px';
  _$S('dayslideTXT').left='0px';
  url = '/index.html?a=1&day=1&t='+dateFormat(curtime*1000,'yyyy-mm-dd');
  setCalDate(curtime);
  processAJAX(url,shiftdaysCB,false);
} //}}}
function shiftdays(newday) { //{{{
  newday=midnight(newday);
  if(!shifting) {
    shifting=true;
    //check to see if we have the calendar entry already created, if not prepend or append as needed
    var lowDt = addDay(newday,-3);
    var highDt = addDay(newday,3);

    var mult=Math.round((parseInt(newday)-parseInt(curtime))/86400);
    if((lowDt < firsttime) || (highDt > lasttime)) {
      //create calendar entries (prepend older dates)
      if(mult<0) {
        var bldday = addDay(firsttime,-1);
        firsttime = addDay(firsttime,mult);

        while (bldday >= parseInt(firsttime)) {
          var bgNode = _$('bgRow').firstChild; 
          var newBgNode = bgNode.cloneNode(true);
          _$('bgRow').insertBefore(newBgNode,bgNode);
          var txtNode = _$('txtRow').firstChild; 
          var newTxtNode = txtNode.cloneNode(true);
          var dttm = new Date(bldday*1000);
          newTxtNode.getElementsByTagName('a')[0].setAttribute('onclick','shiftdays('+bldday+');');
          newTxtNode.getElementsByTagName('a')[0].setAttribute('title',dateFormat(dttm,'mmm').toUpperCase()+" "+dateFormat(dttm,'dd'));
          newTxtNode.getElementsByTagName('h1')[0].innerHTML=dateFormat(dttm,'ddd',true).toUpperCase();
          newTxtNode.getElementsByTagName('h2')[0].innerHTML=dateFormat(dttm,'mmm',true).toUpperCase();
          newTxtNode.getElementsByTagName('h3')[0].innerHTML=dateFormat(dttm,'dd',true);
          newTxtNode.setAttribute("id","day"+bldday);
          _$('txtRow').insertBefore(newTxtNode,txtNode);
          bldday = addDay(bldday,-1);
          $('#dayslideBG').animate({'left':'-=62px'},0);
          $('#dayslideTXT').animate({'left':'-=62px'},0);
        }
      }
      //create calendar entries (append newer dates)
      if(mult>0) {
        //need to let js do the date math (adding 86400 doesn't work on daylight savings time days)
        var bldday = addDay(lasttime,1);
        lasttime = addDay(lasttime,mult);

        while (bldday <= parseInt(lasttime)) {
          var bgNode = _$('bgRow').firstChild; 
          var newBgNode = bgNode.cloneNode(true);
          _$('bgRow').appendChild(newBgNode);
          var txtNode = _$('txtRow').firstChild; 
          var newTxtNode = txtNode.cloneNode(true);
          var dttm = new Date(bldday*1000);
          newTxtNode.getElementsByTagName('a')[0].setAttribute('onclick','shiftdays('+bldday+');');
          newTxtNode.getElementsByTagName('a')[0].setAttribute('title',dateFormat(dttm,'mmm').toUpperCase()+" "+dateFormat(dttm,'dd'));
          newTxtNode.getElementsByTagName('h1')[0].innerHTML=dateFormat(dttm,'ddd').toUpperCase();
          newTxtNode.getElementsByTagName('h2')[0].innerHTML=dateFormat(dttm,'mmm').toUpperCase();
          newTxtNode.getElementsByTagName('h3')[0].innerHTML=dateFormat(dttm,'dd');
          newTxtNode.setAttribute("id","day"+bldday);
          _$('txtRow').appendChild(newTxtNode);
          bldday = addDay(bldday,1);
        }
      }
    }
    $('#dayslideBG').animate({'left':'+='+(mult*62*-1)+'px'},300);
    $('#dayslideTXT').animate({'left':'+='+(mult*62*-1)+'px'},300, function() { 
      curtime = addDay(curtime,mult);
      url = '/index.html?a=1&day=1&t='+dateFormat(curtime*1000,'yyyy-mm-dd');
      setCalDate(curtime);
      processAJAX(url,shiftdaysCB,false);
      shifting=false;
      if(shiftto.length>0) {
        shiftdays(shiftto);
        shiftto="";
      }
    });

  } else {
    shiftto = newday;
  }
} //}}}
function shiftdaysCB(txt) { //{{{
  x = xml(txt);
  fadeReplace('events',x.getElementsByTagName("day")[0].childNodes[0].nodeValue);
} //}}}
function shiftmonths(newmonth) { //{{{
  monArr = new Array ('','JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
  if(!shifting) {
    shifting=true;
    //check to see if we have the calendar entry already created, if not prepend or append as needed
    var lowMon = addMonth(newmonth,-3);
    var highMon = addMonth(newmonth,3);

    var mult=diffMonth(newmonth,curMon);
    if((lowMon < firstMon) || (highMon > lastMon)) {
      //create calendar entries (prepend older dates)
      if(mult<0) {
        var bldMon = addMonth(firstMon,-1);
        firstMon = addMonth(firstMon,mult);

        while (parseInt(bldMon,10) >= parseInt(firstMon,10)) {
          var bgNode = _$('bgRow').firstChild; 
          var newBgNode = bgNode.cloneNode(true);
          _$('bgRow').insertBefore(newBgNode,bgNode);
          var txtNode = _$('txtRow').firstChild; 
          var newTxtNode = txtNode.cloneNode(true);
          var mm = parseInt(bldMon.substr(4,2),10);
          newTxtNode.getElementsByTagName('a')[0].setAttribute('onclick','shiftmonths('+bldMon+');');
          newTxtNode.getElementsByTagName('a')[0].setAttribute('title',monArr[mm]);
          newTxtNode.getElementsByTagName('h1')[0].innerHTML="<br>"+monArr[mm];
          newTxtNode.setAttribute("id","mon"+bldMon);
          _$('txtRow').insertBefore(newTxtNode,txtNode);
          bldMon = addMonth(bldMon,-1);
          $('#dayslideBG').animate({'left':'-=62px'},0);
          $('#dayslideTXT').animate({'left':'-=62px'},0);
        }
      }
      //create calendar entries (append newer dates)
      if(mult>0) {
        var bldMon = addMonth(lastMon,1);
        lastMon = addMonth(lastMon,mult);

        while (parseInt(bldMon,10) <= parseInt(lastMon,10)) {
          var bgNode = _$('bgRow').firstChild; 
          var newBgNode = bgNode.cloneNode(true);
          _$('bgRow').appendChild(newBgNode);
          var txtNode = _$('txtRow').firstChild; 
          var newTxtNode = txtNode.cloneNode(true);
          var yyyy = parseInt(bldMon.substr(0,4),10);
          var mm = parseInt(bldMon.substr(4,2),10);
          newTxtNode.getElementsByTagName('a')[0].setAttribute('onclick','shiftmonths('+bldMon+');');
          newTxtNode.getElementsByTagName('a')[0].setAttribute('title',monArr[mm]);
          newTxtNode.getElementsByTagName('h1')[0].innerHTML="<br>"+monArr[mm];
          newTxtNode.setAttribute("id","mon"+bldMon);
          _$('txtRow').appendChild(newTxtNode);
          bldMon = addMonth(bldMon,1);
        }
      }
    }
    $('#dayslideBG').animate({'left':'+='+(mult*62*-1)+'px'},300);
    $('#dayslideTXT').animate({'left':'+='+(mult*62*-1)+'px'},300, function() { 
      curMon = addMonth(curMon,mult);
      url = '/calendar.html?a=1&mon=1&t='+curMon;
//      setCalDate(curtime);
      processAJAX(url,shiftdaysCB,true);
      shifting=false;
      if(shiftto.length>0) {
        shiftdays(shiftto);
        shiftto="";
      }
    });

  } else {
    shiftto = newmonth;
  }
} //}}}
function setCalDate(dt) { //{{{
  d = new Date(parseInt(dt*1000));
  curday = d.getDay();
  $('#datepicker').datepicker('hide');
  $('#datepicker').datepicker('setDate',d);
} //}}}
function diffMonth(mon1, mon2) { //{{{
  var mult=1;
  if(mon1 < mon2) {
    var tmpMon2 = mon2;
    mon2 = mon1;
    mon1 = tmpMon2;
    mult = -1;
  }
  var diff = 0;
  while(mon2 < mon1) {
    mon2 = addMonth(mon2,1);
    diff++;
  }
  return (diff*mult);
} //}}}
function xml(txt) { //{{{
  if(window.DOMParser) {
    parser=new DOMParser();
    return parser.parseFromString(txt,"text/xml");
  }

  x=new ActiveXObject("Microsoft.XMLDOM");
  x.async="false";
  x.loadXML(txt);
  return x;
} //}}}
function addDay(tT,days) { //{{{
  var dt = new Date(tT*1000);
  dt.setDate(dt.getDate()+days);
  return (dt.getTime()/1000);
} //}}}
function addMonth(ym,months) { //{{{
  ym = ym + '';
  var yyyy = parseInt(ym.substr(0,4),10);
  var mm = parseInt(ym.substr(4,2),10);
  mm += months;
  if(mm<=0) {
    mm += 12;
    yyyy--;
  }
  else if (mm > 12) {
    mm = mm % 12;
    yyyy++;
  }
  return yyyy+pad(mm,2,'0'); 
} //}}}
function formatDate(tT) { //{{{
  var dt = new Date(tT*1000);
  var yyyy = dt.getYear()+1900;
  var mm = dt.getMonth()+1;
  if((""+mm).length==1) { mm = '0'+mm; }
  var dd = dt.getDate();
  if((""+dd).length==1) { dd = '0'+dd; }
  return (yyyy+'-'+mm+'-'+dd);
} //}}}
function cdpick(i) { //{{{
  $('#slide').animate({marginLeft:  String(-343 * i) },200);
} //}}}


