/** This file reads the XML file from  marqueeData.xml and finds out what marquee to display, if any.  If a conflict arises, the last made marquee will be used.
 * IMPORTANT: Requires user to import AJAXutility.js before running the file! */

var DEFAULT_MESSAGE = "Have a great day, Mustangs!";  // what we display if no marquee text is available.
//var MARQUEE_FILE = "marqueeData.xml";                 // the file in which we can find the data for the marquee.
var MARQUEE_FILE = "xml/marqueeData.xml";                 // the file in which we can find the data for the marquee.
var xmlDoc = getActiveObject();			                  // The xml document of the marquee files
var numTraversals = 0;
var on = -1;
var marquees = new Array();

var dayArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);	// date numbers
if(new Date().getFullYear() % 4 == 0) // leap year
{
	dayArray[1] ++;
}

loadXML(xmlDoc, MARQUEE_FILE);

/******* Classes *******/

/** Creates a new Marquee object. */
function Marquee() {
	this.startDay = 0;      // the starting day in relation to 0/0/00
	this.endDay = 0;        // the ending (last) day in relation to 0/0/00
	this.startDayStr = 0;   // the starting day in text form [D]D/[M]M/[[Y]Y]
	this.endDayStr = 0;     // the ending (last) day in text form [D]D/[M]M/[[Y]Y]
	this.text = "";
	
	this.setStartDate = setStartDate;
	this.setEndDate = setEndDate;
	this.setText = setText
	this.add = add;
}
function setStartDate(startDate)
{
 this.startDayStr = startDate;
 this.startDay = evaluateDate(startDate);
}
function setEndDate(endDate)
{
 this.endDayStr = endDate;
 this.endDay = evaluateDate(endDate);
}
function setText(txt)
{
 this.text = txt;
}
/** Adds the data based on what we already have (convenience function) */
function add(data)
{
 if(this.startDay == 0)
  this.setStartDate(data);
 else if(this.endDay == 0)
  this.setEndDate(data);
 else
  this.setText(data);
}

/** Returns the date from [M]M/[D]D/[Y]Y form into just days from 0/0/0, as a standard unit. 
 * Also supported: [M]M/[D]D form, where YY is assumed to be the current year.
 * Also supported: No dateStr. MM/DD/YY will be gotten from a new Date object.  */ 
function evaluateDate(dateStr)
{
  var numDays = 0;
  var day, month, year;
  
  if(dateStr != null)
  {
    // We have some date string. 
    month = parseInt(strip(dateStr.substring(0, dateStr.indexOf("/"))));
    if(dateStr.indexOf("/") < dateStr.lastIndexOf("/"))            // we have MM/DD/YY
    {
      day = parseInt(strip(dateStr.substring(dateStr.indexOf("/")+1, dateStr.lastIndexOf("/"))));
      year = parseInt(strip(dateStr.substring(dateStr.lastIndexOf("/")+1)));
    }
    else                                                           // we have MM/DD (assume YY)
    {
      day = parseInt(strip(dateStr.substring(dateStr.indexOf("/")+1)));
      year = new Date().getFullYear() - 2000;                          // year is given in terms of from 0CE.
    }
  }
  else                                                             // we have nothing (assumg MM/DD/YY)
  {
    var today = new Date();
    day = today.getDate();
    month = today.getMonth() + 1;
    year = today.getFullYear() - 2000;
  }
  
  // Add days
  numDays += day;
  
  // Add months
  for(var i = 0; i < month-1; i ++)
  {
   numDays += dayArray[i];
  }
  
  // Add years
  for(var i = 0; i < year-1; i ++)
  {
   numDays += 365;
   if(i % 4 == 0)
   {
    numDays += 1;
   }
  }
  
  return numDays;
}

/******* Functions *******/
/** Run at each step of loading the file. readyState becomes 4 when it is finished. */
function verify()
{
  if(xmlDoc.readyState!=4)
		return false;
  else {
    	if(!isIE())
  	{
  		var parser = new DOMParser();
  		xmlDoc = parser.parseFromString(xmlDoc.responseText, "text/xml");
  
  		var doc=xmlDoc.documentElement;
     		traverseFF(doc);
  	}
  	else
  	{
  		var doc=xmlDoc.documentElement;
	        traverseIE(doc);
  	}
  	
  	var dayNum = evaluateDate();
	var marqText = "";
  	for(var i = 0; i < marquees.length; i ++)
  	{
  	   if(marquees[i].startDay <= dayNum && marquees[i].endDay >= dayNum)
  	   {
		marqText += marquees[i].text + " | ";
 	   }
  	}

  	
  	if(marqText != "")
    	{
	   marqText = marqText.substring(0, marqText.length - 2);
    	   marquee2.innerHTML = '<marquee align="middle" style="color: #008000; font-weight: bold; font-family: Arial; font-size: 14pt" scrollamount="4" scrolldelay="1">' + marqText + "</marquee>";
    	   marquee1.innerHTML = "";
   	}
  }
}

/** Traverses the specified XML tree for Firefox and related browsers. */
function traverseIE(tree) {
	var i;
	var firstTraversal = ( numTraversals > 0  ?  false  :  true );
	numTraversals ++;

	if(tree.hasChildNodes()) {
		prevTagName = tree.tagName;

		for(i = 0; i < tree.childNodes.length; i ++)
			traverseIE(tree.childNodes(i));
	} else {
		separate(prevTagName, tree.text);
	}
}

/** Traverses the specified XML tree for Firefox and related browsers. */
/*function traverseFF(tree) {
	var i;
	var firstTraversal = ( numTraversals > 0  ?  false  :  true );
	numTraversals ++;

	if(tree.hasChildNodes()) {
		prevTagName = tree.tagName;

		for(i = 0; i < tree.childNodes.length; i ++)
			traverseIE(tree.childNodes(i));
	} else {
		separate(prevTagName, tree.text);
	}
}*/


/** Traverses the specified XML tree for Internet Explorer. */
function traverseFF(tree) {
	var i;
	var firstTraversal = ( numTraversals > 0  ?  false  :  true );
	numTraversals ++;
	if(tree.childNodes.length > 0) {
		prevTagName = tree.tagName;

//		for(i = (tree.childNodes.length == 1 ? 0 : 1); i < tree.childNodes.length; i ++)
		for(i = (tree.childNodes.length == 1 ? 0 : 0); i < tree.childNodes.length; i ++)
		{
			
			//if(!tree.childNodes[i].hasChildNodes() && tree.childNodes[i].nodeValue == null)
				//continue;

			if(tree.childNodes[i].hasChildNodes())
				prevValue = tree.childNodes[i].childNodes[0].nodeValue;

			traverseFF(tree.childNodes[i]);

			/*
			if(i % 2 == 1)
				i += 1;
			else
				i += 0;
			*/
		}
	} else if(prevValue != null) {
		separate(prevTagName, prevValue);
		prevValue = null;
	}
}

/** Separates data based on the tag name and the data contained therein.
 *
 * @param tagName - String containing the tag in which the data is found.
 * @param data    - Data contained inside the tag.
 */
function separate(tagName, data)
{
	on = (++on) % 3;		// Here we hope that all teachers have the same amount of variables...

		if(on == 0){
		  //alert(data);
			marquees[marquees.length] = new Marquee();
		}
		
		marquees[marquees.length-1].add(strip(data));	// add the new data, but be sure to have stipped from it all spaces preceding and succeeding it.
}
