/**
  *
  * styles and more
  *
  **/

function Element(obj) {
	if (this instanceof Element) {
		throw new Error("This is not object!");
	} else {
		if (typeof obj === 'string') {
			obj = document.getElementById(obj);
		}
		
		//object not found
		if (!obj) {
			return null;
		}
		if (typeof obj.pareseStyleName === 'function' ) {
			return obj;
		}
		
		obj.parseStyleName = function(name) {
			var tmp = name.replace(/-(.)/g,function(s, s2) {
				return s2.toUpperCase();
			});
			return tmp;
		};
		
		obj.GetStyle = function(what, type) {
			if (UA.isIE()) {
				eval("var tmp = this.currentStyle." + this.parseStyleName(what));
			} else {
				eval("var tmp = this.style." + this.parseStyleName(what));
				var tmp2 = document.defaultView.getComputedStyle(this, '').getPropertyValue(what);

				if (!(tmp !== "" || (tmp === "" && tmp2 === ""))) {
					tmp = tmp2;
				}
			}
	
			if (typeof type !== 'undefined') {
				
				switch(type.toUpperCase()) {
					case 'INT':
						tmp = parseInt(tmp, 10);
						if (isNaN(tmp)) {
							tmp = 0;
						}
					break;
				}
			}
			
			return tmp;
		};
		
		obj.SetStyle = function(what, value) {
			var command = "this.style." + this.parseStyleName(what) + "=" + (typeof value === 'number' ? value : "'" + value + "'") + ";";
			eval(command);
		};
		
		obj.Get = function(what) {
			return this[what];
		};
		
		obj.Set = function(what, value) {
			this[what] = value;
		};
		
		obj.removeFrom = function(node) {
			node.removeChild(this);
		};
		
		obj.insertTo = function(node, child) {
			if (typeof child === 'undefined') {
				node.appendChild(this);
			} else {
				node.insertBefore(this, child);
			}
		};

        obj.RemoveClass = function(className) {
            Element.RemoveClass(this, className);
        };

        obj.AddClass = function(className) {
            Element.AddClass(this, className);
        };

        obj.isVerticalScrollBar = function() {
            return this.offsetHeight < this.scrollHeight;
        };

        obj.isHorizontalScrollBar = function() {
            return this.offsetWidth < this.scrollWidth;
        };

		return obj;
	}
};

Element.Create = function(node) {
    return Element(document.createElement(node));
};

Element.CreateText = function(text) {
    return document.createTextNode(text);
};

Element.RemoveClass = function(item, className) {
	var re = new RegExp("(^| )"+className+"( |$)");
	item.className = item.className.replace(re, " ");
};

Element.AddClass = function(item, className) {
	var re = new RegExp("(^| )"+className+"( |$)");
	if ( !item.className.match(re) ) {
		item.className += ' '+className;
	}
};
