/*!
 *CC JavaScript Library 
 *http://www.c77cc.cn
 *Author:C7_yaohuaq(c77cc#vip.qq.com)
 *Date:2010-02-10
 */
(function() {
	var CC = function() {
////////////////Public methods/////////////////////////////
		this.$ = function(selector) {
			if(typeof selector == 'object') {
				return selector;
			}
			var selectors = selector.split(' ');
			if(selectors.length==1) {
				return find(selectors[0]);  
			}else if(selectors.length>1)  {
				var re;
				this.each(selectors,function(i) {
					if(i==0) {
						re = find(selectors[i]);
					}else {
						re = find(selectors[i],re);  
					}
				});
				return re;
			}else {
				return null;
			}
		}
		this.ready = function(fn) {
			var done,exec,old_onload,load_events=[];
			var init = function() {
				done = true;
				while (exec = load_events.shift()) {
					exec();
				}              
			}			
			if(document.addEventListener) {
				document.addEventListener('DOMContentLoaded',init,false);
			}
			/*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/
			old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
			load_events.push(fn);			
		}

		//C.html(ele,[,elem]);
		this.html = function(ele,html) {
			if(html) {
				if(ele.length==undefined) {
					return ele.innerHTML = html;
				}else if(ele.length>0) {
					this.each(ele,function(i){
						return ele[i].innerHTML = html;    
					});
				}else {
					return null;
				}
			}else {
				if(ele.length>0) {
					ele = ele[0];
				}
				return ele.innerHTML;       
			}
		}
		//C.val(ele[,value]);
		this.val = function(ele,value) {
			if(value) {
				if(ele.length==undefined) {
					ele.value = value;
				}else if(ele.length>0) {
					this.each(ele,function(i){
						if(ele[i]!=null) {
							ele[i].value = value;  
						}  
					});
				}else {
					return null;
				}
			}else {
				if(ele.length>0) {
					ele = ele[0];
				}
				return ele.value;
			}	
		}
		//获取内联和外联CSS一些获取不到，未解决(如background，jQuery也没解决)
		//C.css(ele,css);
		this.css = function(ele,css) {
			if(typeof css == 'object') {
				var cssText='',i,j;
				for(i in css) {
					cssText += i+':'+css[i]+';';
				}
				if(ele.length==undefined) {
					return ele.style.cssText = cssText;
				}else if(ele.length>0) {
					this.each(ele,function(j){
						if(ele[j]!=null && ele[j].nodeType==1) {
							ele[j].style.cssText = cssText; 
						}   
					})
				}else {
					return null;
				}
			}else if(typeof css == 'string') {
				if(ele.length==undefined) {
					return getStyle(ele,css);
				}else if(ele.length>0) {
					ele = ele[0];
					return getStyle(ele,css);
				}else {
					return null;
				}
			}	
		}

		//C.attr(elements,attr)
		this.attr = function(ele,attr) {
			if(typeof attr == 'object') {
				if(ele.length==undefined) {
					for(var i in attr) {
						ele.setAttribute(i,attr[i]);
					}
				}else if(ele.length>0) {
					this.each(ele,function(i){
						for(var j in attr) {
							ele[i].setAttribute(j,attr[j]);
						}      
					});
				}else {
					return null;
				}
			}else if(typeof attr == 'string') {
				if(ele.length>0) {
					ele = ele[0];
				}
				return ele.getAttribute(attr);
			}else {
				return null;
			}
		}

		//type是事件名称，要注意的是"onclick"要改为"click","onblur"要改为"blur",也就是说事件名不要带"on"。
		this.bind = function(ele,type,fn) {
			if(ele.length==undefined) {
				bind(ele,type,fn);  
			}else if(ele.length>0) {
				this.each(ele,function(i){
					bind(ele[i],type,fn);    
				})
			}else {
				return null;
			}
		}
		this.unbind = function(ele,type,fn) {
			if(ele.length==undefined) {
				unbind(ele,type,fn);  
			}else if(ele.length>0) {
				this.each(ele,function(i){
					unbind(ele[i],type,fn);    
				})
			}else {
				return false;
			}
		}
		//遍历对象或数组，callback为回调函数
		//C.each(obj[,callbck]);
		this.each = function(obj,callback) {
			var obj = obj || new Object();
			if(callback) {
				for(var name in obj) {
					callback.call(obj,name,obj[name]);
				}
			}else {
				return;	
			}
		}
		
		this.append = function(ele,node) {
			var fragment;
			if(ele.length==undefined) {
				if(node.length==undefined) {
					ele.appendChild(node);
				}else if(node.length>0) {					
					fragment = buildFragment(node);
					ele.appendChild(fragment);
				}
			}else if(ele.length>0) {
				this.each(ele,function(i){
					if(ele[i].nodeType==1) {						
						if(node.length==undefined) {
							if(i<this.length-1) {
								ele[i].appendChild(node.cloneNode(true));
							}else {
								ele[i].appendChild(node);
							}	  
						}else if(node.length>0) {
							fragment = buildFragment(node);	
							if (i<this.length-1){
								ele[i].appendChild(fragment.cloneNode(true));
							}else {
								ele[i].appendChild(fragment);
							}  
						}
					}    
				});
			}
		}

		this.before = function(ele,node) {
			if(ele.length==undefined) {
				if(node.length==undefined) {				
					before(ele,node);
				}else if(node.length>0) {
					var fragment = buildFragment(node);
					before(ele,fragment);
				}
			}else if(ele.length>0) {
				this.each(ele,function(i){
					if(node.length==undefined) {				
						if(i<this.length-1) {
							before(ele[i],node.cloneNode(true));
						}else {
							before(ele[i],node);
						}	
					}else if(node.length>0) {
						var fragment = buildFragment(node);
						if(i<this.length-1) {
							before(ele[i],fragment.cloneNode(true));
						}else {
							before(ele[i],fragment);
						}
					}
				});
			}
		}

		this.after = function(ele,node) {
			if(ele.length==undefined) {
				if(node.length==undefined) {				
					after(ele,node);
				}else if(node.length>0) {
					var fragment = buildFragment(node);
					after(ele,fragment);
				}
			}else if(ele.length>0) {
				this.each(ele,function(i){
					if(node.length==undefined) {				
						if(i<this.length-1) {
							after(ele[i],node.cloneNode(true));
						}else {
							after(ele[i],node);
						}	
					}else if(node.length>0) {
						var fragment = buildFragment(node);
						if(i<this.length-1) {
							after(ele[i],fragment.cloneNode(true));
						}else {
							after(ele[i],fragment);
						}
					}
				});
			}
		}
		this.remove = function(ele,parent) {
			var parent = parent || document.body;
			if(ele.length==undefined) {
				parent.removeChild(ele);
			}else if(ele.length>0) {
				this.each(ele,function(i){
					parent.removeChild(ele[i]);
				});
			}
		}
		this.empty = function(ele) {
			if(ele.children.length>0) {
				while(ele.children.length>0) {
					for(var i in ele.children) {
						if(ele.children[i]!=undefined && ele.children[i].nodeType==1) {
							this.remove(ele.children[i],ele);
						}
					}
				}
			}
		}
		this.replace = function(ele,eleWith) {
			var parent = ele.parentNode || document.body;
			if(ele.length==undefined) {
				if(eleWith.length==undefined) {
					parent.replaceChild(eleWith,ele);
				}else if(eleWith.length>0) {
					var fragment = buildFragment(eleWith);
					parent.replaceChild(fragment,ele);
				}
			}else if(ele.length>0) {
				this.each(ele,function(i){
					if(eleWith.length==undefined) {
						if(i<this.length-1) {
							parent.replaceChild(eleWith.cloneNode(true),ele[i]);
						}else {
							parent.replaceChild(eleWith,ele[i]);
						}	
					}else if(eleWith.length>0) {
						var fragment = buildFragment(eleWith);
						if(i<this.length-1) {
							parent.replaceChild(fragment.cloneNode(true),ele[i]);
						}else {
							parent.replaceChild(fragment,ele[i]);
						}
					}
				});				
			}
		}
		this.ajax = function(arg) {
			var xmlHttp;
			if(window.ActiveXObject) {
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			}else {
				xmlHttp = new XMLHttpRequest();
			}
			if(!xmlHttp) alert('初始化XMLHttpRequest失败');
			if(arg.type.toLowerCase()=='post') {
				xmlHttp.open("POST",encodeURI(arg.url),true);
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttp.send(encodeURI(arg.data));
				xmlHttp.onreadystatechange = function() {
					if(xmlHttp.readyState==1 && arg.sending!=undefined) {
						arg.sending();
					}else if(xmlHttp.readyState==4 && xmlHttp.status==200){
						arg.success(xmlHttp.responseText);
					}
				}
			}else{
				xmlHttp.open("GET",encodeURI(arg.url+'?'+arg.data),true);
				xmlHttp.onreadystatechange = function() {
					if(xmlHttp.readyState==1 && arg.sending!=undefined) {
						arg.sending();
					}else if(xmlHttp.readyState==4 && xmlHttp.status==200){
						arg.success(xmlHttp.responseText);
					}
				}
				xmlHttp.send(null);
			}

		}

		this.trim = function(text) {
			return (text || "").replace(/^\s+|\s+$/g,'');	
		}
////////////////Static methods/////////////////////////////    
		var find = function(selector,parents) {
			var parents = parents || document,elems=[];
			if(parents.length==undefined) {
				if(selector.indexOf('#')==0) {
					return getId(selector.substr(1),parents);
				}else if(selector.indexOf('.')==0) {
					return getClassName(selector.substr(1),'',parents);
				}else{
					return getTagName(selector,parents);
				}
			}else if(parents.length>0) {
				for(var i in parents) {
					var allChilds = findAllChild(parents[i]);
					for(var j in allChilds) {
						if(selector.indexOf('#')==0) {
							if(allChilds[j].id==selector.substr(1)) {
								elems.push(allChilds[j]);
							}
						}else if(selector.indexOf('.')==0) {	
							if(allChilds[j].className==selector.substr(1)) {
								elems.push(allChilds[j]);
							}
						}else{
							if(allChilds[j].nodeName.toLowerCase()==selector) {
								elems.push(allChilds[j]);
							}
						}
					}
				}							
				return elems;
			}
		}
		var findAllChild = function(node) {
			var childs=[];
			if(node.childNodes.length>0) {
				childs = getTagName('*',node);
			}
			return childs;
		}
		var getId = function(id,node) {
			var elems=[];
			if(node.length==undefined) {
				elems.push(document.getElementById(id));
			}else{
				for(var i in node) {
					var allChilds = findAllChild(node[i]);
					for(var j in allChilds) {
						if(allChilds[j].id==id) {
							elems.push(allChilds[j]);
						}
					}
				}
			}
			return elems;
		}
		//Object=C.getTagName(tagname[,node]);
		var getTagName = function(tagName,node) {
			var tagName = tagName || '*',node = node || document,elems=o=[];
			o = node.getElementsByTagName(tagName);
			for(var i in o) {
				if(o[i].nodeType==1) {
					elems.push(o[i]);
				}
			}
			return elems;
		}
		//Object=C.getClassName(className[,tagName[,node]]);
		var getClassName = function(className,tagName,node) {
			var elems = [],className = className||'',tagName = tagName||'*',node = node||document,o = getTagName(tagName,node),i;
			for(i=0;i<o.length;i++) {
				if(className == o[i].className) {
					elems.push(o[i]);
				}
			}
			return elems;
		}
		var bind = function(ele,type,fn) {
			//兼容Mozilla系列
			if(ele.addEventListener) {
				ele.addEventListener(type,fn,false);	
			  //兼容IE
			}else if(ele.attachEvent) {
				ele.attachEvent('on'+type,fn);
			} 
		}
		var unbind = function(ele,type,fn) {
			//兼容Mozilla系列
			if(ele.removeEventListener) {
				ele.removeEventListener(type,fn,false);	
			  //兼容IE
			}else if(ele.detachEvent) {
				ele.detachEvent('on'+type,fn);
			}
		}
		var getStyle = function(ele,name) {
			//兼容IE
			if (ele.currentStyle) {   
				return ele.currentStyle[name];   
			//兼容Mozilla系列
			} else if (window.getComputedStyle) {   
				name = name.replace(/([A-Z])/g, "-$1").toLowerCase();   
				//获取style对象并取得属性的值(如果存在的话)
				return window.getComputedStyle(ele, null).getPropertyValue(name);   
			} else {   
				return null;   
			}   
		}
		var before = function(ele,node) {
			var parent = ele.parentNode;
			parent.insertBefore(node,ele);
		}
		var after = function(ele,node) {
			var parent = ele.parentNode;
			if(getLastChild(parent)==ele) {
				parent.appendChild(node);
			}else {
				parent.insertBefore(node,getNextSibling(ele));
			}
		}
		var getNextSibling = function(ele) {
			if(ele.nextSibling.nodeType==1) {
				return ele.nextSibling;
			}else if(ele.nextSibling.nodeType==3) {
				return ele.nextSibling.nextSibling;
			}
		}
		var getLastChild = function(ele) {
			if(ele.lastChild.nodeType==1) {
				return ele.lastChild;	//IE
			}else if(ele.lastChild.nodeType==3) {
				return ele.lastChild.previousSibling;	//兼容firefox
			}else {
				return null;
			}
		}
		var buildFragment = function(node) {
			var fragment = document.createDocumentFragment();
			for(var i in node){
				while (node.length>fragment.childNodes.length) {
					for(var i in node) {
						if(node[i]!=undefined && node[i].nodeType==1){
							//将node先保存到片段 documentFragment 
							fragment.appendChild(node[i]);
						}
					}
				}
			};
			return fragment;
		}
	}//end function CC
	window.C = new CC();
})();

