/*///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\*/
/* ACC - NZIPS Search functions */
/*///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\*/
/*
/* Author:     Scott Whittaker (scott @ headfirst.co.nz)
/* Created:    Feb 2007
/* Modified:   June 2007
*/
/*///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\*/

// Functionality to expand or collapse specific option details.
// Initial open/closed states will be based on the state of the HTML page.
// These states are stored for future manipulation.

var _tabList = _tabList || {};
var _isSafari = navigator.userAgent.indexOf( 'Safari' ) != -1; // Safari detection to fudge tab height values

function init() {
	
	if(document.getElementById( 'search' )) initTabs();
	if(document.getElementById( 'mapToggle' )) initMap();
	init_func();
}

function initTabs() {
	// Target the main content area
	var searchTable = document.getElementById( 'search' );
	// Get all the possible tabs inside
	var tabs =  getElementsByClassName( searchTable, 'div', 'tab' );
	// Now go through that list and shortlist the ones with matching subsections
	var n = tabs.length;
	while ( n-- ) {
		// get the enclosed radio button
		var tabDiv = tabs[ n ];
		var radio = tabDiv.getElementsByTagName( 'input' )[ 0 ];
		radio.onclick = function(){ toggleTabGroup( this ); }
		// use the name and value to locate the target subsection
		var id = radio.name + ":" + radio.value;
		var tabObj = {};
		tabObj.target = document.getElementById( id );
		// container is the element which contains the Tab DIV
		tabObj.div = tabDiv;
		if ( tabObj.target ) {
			_tabList[ id ] = tabObj;
			toggleTab( radio );
		}
	}
}

function initMap() {
	var mapToggleLink = document.getElementById( 'mapToggle' );
	mapToggleLink.className = "";
	mapToggleLink.onclick = toggleMap;

	var map = document.getElementById( 'map' );
	var links = map.getElementsByTagName( 'li' );
	var n = links.length;
	while ( n-- ) {
		var lnk = links[ n ];
		if(lnk.id == 'map_close') {

			lnk.onclick = function(){ toggleMap(); }
			continue
		}
		lnk.onclick = function(){ toggleRegion( this.id );  }
	}
}


/* Collapsible tabs */

function toggleTabGroup( radio ) {
	var tabs = radio.form.elements[ radio.name ];
	var n = tabs.length;
	while ( n-- ) {
		toggleTab( tabs[ n ] );
	}
}

function toggleTab( radio ) {
	var id = radio.name + ":" + radio.value;
	var tabObj = _tabList[ id ];
	if ( tabObj ) {
		var tabEnd = getElementsByClassName( tabObj.div, 'div', 'tabEnd' )[ 0 ];
		var tabDiv = tabObj.div;
		// set CSS styles based on radio selection state
		if ( radio.checked ) {
			tabObj.target.className = "subsection";
			tabDiv.className = "tab selected";
			// set height of the tab to match it's container
			var tabHeight = ( _isSafari ) ? 40 : getHeight( tabDiv.parentNode.parentNode );
			if ( tabDiv.parentNode.parentNode.className != "noSection" ) tabHeight -= ( _isSafari ) ? -10 : 10;
//			alert( tabHeight );
			tabEnd.style.height = tabHeight+"px";
		} else {
			tabObj.target.className = "subsection hidden";
			tabDiv.className = "tab";
			tabEnd.style.height = "auto";
		}
	}
}

/* Map */

function toggleMap() {
	var mapHolder = document.getElementById( 'mapHolder' );
	if ( mapHolder.className == "" ) {
		mapHolder.className = "hidden";
	} else {
		mapHolder.className = "";
	}
}

function toggleRegion( id ) {
	toggleMap();
	var table_div = document.getElementById('region_chkboxes');
	var checkboxes = table_div.getElementsByTagName('input');
	var checkbox = null;
	var n = checkboxes.length;
	while ( n-- ) {
		if ( checkboxes[ n ].id == 'x'+id ) {
			checkbox = checkboxes[ n ];
		}
	}
	if ( checkbox == null ) alert( id+" does not exist in form" );
	checkbox.checked = !checkbox.checked;
}
