function imageMap(elementID)
{
	this.FILE_EXTENSION = '.gif';
	this.IMAGE_DIRECTORY = '/html/assets/images/mapOverlays/';

	try{
		this.ID = elementID;
		this.CONTAINER = document.getElementById(this.ID);
		this.MAP = this.CONTAINER.getElementsByTagName('MAP')[0];
		/**
		* Assume first image in the container is the mapImage
		*/
		this.IMAGE = this.CONTAINER.getElementsByTagName('IMG')[0];
		this.IMAGE_SRC = this.IMAGE.src;
		this.WIDTH = this.IMAGE.getAttribute('width');
		this.HEIGHT = this.IMAGE.getAttribute('height');
		this.loadOverlays();

	}catch(e){
		//alert(e);
	}
}
imageMap.prototype.loadOverlays = function()
{
	this.OVERLAY_IDENTIFIERS = new Array();
	this.OVERLAYS = new Array();
	var divs = this.CONTAINER.getElementsByTagName('DIV');
	for(var i=0;i<divs.length;i++)
	{
		if(divs[i].className=='categories')
		{
			var h3s = divs[i].getElementsByTagName('h3');
			for(var ii=0;ii<h3s.length;ii++)
			{
				try{
					var identifier = h3s[ii].getAttribute('name');
					var usemap = '#Map';


					h3s[ii].AREA_ID = ii;

					h3s[ii].IMAGEMAP = this;
					h3s[ii].onclick = function(){
						this.IMAGEMAP.showOverlay(this.AREA_ID);
					};
					h3s[ii].nextSibling.AREA_ID = ii;
					h3s[ii].nextSibling.IMAGEMAP = this;
					h3s[ii].nextSibling.onclick = function(){this.IMAGEMAP.showOverlay(this.AREA_ID);};
					h3s[ii].style.cursor = 'pointer';

					this.OVERLAYS[ii] = new Image(this.WIDTH,this.HEIGHT);
					this.OVERLAYS[ii].TOGGLE_BUTTON = h3s[ii].getElementsByTagName('SPAN')[0].getElementsByTagName('IMG')[0];
					try{
						this.OVERLAYS[ii].IDENTIFIER = h3s[ii].getAttribute('name').split('__')[1];
					}catch(e){
						alert(e);
					}
					this.OVERLAYS[ii].src = this.IMAGE_DIRECTORY+identifier+this.FILE_EXTENSION;
					this.OVERLAYS[ii].setAttribute('useMap',usemap);
					this.OVERLAYS[ii].setAttribute('width',this.WIDTH);
					this.OVERLAYS[ii].setAttribute('height',this.HEIGHT);
					this.OVERLAYS[ii].style.borderWidth ='0px';
					this.OVERLAYS[ii].style.position ='absolute';
					this.OVERLAYS[ii].style.display = 'none';


					this.CONTAINER.insertBefore(this.OVERLAYS[ii],this.IMAGE);

					/**
					* AREA Hover
					*/
					var areas = this.MAP.getElementsByTagName('AREA');
					for(var iii=0;iii<areas.length;iii++)
					{
						areas[iii].IMAGEMAP = this;
						areas[iii].MASTER_CAT = areas[iii].alt.split(' Categories: ')[1].split(',')[0];
						if(areas[iii].MASTER_CAT==h3s[ii].name.split('__')[1])
						{
							areas[iii].AREA_ID = ii;
							/**
							 * Add Mozilla hacks here
							 *
							areas[iii].setAttribute('HREF',areas[iii].getAttribute('href'));
							areas[iii].onMouseOver = function(){
								alert('hello');
							}
							*/
							areas[iii].onmouseover = function(){
								/**
								 * Remove th return to make the areas display their
								 * default category on mouseover (if no cats have been
								 * clicked)
								 */
								return;
								try{
									var curr;
									if(curr = this.IMAGEMAP.getCurrentOverlay())
									{
										/**
										 * If this area is not assigned to the currently showing cat,
										 * then overide it with this one's MASTER category
										 */
										//alert(this.getAttribute('alt'));
										//alert(this.getAttribute('alt').indexOf(curr.IDENTIFIER));
										if(this.getAttribute('alt').indexOf(curr.IDENTIFIER)==-1)
										{
											this.IMAGEMAP.showOverlay(this.AREA_ID);
										}
									}
									else
									{
										this.IMAGEMAP.showOverlay(this.AREA_ID);
									}
								}catch(e){
									alert(e);
								}
							}
						}
					}

				}catch(e){
					//alert(e);
				}
			}
		}
	}
}
imageMap.prototype.getCurrentOverlay = function()
{
	for(var i=0;i<this.OVERLAYS.length;i++)
	{
		if(this.OVERLAYS[i].style.display!='none')
		{
			return this.OVERLAYS[i];
		}
	}
	return false;
}
imageMap.prototype.showOverlay = function(id)
{
	for(var i=0;i<this.OVERLAYS.length;i++)
	{
		if(i==id)
		{
			this.OVERLAYS[i].style.display = '';
			this.OVERLAYS[i].TOGGLE_BUTTON.style.borderStyle = 'inset';
		}
		else
		{
			this.OVERLAYS[i].style.display = 'none';
			this.OVERLAYS[i].TOGGLE_BUTTON.style.borderStyle = 'outset';
		}
	}
}
imageMap.trim = function(haystack,needle)
{
	var haystack = new String(haystack);
	var needle = new String(needle);
	while (haystack.substring(0,needle.length) == needle)
	{
		haystack = haystack.substring(needle.length,haystack.length);
	}
	while (haystack.substring(haystack.length-needle.length,haystack.length) == needle)
	{
		haystack = haystack.substring(0,haystack.length-needle.length);
	}
	return haystack;
}