/*
 * Kido JavaScript framework, version 1.0
 * Copyright (c) 2006 Lee Won-Gyoon <mail.kido@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * For details, see the Kido web site: http://www.kidoScript.net/
 * 
*******************************************************************************/

var kido = {
	Version : "1.0"
}

/**
 Array
*****************************/
Array.prototype.index = new Object;

Array.prototype.push = function(_object, _makeIndex, _primaryKey) {
	var lastIndex = this.length;
	this[lastIndex] = _object;
	if (_makeIndex==true) {
		if (_primaryKey!=null) {
			this.index[_primaryKey] = lastIndex;
		} else {
			this.index[_object] = lastIndex;
		}
	}
	return _object;
}

Array.prototype.remove = function(_object) {
	for (var i=0; i<this.length; i++) {
		if (this[i]==_object) {
			this[i] = null;
		}
	}
}

Array.prototype.compact = function() {
	var temp = new Array;
	var j = 0;
	for (var i=0; i<this.length; i++) {
		if (this[i]!=null&&this[i]!=undefined) {
			temp[j++] = this[i];
		}
	}
	return temp;
}  

Array.prototype.getIndex = function(_primaryKey) {
	return this.index[_primaryKey];
}

Array.prototype.getValue = function(_primaryKey) {
	if (this.index[_primaryKey]!=undefined) {
		return this[this.index[_primaryKey]];
	} else {
		return null;
	}
}
/**
 Document Object
*****************************/
function $() {
	var obj = null;
	if (typeof arguments[0] == 'string') {
		obj = document.getElementById(arguments[0]);
	} else {
		obj = arguments[0];
	}
	if (obj==null) {
		obj = new Object;
		obj.isNull = true;
		//return obj;
	} else {
		obj.isNull = false;
	}
	
	obj.setStyle = function(_name, _value) {
		if (_name=="display") {
			_value = (""+_value).toLowerCase();
			if (_value=="false"||_value=="none") _value = "none";
			else _value = "";
		} else if (_name=="left"||_name=="top"||_name=="width"||_name=="height") {
			_value = (""+_value).toLowerCase();
			if (_value!=""&&_value.indexOf("px")<0) _value += "px";
		}
		this.style[_name] = _value;
	}
	
	obj.getStyle = function(_name) {
		return this.style[_name];
	}
	
	obj.getW = function() {
		var w = this.offsetWidth;
		return w;
	}
	
	obj.getH = function() {
		var h = this.offsetHeight;
		return h;
	}
	
	obj.getX = function() 
	{
		var posX = this.style.left;
		if (posX=="") {
			posX = 0;
			var tempObj = this;
			if (tempObj.offsetParent) {
				while (tempObj.offsetParent) {
					posX += tempObj.offsetLeft;
					tempObj = tempObj.offsetParent;
				}
			}
		} else {
			posX = parseInt(posX.toLowerCase().replace("px",""));
		}
		return posX;
	}
	
	obj.getY = function() {
	 
		var posY = this.style.top;
		if (posY=="") {
			posY = 0;
			var tempObj = this;
			if (tempObj.offsetParent) {
				while (tempObj.offsetParent) {
					posY += tempObj.offsetTop;
					tempObj = tempObj.offsetParent;
				}
			}
		} else {
			posY = parseInt(posY.toLowerCase().replace("px",""));
		}
		return posY;
	}
	
	obj.setOpacity = function(_value) {
		if (parseFloat(_value)>1) _value = parseFloat(_value)/100;
		this.setStyle("filter","alpha(opacity=" + (_value*100) + ")");
		this.setStyle("opacity",_value);
		this.setAttribute("opacity",_value);
	}
	
	obj.getOpacity = function() {
		var value = this.getAttribute("opacity") || this.getStyle("opacity") || 0;
		return parseFloat(value);
	}
	
	obj.setFadeIn = function(_opacity, _speed, _afterAction, _isReCall) {
		try {
			if (!this.isNull) {
				if (_isReCall!=true&&this.getAttribute("fadeStatus")=="ing") {
					clearTimeout(this.fadeTimer);
				}
				if (_opacity==null) _opacity = 1.0;
				if (_opacity>1) _opacity = _opacity/100;
				if (_speed==null) _speed = 30;
				if (_afterAction==null) _afterAction = "";
				this.setAttribute("fadeStatus","ing");
				var nowOpacity = this.getOpacity();
				var targetOpacity = nowOpacity + 0.1;
				if (targetOpacity > _opacity) targetOpacity = _opacity;
				this.setOpacity(targetOpacity);
				if (targetOpacity<_opacity) {
					this.fadeTimer = setTimeout("$('"+this.id+"').setFadeIn("+_opacity+", "+ _speed+", '"+_afterAction.replaceQuot()+"', true)", _speed);
				} else {
					this.setAttribute("fadeStatus","");
					if (_afterAction!="") {
						try {
							eval(_afterAction);
						} catch(e) {
							
						}
					}
				}
			}
		}catch(e) {
			alert("KidoScript Error Form $().setFadeIn() : " + e);
		}
	}
	
	obj.setFadeOut = function(_opacity, _speed, _afterAction, _isReCall) {
		try {
			if (!this.isNull) {
				if (_isReCall!=true&&this.getAttribute("fadeStatus")=="ing") {
					clearTimeout(this.fadeTimer);
				}
				if (_opacity==null) _opacity = 1.0;
				if (_opacity>1) _opacity = _opacity/100;
				if (_speed==null) _speed = 30;
				if (_afterAction==null) _afterAction = "";
				this.setAttribute("fadeStatus","ing");
				var nowOpacity = this.getOpacity();
				var targetOpacity = nowOpacity - 0.1;
				if (targetOpacity < _opacity) targetOpacity = _opacity;
				this.setOpacity(targetOpacity);
				if (targetOpacity>_opacity) {
					this.fadeTimer = setTimeout("$('"+this.id+"').setFadeOut("+_opacity+", "+ _speed+", '"+_afterAction.replaceQuot()+"', true)", _speed);
				} else {
					this.setAttribute("fadeStatus","");
					if (_afterAction!="") {
						try {
							eval(_afterAction);
						} catch(e) {
							
						}
					}
				}
			}
		}catch(e) {
			alert("KidoScript Error Form $().setFadeOut() : " + e);
		}
	}
	
	
	obj._getNextPoint = function(_prevPont, _targetPoint, _speed) {
		var point = 0;
		var finishPointChecking = false;
		var checkMode = "";
		if (_targetPoint<0) {
			if (_prevPont<0) {
				if (Math.abs(Math.abs(_targetPoint)-Math.abs(_prevPont))<_speed) {
					point = _targetPoint;
					finishPointChecking = true;
				}
				checkMode = "A";
			} else {
				checkMode = "B";
			}
		} else {
			if (_prevPont>=0) {
				if (Math.abs(Math.abs(_targetPoint)-Math.abs(_prevPont))<_speed) {
					point = _targetPoint;
					finishPointChecking = true;
				}
				checkMode = "C";
			} else {
				checkMode = "D";
			}
		}
		
		if (!finishPointChecking) {
			var setPoint = 0;
			var gapPoint = Math.abs(_prevPont-_targetPoint);
			if (checkMode=="A"||checkMode=="C") {
				gapPoint = Math.abs(_prevPont-_targetPoint);
			} else {
				gapPoint = Math.abs(_prevPont+_targetPoint);
			}
			var minusOption = 1;
			if (_prevPont > _targetPoint) minusOption *= -1;
			if (gapPoint>_speed) setPoint = _speed*minusOption;
			else setPoint = gapPoint*minusOption;
			point = _prevPont+setPoint;
		}
		return point;
	}
	
	
	
	obj.resizeToMotionA = function(_w, _h, _afterAction, _isReCall) {
		if (!this.isNull) {
			if (_isReCall==true||this.getAttribute("resizeStatus")!="ing") {
				try {
					this.setAttribute("resizeStatus","ing");
					if (_afterAction==null) _afterAction = "";
					if (_afterAction=="start") {
						this.setStyle("display",true);
						_afterAction = "";
					}
					var speed = 10;
					var w = this.getW();
					var h = this.getH();
					if (_w==null||""+parseInt(_w)=="NaN") _w = w;
					if (_h==null||""+parseInt(_h)=="NaN") _h = h;
					if (_w<1) _w = 1;
					if (_h<1) _h = 1;
					var targetW = this._getNextPoint(w,_w,speed);
					var targetH = this._getNextPoint(h,_h,speed);
					this.setStyle("width",targetW);
					this.setStyle("height",targetH);
					if (w==_w&&h==_h) {
						this.setAttribute("resizeStatus","");
						if (_afterAction=="finish") {
							this.setStyle("display",false);
						} else if (_afterAction=="remove") {
							this.parentNode.removeChild(this);
						} else if (_afterAction!="") {
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						}
					} else {
						setTimeout("$('"+this.id+"').resizeToMotionA("+_w+","+ _h+", '"+_afterAction.replaceQuot()+"', true)", 15);
					}
				}catch(e) {
					alert("KidoScript Error Form $().resizeToMotionA() : " + e);
				}
			}
		}
	}
	
	
	obj.resizeTo = function(_w, _h, _afterAction, _speed, _isReCall) {
	    
		if (!this.isNull) {
			if (_isReCall==true||this.getAttribute("resizeStatus")!="ing") {
				try {
					this.setAttribute("resizeStatus","ing");
					
					var nowW = this.getW();
					var nowH = this.getH();
					
					if (_w==null||""+parseInt(_w)=="NaN") _w = nowW;
					if (_h==null||""+parseInt(_h)=="NaN") _h = nowH;
					if (_speed==null||""+parseInt(_speed)=="NaN") _speed = 2.5;
					
					var targetW = _w;
					var targetH = _h;
					
					if (targetW<1) targetW = 1;
					if (targetH<1) targetH = 1;
					
					if (_afterAction==null) _afterAction = "";
					
					if (targetW==nowW&&targetH==nowH) {
						this.setAttribute("resizeStatus","");
						try {
							eval(_afterAction);
						} catch(e) {
						
						}
					} else {
						var denominator = _speed;
						var addW = 0;
						var addH = 0;
						if (targetW > nowW) {
							addW = Math.ceil((targetW - nowW) / denominator);
						} else {
							addW = (Math.ceil((nowW - targetW) / denominator))*-1;
						}
						if (targetH > nowH) {
							addH = Math.ceil((targetH - nowH) / denominator);
						} else {
							addH = (Math.ceil((nowH - targetH) / denominator))*-1;
						}
						var finalW = nowW + addW;
						var finalH = nowH + addH;
						if (finalW<1) finalW = 1;
						if (finalH<1) finalH = 1;
						this.setStyle("width", finalW);
						this.setStyle("height", finalH);
						setTimeout("$('"+this.id+"').resizeTo('"+_w+"', '"+_h+"', '"+_afterAction.replaceQuot()+"', '"+_speed+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().resizeTo() : " + e);
				}
			}
		}
	}
	
	obj.moveTo = function(_x, _y, _afterAction, _speed, _isReCall) {
		if (!this.isNull) {
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					this.setAttribute("moveStatus","ing");
					
					var nowPosX = this.getX();
					var nowPosY = this.getY();
					
					if (_x==null||""+parseInt(_x)=="NaN") _x = nowPosX;
					if (_y==null||""+parseInt(_y)=="NaN") _y = nowPosY;
					if (_speed==null||""+parseInt(_speed)=="NaN") _speed = 2.5;
					
					var targetPosX = _x;
					var targetPosY = _y;
					
					if (_afterAction==null) _afterAction = "";
					
					if (targetPosX==nowPosX&&targetPosY==nowPosY) {
						this.setAttribute("moveStatus","");
						try {
							eval(_afterAction);
						} catch(e) {
						
						}
					} else {
						var denominator = parseInt(_speed);
						var movePosX = 0;
						var movePosY = 0;
						if (targetPosX > nowPosX) {
							movePosX = Math.ceil((targetPosX - nowPosX) / denominator);
						} else {
							movePosX = (Math.ceil((nowPosX - targetPosX) / denominator))*-1;
						}
						if (targetPosY > nowPosY) {
							movePosY = Math.ceil((targetPosY - nowPosY) / denominator);
						} else {
							movePosY = (Math.ceil((nowPosY - targetPosY) / denominator))*-1;
						}
						this.setStyle("left", nowPosX + movePosX);
						this.setStyle("top", nowPosY + movePosY);
						setTimeout("$('"+this.id+"').moveTo('"+_x+"', '"+_y+"', '"+_afterAction.replaceQuot()+"', '"+_speed+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveTo() : " + e);
				}
			}
		}
	}
	
	obj.moveToMotionA = function(_x, _y, _afterAction, _isReCall) {
		if (!this.isNull) {
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					this.setAttribute("moveStatus","ing");
					if (_afterAction==null) _afterAction = "";
					if (_afterAction=="start") {
						this.setStyle("display",true);
						_afterAction = "";
					}
					var speed = 10;
					var x = this.getX();
					var y = this.getY();
					if (_x==null||""+parseInt(_x)=="NaN") _x = x;
					if (_y==null||""+parseInt(_y)=="NaN") _y = y;
					if (x==_x&&x==_x) {
						this.setAttribute("moveStatus","");
						if (_afterAction!="") {
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						}
					} else {
						var targetX = this._getNextPoint(x,_x,speed);
						var targetY = this._getNextPoint(y,_y,speed);
						this.setStyle("left",targetX);
						this.setStyle("top",targetY);
						setTimeout("$('"+this.id+"').moveToMotionA('"+_x+"', '"+_y+"', '"+_afterAction.replaceQuot()+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveToMotionA() : " + e);
				}
			}
		}
	}
	
	obj.moveToTarget = function(_targetId, _afterAction, _isReCall) {
		if (!this.isNull) {
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					if (!$(_targetId).isNull) {
						this.setAttribute("moveStatus","ing");
						var targetPosX = $(_targetId).getX();
						var targetPosY = $(_targetId).getY();
						
						var nowPosX = this.getX();
						var nowPosY = this.getY();
						
						if (_afterAction==null) _afterAction = "";
						
						if (targetPosX==nowPosX&&targetPosY==nowPosY) {
							this.setAttribute("moveStatus","");
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						} else {
							var denominator = 2.5;
							var movePosX = 0;
							var movePosY = 0;
							if (targetPosX > nowPosX) {
								movePosX = Math.ceil((targetPosX - nowPosX) / denominator);
							} else {
								movePosX = (Math.ceil((nowPosX - targetPosX) / denominator))*-1;
							}
							if (targetPosY > nowPosY) {
								movePosY = Math.ceil((targetPosY - nowPosY) / denominator);
							} else {
								movePosY = (Math.ceil((nowPosY - targetPosY) / denominator))*-1;
							}
							this.setStyle("left", nowPosX + movePosX);
							this.setStyle("top", nowPosY + movePosY);
							setTimeout("$('"+this.id+"').moveToTarget('"+_targetId+"', '"+_afterAction.replaceQuot()+"', true)", 10);
						}
					} else {
						this.setAttribute("moveStatus","");
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveToTarget() : " + e);
				}
			}
		}
	}
	
	return obj;
}








/**
 Event
*****************************/
function EventHandler() {
	this.vars = new Object;
	this.event = null;
	this.eventActions = new Array;
	
	this.getElement = function() {
		return this.event.target || this.event.srcElement;
	}
	
	this.getValidElement = function(_attributeName,_attributeValue) {
		var element = this.getElement();
		while(element) {
			if (element.getAttribute(_attributeName)==_attributeValue) {
				break;
			}
			element = element.parentNode;
		}
		return element;
	}
	
	this.pointerX = function() {
		var x = 0;
		if (this.event!=null) {
			x = this.event.pageX || (this.event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
			//alert("this.event.pageX : " + this.event.pageX + "\n other : " + x);
			//x = (document.documentElement.scrollLeft) ? this.event.clientX : this.event.clientX + document.body.scrollLeft;
		}
		return x;
	}
	
	this.pointerY = function() {
		var y = 0;
		if (this.event!=null) {
			y = this.event.pageY || (this.event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
			//y = (document.documentElement.scrollTop) ? this.event.clientY : this.event.clientY + document.body.scrollTop;
		}
		return y;
	}
	
	
	this.addAction = function(_eventType, _eventGroup, _eventAction) {
		this.eventActions.push(
			{
				  type		: _eventType.trim().toLowerCase()
				, group		: _eventGroup.trim().toLowerCase()
				, action	: _eventAction.trim()
			}
		);
	}
	
	this.removeAction = function(_eventGroup) {
		for (var i=0; i<this.eventActions.length; i++) {
			var eAction = this.eventActions[i];
			if (eAction.group==_eventGroup.trim().toLowerCase()) {
				this.eventActions[i] = null;
			}
		}
		this.eventActions = this.eventActions.compact();
	}
	
	this.stop = function() {
		if (this.event.preventDefault!=undefined) {
			this.event.preventDefault();
			this.event.stopPropagation();
		} else {
			this.event.returnValue = false;
			this.event.cancelBubble = true;
		}
	}
	
	this.onEvent = function(_eventType) {
		for (var i=0; i<this.eventActions.length; i++) {
			var eAction = this.eventActions[i];
			if (eAction.type==_eventType.trim().toLowerCase()) {
				try {
					eval(eAction.action);
				} catch(e) { }
			}
		}
	}
}

var $E = new EventHandler();

function EventListener(_event) {
	_event = window.event ? window.event : _event;
	$E.event = _event;
	$E.onEvent("on"+_event.type);
}

document.onmousedown = EventListener;
document.onmouseup = EventListener;
document.onmousemove = EventListener;
document.onmouseover = EventListener;
document.onselectstart = EventListener;
document.onclick = EventListener;
document.onkeydown = EventListener;



