/**
 * @author Andrew Welch
 */
// This script controls expanding/collapsing lists.

function blocking(nr)
{
	if (document.all)
	{
		current = (document.all[nr].style.display == 'none') ? 'block' : 'none';
		document.all[nr].style.display = current;
	}
	else if (document.getElementById)
	{
		vista = (document.getElementById(nr).style.display == 'none') ? 'block' : 'none';
		document.getElementById(nr).style.display = vista;
	}
}

// ------------------------------------------------------------------------
// This script changes the style of the navigation tabs
// based on the parent document's <body> id.

function switchTab() 
{
var parentBodyId = parent.document.getElementsByTagName('body').item(0).id;
  return parentBodyId;
}

// -------------------------------------------------------------------
// Ajax XML Ticker (txt file source)
// Author: Dynamic Drive (dynamicdrive-dot-com)
// -------------------------------------------------------------------
//////////// No need to edit beyond here //////////////
function createAjaxObj(){
var httprequest = false;
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try {
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
			}
		}
	}
return httprequest;
}

// -------------------------------------------------------------------
// Main Ajax Ticker Object function
// ajax_ticker(xmlfile, divId, divClass, delay, optionalfadeornot)
// -------------------------------------------------------------------

function ajax_ticker(xmlfile, divId, divClass, delay, fadeornot){
this.xmlfile=xmlfile //Variable pointing to the local ticker xml file (txt)
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in milliseconds.
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
this.pointer=0
this.opacitystring=(typeof fadeornot!="undefined")? "width: 100%; filter:progid:DXImageTransform.Microsoft.alpha(opacity=100); -moz-opacity: 1" : ""
if (this.opacitystring!="") this.delay+=500 //add 1/2 sec to account for fade effect, if enabled
this.opacitysetting=0.2 //Opacity value when reset. Internal use.
this.messages=[] //Arrays to hold each message of ticker
this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'">Getting events...</div></div>')
this.getXMLfile()
}

// -------------------------------------------------------------------
// getXMLfile()- Use Ajax to fetch xml file (txt)
// -------------------------------------------------------------------

