
/*
** Added necessary global variables here to accomodate functionality needed
*/
var activeRegion;
var canvascheck = document.createElement('canvas');
var isIE = window.navigator.systemLanguage?1:0;
var isFF = document.namespaces?1:0; var isJG = 0;
var isDR = canvascheck.getContext?1:0; var jg = new Array();


if(isFF) {
	if(document.namespaces['v'] == null) {
		var stl = document.createStyleSheet();
		stl.addRule("v\\:*", "behavior: url(#default#VML); antialias: true;"); 
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); 
	}
}

/*
** The functions listed below are open source, as they are freely available across the internet
** and have no single author
*/
function fetchClass(classes,string){
	var temp = '';
	for (var j=0;j<classes.length;j++) {
		if (classes[j] != string) {
			if (temp) {temp += ' '; }
			temp += classes[j];
		}
	}
	return temp;
}
function fetchClassValue(classes,string){
	var temp = 0; var pos = string.length;
	for (var j=0;j<classes.length;j++) {
		if (classes[j].indexOf(string) == 0) {
			temp = Math.min(classes[j].substring(pos),100);
			break;
		}
	}
	return Math.max(0,temp);
}
function getClassRGBColor(classes,string,color){
	var temp, val = color, pos = string.length;
	for (var j=0;j<classes.length;j++) {
		if (classes[j].indexOf(string) == 0) {
			temp = classes[j].substring(pos);
			val = temp.toLowerCase();
			break;
		}
	}
	// Regexp fixed for performance increase
	if(!val.match(/^[0-9a-f]{6}$/i)) {val = color||'000000'; }
	if(!isDR) {return val; }else {
		function hex2dec(hex){return(Math.max(0,Math.min(parseInt(hex,16),255)));}
		var cr=hex2dec(val.substr(0,2)),cg=hex2dec(val.substr(2,2)),cb=hex2dec(val.substr(4,2));
		return cr+','+cg+','+cb;
	}
}
function getClassAttribute(classes,string){
	var temp = 0; var pos = string.length;
	for (var j=0;j<classes.length;j++) {
		if (classes[j].indexOf(string) == 0) {
			temp = 1; break;
		}
	}
	return temp;
}
/*
** END open source area of code
*/




function getMaps(className){
	var children = document.getElementsByTagName('img'); 
	var elements = new Array(); var i = 0; var mapname = '';
	var child; var classNames; var j = 0; var mapid = '';
	for(i=0;i<children.length;i++) {
		child = children[i]; classNames = child.className.split(' ');
		for(j=0;j<classNames.length;j++) {
			if(classNames[j]==className) {
				mapname = child.useMap.split("#");
				if(mapname[1]!=''&&mapname[1].length>=1) {
					mapid = document.getElementsByName(mapname[1]);
					if(mapid) {
						if(child.id=='') {child.id = 'map_'+i;}
						elements.push(child); break;
					}
				}
			}
		}
	}
	return elements;	
}
function fadeCanvas(id,opac) {
	var obj = document.getElementById(id);
    if(opac<=100) {
		obj.style.opacity = opac/100; opac += 10;
		window.setTimeout("fadeCanvas('"+id+"',"+opac+")",20);
	}
}
function setAreaOver(obj,id,b,c,o,n,f,z) {
	var i, j, x, y, context, p = '', canvas = document.getElementById(id);
	var v = obj.coords.split(","); 
	if(isFF) {
		for(j=2;j<v.length;j+=2) {p += parseInt(v[j])+','+parseInt(v[j+1])+',';}
		canvas.innerHTML = '<v:shape strokeweight="1" filled="t" stroked="'+(n<1?"t":"f")+'" strokecolor="#'+b+'" coordorigin="0,0" coordsize="'+canvas.width+','+canvas.height+'" path="m '+parseInt(v[0])+','+parseInt(v[1])+' l '+p+' x e" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:0px;width:'+canvas.width+'px;height:'+canvas.height+'px;"><v:fill color="#'+c+'" opacity="'+o+'" /></v:shape>'; 
	}else if(isDR) {
		if(f<1) {canvas.style.opacity = 0;}
		context = canvas.getContext("2d");
		context.beginPath();
	  	context.moveTo(parseInt(v[0]),parseInt(v[1]));
		for(j=2;j<v.length;j+=2) {context.lineTo(parseInt(v[j]),parseInt(v[j+1]));} context.closePath();
		context.fillStyle = 'rgba('+c+','+o+')';
		context.strokeStyle = 'rgba('+b+',1)';
		context.fill(); if(n<1) {context.stroke();}
  		if(f<1) {fadeCanvas(id,0);}
	}else {
  		/*jg[z].setColor("#"+c);
		x = new Array(); y = new Array(); i = 0;
		for(j=0;j<v.length;j+=2) {x[i] = parseInt(v[j]); y[i] = parseInt(v[j+1]); i++;} 
		jg[z].fillPolygon(x,y);
		jg[z].paint();*/
	}
}
function setAreaOut(obj,id,f,z) {
	var canvas = document.getElementById(id);
	if(isFF) {canvas.innerHTML = '';}else 
	if(isJG) {jg[z].clear();}else if(isDR) {
		var context = canvas.getContext("2d");
		context.clearRect(0,0,canvas.width,canvas.height);
	}
}


