var msie = (document.all && !window.opera)? true : false;


/* ================= */
/*     Map Class     */
/* ================= */

function Map(eMapContainer) {
	oMap = this;
	this.eMapContainer = eMapContainer;
	this.oMapControls = null;

	this.eMainMap = null;
	this.eZoomMap = null;
	this.eCover = null;
	this.toMove = 0;
	for (var i = 0; (eImg = eMapContainer.getElementsByTagName('img')[i]); i++) {
		if (matchClass(eImg, 'map-main')) this.eMainMap = eImg;
		if (matchClass(eImg, 'map-zoom')) this.eZoomMap = eImg;
	}
	for (var i = 0; (eDiv = eMapContainer.getElementsByTagName('div')[i]); i++) {
		if (matchClass(eDiv, 'cover')) this.eCover = eDiv;
	}
	if (this.eMainMap && this.eZoomMap && this.eCover) {
		this.eMainMap.style.zIndex = 3;
		this.eZoomMap.style.zIndex = 2;
		this.eCover.style.zIndex = 10;
		this.eCover.style.cursor = (msie)? 'hand' : 'pointer';
		this.sActiveMap = 'main';
		this.bDrag = false;
		
		this.iMainWidth = parseInt(this.eMainMap.width);
		this.eMapContainer.style.width = this.iMainWidth + 'px';
		this.iMainHeight = parseInt(this.eMainMap.height);
		this.eMapContainer.style.height = this.iMainHeight + 'px';

		this.iZoomWidth = parseInt(this.eZoomMap.width);
		this.iZoomHeight = parseInt(this.eZoomMap.height);
		this.iClickX = Math.round(this.iMainWidth / 2);
		this.iClickY = Math.round(this.iMainHeight / 2);

		this.iZoomPosX = 0;
		this.iZoomPosY = 0;
		this.iWindowX = 0;
		this.iWindowY = 0;

		addEvent(this.eCover, 'mousedown', function(evt){ oMap.mouseDown(oMap, evt) });
		addEvent(this.eCover, 'selectstart', function(evt){ return false; });
		addEvent(document, 'mousemove', function(evt){ oMap.mouseMove(oMap, evt) });
		addEvent(document, 'mouseup', function(evt){ oMap.mouseUp(oMap, evt) });

		return this;
	} else return null;
}

Map.prototype.mouseDown = function(oMap, evt) {
	if (oMap) {
		if (!evt) evt = window.event;
		oMap.iClickX = (evt.offsetX)? evt.offsetX : evt.layerX;
		oMap.iClickY = (evt.offsetY)? evt.offsetY : evt.layerY;

		oMap.iWindowX = (evt.pageY)? evt.pageX : evt.clientX;
		if (msie) oMap.iWindowX += document.body.scrollLeft;
		oMap.iWindowY = (evt.pageY)? evt.pageY : evt.clientY;
		if (msie) oMap.iWindowY += document.body.scrollTop;

		if (oMap.sActiveMap == 'main') {
			oMap.zoomIn(oMap);
		}
		oMap.bDrag = true;

		if (evt.preventDefault) evt.preventDefault();
		evt.cancelBubble = true;
		return false;
	}
}

Map.prototype.mouseMove = function(oMap, evt) {
	if (oMap && oMap.bDrag) {
		var iCurrX = (evt.pageY)? evt.pageX : evt.clientX;
		if (msie) iCurrX += document.body.scrollLeft;
		var iCurrY = (evt.pageY)? evt.pageY : evt.clientY;
		if (msie) iCurrY += document.body.scrollTop;

		oMap.iZoomPosX += iCurrX - oMap.iWindowX;
		oMap.iZoomPosY += iCurrY - oMap.iWindowY;
		oMap.iWindowX = iCurrX;
		oMap.iWindowY = iCurrY;
		oMap.moveZoomedMap();
	}
}

Map.prototype.mouseUp = function(oMap, evt) {
	if (oMap && oMap.bDrag) {
		oMap.bDrag = false;
	}
}

Map.prototype.zoomIn = function() {
	if (this) {
		this.iZoomPosX = (( this.iClickX * this.iZoomWidth / this.iMainWidth ) - (this.iMainWidth / 2)) * -1;
		this.iZoomPosY = (( this.iClickY * this.iZoomHeight / this.iMainHeight ) - (this.iMainHeight / 2)) * -1;
		if (this.oMapControls) {
			if (this.oMapControls.eBtnZoom && aMapButtons['minus-a']) {
				this.oMapControls.eBtnZoom.src = aMapButtons['minus-a'].src;
			}
		}
		this.moveZoomedMap();
		
		this.eZoomMap.style.zIndex = 4;
		this.eCover.style.cursor = 'move';

		this.sActiveMap = 'zoom';
	}
}

