//- Instinct_Layer_NS -//
function Instinct_Layer_NS(oLayer) {
//- Model - Link -//
	//- static -//
	Instinct_Layer__getLayer = Instinct_Layer_NS__getLayer;
	
	//- method -//	
	oLayer.clearBuffer = clearBuffer;
	oLayer.appendBuffer = appendBuffer;
	oLayer.flushBuffer = flushBuffer;
	oLayer.replaceContent = replaceContent;
	oLayer.generateLayer = generateLayer;
	oLayer.streamLayer = streamLayer;
	oLayer.onLoad = onLoad;
	oLayer.show = show;
	oLayer.hide = hide;
	oLayer.moveTo = moveTo;
	oLayer.moveBy = moveBy;
	oLayer.clip = clip;

	//- get -//
	oLayer.getLayerParent = getLayerParent;
	oLayer.getLayer = Instinct_Layer__getLayer;
	oLayer.getVisibility = getVisibility;
	
	//- get : location -//
	oLayer.getOffsetLeft = getOffsetLeft;
	oLayer.getOffsetTop = getOffsetTop;
	oLayer.getLeft = getLeft;
	oLayer.getTop = getTop;
	oLayer.getWidth = getWidth;
	oLayer.getHeight = getHeight;
	oLayer.getRight = getRight;
	oLayer.getBottom = getBottom;
	oLayer.getZIndex = getZIndex;
	
	//- get : clip -//
	oLayer.getClipLeft = getClipLeft;
	oLayer.getClipTop = getClipTop;
	oLayer.getClipRight = getClipRight;
	oLayer.getClipBottom = getClipBottom;
	oLayer.getClipWidth = getClipWidth;
	oLayer.getClipHeight = getClipHeight;
	
	//- set -//
	oLayer.setClipHeight = setClipHeight;
	oLayer.setClipWidth = setClipWidth;
	oLayer.setZIndex = setZIndex;
	oLayer.setBgColor = setBgColor;
	oLayer.setBgImage = setBgImage;
	
	//- set : event -//
	oLayer.setOnMouseOver = setOnMouseOver;
	oLayer.setOnMouseOut = setOnMouseOut;
	oLayer.setOnClick = setOnClick;
	
	//- other -//
	oLayer.Restore = Restore;
	
//- Model - Implementation -//
	//- Implementation : Method -//
	function clearBuffer() {
		this.buffer = '';
	}
	
	function appendBuffer(cContent) {
		this.buffer += cContent;
	}
	
	function flushBuffer() {
		var oDoc = this.oLayer.document;
		oDoc.open( "text/html");
		oDoc.write( this.buffer);
		oDoc.close();
	}
	
	function replaceContent(cContent) { 
		this.clearBuffer();
		this.appendBuffer( sContent);
		this.flushBuffer();
	}
	
	function generateLayer() {
		var cAttrib = 'style="position:absolute;visibility:hidden;';
		if (this.width > 0) { cAttrib += ' width="' + this.width; }
		if (this.height > 0) { cAttrib += ' height="' + this.height; }
		cAttrib += '"';
		
		var oLayer = new Layer(this.width);
		oLayer.document.writeln(this.buffer);
		oLayer.document.close();
		this.layerName = oLayer.name;
		//this.divName = oLayer.name;
	} 
	function streamLayer() {
		var cAttrib = 'style="position:absolute;visibility:hidden;';
		if (this.width > 0) { cAttrib += ' width="' + this.width + 'px'; }
		if (this.height > 0) { cAttrib += ' height="' + this.height + 'px'; }
		cAttrib += '"';
		document.writeln('<layer name="' + this.divName + '" ' + cAttrib + ' visibility="hidden">');
		document.writeln(this.buffer);
		document.writeln('</layer>');
	} 
	
	function onLoad() {
		this.oLayer = document.layers[this.layerName];
		window.onresize = this.Restore;
	}
	
	function show() { 
		this.oLayer.visibility = "show";
		if (this.oEventLayer != null) {
			this.oEventLayer.show()
		}
	}
	
	function hide() { 
		this.oLayer.visibility =  "hide";
		if (this.oEventLayer != null) {
			this.oEventLayer.hide();
		}
	}
	
	function moveTo(nX, nY) { 
		var oLayer = this.oLayer;
		oLayer.left = nX; 
		oLayer.top = nY;
		if (this.oEventLayer != null) {
			this.oEventLayer.moveTo(nX, nY);
		}
	}
	
	function moveBy(nX, nY) {
		var oLayer = this.oLayer;
		oLayer.left += nX;	//?:oLayer.oLayer.left
		oLayer.top += nY;		//?:oLayer.oLayer.top
		if (this.oEventLayer != null) {
			this.oEventLayer.moveBy(nX, nY);
		}
	} 
	
	function clip(clipLeft, clipTop, clipRight, clipBottom) {
		var oLayer = this.oLayer;
		oLayer.clip.left = clipLeft;
		oLayer.clip.top = clipTop;
		oLayer.clip.right = clipRight;
		oLayer.clip.bottom = clipBottom;
		if (this.oEventLayer != null) {
			this.oEventLayer.clip(clipLeft, clipTop, clipRight, clipBottom);
		}
	}

	//- Implementation : Get -//
	function getLayerParent(cName) {
		var oLayer = getLayer(cName);
		if (oLayer != null) {
			return(oLayer.parentObject);
		} else {
			return(null);
		}
	} 
	
	function getVisibility() {
		var oLayer = this.oLayer;
		if (oLayer.visibility == "show") {
			return("visible")
		} else if (oLayer.visibility == "hide") {
			return("hidden")
		} else {
			return(oLayer.visibility);
		}
	}

	//- Implemenration : Get_Location -//
	function getOffsetLeft() {
		return(this.oLayer.left);
	}
	function getOffsetTop() {
		return(this.oLayer.top);
	}

	function getLeft() {
		return(this.oLayer.pageX);
	}
	
	function getTop() {
		return(this.oLayer.pageY);
	}
	
	function getWidth() {
		var oLayer = this.oLayer;
		if (oLayer.document.width) {
			return(oLayer.document.width);
		} else {
			return(oLayer.clip.right - oLayer.clip.left);
		}			
	}
	
	function getHeight() {
		var oLayer = this.oLayer;
		if (oLayer.document.height) {
			return(oLayer.document.height);
		} else {
			return(oLayer.clip.bottom - oLayer.clip.top);
		}
	}
	
	function getRight() {
		return(this.oLayer.left + this.getWidth());
	}
	function getBottom() {
		return(this.oLayer.top + this.getHeight());
	}
	function getZIndex() {
		return(this.oLayer.zIndex);
	}

	//- Implementation : Get_Clip -//
	function getClipLeft() {
		return(this.oLayer.clip.left);
	}
		
	function getClipTop() {
		return(this.oLayer.clip.top);
	}
	
	function getClipRight() {
		return(this.oLayer.clip.right);
	}
	
	function getClipBottom() {
		return(this.oLayer.clip.bottom);
	}
	
	function getClipWidth() {
		return(this.oLayer.clip.width);
	}
	
	function getClipHeight() {
		return(this.oLayer.clip.height);
	}

	//- Implementation : Set -//
	function setClipHeight(nHeight) {
		this.height = nHeight;
		this.oLayer.clip.height = nHeight;
	}
	function setClipWidth(nWidth) {
		this.width = nWidth;
		this.oLayer.clip.width = nWidth;
	}
	function setZIndex(nZ) { 
		if (!isNaN(parseInt(nZ))) {
			this.oLayer.zIndex = nZ;
			if (this.oEventLayer != null) {
				this.oEventLayer.setZIndex(nZ);
			}
		}
	}
	
	function setBgColor(cColor) {
		if (cColor.length > 0) {
			this.oLayer.bgColor = cColor;
		}
	}
	
	function setBgImage(cSource) {
		this.oLayer.background.src = cSource;
	}
		
	function CreateEventLayer(oLayer) {
		oLayer.oEventLayer = new Instinct_Layer(oInstinct, 'inv_' + oLayer.name);

		oLayer.oEventLayer.oLayer = new Layer(oLayer.width);
		oLayer.oEventLayer.setZIndex(oLayer.getZIndex() + 1);

		oLayer.oEventLayer.oLayer.parentObject = oLayer.oLayer.parentObject;
		oLayer.oEventLayer.setClipHeight(oLayer.getHeight());
		oLayer.oEventLayer.oLayer.moveTo(oLayer.getLeft(), oLayer.getTop());
		if (oLayer.getVisibility() == 'visible') {
			oLayer.oEventLayer.show();
		} else {
			oLayer.oEventLayer.hide();
		}
	}
		
	function setOnMouseOver(hEvent) {
		if (this.oEventLayer == null) {
			CreateEventLayer(this);
		}
		this.oEventLayer.oLayer.onmouseover = hEvent;
	}
	
	function setOnMouseOut(hEvent) {
		if (this.oEventLayer == null) {
			CreateEventLayer(this);
		}
		this.oEventLayer.oLayer.onmouseout = hEvent;
	}
	
	function setOnClick(hEvent) {
		if (this.oEventLayer == null) {
			CreateEventLayer(this);
		}
		this.oEventLayer.oLayer.document.onmousedown = hEvent;
		this.oEventLayer.oLayer.document.parentObject = this.oLayer.parentObject;			
	}

// -- Other -- //
	function Restore() { 
		window.location.href = window.location.href;
	}
}

//- static -//
function Instinct_Layer_NS__getLayer(cName) {
	var oLayer = document.layers[cName];
	if (oLayer == 'undefined') {
		for(var hLayer = 0; hLayer < document.layers.length; hLayer++) {
			var oLayer = document.layers[hLayer].parentObject;
			if ((oLayer == 'undefined') || (oLayer.menuBarLayer == 'undefined')) {continue;}

			if (oLayer.menuBarLayer.divName == cName) {return document.layers[hLayer];}
		}
	}
}
	

