var tmrScrollEvent = 0;
var intScrollTimer = 50;
var intAutoRefresh = 60000;
var tmrWindowRefresh;
var blnAutoRefresh = true;
var arrIconList = new Array('fire','medical','hazmat','other');

var ie = (document.all) ? true : false;

/* Function to control the scroll when the user clicks on one of the "buttons" */
function stopScroll() {
	window.clearTimeout(tmrScrollEvent);
}

function scrollDisplayUp() {
	document.getElementById('event_list').scrollTop -= 15;
	tmrScrollEvent = window.setTimeout('scrollDisplayUp()',intScrollTimer);
}

function scrollDisplayDown() {
	document.getElementById('event_list').scrollTop += 15;
	tmrScrollEvent = window.setTimeout('scrollDisplayDown()',intScrollTimer);
}


/* Enable/disable row highlighting on the table */
function highlightOn (objTableRow) {
	if (objTableRow) {
		for ( var intCellIndex = 0; intCellIndex < objTableRow.cells.length; intCellIndex ++ ) {
			objTableRow.cells[intCellIndex].style.backgroundColor = 'lightgreen';
		}
	}
}

function highlightOff (objTableRow) {
	if (objTableRow) {
		for ( var intCellIndex = 0; intCellIndex < objTableRow.cells.length; intCellIndex ++ ) {
			objTableRow.cells[intCellIndex].style.backgroundColor = '';
		}
	}
}	

/* Show or hide a column based on a particular CSS class */
function hideColumn(strColumnClass) {
	strCurrentDisp = getStyleByClass('td','event_header_' + strColumnClass,'display');
	if (strCurrentDisp != 'none') {
		setStyleByClass('td','event_' + strColumnClass,'display','none');
		setStyleByClass('td','event_header_' + strColumnClass,'display','none');
	} else {
		setStyleByClass('td','event_' + strColumnClass,'display','');
		setStyleByClass('td','event_header_' + strColumnClass,'display','');
	}	
}

/* Highlight a specific event type in the list: */
function toggleEventType(strClassToToggle) {
	if ( document.getElementById('icon_' + strClassToToggle).style.visibility == 'visible') {
		// remove filter
		for (var intIndex = 0; intIndex < arrIconList.length; intIndex ++) {
			document.getElementById('icon_' + arrIconList[intIndex]).style.visibility = 'hidden';
			setStyleByClass('tr',arrIconList[intIndex],'display','');
		}
	} else {
		// apply the filter by showing the icon and setting all the OTHER classes
		// display to none:
		for (var intIndex = 0; intIndex < arrIconList.length; intIndex ++) {
			document.getElementById('icon_' + arrIconList[intIndex]).style.visibility = 'hidden';
			setStyleByClass('tr',arrIconList[intIndex],'display','none');
		}
		
		setStyleByClass('tr',strClassToToggle,'display','');
		document.getElementById('icon_' + strClassToToggle).style.visibility = 'visible';
	}
}

/* Turn the auto-refresh on/off */
function toggleRefresh() {
	strIconVisibility = 'visible';
	if (blnAutoRefresh) {
		window.clearTimeout(tmrWindowRefresh);
		document.getElementById('refresh').innerText = 'Auto-refresh has been disabled.';
		strIconVisibility = 'hidden';
	} else {
		document.getElementById('refresh').innerText = 'Refreshing in about ' + (intAutoRefresh / 1000) + ' seconds.'
		tmrWindowRefresh = window.setTimeout(autoRefresh,1000);
	}
	blnAutoRefresh = !blnAutoRefresh;
	document.getElementById('icon_refresh').style.visibility = strIconVisibility;
}

/* Handle the auto-refresh countdown and eventual refresh */
function autoRefresh() {
	intAutoRefresh -= 1000;
	if (intAutoRefresh <= 0 ) { 
		document.getElementById('refresh').innerHTML = '<a href="' + window.location + '">Click here to refresh</a>';
		window.location = window.location 
	} else {
		document.getElementById('refresh').innerText = 'Refreshing in about ' + (intAutoRefresh / 1000) + ' seconds.'
		tmrWindowRefresh = window.setTimeout(autoRefresh,1000);
	}
}

// set a CSS style property for a certain class
function setStyleByClass(t,c,p,v) {
	var elements;
	if (t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	
	for (var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			strNodeName = node.attributes.item(j).nodeName;
			// Opera seems to return an upper case string
			if(strNodeName.toLowerCase() == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

function getStyleByClass(t, c, p) {
	var elements;
	var theStyle = '';
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			// Opera seems to return an upper case string
			strNodeName = node.attributes.item(j).nodeName;
			if( strNodeName.toLowerCase() == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					var theStyle = eval('node.style.' + p);
					if((theStyle != "") && (theStyle != null)) {
						return theStyle;
					}
				}
			}
		}	
	}
	return theStyle;
}
