/*  SCRIPT FOR CHANGING DISPLAY OF PROGRAM 
	WHEN USER CLICKS ON PROGRAM TABS OR LINEAR NAV LINKS */

/* Cross-browser function for getting tab link that was clicked on */
function getLink(e){
	if(window.event && window.event.srcElement){//IE - event object is passed to the window object
		progLink=window.event.srcElement;} 
	if(e && e.target){//DOM-compliant - event object is passed in as 'e'
		progLink=e.target;}
	if(!progLink){
		//tabs won't work so display all programs, hide tabMenu and linear nav, then return
		for (i = 0; i < arProgIDs.length; i++){
			document.getElementById(arProgIDs[i]).style.display='block';}
		document.getElementById('tabMenu').style.display='none';
		for (i = 0; i < arLinearNavLinks.length; i++){
			arLinearNavLinks[i].style.display='none';}
		return progLink='none';}
	return progLink;
}

/* initialise - gets tab links and linear links and adds event listeners for them, set default program and tabMenu display */
function initProgDisplay(){
	/*if functions not supported return without setting up prog display and event listeners */
	if(!document.getElementById || !document.getElementsByTagName){	
		return;
		}
	/* put tab menu links into an array, these represent the href's to each day's program: #sun, #mon, #tue etc. */
	arTabLinks=document.getElementById('tabMenu').getElementsByTagName('a');
	
	/* put corresponding program div ids into an array (in the right order
		so that index numbers correspond to arTabLinks index numbers) */
	arProgIDs = new Array('prog_sun','prog_mon','prog_tue','prog_wed','prog_thu');

	/* put linear nav links into an array by getting array of all links inside the program_container div, 
	   then looping to get the ones with rel='linearNav' */
	arLinksAll=document.getElementById('program_container').getElementsByTagName('a');
	arLinearNavLinks=new Array();
	for(i=0; i < arLinksAll.length; i++){
		/* if the link is a linear nav link, add it to the linear nav links array */
		if(arLinksAll[i].getAttribute('rel')=='linearNav'){
			arLinearNavLinks[arLinearNavLinks.length]=arLinksAll[i];
		}	
	}
	
	/* loop to add an event listener for each tab link */
	for(i = 0; i < arTabLinks.length; i++){
		addEvent(arTabLinks[i],'click',evtListen_handleLink,false);}
	/* loop to add an event listener for each LinearNav link */
	for(i = 0; i < arLinearNavLinks.length; i++){
		addEvent(arLinearNavLinks[i],'click',evtListen_handleLink,false);}	
	
	/* set index to use for setting display for programs and tabMenu 
		0=Sunday, 1=Monday and so on */
	var dayIndexForArrays = 0;
	
	/*  TO OPEN THE PROGRAM AT A PARTICULAR DAY, PASS THE "day" PARAMETER IN THE QUERY STRING
		e.g. make a link to the page like "program/scientific_times.cfm?day=tue" to load the page with Tuesday displayed
		
		if URL contains query variable 'day', change the display to that day */
	var URL_day = getURLqueryValue('day');
	if(URL_day != null){
		switch (URL_day){
			case "sun": dayIndexForArrays=0;
			break;
			case "mon": dayIndexForArrays=1;
			break;
			case "tue": dayIndexForArrays=2;
			break;
			case "wed": dayIndexForArrays=3;
			break;
			case "thu": dayIndexForArrays=4;
			break;
			}
	}
	
	/* set display for programs and tabMenu  */
	showHideProgs(arProgIDs[dayIndexForArrays]);
	tabMenuDisplay(dayIndexForArrays);
	
	/* turn on display of all progLinearNav div's (hidden by default in CSS file in case no javascript)
	  - walk up from the DOM tree from the linear nav links to the containing progLinearNav div, then display it */
	for(i = 0; i < arLinearNavLinks.length; i++){
	  	var thisNode=arLinearNavLinks[i];
		while(thisNode.className != 'progLinearNav'){
	 		thisNode=thisNode.parentNode;
			//return if body tag reached
			if(thisNode.nodeName.toLowerCase() == 'body'){
				return;}
		}
		if(thisNode.className == 'progLinearNav'){
			thisNode.style.display='block';}
	}
	
	/* add event listener for 'open print friendly' link */
	addEvent(document.getElementById('printProgram'),'click',openPrintWin,false);
	
}
/* Event listener - display relevant program on clicking link */
function evtListen_handleLink(e){
	var progLink = getLink(e);
	if(progLink=='none'){
		return;}
	
	/*some browsers pass the link text instead of the 'a' element, 
		so move up the DOM tree until the 'a' element is found, don't go past the 'body' container*/
	while(progLink.nodeName.toLowerCase() != 'a' && progLink.nodeName.toLowerCase() != 'body'){
		progLink=progLink.parentNode;
		//prevent accidental infinite loop by returning if body tag reached
		if(progLink.nodeName.toLowerCase() == 'body'){
			return;}
	}	
		
	/*loop to match link clicked (progLink) with the right program div id, then call showHideProgs() and tabMenuDisplay()
		(index numbers of arTabLinks correspond to index numbers of arProgIDs 
		so that the links href's correspond to the div id's of the programs) */
	for (i = 0; i < arTabLinks.length; i++){
		if(progLink.href==arTabLinks[i].href){
			showHideProgs(arProgIDs[i]);//pass the corresponding program div id (e.g. 'prog_sun', 'prog_mon' etc.)
			tabMenuDisplay(i);//pass the corresponding index number of arTabLinks
			scrollToProgTop();
		}
	}
	
	//remove IE dotted outline from the tab link clicked
	progLink.blur();
	//don't return control to the link clicked or window will jump to the anchor tag
	cancelClick(e);
	return false;
}
function showHideProgs(prog_id){
	//loop to hide all progs
	for (j = 0; j < arProgIDs.length; j++){
		document.getElementById(arProgIDs[j]).style.display='none';}
		 
	//display the selected prog
	document.getElementById(prog_id).style.display='block';
}
function tabMenuDisplay(tabLinkindexNum){
	//loop all tab links to style them as 'off'
	for(k = 0; k < arTabLinks.length; k++){
		arTabLinks[k].className ='tabOFF';
	}
	//style selected tab link as 'on'
	arTabLinks[tabLinkindexNum].className = 'tabON';
}
function scrollToProgTop(){
	window.scrollTo(0, 400);
}

addEvent(window, 'load', initProgDisplay, false);


/* open printer window */
function openPrintWin(e) 
{ 
	var sOption="width=750,height=600,left=100,top=25,menubar=yes,"; 
		 sOption+="toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes";  
	var url="print.cfm";
	winprint=window.open(url,"Print",sOption); 
	winprint.focus();
	cancelClick(e);
	return false;
}
