/*
 *
 *	index.js
 *	Javascript for /calendar/index.php
 *	Created by Ian White
 *	Copyright 2008.
 *
 */
 
var currentHighlight = null;
var indicatorInterval = null;

var fadeQ = new Array();
var fadeA;


/*
 * init()
 *
 * This function adjusts the sizes of the cells so that the calendar is drawn to take
 * up the entire window. It also adds event handlers to the catagory names and calendar
 * names in the left-hand menus.
 *
 */
function init() {

	tables = document.getElementsByTagName('table');
	
	tables[0].style.width = window.innerWidth + 'px';
	tables[0].style.height = window.innerHeight + 'px';
	
	tables[0].rows[0].cells[0].style.height = (window.innerHeight - 151) + 'px';
	if(tables[0].rows[0].cells[0].clientWidth < 220) {
		tables[0].rows[0].cells[0].style.width = '220px'
	}
	
	inputs = document.getElementsByTagName('input');
	
	for(i=0; i < inputs.length; i++) {
		input = inputs[i];
		if(input.type == 'checkbox') {
			if (input.addEventListener){
			  input.parentNode.addEventListener('click', highlightCalendar, false);
			  input.parentNode.childNodes[1].addEventListener('click', highlightCalendar, false);
			  input.addEventListener('click', highlightCalendar, false);
			} else if (input.attachEvent){
			  input.parentNode.attachEvent('onclick', highlightCalendar);
			  input.parentNode.childNodes[1].attachEvent('onclick', highlightCalendar);
			  input.attachEvent('onclick', highlightCalendar);
			}
		}
		
	}
	
	links = document.getElementsByTagName('a');

	for(i=0; i < links.length; i++) {
		link = links[i];
		if(link.className == 'catagory') {
			if (link.addEventListener){
			  link.addEventListener('click', toggleCatagory, false);
			} else if (input.attachEvent){
			  link.attachEvent('onclick', toggleCatagory);
			}
		}
		
	}
	
}


/*
 * highlightCalendar()
 * arg: e - click event
 *
 * This function applies the highlighting to a calendar name when a user clicks on it.
 * It also makes sure that only one calendar is highlighted at a time.
 *
 */
function highlightCalendar(e) {
	e      = e || window.event;
	target = e.target || e.srcElement;

	if(currentHighlight) {
		currentHighlight.style.backgroundColor = '#EFEFEF';
		currentHighlight.style.backgroundImage = '';
		currentHighlight.style.borderTop = '1px solid #EFEFEF';
		currentHighlight.style.borderBottom = '1px solid #EFEFEF';
		if(fadeA) {
			clearInterval(fadeA.timer);
			fadeA = null;
			currentHighlight.childNodes[1].style.opacity = '1';
		}
	}
	
	if(target.tagName == 'INPUT' || target.tagName == 'SPAN') {
		target = target.parentNode;
	}
	
	
	target.style.backgroundColor = '#93b7db';
	target.style.borderTop = '1px solid #6699CC';
	target.style.borderBottom = '1px solid #6699CC';
	target.style.backgroundImage = 'url(http://www.ianowhite.com/calendar/images/highlighted.png)';

	currentHighlight = target;
	window.getSelection().removeAllRanges();
	
	fadeA = new Object();
	fadeA.pane = target.childNodes[1];
	fadeA.fadein = false;
	fadeA.end = 0.7;
	fadeA.increment = 0.1;
	fadeA.current = 1;
	//fadeA.timer = setInterval("fadeContainer()", 100);
	
	
	
}

/*
 * toggleCatagory()
 * arg: e - click event
 *
 * This function opens and closes the calendar catagories as the user clicks on
 * a catagory name. It also rotates the triangle next to the catagory name to
 * indicate whether the catagory is open or closed.
 *
 */
function toggleCatagory(e) {
	e      = e || window.event;
	target = e.target || e.srcElement;
	
	if(target.rel == 'closed') {
		target.parentNode.style.height = '';
		target.style.backgroundImage = "url(http://www.ianowhite.com/calendar/images/arrow_open.png)";
		target.rel = 'open';
	}
	else if (target.rel == 'open') {
		target.parentNode.style.height = '0.7em';
		target.style.backgroundImage = "url(http://www.ianowhite.com/calendar/images/arrow_closed2.png)";
		target.rel = 'closed';
	}

}


if (window.addEventListener){
	window.addEventListener('load', init, false); 
} else if (window.attachEvent){
	alert('Sorry! The Coder Javascript Demo is only available for Firefox (v1.5 and above)     \nand Safari (v2 and above). Your browser is not supported.');
	document.location.href= 'http://www.ianowhite.com';
}

