
//var mapLyr_mapLyrID = 'mapLayer', mapLyr_mapImgID;
//var mapLyr_mapImg;
//var mapLyr_mapTop, mapLyr_mapLeft, mapLyr_mapWidth, mapLyr_mapHeight;
var mapLyr_mapToMove = null;

function MapLayerObject (mapLyrID, mapLeft, mapTop, mapWidth, mapHeight, bgColor, moveable) {
	if (ns6) {	mapWidth += 1; }
	var mapImgID = 'mapImage' + mapLyrID;
	
	var mapImgStr = '<img  name="' + mapImgID + '" id="' + mapImgID + '" border=0 vspace="0" hspace="0" border=0 src="maptools/images/spacer.gif" width="' 
		+ mapWidth + '" height="' + mapHeight + '" usemap="#IMAGEMAP">';
	if (ns4) {
		mapImgStr = '<form name="mapForm">' + mapImgStr + '</form>';
	}
	createLayer(mapLyrID, null, mapLeft, mapTop, mapWidth, mapHeight, mapImgStr, '');
	if (ie4 || ie5 ) {
		this.mapLyrElm = document.all[mapLyrID];
		this.mapLyr_mapImg = document.all[mapImgID];
	} else if (ns6) {
		this.mapLyrElm = document.getElementById(mapLyrID);
		this.mapLyr_mapImg = document.getElementById(mapImgID);
	} else if (ns4) {
		this.mapLyrElm = document.layers[mapLyrID];
		this.mapLyr_mapImg = document.layers[mapLyrID].document.forms[0][mapImgID];
		if (moveable) {
			this.mapLyrElm.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);	
		}
	}
	
	if (moveable) {
		this.mapLyrElm.onmousedown = mapLyrMouseDown;
		this.mapLyrElm.onmouseup = mapLyrMouseUp;
		this.mapLyrElm.onmousemove = mapLyrMouseMove;
		this.moveable = true;
	}
	this.mapLyr = new DynLayer(mapLyrID);

	this.mapLyrID = mapLyrID;
	this.mapImgID = mapImgID;
	this.mapLeft = mapLeft;
	this.mapTop = mapTop;
	this.mapWidth = mapWidth;
	this.mapHeight = mapHeight;
		
	this.createMapBorder = mapLyrCreateMapBorder;
	this.setMapImage = mapLyrSetMapImage;
	this.getMapLayerID = mapLyrGetMapLayerID;
	this.getMapImageID = mapLyrGetMapImageID;
	this.getMapLeft = mapLyrGetMapLeft;
	this.getMapTop = mapLyrGetMapTop;
	this.getMapWidth = mapLyrGetMapWidth;
	this.getMapHeight = mapLyrGetMapHeight;
	this.show = mapLyrShow;
	this.hide = mapLyrHide;
	this.showMapBorder = mapLyrShowMapBorder;
	this.hideMapBorder = mapLyrHideMapBorder;
	this.moveTo = mapLyrMoveTo;
	this.moveBy = mapLyrMoveBy;
	this.resizeTo = mapLyrResizeTo;
	
	this.setDragableMap = mapLyrSetDragableMap;
}

function mapLyrSetDragableMap (mapObject) {
	if (mapObject != null && mapObject != '') {
		mapLyr_mapToMove = mapObject;
	}
}
function mapLyrCreateMapBorder (borderWidth, borderColor) {
	this.borders = new Array(4);
	
	createLayer('mapBorderTop'+this.mapLyrID, null, this.mapLeft, this.mapTop-borderWidth, this.mapWidth, borderWidth, '', borderColor);
	this.borders[0] = new DynLayer('mapBorderTop'+this.mapLyrID);
	createLayer('mapBorderButtom'+this.mapLyrID, null, this.mapLeft, (this.mapTop+this.mapHeight), this.mapWidth, borderWidth, '', borderColor);	
	this.borders[1] = new DynLayer('mapBorderButtom'+this.mapLyrID);
	createLayer('mapBorderLeft'+this.mapLyrID, null, this.mapLeft-borderWidth, this.mapTop-borderWidth, borderWidth, this.mapHeight+ (2*borderWidth), '', borderColor);
	this.borders[2] = new DynLayer('mapBorderLeft'+this.mapLyrID);
	createLayer('mapBorderRight'+this.mapLyrID, null, (this.mapLeft+this.mapWidth), this.mapTop-borderWidth, borderWidth, this.mapHeight+ (2*borderWidth), '', borderColor);
	this.borders[3] = new DynLayer('mapBorderRight'+this.mapLyrID);
	this.haveBorder = true;
	
	this.borderWidth = borderWidth;
	this.borderColor = borderColor;
}
function mapLyrUpdateMapBorder (mapLyrID, l, t, w, h, borderWidth, borderColor, oldBordersArray) {
	
//	this.borders = new Array(4);
	destroyLayer('mapBorderTop'+mapLyrID);
	destroyLayer('mapBorderButtom'+mapLyrID);
	destroyLayer('mapBorderLeft'+mapLyrID);
	destroyLayer('mapBorderRight'+mapLyrID);

	createLayer('mapBorderTop'+mapLyrID, null, l, t-borderWidth, w, borderWidth, '', borderColor);
	oldBordersArray[0] = new DynLayer('mapBorderTop'+mapLyrID);
	createLayer('mapBorderButtom'+mapLyrID, null, l, (t+h), w, borderWidth, '', borderColor);	
	oldBordersArray[1] = new DynLayer('mapBorderButtom'+mapLyrID);
	createLayer('mapBorderLeft'+mapLyrID, null, l-borderWidth, t-borderWidth, borderWidth,h+ (2*borderWidth), '', borderColor);
	oldBordersArray[2] = new DynLayer('mapBorderLeft'+mapLyrID);
	createLayer('mapBorderRight'+mapLyrID, null, (l+h), t-borderWidth, borderWidth, h+(2*borderWidth), '', borderColor);
	oldBordersArray[3] = new DynLayer('mapBorderRight'+mapLyrID);
	
	this.borderWidth = borderWidth;
	this.borderColor = borderColor;
	this.bordes = oldBordersArray;
}

