var containerMaps = new Object();

/* expects <area> elem ref */
function showMapPane()
{
	var loadingPane = document.getElementById( "LoadingPane" );
	var mainPane = document.getElementById( "MainPane" );
	if( loadingPane )
		loadingPane.style.display = "none";
	if( mainPane )
		mainPane.style.display = "block";
}

function getAreaName( path )
{
	if( !path )
		return null;
	if( !path )
		return null;
	var segs = path.split( "_" );
	var areaName = segs[segs.length-1];
	return areaName;
}

function getOverlay( path )
{
	var imgId = getAreaName( path );
	if( !imgId )
		return null;
	return document.getElementById( imgId + "-overlay" );
}

function selectAreaByName( path, ignoreCrossPost, dontRefresh )
{
	// check for multiple selections
	if( path.indexOf( "," ) != -1 ) {
		var paths = path.split( "," );
		for( var i=0; i<paths.length; ++i ) {
			if( i+1 < paths.length ) {
				selectAreaByName( paths[i], ignoreCrossPost, true );
			}
			else {
				selectAreaByName( paths[i], ignoreCrossPost, false );
			}
		}
		return;
	}

	var overlay = getOverlay( path );
	if( overlay ) {
		overlay.style.visibility = "visible";
	}
	
	var area = document.getElementById( path );
	if( area ) {
		area.onclick = deselectArea;
	}
	
	var hidden = document.MainForm.selections;
	if( hidden ) {
		if( !hidden.value ) {
			hidden.value = path;
		}
		else {
			var values = hidden.value.split( /,/ );
			var found = false;
			for( var i=0; i<values.length; ++i ) {
				if( values[i] == path ) {
					found = true;
					break;
				}
			}
			if( !found ) {
				hidden.value += "," + path;
			}
		}
		//alert( "HIDDEN: " + hidden.name + ":" + hidden.value );
	}
	
	if( !ignoreCrossPost ) {
		if( typeof onMapSelect != 'undefined' ) {
			onMapSelect( path );
		}
		else if( parent && ( typeof parent.onMapSelect != 'undefined' ) ) {
			parent.onMapSelect( path );
		}

	}
	
	// see if we are on the right map
	if( !dontRefresh ) {
		var idx = path.lastIndexOf( "_" );
		if( idx == -1 )
			return;
		var immediateParent = path.substring( 0, idx );
		if( immediateParent != document.MainForm.mapId.value ) {
			document.MainForm.mapId.value = immediateParent;
			document.MainForm.submit();
		}
	}
}

function deselectAreaByName( path, ignoreCrossPost, dontRefresh )
{
	// check for multiple selections
	if( path.indexOf( "," ) != -1 ) {
		var paths = path.split( "," );
		for( var i=0; i<paths.length; ++i ) {
			if( i+1 < paths.length ) {
				deselectAreaByName( paths[i], ignoreCrossPost, true );
			}
			else {
				deselectAreaByName( paths[i], ignoreCrossPost, false );
			}
		}
		return;
	}
	
	var overlay = getOverlay( path );
	if( overlay ) {
		overlay.style.visibility = "hidden";
	}
	
	var hidden = document.MainForm.selections;
	if( hidden ) {
		if( hidden.value ) {
			var values = hidden.value.split( /,/ );
			var newValue = "";
			for( var i=0; i<values.length; ++i ) {
				if( values[i] == path ) {
					continue;
				}
				if( newValue.length > 0 ) {
					newValue += "," + values[i];
				}
				else {
					newValue = values[i];
				}
			}
			hidden.value = newValue;
		}
		//alert( "HIDDEN: " + hidden.name + ":" + hidden.value );	
	}
	
	if( !ignoreCrossPost ) {
		if( typeof onMapDeselect != 'undefined' ) {
			onMapDeselect( path );
		}
		else if( parent && ( typeof parent.onMapDeselect != 'undefined' ) ) {
			parent.onMapDeselect( path );
		}
	}
	
	var area = document.getElementById( path );
	if( area ) {
		area.onclick = selectArea;
	}
	
	// see if we are on the right map
	if( !dontRefresh ) {
		var idx = path.lastIndexOf( "_" );
		if( idx == -1 )
			return;
		var immediateParent = path.substring( 0, idx );
		if( immediateParent != document.MainForm.mapId.value ) {
			document.MainForm.mapId.value = immediateParent;
			document.MainForm.submit();
		}
	}	
}

function selectArea( evt )
{
	evt = ( evt ) ? evt : event;
	if( !evt )
		return;
	
	var elem = ( evt.target ) ? evt.target : ((evt.srcElement) ? evt.srcElement : null );
	if( !elem )
		return;
		
	selectAreaByName( elem.id );
	
	// stop event from propagating
	evt.cancelBubble = true;
}

function deselectArea( evt )
{
	evt = ( evt ) ? evt : event;
	if( !evt )
		return;
		
	var elem = ( evt.target ) ? evt.target : ((evt.srcElement) ? evt.srcElement : null );
	if( !elem )
		return;
		
	deselectAreaByName( elem.id );
	
	// stop event from propagating
	evt.cancelBubble = true;
}

