(function(){
var SK = window.$ = window.SK = function(s){return new SK.fn.init(s)};

SK.fn = SK.prototype = {
	selector: "",
	init: function(s){
		this.selector = new Array();
		s=s||document;
		if(s.nodeType) {
			this.selector.push(s);
			return this;
		}
		if (typeof s==="string") {
			d=s.substr(0,1);
			if (d=="#"){
				this.selector.push(document.getElementById(s.substr(1,s.length-1)));
			}
			else if (d==".") {
				var t = document.getElementsByTagName("*");
				for (i=0; i<t.length;i++) {
					if (t[i].className && t[i].className.indexOf(s.substr(1,s.length-1))!=-1) {
						this.selector.push(t[i]);
					}
				}
			}
			else {
				var t = document.getElementsByTagName(s);
				for (i=0; i<t.length;i++)
					this.selector.push(t[i]);
			}
		}
		else if (typeof s=="function") {
			this.attachevent(window,"load",s)
		}
	},
	
	// add css class
	addClass: function(n) {
		for (i=0;i<this.selector.length;i++)
			this.selector[i].className += (this.selector[i].className ? " " : "") + n;
		return this;
	},
	
	// attach event
	attachevent: function(o,e,c){
		/*oo=this.isHtmlObject(o);
		if(oo.attachEvent)
			oo.attachEvent("on"+e,c)
		else
			oo.addEventListener(e,c,false)*/
		if(o.attachEvent)
			o.attachEvent("on"+e,c)
		else
			o.addEventListener(e,c,false)
	},
	
	// append child
	append: function(c){
		for (i=0;i<this.selector.length;i++) {
			cc=c;
			if(typeof c==="string")cc=this.ct(c);
			this.selector[i].appendChild(cc);
		}
		return this
	},
	
	// bind event
	bindevent: function(e,c){
		for (i=0;i<this.selector.length;i++)
			this.attachevent(this.selector[i],e,c);
		return this;
	},
	
	// get/set css value
	css: function(n,v){
		if (v===undefined) { //get ou collection set
			if (typeof n==="string") { //get
				if (this.selector[0].currentStyle) { //ie
					return this.selector[0].currentStyle[n];
				} else { // firefox, opera, safari, chrome, etc
					n=n.replace( /([A-Z])/g, "-$1" ).toLowerCase();
					return window.getComputedStyle(this.selector[0],null).getPropertyValue(n);
				}
			}
			else { // collection set
				for (o in n)
					for (i=0;i<this.selector.length;i++)
						this.selector[i].style[o] = n[o];
				return this;
			}
		}
		else { //set
			for (i=0;i<this.selector.length;i++)
				this.selector[i].style[n] = v;
			return this;
		}
	},
	
	// create textnode
	ct: function(t){return document.createTextNode(t)},
	
	// each
	each: function(c){
		return this.innereach(this.selector,c);
	},
	
	// special function for hover (mouseover + mouseout)
	hover: function(f1,f2) {
		for (i=0;i<this.selector.length;i++) {
			this.attachevent(this.selector[i],"mouseover",f1);
			this.attachevent(this.selector[i],"mouseout",f2);
		}
		return this;
	},
	
	// set/get innerHTML
	html: function(h){
		if (h===undefined)
			return this.selector[0].innerHTML
		else
			for (i=0;i<this.selector.length;i++)
				this.selector[i].innerHTML=h;
			return this;
	},

	// innereach (each interne)
	innereach: function(o,c){
		var i=0,l=o.length;
		if(l==0||l===undefined)return;
		for(var v=o[0];i<l&&c.call(v,i,v)!==false;v=o[++i]){}
	},
	
	// innerval
	innerval: function(o,v){
		s = o.nodeName.toLowerCase();
		if (s!="input"&&s!="select"&&s!="textarea")return;
		if(v===undefined) { //get
			if (s=="input"){
				if (o.type=="checkbox" || o.type=="radio") return o.checked;
				else return o.value;
			}
			else if (s=="select"){
				return o.options[o.selectedIndex].value;
			}
			else {
				return o.innerHTML;
			}
		}
		else { //set
			if (s=="input"){
				if (o.type=="checkbox" || o.type=="radio") o.checked=v;
				else o.value=v;
			}
			else if (s=="select"){
				//return o.options[o.selectedIndex].value;
				for (i=0;i<o.options.length;i++){
					if (o.options[i].value==v)
						o.selectedIndex=i;
				}
			}
			else {
				o.innerHTML=v;
			}
			return this;
		}
	},

	// is html object
	isHtmlObject: function(o){
		if(typeof o !== "object")
			return document.getElementById(o);
		return o
	},
	
	// is
	is: function(a,b){return a instanceof b},
	
	// remove a html element
	remove: function(){
		for (i=0;i<this.selector.length;i++){
			this.selector[i].parentNode.removeChild(this.selector[i]);
		}
	},
	
	// remove css class
	removeClass: function(n){
		reg = new RegExp("("+n+")","g");
		for (i=0;i<this.selector.length;i++) {
			tn=" "+n+" ";
			if ((" "+this.selector[i].className+" ").indexOf(tn)) {
				this.selector[i].className=this.selector[i].className.replace(reg,"");
			}
		}
	},
	
	// set/get form input value
	val: function(v){
		return this.innerval(this.selector[0],v);
	}
};
SK.fn.init.prototype = SK.fn;

SK.fn.innereach("click,mouseover,mouseout".split(","),function(i,n) {
	SK.fn[n] = function(fn) {return this.bindevent(n,fn)}
});

})();