Map = Base.extend({
	config: {},
	oSmoothHL: null,
	oCurDesc: null,
	bDisplayingDesc: null,
	constructor: function(oConfig) {

		this.config = oConfig;

		this.oImg = $$(this.config.img).first();
		this.oOverlay = $$(this.config.imgoverlay).first();
		this.oMap = $$(this.config.map).first();

		Element.clonePosition(
			this.oOverlay,
			this.oImg
		);
		
		this.oMap.select("area").each(function(oArea) {

			oArea.oParent = this;
			oArea.sRegion = oArea.alt;
			oArea.alt = "";

			oTemp = $$(this.config.descriptions + " DIV." + oArea.sRegion);
			if(oTemp.size() > 0) {
				oArea.oDesc = oTemp.first();
			} else {
				oArea.oDesc = false;
			}

			oArea.img = new Image();
			oArea.img.src = "map/images/" + oArea.sRegion + ".gif";
			//document.body.appendChild(oArea.img);

			Event.observe(
				oArea,
				"mouseover",
				this.mouseEnter.bindAsEventListener(oArea)
			);

			Event.observe(
				oArea,
				"mouseout",
				this.mouseLeave.bindAsEventListener(oArea)
			);

			if(oArea.oDesc) {
				if((oClose = oArea.oDesc.select("DIV.close").first())) {
					oArea.oClose = oClose;
					Event.observe(
						oArea.oClose,
						"click",
						this.mouseClickClose.bindAsEventListener(oArea)
					);
				}

				Event.observe(
					oArea,
					"click",
					this.mouseClick.bindAsEventListener(oArea)
				);
			}

		}.bind(this));
	},
	mouseEnter: function() {

		if(!this.oParent.bDisplayingDesc) {
			if(this.oParent.oSmoothHL) {
				this.oParent.oSmoothHL.stop();
				this.oParent.oSmoothHL = null;
			}

			this.oParent.oOverlay.src="map/images/" + this.sRegion + ".gif";
		}
	},
	mouseLeave: function() {

		if(!this.oParent.bDisplayingDesc) {
			if(this.oParent.oSmoothHL) {
				this.oParent.oSmoothHL.stop();
				this.oParent.oSmoothHL = null;
			}
			
			this.oParent.oSmoothHL = new PeriodicalExecuter(
				this.oParent.closeHighlight.bind(this),
				this.oParent.config.smoothCloseHighlight
			);
		}

		//console.log("LEAVE:" + this.alt);
	},
	mouseClick: function() {

		if(!this.oParent.bDisplayingDesc) {
			if(this.oDesc) {

				// description div is associated

				if(this.oParent.oSmoothHL) {
					this.oParent.oSmoothHL.stop();
					this.oParent.oSmoothHL = null;
				}

				this.oDesc.style.display = "block";
				this.oDesc.absolutize();
				this.oParent.oOverlay.src="map/images/" + this.sRegion + ".gif";
				this.oParent.oCurDesc = this.oDesc;

				this.oParent.bDisplayingDesc = true;

			} else {

				// no description div
				if(this.oParent.oCurDesc) {
					this.oParent.oCurDesc.style.display = "none";
				}
			}
		}
	},
	mouseClickClose: function() {

		if(this.oDesc) {

			// description div is associated

			this.oDesc.style.display = "none";
			this.oParent.oCurDesc = null;
			this.oParent.bDisplayingDesc = false;
			var _temp = this.oParent.closeHighlight.bind(this);
			_temp();
		}
	},
	closeHighlight: function() {
		//console.log("CLOSING:" + this.alt);
		if(this.oParent.oSmoothHL) {
			this.oParent.oSmoothHL.stop();
			this.oParent.oSmoothHL = null;
		}

		oEffects = [];

		new Effect.Fade(
			this.oParent.oOverlay, {
				duration: 0.2, 
				"afterFinish": function() {
					this.oParent.oOverlay.src = "map/images/blank.gif";
					new Effect.Appear(this.oParent.oOverlay, {duration: 0});
				}.bind(this)
			}
		);
	}
});
