var Dom = {};
Dom.get=function(el){return(typeof el=='object')?el:document.getElementById(el);}


Function.prototype.method = function(name,fn) {
    this.prototype[name]=fn;
    return this;
};


if ( typeof Alpha == 'undefined' ) {
    var Alpha = {};
};

(function(){
	Alpha.register = function(REG){
		function _$(els) {
			this.elements = [];
			for (var i = 0, len = els.length; i < len; ++i) {
			  var element = els[i];
			  if (typeof element == 'string') {
				element = document.getElementById(element);
			  }
			  this.elements.push(element);
			}
 		};		
		_$.method(REG.each, function(fn){
			for (var i =0, len= this.elements.length; i <len; ++i){
				fn.call(this,this.elements[i]);
			}
			return this;
			
		}).method(REG.on,function(type,fn){
			var add = function(el){
				var strArr = type.split(",");
				for(var i = 0; i < strArr.length; i++){
					var strItem = strArr[i];
					if(window.addEventListener){
						if(el==null){return;}
						el.addEventListener(strItem,fn,false);
					}else if(window.attachEvent){
						if(el==null){return;}
						el.attachEvent('on'+strItem,fn);
					}
				}
				
			};
			this.each(function(el){
				add(el);
			});
			return this;
			
		}).method(REG.hide,function(){
			this.each(function(el){
				if(el===null){
					return;
				}
				el.style.display = 'none';
			})
			return this;
			
		}).method(REG.show,function(){
			this.each(function(el){
				if(el===null){ return }			   
				el.style.display = 'block';
			})
			return this;
			
		}).method(REG.toggle,function(){
			this.each(function(el){
				if ( el.style.display == 'none') {
                    el.style.display = "block";
                }else{
               		el.style.display = "none";
            	}
			})
			
			return this;
		}).method(REG.create, function(el, id, cb) {
            var el = document.createElement(el);
                el.setAttribute("id", id);
            if (cb) {
                cb.call(this, el);
            }
            return this;
			
        }).method(REG.append, function(element) {
			this.each(function(el){
				el.appendChild(element);
			})
			
            return this;
			
        }).method(REG.setStyle,function(prop,val){
			var set= function(el){
				if(el===null){
					return;
				}
				el.style[prop] = val;
			}
			this.each(function(el){
				set(el);
			})
			return this;
		
		}).method(REG.setCss,function(styles){
			for ( var prop in styles ) {
				if (!styles.hasOwnProperty(prop)) continue;
				this.setStyle(prop, styles[prop]);
			}
			
			return this;
		})
		
		window[REG.namespace] = function(){
            return new _$(arguments);
        };
		Alpha.extendChain = function(name, fn) {
            _$.method(name, fn);
        };
		
	};
})();

Alpha.register({
	namespace : '$',
	each : 'each',
	on : 'on',
	hide : 'hide',
	show : 'show',
	toggle : 'toggle',
	create : 'create',
	append : 'append',
	setStyle:'setStyle',
	setCss:'setCss'
});


var my9724 = {
	screenWidth: window.screen.width,
	Browser:{
		ie:/msie/.test(window.navigator.userAgent.toLowerCase()),
		ie6:/msie 6.0/.test(window.navigator.userAgent.toLowerCase()),
		moz:/gecko/.test(window.navigator.userAgent.toLowerCase()),
		opera:/opera/.test(window.navigator.userAgent.toLowerCase()),
		safari:/safari/.test(window.navigator.userAgent.toLowerCase())
	},
	Cookie:{
		set:function(name,value,expires,path,domain){
			if(typeof expires=="undefined"){
				expires=new Date(new Date().getTime()+1000*3600*24*30);
			}
			document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+path:"; path=/")+((domain)?";domain="+domain:"");
		},
		get:function(name){
			var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
			if(arr!=null){
				return unescape(arr[2]);
			}
			return null;
		},
		clear:function(name,path,domain){
			if(this.get(name)){
			document.cookie=name+"="+((path)?"; path="+path:"; path=/")+((domain)?"; domain="+domain:"")+";expires=Fri, 02-Jan-1970 00:00:00 GMT";
			}
		}
	},
	getStyle: function(o,s){
		if(document.defaultView){// firefox,opera,safari
			return document.defaultView.getComputedStyle(o,null).getPropertyValue(s);
		} else {//ie
			s=s.replace(/\-([a-z])([a-z]?)/ig,function(s,a,b){return a.toUpperCase()+b.toLowerCase();});//转化为驼峰写法
			return o["currentStyle"][s];
		}
	},
	
	Ajax : function(url, callback) {

		var xhr;
		if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
		else {
			var versions = ["Microsoft.XmlHttp", 
			 				"MSXML2.XmlHttp",
			 			    "MSXML2.XmlHttp.3.0", 
			 			    "MSXML2.XmlHttp.4.0",
			 			    "MSXML2.XmlHttp.5.0"];
			 for(var i = 0, len = versions.length; i < len; i++) {
			 	try {
			 		xhr = new ActiveXObject(versions[i]);
			 		break;
			 	}
			 	catch(e){}
			 } // end for
		}
		xhr.onreadystatechange = ensureReadiness;
		function ensureReadiness() {
			if(xhr.readyState < 4) {
				return;
			}
			
			if(xhr.status !== 200) {
				return;
			}
			if(xhr.readyState === 4) {
				callback(xhr);
			}			
		}
		xhr.open('GET', url, true);
		xhr.send('');
	
	},
	JsLoader: function(sUrl,sId,fCallback,code){
			var _script=document.createElement('script');
			if(code){
				_script.setAttribute('charset',code);
			}else{
				_script.setAttribute('charset','utf-8');
			}
			_script.setAttribute('type','text/javascript');
			_script.setAttribute('src',sUrl);
			_script.id = sId;
			document.getElementsByTagName('head')[0].appendChild(_script);
			if(fCallback){
				if(my9724.Browser.ie){
					_script.onreadystatechange=function(){
						if(this.readyState=='loaded'||this.readyStaate=='complete'){
							fCallback();
						}
					};
				}else if(my9724.Browser.moz){
					_script.onload=function(){
						fCallback();
					};
				}else{
					fCallback();
				}
			}

	},


	addFav:function (title) {
		if( document.all ) {
			window.external.AddFavorite( location.href, title);
		}else if (window.sidebar) {
			window.sidebar.addPanel(title, location.href,"");
		} else if( window.opera && window.print ) {
			return true;
		}
	},
	setHomepage:function(obj){
		if(my9724.Browser.moz){
			alert("设定失败！该操作不支持FireFox浏览器！")
		}else{
			obj.style.behavior = 'url(#default#homepage)';
        	obj.setHomePage(location.href);	
		}
	}
	
	
}