Map.prototype.zoomOut = function() {
	if (this) {
		this.eZoomMap.style.zIndex = 2;
		this.eCover.style.cursor = (msie)? 'hand' : 'pointer';
		this.sActiveMap = 'main';
		if (this.oMapControls) {
			if (this.oMapControls.eBtnZoom && aMapButtons['plus-a']) {
				this.oMapControls.eBtnZoom.src = aMapButtons['plus-a'].src;
			}
			if (
				this.oMapControls.eBtnUp && this.oMapControls.eBtnRight
				&& this.oMapControls.eBtnDown && this.oMapControls.eBtnLeft
			) {
				this.oMapControls.disableBtn(this.oMapControls.eBtnUp, 'up', true);
				this.oMapControls.bBtnUpDisabled = true;
				this.oMapControls.disableBtn(this.oMapControls.eBtnRight, 'right', true);
				this.oMapControls.bBtnRightDisabled = true;
				this.oMapControls.disableBtn(this.oMapControls.eBtnDown, 'down', true);
				this.oMapControls.bBtnDownDisabled = true;
				this.oMapControls.disableBtn(this.oMapControls.eBtnLeft, 'left', true);
				this.oMapControls.bBtnLeftDisabled = true;
			}
		}
	}
}

Map.prototype.correctPosition = function() {
	if (this.iZoomPosX > -2) {
		this.iZoomPosX = 0;
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnLeft, 'left', true);
			this.oMapControls.bBtnLeftDisabled = true;
		}
	} else {
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnLeft, 'left', false);
			this.oMapControls.bBtnLeftDisabled = false;
		}
	}
	if (this.iZoomPosY > -2) {
		this.iZoomPosY = 0;
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnUp, 'up', true);
			this.oMapControls.bBtnUpDisabled = true;
		}
	} else {
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnUp, 'up', false);
			this.oMapControls.bBtnUpDisabled = false;
		}
	}

	if (this.iZoomPosX + this.iZoomWidth < this.iMainWidth + 1) {
		this.iZoomPosX = (this.iZoomWidth - this.iMainWidth) * -1;
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnRight, 'right', true);
			this.oMapControls.bBtnRightDisabled = true;
		}
	} else {
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnRight, 'right', false);
			this.oMapControls.bBtnRightDisabled = false;
		}
	}
	if (this.iZoomPosY + this.iZoomHeight < this.iMainHeight + 1) {
		this.iZoomPosY = (this.iZoomHeight - this.iMainHeight) * -1;
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnDown, 'down', true);
			this.oMapControls.bBtnDownDisabled = true;
		}
	} else {
		if (this.oMapControls) {
			this.oMapControls.disableBtn(this.oMapControls.eBtnDown, 'down', false);
			this.oMapControls.bBtnDownDisabled = false;
		}
	}
}

Map.prototype.moveZoomedMap = function() {
	if (this) {
		this.correctPosition();
		this.eZoomMap.style.left = this.iZoomPosX + 'px';
		this.eZoomMap.style.top = this.iZoomPosY + 'px';
	}
}

Map.prototype.moveMap = function(iMoveX, iMoveY) {
	if (this) {
		if (this.toMove) {
			clearTimeout(this.toMove);
			this.toMove = 0;
		}
		moveZoomedMap(this, iMoveX, iMoveY);
	}
}

function moveZoomedMap(oMap, iMoveX, iMoveY) {
	if (oMap) {
		iMoveX = (iMoveX > 0)? Math.floor(iMoveX / 2) : Math.ceil(iMoveX / 2);
		iMoveY = (iMoveY > 0)? Math.floor(iMoveY / 2) : Math.ceil(iMoveY / 2);
		if (iMoveX || iMoveY) {
			oMap.iZoomPosX += iMoveX;
			oMap.iZoomPosY += iMoveY;
			oMap.moveZoomedMap();
			oMap.toMove = setTimeout('moveZoomedMap(aMaps["' + oMap.eMapContainer.id + '"], ' + iMoveX + ', ' + iMoveY + ')', 50)
		}
		oMap.toMove = 0;
	}
}


/* ==================== */
/*     Map Controls     */
/* ==================== */

var aMapBtnNames = ['up', 'right', 'down', 'left', 'plus', 'minus'];
var aMapButtons = new Array();
for (var i = 0; i < aMapBtnNames.length; i++) {
	// disabled
	aMapButtons[aMapBtnNames[i]] = new Image();
	aMapButtons[aMapBtnNames[i]].src = '/i/btns/btn-' + aMapBtnNames[i] + '.gif';
	// active
	aMapButtons[aMapBtnNames[i] + '-a'] = new Image();
	aMapButtons[aMapBtnNames[i] + '-a'].src = '/i/btns/btn-' + aMapBtnNames[i] + '-a.gif';
}


