
CJP.Map = Class.create();
CJP.Map.prototype = {
	initialize: function(xml) {
		this.isIE = (/MSIE/.test(navigator.userAgent));
		this.mapXML = null;
		this.id = 0;
		this.name = '';
		this.baseImageDirectory = '';
		this.showAddress = false;
		this.showCity = false;
		this.showLocationReference = false;
		this.popMenuWidth = 0;
		this.mapIndicatorCode = '';
		this.contentTopLeftLink = '';
		this.contentTopRightLink = '';
		this.contentBottomLeftLink = '';
		this.contentBottomRightLink = '';			
		this.verticalMatteSize = 0;
		this.defaultMapCoordinate = '';
		this.horizontalMatteSize = 0;	
		
		this.mapOverlayIsVisible = false;
		this.mapLinkages = new Array();
		
		this.setMapXML(xml);
		
		this.mapImage = null;
		this.areaMaps = new Array();
		
		this.loaded = false;
	},
	
	resetMap: function(){
		this.mapXML = null;
		this.id = 0;
		this.name = '';
		this.baseImageDirectory = '';
		this.showAddress = false;
		this.showCity = false;
		this.showLocationReference = false;
		this.popMenuWidth = 0;
		this.mapIndicatorCode = '';
		this.contentTopLeftLink = '';
		this.contentTopRightLink = '';
		this.contentBottomLeftLink = '';
		this.contentBottomRightLink = '';
		this.defaultMapCoordinate = '';			
		this.verticalMatteSize = 0;
		this.horizontalMatteSize = 0;		
		this.mapLinkages = new Array();
		
		this.mapImage = null;
		this.areaMaps = new Array();
	},
	
	setMapXML: function(xml){
		this.resetMap();	
		this.mapXML = xml;
		this.createMapFromXML(xml);
		this.createMapLinkagesFromXML(xml);
	},
	
	createMapFromXML: function(xml){
		var mapElement = xml.getElementsByTagName('map')[0];		
		this.id = mapElement.getAttribute("id");
		this.name = mapElement.getAttribute("name");
		this.baseImageDirectory = mapElement.getAttribute("baseImageDirectory");	
		this.showAddress = (mapElement.getAttribute("showAddress") == 'true');	
		this.showCity = (mapElement.getAttribute("showCity") == 'true');	
		this.showLocationReference = (mapElement.getAttribute("showLocationReference") == 'true');
		this.popMenuWidth = parseInt(mapElement.getAttribute("popMenuWidth")) + 37;	
		this.mapIndicatorCode = mapElement.getAttribute("mapIndicatorCode");
		this.contentTopLeftLink = mapElement.getAttribute("contentTopLeftLink");
		this.contentTopRightLink = mapElement.getAttribute("contentTopRightLink");
		this.contentBottomLeftLink = mapElement.getAttribute("contentBottomLeftLink");
		this.contentBottomRightLink = mapElement.getAttribute("contentBottomRightLink");
		var dmc = mapElement.getAttribute("defaultMapCoordinate");
		this.defaultMapCoordinate = {x: parseInt(dmc.substr(0, dmc.indexOf(','))), y: parseInt(dmc.substr(dmc.indexOf(',') + 1))};			
		this.verticalMatteSize = parseInt(mapElement.getAttribute("verticalMatteSize"));
		this.horizontalMatteSize = parseInt(mapElement.getAttribute("horizontalMatteSize"));	
	},		

	createMapLinkagesFromXML: function(xml){
		for(var i = 0; i < xml.getElementsByTagName("mapLinkage").length; i++){
			var currentMapLinkage = xml.getElementsByTagName("mapLinkage")[i];
			var mapLinkageName = currentMapLinkage.getAttribute('mapLinkageName');			
			var destinationMapID = currentMapLinkage.getAttribute('mapID');		
			var areaMapShape = currentMapLinkage.getAttribute('areaShape');
			var areaMapCoordinates = currentMapLinkage.getAttribute('coordinates');	
			var overlayCoordinates = currentMapLinkage.getAttribute('overlayCoordinates');			
			var overlayLinkCoordinates = currentMapLinkage.getAttribute('overlayLinkCoordinates');
			var overlayImagePath = currentMapLinkage.getAttribute('overlayImagePath');
			var bid = currentMapLinkage.getAttribute('baseImageDirectory');	
				
			this.mapLinkages[this.mapLinkages.length] = new CJP.MapLinkage(currentMapLinkage.outerXML, mapLinkageName, this.mapID, destinationMapID, areaMapShape, areaMapCoordinates, overlayCoordinates, overlayLinkCoordinates, overlayImagePath, bid);
		}
	},	
	
	getMapLinkageByID: function(mapLinkageID){
		for(var i = 0; i < this.mapLinkages.length; i++){
			if(this.mapLinkages[i].id == mapLinkageID){return this.mapLinkages[i];}
		}
	},
	
	renderMapImage: function(container){
		if(this.mapImage != null){
			this.mapImage.style.display = 'block';
		}else{
			//var imageHTML = '<IMG id="mainMapImage'+map.id+'" src="'+map.baseImageDirectory+'map.gif" border="0" usemap="#map-'+map.id+'">';	
			var mapImage = container.ownerDocument.createElement('IMG');			
			container.appendChild(mapImage);
			mapImage.setAttribute('id', 'mainMapImage' + this.id);	
			this.mapImage = mapImage;
			this.mapImage.className = 'mapHolderImage';
		}
		this.mapImage.src = this.baseImageDirectory + 'map.gif';		
		this.mapImage.style.left = this.horizontalMatteSize + 'px';
		this.mapImage.style.top = this.verticalMatteSize + 'px';
	},
	
	renderAreaMap: function(container, locations, zoom){
		var baseZoom = zoom.getBaseZoomLevel();
		
		if(this.areaMaps[baseZoom.id] == null){
			for(var j = 0; j < zoom.zoomLevels.length; j++){
				//zoom.zoomLevels[j].zoomScalePercent = zoom.getZoomStepMultiplier(zoom.zoomLevels[j]);	
				var map = container.ownerDocument.createElement('map');
				map.id = 'map-' + this.id + zoom.zoomLevels[j].zoomSuffix;
				map.name = 'map-' + this.id + zoom.zoomLevels[j].zoomSuffix;				
				container.appendChild(map);	
								
				for(var i = 0; i < locations.length; i++){
					var location = container.ownerDocument.createElement('area');
					location.id = 'location-' + locations[i].id + zoom.zoomLevels[j].zoomSuffix;					
					map.appendChild(location);
					location.shape = locations[i].plotShape;
					location.noHref = true;
					location.className = 'mapLocation';
					location.coords = zoom.getZoomedCoordinates(zoom.zoomLevels[j].zoomScalePercent, locations[i].plottingDimensions);
					locations[i].areaMaps[zoom.zoomLevels[j].id] = location;}
					
					//zoomedAreaMapHTML += '<AREA shape="'+locations[i].plotShape+'" coords="' + zoom.getZoomedCoordinates(zoom.zoomLevels[j].step, locations[i].plottingDimensions) + '" class="mapLocation"';
					//zoomedAreaMapHTML += ' id="location-' + locations[i].id + zoom.zoomLevels[j].zoomSuffix + '" href="javascript:;" style="cursor:hand;"></AREA>';}			
				for(var i = 0; i < this.mapLinkages.length; i++){					
					var mapLinkage = container.ownerDocument.createElement('area');
					mapLinkage.id = 'mapLinkage-' + i + zoom.zoomLevels[j].zoomSuffix;					
					map.appendChild(mapLinkage);
					mapLinkage.shape = this.mapLinkages[i].areaShape;
					mapLinkage.noHref = true;
					mapLinkage.className = 'mapToMapLink';
					mapLinkage.coords = zoom.getZoomedCoordinates(zoom.zoomLevels[j].zoomScalePercent, this.mapLinkages[i].coordinates);
					
					var mapLinkageUnderlay = container.ownerDocument.createElement('img');
					container.parentNode.appendChild(mapLinkageUnderlay);
					this.mapLinkages[i].underlay = mapLinkageUnderlay;
					mapLinkageUnderlay.src = this.mapLinkages[i].overlayImagePath;
					mapLinkageUnderlay.className = 'underlayImage';}
					
					//zoomedAreaMapHTML += '<AREA shape="' + this.mapLinkages[i].areaShape + '" coords="' + zoom.getZoomedCoordinates(zoom.zoomLevels[j].step, mapLinkages[i].coordinates) + '" class="mapToMapLink"';
					//zoomedAreaMapHTML += ' id="mapLinkage-' + i + zoom.zoomLevels[j].zoomSuffix + '" href="javascript:;" style="cursor:hand;"></AREA>';}						
				
				this.areaMaps[zoom.zoomLevels[j].id] = map;
			}
		}
	},
	
	hideMap: function(){
		if(this.mapImage != null){
			this.mapImage.style.display = 'none';
		}
	}
};