ajax_ticker.prototype.getXMLfile = function() {
	if (this.ajaxobj) {
		var instanceOfTicker = this;
		var url = this.xmlfile+"?bustcache="+new Date().getTime();
		this.ajaxobj.onreadystatechange = function() {
			instanceOfTicker.initialize();
		}
		this.ajaxobj.open('GET', url, true);
		this.ajaxobj.send(null);
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of xml file and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

ajax_ticker.prototype.initialize=function(){ 
if (this.ajaxobj.readyState == 4) { //if request of file completed
  if (this.ajaxobj.status==200 || window.location.href.indexOf("http")==-1) { //if request was successful
  this.contentdiv=document.getElementById(this.tickerid).firstChild //div of inner content that holds the messages
  var xmldata=this.ajaxobj.responseText;
  this.contentdiv.style.display="none";
  this.contentdiv.innerHTML=xmldata;
    if (this.contentdiv.getElementsByTagName("div").length==0){ //if no messages were found
    this.contentdiv.innerHTML="<b>Error</b> loading events!";
    return
    }
  var instanceOfTicker=this
  document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
  document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
//modification by ajw
  var expireDate = getElementsByClassName(document,"*","kids");
  var todayDate = getDate('rev');
//End modification
    if (window.attachEvent)  //Clean up loose references in IE
    window.attachEvent("onunload", function(){instanceOfTicker.contentdiv=instanceOfTicker.ajaxobj=null})
//Cycle through XML object and store each message inside array
      for (var i=0; i<this.contentdiv.getElementsByTagName("div").length; i++){
//modification by ajw
        var expireIdName = this.contentdiv.getElementsByTagName("div")[i].id;
        var activateId = expireIdName.substr(3,10); // reveal only 'activate' date
//		var thisYear = todayDate.slice(6); // isolate today's 4-digit year;
//		var actYear = activateId.slice(6,10); // isolate activation's 4-digit year;
//		var expYear = expireIdName.slice(22); // isolate expiration's 4-digit year;
        var validId = expireIdName.slice(16); // reveal only 'expire' date
        var regExTest = new RegExp("(\\s)*(kids)(\\s)*");
        var theClasses = this.contentdiv.getElementsByTagName("div")[i].className;
//        if (theClasses.match(regExTest) && ((((actYear < thisYear) || (activateId <= todayDate)) && validId >= todayDate) || thisYear < expYear)) {
        if (theClasses.match(regExTest) && activateId <= todayDate && validId >= todayDate) {
//end modification
//        if (regExTest(this.contentdiv.getElementsByTagName("div")[i].className) && activateId <= todayDate && validId >= todayDate) {
//modification by ajw to limit number of chars in the event window
//		var evtConcat;
//			if (this.contentdiv.getElementsByTagName("div")[i].innerHTML.length > 380) {
//			evtConcat = this.contentdiv.getElementsByTagName("div")[i].innerHTML.slice(0,380) + '... <a href="/screens/programs/events.html">more &#187;</a>';
//			}
//			else {
//			evtConcat = this.contentdiv.getElementsByTagName("div")[i].innerHTML;
//			}
//		this.messages[this.messages.length] = evtConcat;
//end modification
        this.messages[this.messages.length]=this.contentdiv.getElementsByTagName("div")[i].innerHTML;
        }
      }
    this.contentdiv.innerHTML="";
    this.contentdiv.style.display="block";
    this.rotatemsg();
    }
  }
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through ticker messages and displays them
// -------------------------------------------------------------------

ajax_ticker.prototype.rotatemsg=function(){
var instanceOfTicker=this
if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
else{ //else, construct item, show and rotate it!
this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
this.contentdiv.innerHTML=this.messages[this.pointer]
this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
this.pointer=(this.pointer<this.messages.length - 1)? this.pointer + 1 : 0
setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container periodically
}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

ajax_ticker.prototype.fadetransition=function(fadetype, timerid){
var contentdiv = this.contentdiv;
if (fadetype == "reset")
this.opacitysetting=0.2;
if (contentdiv.filters && contentdiv.filters[0]){
if (typeof contentdiv.filters[0].opacity=="number") //IE6+
contentdiv.filters[0].opacity=this.opacitysetting*100
else //IE 5.5
contentdiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
}
else if (typeof contentdiv.style.MozOpacity!="undefined" && this.opacitystring!=""){
contentdiv.style.MozOpacity=this.opacitysetting;
}
else
this.opacitysetting=1;
if (fadetype=="up")
this.opacitysetting+=0.1;
if (fadetype=="up" && this.opacitysetting>=1)
clearInterval(this[timerid]);
}

// End of ajax_ticker script.

// --------------------------------------------------------------------
// getElementsByClassName()
// Written by Jonathan Snook, http://www.snook.ca/jonathan
// Add-ons by Robert Nyman, http://www.robertnyman.com
// Can be called multiple ways:
//  Get all 'a' elements with the 'info-links' class within a document:
//    getElementsByClassName(document, "a", "info-links");
//  Get all 'div' elements with a 'col' class within an element with a 'container' id:
//    getElementsByClassName(document.getElementById("container"), "div", "col"); 
//  Get all elements with a 'click-me' class within a document:
//    getElementsByClassName(document, "*", "click-me"); 
// --------------------------------------------------------------------
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
} // End of getElementsByClassName()

// *******************************************************************************
function getDate(dir) {
// Retrieves and formats today's date. If dir="rev", format is yyyy-mm-dd. Otherwise,
// format is mm-dd-yyyy.
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = monthnumber + 1;
   var monthday    = now.getDate();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   if(monthday < 10) { monthday = '0' + monthday; }
   if(monthname < 10) { monthname = '0' + monthname; }
   var dateString;
   if (dir == 'rev') { dateString = year + '-' + monthname + '-' + monthday; }
   else { dateString = monthname + '-' + monthday + '-' + year; }
   return dateString;
} // End of getDate()

function expireThis(strClassName) { //uses the reverse date (2008-01-01)
// Puts getElementsByClassName() and getDate() together
// to activate and expire (ie, show and hide) block-level elements.
var expDate = getElementsByClassName(document,"*",strClassName);
var todayDate = getDate('rev');
	for (i=0; i<expDate.length; i++) { // For element where className == strClassName
		var expIdName = expDate[i].id;
		var expTag = expDate[i].tagName;
		var actId = expIdName.substr(3,10); // reveal only the 'activate' date;
		var validId = expIdName.slice(16); // reveal only the 'expire' date;

// The element id contains the activate date (as the string 'act<date>') and expire
// date (as the string 'exp<date>'). E.g., an element that should activate on Jul. 7, 2006
// and expire on Aug. 2, 2006 would have id="act2006-07-07exp2006-08-02".
// The id's value cannot just be the date string or contain '/', because valid XHTML 
// doesn't allow an id to begin with a numeral.
// If the element's activate date is on or before today AND its expire date is on or 
// after today, change the element's display property to 'block' (unless it's a table row)...
		if (actId <= todayDate && validId >= todayDate) {
			if (expTag == "TR") { expDate[i].style.display = ""; }
			else { expDate[i].style.display = "block"; }
			expDate[i].className.replace(/expireMe/, "expireMe message");
		}
// ...but if the element's activate date is on or before today AND its expire date
// is also on or before today, OR if the activate date is after today, hide the element.
		else if ((actId <= todayDate && validId < todayDate) || (actId > todayDate)) {
			expDate[i].style.display = "none";
		}
	}	
} // End of expireThis()

function getEvent(eventSrc, divID) {
var ajaxOK = createAjaxObj();
	if (!ajaxOK) {}
	else if (ajaxOK) {
		var obj = document.getElementById(divID);
		ajaxOK.open("GET", eventSrc);
		
		ajaxOK.onreadystatechange = function() {
			if (ajaxOK.readyState == 4 &&
			ajaxOK.status == 200) {
				obj.innerHTML = ajaxOK.responseText;
			}
		}
		ajaxOK.send(null);	
	}
}
