function toggle(key) {

	var toggleContentElm = 'toggled_content_' + key;
	var toggleContentButtonElm = 'toggled_button_' + key;
	
	if (document.getElementById(toggleContentElm) && document.getElementById(toggleContentButtonElm)) {
	
		var toggleContent = document.getElementById(toggleContentElm);
		var toggleContentButton = document.getElementById(toggleContentButtonElm);
		
		if (toggleContent.style.display == 'none') {
			toggleContent.style.display = '';
			toggleContentButton.style.backgroundImage = 'url(../images/arrow-down.gif)';
		} else {
			toggleContent.style.display = 'none';
			toggleContentButton.style.backgroundImage = 'url(../images/arrow-right.gif)';
		}
	}
}

function showToggledContent() {
	var hash = document.location.hash;
	if (hash.length > 0) {
		hash = hash.replace(/#/i, ''); // remove the # from the hash
		toggle(hash);
	}
}

function showLargePic() {
	var hash = document.location.hash;
	if (hash.length > 0) {
		hash = hash.replace(/#/i, ''); // remove the # from the hash
		// split using ':'
		var bits = hash.split(':');
		var pic = bits[0];
		var type = bits[1];
		large_pic(pic, type);
	}
}

function large_pic(pic, type) {

	//alert('pic='+pic+':type='+type);
	
	if (!document.getElementById('large_pic')) return false;

	var url = getPathToRoot() + 'inc/update_pic.php?pic='+escape(pic)+'&type='+escape(type);
	doAjaxRequest(url);
	
	function doAjaxRequest(url) {	
		// setup the ajax object, define the response handler and send the data
		http = createAjaxObject();
		http.open('GET', url);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
	// update element
	function handleResponse() {
		if (http.readyState == 4) {
			var response = http.responseText;
			document.getElementById('large_pic').innerHTML = response;
		}
	}
	
}

function toggleItem(targetID, target2ID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		target2 = document.getElementById(target2ID);
		if (target.style.display == "none") {
			target.style.display = "";
			target2.style.backgroundImage = "url('../images/triangle-open.gif')";
		} else {
			target.style.display = "none";
			target2.style.backgroundImage = "url('../images/triangle-close.gif')";
		}
	}
}

function setupURLJumperSelect() {
	// get all select elements with the class 'url_jump_select'
	var selects = getElementsByClass('url_jump_select', document, 'SELECT');
	// loop through, add event on change
	for (var i = 0; i < selects.length; i++) {
		selectElement = selects[i];
		// attach onclick function
		addEvent(selectElement, 'change', urlJumpSelect, false);
	}	
}

function urlJumpSelect(e) {
	// find the target
	var target = findTarget(e);
	var path = target.value;
	if (path.length > 0) {
		document.location.href = path;
	}
}

function setUpRadiosAndCheckboxes() {
	// get all select elements with the class 'url_jump_select'
	var inputs = document.getElementsByTagName('INPUT');
	// loop through, add class 'radio' if they are a radio or checkbox
	for (var i = 0; i < inputs.length; i++) {
		inputElement = inputs[i];
		if (inputElement.getAttribute('type') == 'checkbox' || inputElement.getAttribute('type') == 'radio') {
			// add class
			inputElement.className += ' radio';
		}
	}	
}

function toggleList(e) {
	
	// get a reference to the anchor and it's parentNode (the <li> element)
	if (window.event) {
		// IE does it differently... stores the event in a window.event object
		var thisA = window.event.srcElement;
		//alert(thisA);
		var thisLI = thisA.parentNode;
	} else {
		var thisA = this;
		var thisLI = this.parentNode;
	}
	// if this li has nested ul elements...
	if (thisLI.getElementsByTagName('ul').length > 0) {
		
		var toggleListTarget = thisLI.getElementsByTagName('ul')[0];
		//...toggle visibility of first ul
		if (toggleListTarget.style.display != 'none' ) {
			toggleListTarget.style.display = 'none';
			//thisA.style.color = '';
			thisA.style.backgroundImage = 'url(../images/arrowright.gif)';
			//closeListArrow();
		} else {
			toggleListTarget.style.display = '';
			//thisA.style.color = 'white';
			thisA.style.backgroundImage = 'url(../images/arrowdown.gif)';
			//expandListArrow();
		}
		// cancel bubble and href of this anchor
		cancelClick(e);
		// hack for Safari (stops browser following href link)
		thisA.onclick = function() { return false; }
	}
}

function setUpCollapsingList() {

	// find the toggle list
	var lists = getElementsByClass('collapsible_list', document, 'ul');
	
	for (var x = 0; x < lists.length; x++) {
		//list = document.getElementById('collapsingList');
		list = lists[x];
		// find all it's child UL elements
		var childLists = list.getElementsByTagName('ul');
		// hide them all
		for (var i = 0; i < childLists.length; i++) {
			var childList = childLists[i];
			childList.style.display = 'none';
		}
		
		// attach toggleList function to onclick of each anchor
		var childAnchors = list.getElementsByTagName('a');
		// hide them all
		for (var i = 0; i < childAnchors.length; i++) {
			var childAnchor = childAnchors[i];
			// attach toggleList function to onclick of each anchor
			addEvent(childAnchor, 'click', toggleList, false);
		}
	}
}

function load_ticker() {
	if (document.getElementById('ticker')) {
		var ticker = document.getElementById('ticker');

		// remove all chidren inside ticker
		while (ticker.hasChildNodes()) {
			ticker.removeChild(ticker.firstChild);
		}
		
		var iframe_src = getPathToRoot() + 'inc/news_ticker.php?section_id=' + section_id;
		
		// create the iframe element
		var iframe = document.createElement('iframe');
		iframe.setAttribute('height', 16);
		iframe.setAttribute('width', 395);
		iframe.setAttribute('marginwidth', 0);
		iframe.setAttribute('marginheight', 0);
		iframe.setAttribute('frameBorder', 0); // note the case-sensitivity for IE6
		iframe.setAttribute('scrolling', 'no');
		iframe.setAttribute('src', iframe_src);
		
		// add to the ticker DIV
		ticker.appendChild(iframe);
	}
}

//addLoadEvent(load_ticker);
addLoadEvent(setupURLJumperSelect);
addLoadEvent(setUpRadiosAndCheckboxes);
//addLoadEvent(setUpCollapsingList);
addLoadEvent(showToggledContent);
addLoadEvent(showLargePic);