CJP.MapLinkage = Class.create();
CJP.MapLinkage.prototype = {
	initialize: function(xml, mln, smid, dmid, as, c, oc, olc, oi, bid) {
		this.mapLinkageXML = xml;
	
		this.mapLinkageName = mln;
		this.sourceMapID = smid;
		this.destinationMapID = dmid;
		this.areaShape = as;
		this.coordinates = c;
		this.overlayCoordinates = oc;
		this.overlayLinkCoordinates = olc;
		this.overlayImagePath = oi;
		this.baseImageDirectory = bid;
		this.imageBaseHeight = 0;
		this.imageBaseWidth = 0;
		
		this.hasOverlayRollover = false;
		this.overlayRollover = new Image();
		
		this.underlay = null;
		
		this.zoom = null;
		this.map = null;
	},
	
	showMapLinkageOverlay: function(){
		var oc = this.zoom.getZoomedCoordinates(this.zoom.currentZoomLevel.zoomScalePercent, this.overlayCoordinates);
		
		this.underlay.style.display = 'block';
		this.underlay.style.position = 'absolute';
		this.underlay.style.left = parseInt(oc.split(',')[0]) + this.map.horizontalMatteSize + 'px';
		this.underlay.style.top = parseInt(oc.split(',')[1]) + this.map.verticalMatteSize + 'px';	
		this.underlay.style.zIndex = 2;
		this.underlay.src = this.overlayImagePath;
		this.underlay.style.display = 'block';
		
		if(this.underlay.height > 0){
			if(this.imageBaseHeight == 0){
				this.imageBaseHeight = this.underlay.height;
				this.imageBaseWidth = this.underlay.width;
			}
			this.underlay.style.height = (this.imageBaseHeight * this.zoom.currentZoomLevel.zoomScalePercent)+'px';
			this.underlay.style.width = (this.imageBaseWidth * this.zoom.currentZoomLevel.zoomScalePercent)+'px';
		}
		
		this.map.mapOverlayIsVisible = true;
	},
	
	hideMapLinkageOverlay: function(isNoneDisplayHiding){
		if(isNoneDisplayHiding){
			this.underlay.style.display = 'none';
		}else{
			this.underlay.src = this.overlayImagePath;
		}
	}
};



