
function showEvent(link)
{
  document.location.href = link;
}
var selTab = '';

function tabToggle(e)
{
  if (e.id == selTab) return;
  if (e.className == 'calInactive')
  {
    e.className = 'calActive';
    //e.style.background = 'url(images/bckgHov.gif)';
  }
  else
  {
    e.className = 'calInactive';
    //e.style.background = 'url(images/bckgNorm.gif)';
  }
}

function tabClick(link)
{
  if (link.length > 0)
    document.location.href = link;
}

function createDiv(id, text, link, color)
{
  var sty = "";
  if (color) sty = 'style="border:1px solid ' + color + '; color:' + color + '; "';

  // Standards Compliant code fork...
  if (document.getElementById)
  {
    var div                   = document.createElement("DIV");
    div.id = id;
   	div.className = 'calEvent tooltipAnchor';
    div.style.border = '1px solid #000000';
    //if (document.getElementById('ctMainPaneInner')) {
    div.style.position = 'absolute';
    if (color) { div.style.color = color; }

    XBrowserAddHandler(div, "click", function() {showEvent(link)});

    var txt     = document.createTextNode(text);
    div.appendChild(txt);

    if (document.getElementById('ctMainPaneInner')) {
    	document.getElementById('ctMainPaneInner').appendChild(div);
    }
    else if (document.getElementById('contentInner')) {
    	document.getElementById('contentInner').appendChild(div);
    }
    else {
    	document.body.appendChild(div);
    }
    
  }

  // IE 4/5 code fork...
  else if (document.all)
  {
    document.body.insertAdjacentHTML("beforeEnd", "<div id='" + id + "' class='calEvent' title='" + text + "'><\/div>");
    XBrowserAddHandler(document.body[id], "click", function() {showEvent(link)});
  }

  // Nav 4.x code fork..
  else if (document.layers)
  {
    document.layers[id] = new Layer(100);
     document.layers[id].document.write(text);
     XBrowserAddHandler(document.layers[id], "click", function() {showEvent(link)});
  }

  // other browsers
  else
  {

  }
 }

// Event Handling
function XBrowserAddHandler(target, eventName, fnHandler)
{
  var originalHandler = target["on" + eventName];
  if ( originalHandler )
  {
    // note that we have just created a memory leak in IE because the closures from
    // these two anonymous functions reference "target", an HTML element. nuts :(
    target["on" + eventName] = function(e)
      {
        XBrowserApplyHandler(this, originalHandler, e);
        XBrowserApplyHandler(this, fnHandler, e);
      };
  }
  else
  {
    target["on" + eventName] = function(e)
      {
        XBrowserApplyHandler(this, fnHandler, e)
      };
  }
}

function XBrowserApplyHandler(target, fn, e)
{
  if (!e) e = window.event;
  if (!e) alert("Problem with XBrowserApplyHandler: could not find event object.");

  if (Function.prototype.call)
  {
    fn.call(target, e);
  }
  else
  {
    target.__XBrowserElementApply = fn;
    target.__XBrowserElementApply(e)
  }
}

isIE = (document.all ? true : false);
isDOM = (document.getElementById ? true : false);
var appVer = navigator.userAgent.toLowerCase();
var isKHTML = (appVer.indexOf("khtml") != -1);

// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
function getAbsX(elt) { return (elt.x && !isKHTML) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y && !isKHTML) ? elt.y : getAbsPos(elt, "Top"); }
function getAbsPos(elt,which)
{
   iPos = 0;
   while (elt != null && elt.id !== 'ctMainPaneInner' && elt.id !== 'contentInner')
   {
      iPos += elt["offset" + which];
      elt = elt.offsetParent;
   }
   return iPos;
}

function getDiv(divName)
{
  var obj;
   if (isDOM) { obj = document.getElementById(divName) }
   else { obj = isIE ? document.all[divName] : document.layers[divName]; } // NS4
   return obj;
}

function getDivStyle(divName)
{
   var style;
   if (isDOM) { style = document.getElementById(divName).style; }
   else { style = isIE ? document.all[divName].style : document.layers[divName]; } // NS4
   return style;
}

function hideElement(divname)
{
   getDivStyle(divname).visibility = 'hidden';
}


// ================================================================================================
// calCells
var arCalCell = [];

// Cell Constructor
function calCell(id, pos, height)
{
  this.id      = id;
  this.offset = pos;
  this.height = height;
}

function CalCellTopOffset(id)
{
  for (var i=0; i < arCalCell.length; i++)
  {
    if (arCalCell[i].id == id) { return  arCalCell[i].offset; }
  }
  return 0;
}

