//メニューの表示制御（システム）
function MenuController(target, mode, start, stop, moveSize, speed, timeout, opened) {
	//Member data
	this.targetOpenClose = target;
	this.modeOpenClose = mode;
	this.startOpenClose = start;
	this.stopOpenClose = stop;
	this.moveSizeOpenClose = moveSize;
	this.speedOpenClose = speed;
	this.timeoutOpenClose = timeout;
	this.timerHandle = null;
	this.timeoutHandle = null;
	this.isOpen = opened;
	//Member method
	this.moveToOpen = moveOpen;
	this.moveToClose = moveClose;
	this.childHidden  = childHidden;
	this.childVisible = childVisible;
	this.closeByTimeout = timeoutClose;
	//Delegate method
	this.targetOpenClose.onmouseover = eventMouseOver;
	this.targetOpenClose.onmouseout = eventMouseOut;
	//stuck Array
	this.index = Function.__MenuControllers.length;	
	Function.__MenuControllers[Function.__MenuControllers.length] = this;
	this.targetOpenClose.MenuControllerIndex = this.index;

	if(opened && timeout > 0) {
		this.closeByTimeout();
	}
}
Function.prototype.__MenuControllers = new Array();

function eventMouseOver() {
	var index = this.MenuControllerIndex;
	var obj = Function.__MenuControllers[index];
	if(obj.timeoutHandle != null) {
		clearInterval(obj.timeoutHandle);
		obj.timeoutHandle = null;
	}
}

function eventMouseOut() {
	var index = this.MenuControllerIndex;
	var obj = Function.__MenuControllers[index];
	if(obj.timeoutHandle == null) {
		if(obj.timeoutOpenClose > 0) {
			obj.closeByTimeout();
		}
	}
}

function moveOpen() {
	if(!this.isOpen && this.timerHandle == null) {
		this.isOpen = true;
		var mode = this.modeOpenClose.toLowerCase();
		this.childHidden();
		this.targetOpenClose.style.visibility = 'visible';
		if(mode == 'y') {
			this.timerHandle = setInterval("timerOpenY("+this.index+")", this.speedOpenClose);
		}
		if(mode == 'x') {
			this.timerHandle = setInterval("timerOpenX("+this.index+")", this.speedOpenClose);
		}
	}
}

function moveClose() {
	if(this.isOpen && this.timerHandle == null) {
		var mode = this.modeOpenClose.toLowerCase();
		this.childHidden();
		if(mode == 'y') {
			var start = this.targetOpenClose.style.height;
			this.timerHandle = setInterval("timerCloseY("+this.index+")", this.speedOpenClose);
		}
		if(mode == 'x') {
			var start = this.targetOpenClose.style.width;
			this.timerHandle = setInterval("timerCloseX("+this.index+")", this.speedOpenClose);
		}
	}
}

function childHidden() {
	var targetChild = this.targetOpenClose.childNodes;
	for(var i=0;i<targetChild.length;i++) {
		if(targetChild[i].style) {
			targetChild[i].style.visibility = 'hidden';
		}
	}
}

function childVisible() {
	var targetChild = this.targetOpenClose.childNodes;
	for(var i=0;i<targetChild.length;i++) {
		if(targetChild[i].style) {
			targetChild[i].style.visibility = 'visible';
		}
	}
}

function timeoutClose() {
	if(this.timeoutOpenClose > 0) {
		this.timeoutHandle = setInterval("catchTimeout("+this.index+")", this.timeoutOpenClose);
	}
}

function catchTimeout(index) {
	var obj = Function.__MenuControllers[index];
	var mode = obj.modeOpenClose.toLowerCase();
	clearInterval(obj.timeoutHandle);
	obj.timeoutHandle = null;
	obj.childHidden();
	if(mode == 'y') {
		var start = obj.targetOpenClose.style.height;
		obj.timerHandle = setInterval("timerCloseY("+index+")", obj.speedOpenClose);
	}
	if(mode == 'x') {
		var start = obj.targetOpenClose.style.width;
		obj.timerHandle = setInterval("timerCloseX("+index+")", obj.speedOpenClose);
	}
}

//表示（上下）
function timerOpenY(index) {
	var mainObj = Function.__MenuControllers[index];
	var target = mainObj.targetOpenClose;
	var end = mainObj.stopOpenClose;
	var length = mainObj.moveSizeOpenClose;
	var timeout = mainObj.timeoutOpenClose;
	var hTimer = mainObj.timerHandle;
	var height = target.style.height.toLowerCase().indexOf('px') > 0 ? target.style.height.substring(0, target.style.height.toLowerCase().toLowerCase().indexOf('px')) : target.style.height;
	
	height = new Number(height);
	height+=length;
	if(height > end) {
		height = end;
	} 
	target.style.height = height+'px';
	
	if(height >= end) {
		clearInterval(hTimer);
		mainObj.timerHandle = null;
		if(timeout > 0) {
			mainObj.closeByTimeout();
		}
		mainObj.childVisible();
	}
}

//表示（左右）
function timerOpenX(index) {
	var mainObj = Function.__MenuControllers[index];
	var target = mainObj.targetOpenClose;
	var end = mainObj.stopOpenClose;
	var length = mainObj.moveSizeOpenClose;
	var timeout = mainObj.timeoutOpenClose;
	var hTimer = mainObj.timerHandle;
	var width = target.style.width.toLowerCase().indexOf('px') > 0 ? target.style.width.substring(0, target.style.width.toLowerCase().toLowerCase().indexOf('px')) : target.style.width;

	width = new Number(width);
	width+=length;
	if(width > end) {
		width = end;
	} 
	target.style.width = width+'px';
	
	if(width >= end) {
		clearInterval(hTimer);
		mainObj.timerHandle = null;
		if(timeout > 0) {
			mainObj.closeByTimeout();
		}
		mainObj.childVisible();
	}
}

//非表示（上下）
function timerCloseY(index) {
	var mainObj = Function.__MenuControllers[index];
	var target = mainObj.targetOpenClose;
	var start = mainObj.startOpenClose;
	var length = mainObj.moveSizeOpenClose;
	var timeout = mainObj.timeoutOpenClose;
	var hTimer = mainObj.timerHandle;
	var height = target.style.height.toLowerCase().indexOf('px') > 0 ? target.style.height.substring(0, target.style.height.toLowerCase().toLowerCase().indexOf('px')) : target.style.height;

	height = new Number(height);
	height-=length;
	if(height < start) {
		height = start;
	} 
	target.style.height = height+'px';
	
	if(height <= start) {
		clearInterval(hTimer);
		mainObj.timerHandle = null;
		target.style.visibility = 'hidden';
		mainObj.isOpen = false;
	}
}

//非表示（左右）
function timerCloseX(index) {
	var mainObj = Function.__MenuControllers[index];
	var target = mainObj.targetOpenClose;
	var start = mainObj.startOpenClose;
	var length = mainObj.moveSizeOpenClose;
	var timeout = mainObj.timeoutOpenClose;
	var hTimer = mainObj.timerHandle;
	var width = target.style.width.toLowerCase().indexOf('px') > 0 ? target.style.width.substring(0, target.style.width.toLowerCase().toLowerCase().indexOf('px')) : target.style.width;

	width = new Number(width);
	width-=length;
	if(width < start) {
		width = start;
	} 
	target.style.width = width+'px';
	
	if(width <= start) {
		clearInterval(hTimer);
		mainObj.timerHandle = null;
		target.style.visibility = 'hidden';
		mainObj.isOpen = false;
	}
}
