
/*

	# -------------------------------------------------------------------------------------------- #
	#																			#
	# Object.tweak.js																#
	#																			#
	# Object Class Tweak															#
	# Copyright 2011 Thommy Morgan													#
	# All Rights Reserved															#
	#																			#
	# -------------------------------------------------------------------------------------------- #
	
		
*/


/* -------------------------------------------------------- */
/* Event handling for optimum universability				*/
/* -------------------------------------------------------- */


Object.prototype.addEventTo = function(action, func, capture) {

	if(!action || !func) return ERROR; // Error
	if(!capture) capture = false;
	
	var self = this;
	function evaluate_action(an_action, func, capture) {

		switch(an_action.toLowerCase()) {
			case "click":
				self.onclick		= func;
				break;
			case "mousemove":
				self.onmousemove 	= func;
				break;
			case "mouseover":
				self.onmouseover 	= func;
				break;
			case "mouseout":
				self.onmouseout 	= func;
				break;
			case "mousedown":
				self.onmousedown 	= func;
				break;
			case "mouseup":
				self.onmouseup 	= func;
				break;
			case "keydown":
				self.onkeydown 	= func;
				break;
			case "keypress":
				self.onkeypress 	= func;
				break;
			case "keyup":
				self.onkeyup		= func;
				break;
			case "paste":
				self.onpaste = func;
				break;
			case "input":
				self.oninput = func;
				break;
			case "mousewheel":
				self.onmousewheel = func;
				break;
			case "focus":
				self.onfocus = func;
				break;
			case "blur":
				self.onblur = func;
				break;
			case "selectstart":
				self.onselectstart = func;
				break;
			default:
				try {
					self.addEventListener(an_action, func, capture);
				}
				catch(err) {
					self.attachEvent("on" + an_action, func, capture);
				}
		}
	
	}
	
	if(!is_array(action)) {
		evaluate_action(action, func, capture);
	}
	else
	if(is_array(action)) {
		for(var i = 0; i < action.length; i++) {
			evaluate_action(action[i], func, capture);	
		}	
	}
	
}


Object.prototype.removeEventFrom = function(action, func, capture) {

	if(action == null || func == null) return; // Error
	if(capture == null) capture = false;

	try {
		this.removeAttribute(action);
	}
	catch(err) {}

	try {
		
		this.removeEventListener(action, func, capture);
	}
	catch(err) {
		this.detachEvent("on" + action, func, capture);
	}
	
}


Object.prototype.getStyle = function(attr) {
	return eval("this.style." + attr);
}


Object.prototype.setStyle = function(attr, val) {
	eval("this.style." + attr + " = \"" + val + "\"");	
}


Object.prototype.getAttr = function(strCssRule) {
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle)
		strValue = document.defaultView.getComputedStyle(this, "").getPropertyValue(strCssRule);
	else
	if(this.currentStyle) {
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1) {
			return p1.toUpperCase();
		});
		strValue = this.currentStyle[strCssRule];
	}
	return strValue;
}


/* -------------------------------------------------------- */
/* Setting Object Attributes							*/
/* -------------------------------------------------------- */


Object.prototype.setID = function(id) {
	this.setAttribute("id", id);
}


Object.prototype.setClass = function(cls, concat) {
	if(!concat) {
		this.setAttribute("class", cls);
		this.setAttribute("className", cls);
	}
	else {
		this.setAttribute("class", this.getClass() + " " + cls);
		this.setAttribute("className", this.getClass() + " " + cls);
	}
}


Object.prototype.setName = function(nm) {
	this.setAttribute("name", nm);
}


Object.prototype.setType = function(type) {
	if(DC.browserType == BrowserTypeIE) {
		this.type = type;
		return;
	}
	this.setAttribute("type", type);
}


Object.prototype.setValue = function(val) {
	this.setAttribute("value", val);
}


Object.prototype.setSRC = function(src) {
	this.setAttribute("src", src);
}


/* -------------------------------------------------------- */
/* Getting attributes								*/
/* -------------------------------------------------------- */


Object.prototype.getID = function() {
	return this.getAttribute("id");
}


Object.prototype.getClass = function() {
	return this.getAttribute("class");
}


Object.prototype.getName = function() {
	return this.getAttribute("name");
}


Object.prototype.getType = function() {
	return this.getAttribute("type");
}


Object.prototype.getValue = function() {
	return (this.value) ? this.value : this.getAttribute("value");
}


/* -------------------------------------------------------- */
/* Getting position and style							*/
/* -------------------------------------------------------- */


Object.prototype.getWidth = function(absolute) {
	return this.getParam(absolute, "width");
}


Object.prototype.getHeight = function(absolute) {
	return this.getParam(absolute, "height");
}


Object.prototype.getLeft = function(absolute) {
	return this.getParam(absolute, "left");
}


Object.prototype.getTop = function(absolute) {
	return this.getParam(absolute, "top");
}


Object.prototype.getParam = function(absolute, param, pO) {
	if(!pO) pO = this;
	return (pO.getStyle(param) && !absolute) 						? 
		pO.getStyle(param).replace("px", "").toInt() 				:
		(pO.getAttr(param) && pO.getAttr(param).toString() != "auto")	?
		pO.getAttr(param).replace("px", "").toInt()					:
		(pO.getAttr(param).toString() == "auto" && absolute)			?
		pO.getParam(absolute, param, pO.offsetParent) + (
			(param == "left") 	? pO.getLeft() 	:
			(param == "top") 	? pO.getTop()	 	:
			(param == "width") 	? pO.getWidth() 	: 
			(param == "height")	? pO.getHeight()	: 
			null)											:
		(param == "left") 	? pO.offsetLeft 	:
		(param == "top") 	? pO.offsetTop 	:
		(param == "width") 	? pO.offsetWidth 	: 
		(param == "height")	? pO.offsetHeight	: 
		null;
}


/* -------------------------------------------------------- */
/* Setting position and style							*/
/* -------------------------------------------------------- */


Object.prototype.moveToX = function(x) {
	this.style.left = x + "px";
}


Object.prototype.moveToY = function(y) {
	this.style.top = y + "px";	
}


Object.prototype.moveTo = function(x, y) {
	if(x && x != "keep") this.moveToX(x);
	if(y && y != "keep") this.moveToY(y);
}


Object.prototype.setWidth = function(w) {
	this.style.width = w + "px";	
}


Object.prototype.setHeight = function(h) {
	this.style.height = h + "px";	
}


Object.prototype.setSize = function(w, h) {
	if(w && w != "keep") this.setWidth(w);
	if(h && h != "keep") this.setHeight(h);
}


Object.prototype.setOpacity = function(alpha) {
	this.style.opacity = alpha;
	this.style.MozOpacity = alpha;
	this.style.KhtmlOpacity = alpha;
	this.style.filter = "Alpha(Opacity=" + (alpha * 100) + ");";
	this.style.MsFilter = this.style.filter;
}

