// calendarOperations.js
// calendar manipulations

function clearEmptyRow() {  // if no dates on month occupy the sixth table row, remove it
  var dateTable = document.getElementById("cal");
  var dateCells = dateTable.getElementsByTagName("td");
  var checkForEmptyCell=dateCells[35].getElementsByTagName("a");   
  if (checkForEmptyCell.length==0) {  // if cell 35 is empty, so is week 6
    var tableRows = dateTable.getElementsByTagName("tr");
    var parent = tableRows[5].parentNode;
    var removeRow = parent.removeChild(tableRows[5]);
  }  
  checkForEmptyCell=dateCells[6].getElementsByTagName("a");
  if (checkForEmptyCell.length==0) {  // if cell 6 is empty, so is week 1
    var tableRows = dateTable.getElementsByTagName("tr");
    var parent = tableRows[0].parentNode;
    var removeRow = parent.removeChild(tableRows[0]);
  }  
}

function shortenLongEntries () {  // shorten long entries to fit into calendar cell
  var dateTable = document.getElementById("cal");
  var dateCells = dateTable.getElementsByTagName("td");
  for(i=0; i<dateCells.length; i++) {
    var checkForEvent = dateCells[i].getElementsByTagName("a");
    if(checkForEvent[0]) {
      if(checkForEvent[0].firstChild) {
        cellHeight = dateCells[0].offsetHeight;
        dateElement = dateCells[i].getElementsByTagName("p");
        totalHeight=0;
        for(j=0; j<dateElement.length; j++) { 
          totalHeight += dateElement[j].offsetHeight;
        }
        if(totalHeight > 100) {
            pElements = dateCells[i].getElementsByTagName("a");
            newNode = document.createElement("p");
            newNode.setAttribute("class", "more");
            newNodeText = document.createTextNode("(more info)");
            newNode.appendChild(newNodeText);
            pElements[0].appendChild(newNode);
            totalHeight += pElements[0].lastChild.offsetHeight;
          while (totalHeight > 100) {
            lastElement = pElements[0].lastChild;
            childToRemove = lastElement.previousSibling;
            pElementHeight = childToRemove.offsetHeight;
            totalHeight -= pElementHeight;
            pElements[0].removeChild(childToRemove);
          }
        }
      }
    }
  }
}


function addOnClickHandler () {  // find date cells with events and attach onclick handler to them
  var dateTable = document.getElementById("cal");
  var dateCells = dateTable.getElementsByTagName("td");
  for(i=0; i<dateCells.length; i++) {
    var checkForEvent = dateCells[i].getElementsByTagName("a");
    if(checkForEvent[0]) {
      if(checkForEvent[0].firstChild) {
        var div = checkForEvent[0].parentNode; // get the div this eventID falls under
        var td = div.parentNode; // attach onclick event to this variable
        td.onclick=function() {
          attachOnClick(this);
        }
        
        td.onmouseover=function() { // set color here [use CSS mouseover class]
          
        }
        td.onmouseout=function() { // set color here [use CSS mouseover class]
          //alert("mouse out");    // set cursor as well
        }
      }
    }
  }
}

function attachOnClick(element) { // onclick handler to attach to date cells with events
   var getEventID = element.getElementsByTagName("a");
   if(getEventID[0].firstChild.nextSibling.firstChild) {
     eventNumber = getEventID[0].firstChild.nextSibling.firstChild.nodeValue;
   

     new Ajax.Request(
       "../scripts/calendarDetail.php?inputText=" + eventNumber,
       {
         method:"get",
         onComplete:function(xhr) {               
           var response = xhr.responseText;
           var outputDiv = document.getElementById("output");
           clearOutputDiv();        
           var calendarDetailWindow = document.createElement("div");
           calendarDetailWindow.setAttribute("id", "response");   
           calendarDetailWindow.innerHTML = response;
           outputDiv.appendChild(calendarDetailWindow); 
           attachCalendarDetailWindowControls();
         }       
       }
     );
   }
}

function clearOutputDiv () {
  var outputDiv = document.getElementById("output");
  while (outputDiv.firstChild){ // clear outputDiv if there is already data there
    var childToRemove = outputDiv.firstChild;
    outputDiv.removeChild(childToRemove); 
  }
}

function attachCalendarDetailWindowControls () {
  var closeControl = document.getElementById("close");
  closeControl.onclick=function() {
    clearOutputDiv();
  }
  var printControl = document.getElementById("print");
  printControl.onclick=function() {
    window.print();
  }
}