function setCalCellSize(id, height, bforce)
{
  for (var i=0; i < arCalCell.length; i++)
  {
    if (arCalCell[i].id == id)
    {
      if (bforce)
      {
        arCalCell[i].height = height;
      }
      else
      {
        arCalCell[i].height += height;
      }
      return arCalCell[i].height;
    }
  }
  arCalCell.push(new calCell(id, 0, height));
  return height;
}

function setCalCellTopOffset(id, offset)
{
  for (var i=0; i < arCalCell.length; i++)
  {
    if (arCalCell[i].id == id)
    {
      if (arCalCell[i].offset < offset) arCalCell[i].offset = offset;
      return;
    }
  }
  arCalCell.push(new calCell(id, offset, 0));
}

function getCalCellTopOffset(id, offSet)
{
  var retOffset = 0;

  for (var i=0; i < arCalCell.length; i++)
  {
    if (arCalCell[i].id == id)
    {
      retOffset = arCalCell[i].offset;
      arCalCell[i].offset += offSet;
      return retOffset;
    }
  }

  arCalCell.push(new calCell(id, offSet, 0));
  return retOffset ;
}

function getCell(id)
{
  var oCell;
  if (isIE) { oCell = document.all[id]; }
   else if (isDOM) { oCell= document.getElementById(id); }
  else { oCell = document.images[id]; }
  return oCell;
}

var arDaysInMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
var objQstr = new PageQuery(document.location.search);
var calQsMonth = objQstr.getValue("m") == -1 ? 0 : parseInt(objQstr.getValue("m"));
var calQsYear = objQstr.getValue("y") == -1 ? 0 : parseInt(objQstr.getValue("y"));
if (calQsMonth > 0 && calQsYear > 0) { curDate = new Date(calQsMonth + '/1/' + calQsYear); }
else { curDate = new Date(); }
curDate = curDate.getYear();
if(((curDate % 4 == 0) && (curDate % 100 != 0)) || (curDate % 400 == 0)) arDaysInMonth[2] = 29;

function getNextCell(cellName, intModifier)
{
  var arCellName;
  if (cellName.id) arCellName = cellName.id.split('c');
  else arCellName = cellName.split('c');

  if ( (intModifier > 0) && (arDaysInMonth[arCellName[0]] == arCellName[1]) )
  {
    arCellName[0] = (arCellName[0] == 12) ? 1 : parseInt(arCellName[0]) + 1;
    arCellName[1] = 1
  }
  else if ( (intModifier < 0) && (parseInt(arCellName[1]) == 1) )
  {
    arCellName[0] = (arCellName[0] == 1) ? 12 : parseInt(arCellName[0]) - 1;
    arCellName[1] = arDaysInMonth[arCellName[0]];
  }
  else
  {
     arCellName[1] = parseInt(arCellName[1]) + intModifier;
   }

   return arCellName.join('c');
}

// ================================================================================================
// Event Array
var arEvent = [];

// Event Constructor
function objEvent(divId, link, text, startCell, endCell, color, status, allDay)
{
  this.id            = divId;
  this.link          = link;
  this.text          = text;
  this.startCell    = startCell;
  this.endCell      = endCell;
  this.multipleDay  = (startCell != endCell);
  this.color        = color;
  this.open          = false;
  this.status        = status;
  this.allDay        = allDay == "1" ? true : false;
}

function setEvent(divId, link, text, startCell, endCell, color, status, allDay)
{
  arEvent.push(new objEvent(divId, link, text, startCell, endCell, color, status, allDay));
}

function setupCells()
{
  //Reset all cell heights
  for (var i=0; i < arCalCell.length; i++) { arCalCell[i].height = 0; }

  //Calculate cell heights
   var sCell;
   var eCell;

  for (var i=0; i<arEvent.length; i++)
  {
    sCell = arEvent[i].startCell;
    eCell = arEvent[i].endCell;

    while (!getDiv(sCell))
    {
      sCell = getNextCell(sCell, 1);
      arEvent[i].startCell = sCell;
    }

     while (!getDiv(eCell))
    {
      eCell = getNextCell(eCell, -1);
      arEvent[i].endCell = eCell;
    }

    var intSize = setCalCellSize(sCell, 18);
    var intRowIdx = getRowIndex(sCell);
     while(sCell != eCell)
     {
       sCell = getNextCell(sCell, 1);
       var tmpRowIdx = getRowIndex(sCell);
       if (tmpRowIdx == intRowIdx)
       {
        intSize = setCalCellSize(sCell, intSize, true);
       }
       else
       {
         intRowIdx = tmpRowIdx;
        intSize = setCalCellSize(sCell, 18);
       }
     }
   }

   //Resize Calendar cells if necessary
  var strOut = "";
  var objCell;
  for (var i=0; i<arCalCell.length; i++)
  {
    objCell = getCell(arCalCell[i].id);
    if (objCell)
    {
      if (arCalCell[i].height > (objCell.height - 5)) objCell.height = (arCalCell[i].height + 5);
      strOut += arCalCell[i].id + ": " + arCalCell[i].height + '\n';
    }
  }
}