/*
** This function sets the active region in preparation for the display
** of different regions' stores and links.
*/
function setActiveRegion(myObj) {
	activeRegion = myObj.getAttribute('name');
}

/*
** This function toggles the display of the links and stores for Alaska
*/
function showAlaska() {
	handler = document.getElementById('infoRegionalUSA').getElementsByTagName("div");
	for(var x=0; x<handler.length; x++) {
		if(handler[x].getAttribute("id").replace(/region/, '') == "9") {
			handler[x].style.display = "block";
		} else {
			handler[x].style.display = "none";
		}
	}
	document.getElementById('infoRegionalUSA').style.display = "block";
}

/*
** This function toggles the display of the links and stores for Hawaii
*/
function showHawaii() {
	handler = document.getElementById('infoRegionalUSA').getElementsByTagName("div");
	for(var x=0; x<handler.length; x++) {
		if(handler[x].getAttribute("id").replace(/region/, '') == "10") {
			handler[x].style.display = "block";
		} else {
			handler[x].style.display = "none";
		}
	}
	document.getElementById('infoRegionalUSA').style.display = "block";
}

function showAlaskaHawai() {
	handler = document.getElementById('infoRegionalUSA').getElementsByTagName("div");
	for(var x=0; x<handler.length; x++) {
		if(handler[x].getAttribute("id").replace(/region/, '') == "11") {
			handler[x].style.display = "block";
		} else {
			handler[x].style.display = "none";
		}
	}
	document.getElementById('infoRegionalUSA').style.display = "block";
}
function showOnline() {
	handler = document.getElementById('infoRegionalUSA').getElementsByTagName("div");
	for(var x=0; x<handler.length; x++) {
		if(handler[x].getAttribute("id").replace(/region/, '') == "12") {
			handler[x].style.display = "block";
		} else {
			handler[x].style.display = "none";
		}
	}
	document.getElementById('infoRegionalUSA').style.display = "block";
}

/*
** This function toggles the display of the links and stores for all regions
** other than Hawaii and Alaska, and depends on the setActiveRegion() to 
** function properly
*/
function showInfo() {
	containerElem = document.getElementById('infoRegionalUSA');
	children = containerElem.getElementsByTagName("div");
	for(var i=0; i < children.length; i++) {
		if(children[i].getAttribute('id').replace(/region/, '') == activeRegion) {
			thisObj = document.getElementById('region'+activeRegion);
			thisObj.style.display = "block";
			containerElem.style.display = "block";			
		} else {
			children[i].style.display = "none";
		}		
	}
}