function mapLyrSetMapImage (imgSrc) {
	this.mapLyr_mapImg.src = imgSrc;
	if (ie4 || ie5) {
		this.mapImageSrc = imgSrc;
	}
}
function mapLyrGetMapLayerID () {
	return this.mapLyrID;
}
function mapLyrGetMapImageID () {
	return this.mapImgID;
}
function mapLyrGetMapLeft () {
	return this.mapLeft;
}
function mapLyrGetMapTop () {
	return this.mapTop;
}
function mapLyrGetMapWidth () {
	return this.mapWidth;
}
function mapLyrGetMapHeight () {
	return this.mapHeight;
}
function mapLyrShow () {
	this.mapLyr.show();
}
function mapLyrHide () {
	this.mapLyr.hide();
}
function mapLyrShowMapBorder () {
	if (this.haveBorder) {
		for (var i=0; i < this.borders.length; i++) {
			this.borders[i].show();
		}
	}
}
function mapLyrHideMapBorder () {
	if (this.haveBorder) {
		for (var i=0; i < this.borders.length; i++) {
			this.borders[i].hide();
		}
	}
}
function mapLyrMoveTo (left, top) {
	var dx = left - this.mapLeft;
	var dy = top - this.mapTop;
	this.mapLyr.moveTo(left, top);

	if (this.haveBorder) {
		for (var i=0; i < this.borders.length; i++) {
			this.borders[i].moveBy(dx, dy);
		}
	}

	this.mapLeft = left;
	this.mapTop = top;
}
function mapLyrMoveBy (dx, dy) {
	this.mapLyr.moveBy(dx, dy);
	if (this.haveBorder) {
		for (var i=0; i < this.borders.length; i++) {
			this.borders[i].moveBy(dx, dy);
		}
	}
	this.mapLeft += dx;
	this.mapTop += dy;
}
function mapLyrResizeTo (w, h) {	
	if (ns4 || ns6) {
		this.mapLyr_mapImg.width = w;
		this.mapLyr_mapImg.height = h;
		this.mapLyr.resizeTo(w, h);
	} else if (ie4 || ie5) {
		var mapImgStr = '<img  name="' + this.mapImgID + '" id="' + this.mapImgID + '" border=0 vspace="0" hspace="0" border=0 src="' + this.mapImageSrc + '" width="' 
			+ w + '" height="' + h + '">';
		destroyLayer(this.mapLyrID);
		createLayer(this.mapLyrID, null, this.mapLeft, this.mapTop, w, h, mapImgStr, 'red');
		this.mapLyr = new DynLayer(this.mapLyrID);
	}
	this.mapWidth = w;
	this.mapHeight = h;
	if (this.haveBorder) {
		mapLyrUpdateMapBorder(this.mapLyrID, this.mapLeft, this.mapTop, w, h, this.borderWidth, this.borderColor, this.borders);
	}
	if (ns4) {
		this.mapLyrElm = document.layers[this.mapLyrID];
		this.mapLyr_mapImg = document.layers[this.mapLyrID].document.forms[0][this.mapImgID];	
	} else if (ie4 || ie5) {
		this.mapLyrElm = document.all[this.mapLyrID];
		this.mapLyr_mapImg = document.all[this.mapImgID];
	}
	if (this.moveable) {
		if (ns4) {
			this.mapLyrElm.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);	
		} 

		this.mapLyrElm.onmousedown = mapLyrMouseDown;
		this.mapLyrElm.onmouseup = mapLyrMouseUp;
		this.mapLyrElm.onmousemove = mapLyrMouseMove;
	}
	
}

function mapLyrMouseDown (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;
		if (mapLyr_mapToMove == null) return false;
		this.startx = x;
		this.starty = y;
		this.dragActive = true;
		return false;
	}
	return true;
}
function mapLyrMouseUp (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;

		if (this.dragActive) {
			this.dragActive = false;
		}
		return false;
	}
	return true;
}
function mapLyrMouseMove (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;
		if (this.dragActive) {
			var dx = x - this.startx;
			var dy = y - this.starty;
			eval(mapLyr_mapToMove).moveBy(dx, dy);
			this.startx = x;
			this.starty = y;
		}
		return false;
	}
	return true;
}