function getRowIndex(cellID)
{
  oCell = getCell(cellID);
  if (oCell.parentNode.tagName == 'TD')
  {
    return oCell.parentNode.parentNode.rowIndex;
  }
  else { return null; }
}

function resizeCells()
{
  //Reset all cell offsets
  for (var i=0; i < arCalCell.length; i++) { arCalCell[i].offset = 0; }

  for (var i=0; i<arEvent.length; i++)
  {
    setPosition(arEvent[i].id, arEvent[i].link, arEvent[i].text, arEvent[i].startCell, arEvent[i].endCell, arEvent[i].color, arEvent[i].status, false, arEvent[i].allDay);
  }
}

function hideCells()
{
  for (var i=0; i<eventDivs.length; i++) { hideElement(eventDivs[i]); }
}

var intWrapModifier = 0;
function setPosition(divId, link, text, startCell, endCell, color, status, open, allDay)
{
  var bolWrapsToPrev = false;
  var bolWrapsToNext = false;

  var oStart = getCell(startCell);
  var intCnt = 0;
  while (!oStart && intCnt < 10)
  {
    bolWrapsToPrev = true;
    startCell = getNextCell(startCell, 1);
    oStart = getCell(startCell);
    intCnt++;
  }
  var oEnd = getCell(endCell);
  intCnt = 0;
  while (!oEnd && intCnt < 10)
  {
    bolWrapsToNext = true;
    endCell = getNextCell(endCell, -1);
    oEnd = getCell(endCell);
    intCnt++;
  }

  if (!oEnd || !oStart) return;

  if (!getDiv(divId)) createDiv(divId, text, link, color);
  var elt = getDivStyle(divId);

  var intStart           = getAbsX(oStart);
  var intEnd            = getAbsX(oEnd);

   elt.left             = (intStart + 2) + 'px';
   elt.top = (getAbsY(oStart) + getCalCellTopOffset(startCell, 18)) + 'px';

   if (oEnd == oStart && !open)
   {
     if (!allDay)
     {
       elt.backgroundColor = 'transparent';
       elt.border = '0px';
     }
     else
     {
       elt.backgroundImage      = 'url(/images/1ptrans.gif)';
     }
   }

   if (oEnd != oStart && !open)
   {
     elt.backgroundImage      = 'url(/images/1ptrans.gif)';
   }

   if (open || bolWrapsToPrev)
   {
     elt.borderLeft         = '0px';
     elt.backgroundImage      = 'url(/images/1ptrans.gif)';
   }
   if (bolWrapsToNext) elt.borderRight = '0px';

   //Event is wrapping
   if ( (intEnd < intStart) || (getAbsY(oStart) != getAbsY(oEnd)) )
   {

     var newEndCell = endCell;
     while ( (intEnd < intStart) || (getAbsY(oStart) != getAbsY(newEndCell)) )
     {
       newEndCell  = getCell(getNextCell(newEndCell, -1));
       intEnd     = getAbsX(newEndCell);
     }
     intWrapModifier ++;
     setPosition(divId + intWrapModifier, link, text, getNextCell(newEndCell, 1), endCell, color, status, true, allDay);

     oEnd         = newEndCell;
     elt.borderRight   = '0px';
   }

   //Reset the wrap modifier so We don't recreat divs
  intWrapModifier = 0;

   //Calculate offsets for multiple day events
   var newStartCell = startCell;
   var newOffset = CalCellTopOffset(startCell);

   while(newStartCell != oEnd.id)
   {
     newStartCell   = getNextCell(newStartCell, 1);
    setCalCellTopOffset(newStartCell, newOffset);
   }

   elt.width     = ((intEnd + oEnd.width) - intStart - 12) + 'px';
   elt.visibility   = 'visible';

   if (status == 'confirmed')
   {
     elt.backgroundImage  = 'url(/cal2/images/check.gif)';
     elt.paddingLeft = '9px';
     elt.backgroundColor = 'lemonChiffon';
   }
}

var bolLoaded = false;
function setCells() {
  createEvents();
  setupCells();
  resizeCells();
  bolLoaded = true;
}