function addMapper() {
	var themaps = getMaps('mapper');
	var image, object, bgrnd, canvas, context, mapid, mname; 
	var classes = '', newClasses = '', i, j, o, b , c, n, f;
	for(i=0;i<themaps.length;i++) {
		image = themaps[i]; object = image.parentNode;
		object.style.position = (object.style.position=='static'||object.style.position==''?'relative':object.style.position);
		object.setAttribute("id", "mapOverlay"+i);
		object.style.height = image.height+'px';
		object.style.width = image.width+'px';
		bgrnd = document.createElement('img');
		n = 0; f = 0; b = '0000ff'; c = '000000'; o = 33;
		if(isDR) {
			thisCanvas = document.createElement('canvas');
		} else if(isFF) {
			thisCanvas = document.createElement(['<var style="zoom:1;overflow:hidden;display:block;width:'+image.width+'px;height:'+image.height+'px;padding:0;">'].join(''));
		} else {
			thisCanvas = document.createElement('div');
		}
		thisCanvas.setAttribute('id', 'mapOverlay_'+i);
		thisCanvas.id = image.id+'_canvas';
		classes = image.className.split(' '); 
		o = fetchClassValue(classes,"iopacity");
		b = getClassRGBColor(classes,"iborder",'0000ff');
		c = getClassRGBColor(classes,"icolor",'000000');
		n = getClassAttribute(classes,"noborder");
		f = getClassAttribute(classes,"nofade");
		o = o==0?0.33:o/100;
		newClasses = fetchClass(classes,"mapper");
		image.className = newClasses;
		mname = image.useMap.split("#");
		mapid = document.getElementsByName(mname[1]);
		o = 0.65;
		
		
	/*
	** The following loop had to be rewritten to function properly
	*/
		for(j=0;j<mapid[0].areas.length;j++) {
			
			if(mapid[0].areas[j].shape.match(/(rect|poly|circle)/i)&&mapid[0].areas[j].coords!='') {
				
				if(isFF||isIE) {
					mapid[0].areas[j].onmouseover = new Function('setAreaOver(this,"'+thisCanvas.id+'","'+b+'","'+c+'","'+o+'",'+n+','+f+','+i+'); setActiveRegion(this);'); 
					mapid[0].areas[j].onmouseout = new Function('setAreaOut(this,"'+thisCanvas.id+'",'+f+','+i+');');
					mapid[0].areas[j].onclick = function() { showInfo(); };
				}else {
					mapid[0].areas[j].setAttribute("onmouseover","setAreaOver(this,'"+thisCanvas.id+"','"+b+"','"+c+"','"+o+"',"+n+","+f+","+i+"); setActiveRegion(this);"); 
					mapid[0].areas[j].setAttribute("onmouseout","setAreaOut(this,'"+thisCanvas.id+"',"+f+","+i+");");
					mapid[0].areas[j].setAttribute("onclick","showInfo();");				
				
				}
			}
		}
	/*
	** END changes
	*/
		thisCanvas.style.height = image.height+'px';
		thisCanvas.style.width = image.width+'px';
		thisCanvas.height = image.height;
		thisCanvas.width = image.width;
		thisCanvas.left = 0; thisCanvas.top = 0;
		thisCanvas.style.position = 'absolute';
		thisCanvas.style.left = 0+'px';
		thisCanvas.style.top = 0+'px';
		image.className = '';
		image.style.cssText = '';
		image.left = 0; image.top = 0;
		image.style.position = 'absolute';
		image.style.height = image.height+'px';
		image.style.width = image.width+'px';
		image.style.left = 0+'px';
		image.style.top = 0+'px';
		if(isIE) {image.style.filter = "Alpha(opacity=0)";
		}else {image.style.opacity = 0;
  		image.style.MozOpacity = 0;
  		image.style.KhtmlOpacity = 0;}
		bgrnd.src = image.src;
		bgrnd.left = 0; bgrnd.top = 0;
		bgrnd.style.position = 'absolute';
		bgrnd.style.height = image.height+'px';
		bgrnd.style.width = image.width+'px';
		bgrnd.style.left = 0+'px';
		bgrnd.style.top = 0+'px';
		object.insertBefore(thisCanvas,image);
		o = .95;
		if(isDR) {
			context = thisCanvas.getContext("2d");
			context.clearRect(0,0,thisCanvas.width,thisCanvas.height);
		}else if(!isFF && !isDR) {if(isIE) {
			thisCanvas.style.filter = "Alpha(opacity="+(o*100)+")";
			}else { thisCanvas.style.opacity = o;
  			thisCanvas.style.MozOpacity = o;
  			thisCanvas.style.KhtmlOpacity = o;}
			if(typeof(window['jsGraphics']) !== 'undefined') {
				jg[i] = new jsGraphics(thisCanvas); isJG = 1;
			}
		}		
		object.insertBefore(bgrnd,thisCanvas);
	}
}

var mapperOnload = window.onload;
window.onload = function () { if(mapperOnload) mapperOnload(); addMapper();	}