function initMap()
{
	var map = document.getElementById( "MapContainer" );
	var container = document.getElementById( "ImageMap" );
	if( !container )
		return;
	if( container.hasChildNodes() ) {
		for( var i=0; i<container.childNodes.length; ++i ) {
			var child = container.childNodes[i];
			if( "area" == child.nodeName.toLowerCase() ) {
				var path = child.id;
				var selected = false;
				if( path ) {
					var overlay = getOverlay( path );
					if( overlay && !containerMaps[getAreaName(path)] ) {
						selected = ( overlay.style.visibility == "visible" );
						if( selected )
							child.onclick = deselectArea;
						else
							child.onclick = selectArea;
					}
					else {
						child.onclick = function() { eval("drill(this.id)"); };
					}
				}
				if( map ) {
					child.onmouseover = function() { map.style.cursor = 'pointer'; };
					child.onmouseout = function() { map.style.cursor = 'default'; };
				}
			}
		}
	}
	
		
	if( typeof browser != 'undefined' ) {
		if( !browser.isIE ) {
			showMapPane();
		}
	}
	
	// in any case, don't let progress go on for more than 3 secs
	setTimeout( "showMapPane()", 3000 );
}

function initMapForSelect()
{
	var map = document.getElementById( "MapContainer" );
	var container = document.getElementById( "ImageMap" );
	if( !container )
		return;
	if( container.hasChildNodes() ) {
		for( var i=0; i<container.childNodes.length; ++i ) {
			var child = container.childNodes[i];
			if( "area" == child.nodeName.toLowerCase() ) {
				var path = child.id;
				var selected = false;
				if( path ) {
					var idx = path.lastIndexOf( "_" );
					if( idx != -1 ) {
						var overlayId = path.substring( idx + 1 );
						var overlay = document.getElementById( overlayId );
						if( overlay ) {
							selected = ( overlay.style.visibility == "visible" );
						}
					}
				}
				if( selected )
					child.onclick = deselectArea;
				else
					child.onclick = selectArea;
					
				if( map ) {
					child.onmouseover = function() { map.style.cursor = 'pointer'; };
					child.onmouseout = function() { map.style.cursor = 'default'; };
				}
			}
		}
	}
	
		
	if( typeof browser != 'undefined' ) {
		if( !browser.isIE ) {
			showMapPane();
		}
	}
	
	// in any case, don't let progress go on for more than 3 secs
	setTimeout( "showMapPane()", 3000 );
}

function selectParentAreas( areas )
{
	if( areas && areas.length > 0 ) {
		selectParentArea( areas.join(',') );
	}
}

function selectParentArea( area )
{
	var areas = document.getElementById( "areas_value" );
	if( areas != null ) {
		areas.value = area;
		document.MainForm.submit();
	}
}

function deselectParentArea( area )
{
	var areas = document.getElementById( "clearAreas_value" );
	if( areas != null ) {
		areas.value = area;
		document.MainForm.submit();
	}
}

function selectAll()
{
	var container = document.getElementById( "ImageMap" );
	if( !container )
		return;
		
	var all = "";
	var parentAreas = [];
	if( container.hasChildNodes() ) {
		var e = new Object();
		for( var i=0; i<container.childNodes.length; ++i ) {
			var child = container.childNodes[i];
			if( "area" == child.nodeName.toLowerCase() ) {
				if(  !containerMaps[getAreaName(child.id)] ) {
					e.target = child;
					selectArea( e );
				}
				else {
					parentAreas.push( child.id );
				}
			}
		}
	}	
	selectParentAreas( parentAreas );
}

function deselectAll()
{
	var container = document.getElementById( "ImageMap" );
	if( !container )
		return;
	if( container.hasChildNodes() ) {
		var e = new Object();
		for( var i=0; i<container.childNodes.length; ++i ) {
			var child = container.childNodes[i];
			if( "area" == child.nodeName.toLowerCase() ) {
				e.target = child;
				deselectArea( e );
			}
		}
	}
}

function drill( mapId )
{
	if( !mapId )
		return;
	document.MainForm.mapId.value = mapId;
	document.MainForm.submit();
}

function undrill( mapId )
{
	document.MainForm.mapId.value = mapId;
	document.MainForm.submit();
}

function initMapForDrill()
{
	var map = document.getElementById( "MapContainer" );
	var container = document.getElementById( "ImageMap" );
	if( !container )
		return;
	if( container.hasChildNodes() ) {
		for( var i=0; i<container.childNodes.length; ++i ) {
			var child = container.childNodes[i];
			if( "area" == child.nodeName.toLowerCase() ) {
				child.onclick = function() { eval("drill(this.id)"); };
				if( map ) {
					child.onmouseover = function() { map.style.cursor = 'pointer'; };
					child.onmouseout = function() { map.style.cursor = 'default'; };
				}
			}
		}
	}
	
	if( typeof browser != 'undefined' ) {
		if( !browser.isIE ) {
			showMapPane();
		}
	}
	
	// in any case, don't let the progress go on for longer than 3 secs
	setTimeout( "showMapPane()", 3000 );
}
