/**
 * Converts a page of content to use text panels.
 * Text panels begin with a H3 element, the content of which is used
 * as the link text for the panel. Panels are terminated by a H2, H3 or SCRIPT
 * element. A script element calling this function should be placed directly
 * after the page content to ensure correct content conversion.
 *
 * @access	public
 */
function loadTextPanels(expanded) {

	if (typeof(expanded) == 'undefined' || expanded == null) {
		expanded = false;
	}

	var h3s = document.getElementsByTagName('H3');
	var sibling = null;
	var panelId = 0;
	for (var i = 0; i < h3s.length; i++) {

		if (h3s[i].innerHTML == '') {
			h3s[i].parentNode.removeChild(h3s[i]);
			continue;
		}

		// Create new container for hidden text.
		var div = document.createElement('DIV');
		div.id = 'textPanel' + panelId;
		div.className = 'textPanel';
		div.style.display = expanded ? 'block' : 'none';

		// Loop through siblings of the H3 element.
		// Finish if another H3 element, a H2 element or this script element is detected.
		sibling = h3s[i].nextSibling;
		while (sibling.tagName != 'H3' && sibling.tagName != 'H2' && sibling.tagName != 'SCRIPT') {

			// If this isn't a text node, remove it from it's
			// parent and append to the hidden text div.
			newSibling = sibling.nextSibling;
			if (sibling.tagName != null) {
				sibling.parentNode.removeChild(sibling);
				div.appendChild(sibling);
			}

			// Locate next sibling.
			sibling = newSibling;

		}

		// Create link to replace H3 element.
		var link = document.createElement('DIV');
		var linkA = document.createElement('A');
		link.className = 'textPanelLink';
		linkA.innerHTML = '<span class="textPanelIcon" id="textPanelIcon' + panelId + '">' + (expanded ? '-' : '+') + '</span><span class="textPanelH3">' + h3s[i].innerHTML + '</span>';
		linkA.href = 'javascript:toggleTextPanel(' + panelId + ');';
		link.appendChild(linkA);

		h3s[i].parentNode.insertBefore(div, h3s[i].nextSibling);
		h3s[i].parentNode.insertBefore(link, h3s[i].nextSibling);
		h3s[i].parentNode.removeChild(h3s[i]);

		panelId++;
		i--;

	}

} // end function loadTextPanels.

/**
 * Opens or closes a text panel.
 *
 * @param	String	i	Text panel id.
 * @access	public
 */
function toggleTextPanel(i) {

	var panel = document.getElementById('textPanel' + i);
	var icon = document.getElementById('textPanelIcon' + i);
	icon.innerHTML = (panel.style.display == 'none' ? '&ndash;' : '+');
	panel.style.display = (panel.style.display == 'none' ? '' : 'none');

}

/**
 * Opens an online form for viewing or editing.
 *
 * @param	String	url		The url of the online form.
 * @access	public
 */
var form;
function openForm(url) {
	if (form == null || form.closed) {
		var winWidth = 756;
		var winHeight = 700;
		var windowURL = url;
		var windowName = 'formWindow';
		var windowfeatures = 'width='+winWidth+',height='+winHeight+',top=0,left=0,screenX=0,screenY=0,directories=0,location=0,scrollbars=1,menubar=0,status=1,toolbar=0,resizable=1';
		form = window.open(windowURL, windowName, windowfeatures);
		form.focus();
	} else {
		form.focus();
	}
}

/**
 * Loads the top naviagation menu from list items.
 *
 * @access	public
 */
function loadMenu() {

	if (document.all && document.getElementById) {
		menuRoot = document.getElementById('menu');
		for (x = 0; x < menuRoot.childNodes.length; x++) {
			node = menuRoot.childNodes[x];
			if (node.nodeName == 'LI') {
				node.onmouseover = function() {
					this.className += ' selected';
				}
				node.onmouseout = function() {
					this.className = this.className.replace(' selected', '');
				}
			}
		}
	}

}

/**
 * Updates classes for notices on mouseover or mouseout events.
 *
 * @param	Object	obj
 * @access	public
 */
function noticeboardHover(obj) {

	var newClass = (obj.className == '' ? 'hover' : '');
	var siblings = obj.parentNode.getElementsByTagName('TD');

	for (var i in siblings) {
		siblings[i].className = newClass;
	}

} // end function noticeboardHover.

/**
 * Replaces councillors link with councillor and division
 * drop down boxes.
 *
 * @access	public
 */
function loadCouncillorDropdowns() {

	var councillorBox = document.getElementById('councillorSelectBody');
	if (councillorBox == null) {
		return;
	}

	var councillors = new Array();
	councillors['mayor'] = 'Cr Paul Pisasale';
	councillors['division_1'] = 'Cr David Morrison';
	councillors['division_2'] = 'Cr Paul Tully';
	councillors['division_3'] = 'Cr Victor Attwood';
	councillors['division_4'] = 'Cr Trevor Nardi';
	councillors['division_5'] = 'Cr Heather Morrow';
	councillors['division_6'] = 'Cr Cheryl Bromage';
	councillors['division_7'] = 'Cr Andrew Antoniolli';
	councillors['division_8'] = 'Cr Charlie Pisasale';
	councillors['division_9'] = 'Cr Shelia Ireland';
	councillors['division_10'] = 'Cr David Pahlke';

	var divisions = new Array();
	divisions['mayor'] = 'Mayor';
	divisions['division_1'] = 'Division 1';
	divisions['division_2'] = 'Division 2';
	divisions['division_3'] = 'Division 3';
	divisions['division_4'] = 'Division 4';
	divisions['division_5'] = 'Division 5';
	divisions['division_6'] = 'Division 6';
	divisions['division_7'] = 'Division 7';
	divisions['division_8'] = 'Division 8';
	divisions['division_9'] = 'Division 9';
	divisions['division_10'] = 'Division 10';

	var onchange = function() {
		if (this.value != '') {
			window.location = 'http://ww2.ipswich.qld.gov.au/about_council/mayor_and_councillors/' + this.value;
		}
	}

	var councillorSelector = document.createElement('SELECT');
	councillorSelector.onchange = onchange;
	councillorSelector.options.add(new Option('Select a Councillor', '', true, false));
	for (var i in councillors) {
		councillorSelector.options.add(new Option(councillors[i], i, false, false));
	}

	var divisionSelector = document.createElement('SELECT');
	divisionSelector.onchange = onchange;
	divisionSelector.style.marginBottom = '4px';
	divisionSelector.options.add(new Option('Select a Division', '', true, false));
	for (var i in divisions) {
		divisionSelector.options.add(new Option(divisions[i], i, false, false));
	}

	councillorBox.innerHTML = '';
	councillorBox.appendChild(councillorSelector);
	councillorBox.appendChild(document.createElement('BR'));
	councillorBox.appendChild(divisionSelector);

} // end function loadCouncillorDropdowns.

/**
 * Call setup functions on page load.
 */
if (window.attachEvent) {
	window.attachEvent('onload', loadMenu)
	window.attachEvent('onload', loadCouncillorDropdowns);
} else {
	window.onload = function() {
		loadMenu();
		loadCouncillorDropdowns();
	}
}