function MapControls(eMapControlsContainer) {
	if (eMapControlsContainer) {
		oMapControls = this;
		this.eMapControlsContainer = eMapControlsContainer;
		// get map
		this.oMap = null;
		if (eMapControlsContainer.className.indexOf('for-') >= 0) {
			var sMapId = eMapControlsContainer.className.substr(eMapControlsContainer.className.indexOf('for-') + 4)
			if (sMapId.indexOf(' ') > 0) sMapId = sMapId.substring(0, sMapId.indexOf(' '));
			if (aMaps[sMapId]) {
				this.oMap = aMaps[sMapId];
				this.oMap.oMapControls = oMapControls;
			}
		}
		if (!this.oMap) return null;
		
		// catch buttons
		this.eBtnUp = null;
		this.bBtnUpDisabled = true;
		this.eBtnRight = null;
		this.bBtnRightDisabled = true;
		this.eBtnDown = null;
		this.bBtnDownDisabled = true;
		this.eBtnLeft = null;
		this.bBtnLeftDisabled = true;
		this.eBtnZoom = null;
		this.bBtnZoomDisabled = true;
		for (var i = 0; (eImg = eMapControlsContainer.getElementsByTagName('img')[i]); i++) {
			if (matchClass(eImg, 'up')) {
				this.eBtnUp = eImg;
				addEvent(this.eBtnUp, 'click', function() { oMapControls.clickUp(oMapControls) } );
			}
			if (matchClass(eImg, 'right')) {
				this.eBtnRight = eImg;
				addEvent(this.eBtnRight, 'click', function() { oMapControls.clickRight(oMapControls) } );
			}
			if (matchClass(eImg, 'down')) {
				this.eBtnDown = eImg;
				addEvent(this.eBtnDown, 'click', function() { oMapControls.clickDown(oMapControls) } );
			}
			if (matchClass(eImg, 'left')) {
				this.eBtnLeft = eImg;
				addEvent(this.eBtnLeft, 'click', function() { oMapControls.clickLeft(oMapControls) } );
			}
			if (matchClass(eImg, 'zoom')) {
				this.eBtnZoom = eImg;
				if (aMapButtons['plus-a'] && aMapButtons['minus-a']) {
					this.eBtnZoom.src = (this.oMap.sActiveMap == 'main')? aMapButtons['plus-a'].src : aMapButtons['minus-a'].src;
					this.bBtnZoomDisabled = false;
				}
				addEvent(this.eBtnZoom, 'click', function() { oMapControls.clickZoom(oMapControls) } );
			}
		}

	} else return null;
}

MapControls.prototype.clickZoom = function(oMapControls) {
	if (oMapControls && !oMapControls.bBtnZoomDisabled && oMapControls.oMap) {
		if (oMapControls.oMap.sActiveMap == 'main') oMapControls.oMap.zoomIn();
		else oMapControls.oMap.zoomOut();
	}
}

MapControls.prototype.clickUp = function(oMapControls) {
	if (oMapControls && !oMapControls.bBtnUpDisabled && oMapControls.oMap) oMapControls.oMap.moveMap(0, 128);
}

MapControls.prototype.clickRight = function(oMapControls) {
	if (oMapControls && !oMapControls.bBtnRightDisabled && oMapControls.oMap) oMapControls.oMap.moveMap(-256, 0);
}

MapControls.prototype.clickDown = function(oMapControls) {
	if (oMapControls && !oMapControls.bBtnDownDisabled && oMapControls.oMap) oMapControls.oMap.moveMap(0, -128);
}

MapControls.prototype.clickLeft = function(oMapControls) {
	if (oMapControls && !oMapControls.bBtnLeftDisabled && oMapControls.oMap) oMapControls.oMap.moveMap(256, 0);
}

MapControls.prototype.disableBtn = function(eBtn, sImgName, bDisabled) {
	if (eBtn && aMapButtons[sImgName] && aMapButtons[sImgName + '-a']) {
		eBtn.src = (bDisabled)? aMapButtons[sImgName].src : aMapButtons[sImgName + '-a'].src;
	}
}


/* Map initialization */
var aMaps = new Array();
var aMapControls = new Array();
var iZMapCnt = 0;
function initMap() {
	for (var i = 0; (eDiv = document.getElementsByTagName('div')[i]); i++) {
		if (matchClass(eDiv, 'map-container')) {
			var sMapId = eDiv.id;
			if (!sMapId || !sMapId.length) {
				sMapId = 'ZMap' + iZMapCnt;
				eDiv.id = sMapId;
			}
			iZMapCnt++;
			aMaps[sMapId] = new Map(eDiv);
		}
	}
	for (var i = 0; (eTable = document.getElementsByTagName('table')[i]); i++) {
		if (matchClass(eTable, 'map-controls')) {
			aMapControls[aMapControls.length] = new MapControls(eTable);
		}
	}
}

if (window.addEvent) addEvent(window, 'load', initMap);

