// JavaScript Document

basemap = "nz.gif";
selectedRegionName = document.searchForm.region_id.options[document.searchForm.region_id.selectedIndex].text.toLowerCase().replace(/ /gi,"").replace(/&.*/gi,"").replace(/\/.*/gi,"");
if (selectedRegionName != "allregions" ) basemap = "nz_"+selectedRegionName+".gif";

function mapSet() {
	document.images['mapImg'].src = 'images/map3/'+basemap; 
	if(document.searchForm.region_id.selectedIndex) districtLinkList();
	areas = document.getElementsByTagName("area");
	for (var i=0; i<areas.length; i++) {
		for (var j = 0; j < regions.length; j++) {
			if (regions[j].label.toLowerCase().replace(/ /gi,"").replace(/&.*/gi,"").replace(/\/.*/gi,"") == areas[i].alt.toLowerCase().replace(/ /gi,"")) areas[i].region_id = regions[j].id;
		}
		areas[i].onmouseover = function() {
			document.images['mapImg'].src = 'images/map3/nz_'+this.alt.toLowerCase().replace(/ /gi,"")+'.gif'; 
		};
		areas[i].onmouseout = function() {
			document.images['mapImg'].src = 'images/map3/'+basemap; 
		};
		areas[i].onclick = function() {
			basemap = 'nz_'+this.alt.toLowerCase().replace(/ /gi,"")+'.gif';
			mapSetRegion(this.region_id);
		};
	}
}

function mapSetRegion(id) {
	// Refresh the search form
	for (var i=0; i<document.searchForm.region_id.options.length; i++) {
		if (document.searchForm.region_id.options[i].value == id) document.searchForm.region_id.selectedIndex = i;
	}
	document.searchForm.region_id.onchange();
	
	if (id) { // If something is selected update the district list
		districtLinkList();
	} else { // otherwise insert an empty list
		var element = document.getElementById('districtList');
		element.parentNode.replaceChild ($.UL({id: 'districtList'}), element);
	}
}

function mapSetDistrict(districtListObj, id) {
	for (var i=0; i<document.searchForm.district_id.options.length; i++) {
		if (document.searchForm.district_id.options[i].value == id) document.searchForm.district_id.selectedIndex = i;
	}
	document.searchForm.district_id.onchange();
	
	if (id) { // If something is selected update the suburb list
		suburbLinkList(districtListObj);
	} else { // otherwise inser an empty list
		var element = document.getElementById('suburblist');
		if (element) element.parentNode.removeChild (element);
	}
	
	//Hilite selected district
	var districtList = document.getElementById('districtList');
	for (i=0; i<districtList.childNodes.length; i++) removeCSSClass( districtList.childNodes[i], 'selected' );
	addCSSClass( districtListObj.parentNode, 'selected' );
}

function mapSetSuburb(suburbListObj, id) {
	var selectedID = 0;
	for (var i=0; i<document.searchForm.suburb_id.options.length; i++) {
		if (document.searchForm.suburb_id.options[i].value == id) selectedID = i;
	}
	document.searchForm.suburb_id.selectedIndex = selectedID;
	
	//Hilite selected suburb
	var suburbList = document.getElementById('suburbList');
	for (i=0; i<suburbList.childNodes.length; i++) removeCSSClass( suburbList.childNodes[i], 'selected' );
	addCSSClass( suburbListObj.parentNode, 'selected' );
}

function districtLinkList () {
	var element = document.getElementById('districtList');
	var list = $.UL({id: 'districtList', className:'mapDistrictList'});

	if (document.searchForm.district_id.options.length > 1) {
		for (var i=0; i<document.searchForm.district_id.options.length; i++) {
			var value = document.searchForm.district_id.options[i].value;
			var label = document.searchForm.district_id.options[i].text;
			if (document.searchForm.district_id.selectedIndex == i) {var selectedClass = 'selected'} else {var selectedClass = ''};
			list.appendChild (
				$.LI({className:selectedClass},$.A({href:'javascript:void(0);', district:value, onclick:function() {mapSetDistrict(this, this.district)}},label))
			);
			if (document.searchForm.district_id.selectedIndex == i) suburbLinkList(list.lastChild.lastChild);
		}
	}
	element.parentNode.replaceChild (list, element);
}

function suburbLinkList (element_obj) {
	// Remove old suburb list
	var element = document.getElementById('suburblist');
	if (element) element.parentNode.removeChild (element);

	// Build new suburb list
	var list = $.UL({id: 'suburblist', className:'mapSuburbList'});
	
	if (document.searchForm.suburb_id.options.length > 1) {
		for (var i=0; i<document.searchForm.suburb_id.options.length; i++) {
			var value = document.searchForm.suburb_id.options[i].value;
			var label = document.searchForm.suburb_id.options[i].text;
			if (document.searchForm.suburb_id.selectedIndex == i) {var selectedClass = 'selected'} else {var selectedClass = ''};
			list.appendChild (
				$.LI({className:selectedClass},$.A({href:'javascript:void(0);', suburb:value, onclick:function() {mapSetSuburb(this,this.suburb)}},label))
			);
		}
	}
	element_obj.parentNode.appendChild (list);
}

addEvent(window, "load", mapSet);
