﻿/*
* @author Eval--eval100@126.com
* @update 2008/8/17
* @fftools JavaScript framework, version 1.0.0
*/
//(function(){

	/*
		* Script: basic.js
	*/
	var fftools=window.fftools={
		extend:function(destination, source){//扩展对象
			for(var property in source){
				destination[property] = source[property]
			}
			return destination;
		},
		
		toArray:function(name){//把参数转换成数组
			return name.split(' ');
		}
		
	};
	
	/*
		* Native object extend.js
	*/
	var $array=window.$array=function(array){//把取得的数组转换成数组对象
		var newArray = [];
		for (var i=0,l=array.length;i<l;i++) newArray.push(array[i]);return newArray;
	};
	
	Array.prototype.each=function(func,i){//遍历操作数组中每个对象
		for(var i=0;i<this.length;i++) { func(this[i]); fftools.extend(this[i],$style)}
	};
	
	/*
		*******************
	*/
	var $=window.$=function(e){
		var elem=typeof(e)=='string'?document.getElementById(e):e;
		if(elem){
			//让取得的对象继承$style,$dom,$cookie,$browser,$ajax对象的属性和方法
			fftools.extend(elem,$style);
			fftools.extend(elem,$dom);
			fftools.extend(elem,$browser);
			fftools.extend(elem,$cookie);
			fftools.extend(elem,$ajax);
		}
		return elem;
	};
	
	/*
		* Script: browser.js
	*/
	var	$browser=window.$browser={
		IE:     !!(window.attachEvent && !window.opera),//IE
    	Opera:  !!window.opera,//Opera
    	WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,//Safari,苹果机自带浏览器
    	Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1//Mozilla
	};
	
	/*
		* Script: style.js
	*/
	var $style=window.$style={
		getPosition:function(){//如果父元素进行了绝对定位，获取相对于绝对定位的父元素，所处的X,Y
			var el=this,x=0,y=0;
			do {
				x += el.offsetLeft || 0;
				y += el.offsetTop  || 0;
				el = el.offsetParent;
			} 
			while (el);
			return [x,y];	
		},
		
		Width:function(){return parseInt(this.offsetWidth)||parseInt(this.clientWidth);},//获取对象的可见宽度
		
		Height:function(){return parseInt(this.offsetHeight)||parseInt(this.clientHeight);},//获取对象的可见高度
		
		display:function(){this.style.display=(this.style.display=='none')?'block':'none'; return this;},
		
		getStyle:function(prop){//获取元素样式中的属性值
			var value=null;
			if(this.style[prop]){ value=this.style[prop];}
			else if(this.currentStyle){ value=this.currentStyle[prop];}
			else if(window.getComputedStyle){
				prop=prop.replace(/([A-Z])/g,"-$1").toLowerCase();
				value=window.getComputedStyle(this,null).getPropertyValue(prop);
			}
			else{ return null;}
			return value;
		},
		
		setStyle:function(s){//设置元素样式中的属性值,样式字符串必须符合JS样式规则
			var stylelist=s.split(';');
			for (var i=0 ; i<stylelist.length-1 ; i++){
				var k = stylelist[i].split(":");
				this.style[k[0]] = k[1];
            }
			return this;
		},
		
		addClass:function(name){//增加元素新类，多个类名之间用分号隔开('class1 class2')
			this.className+=(this.className.length>0?' ':'')+name; return this;
		},
		
		removeClass:function(name){//删除元素类
			if(this.className.length<1) return false;
			var newName=fftools.toArray(name);
			for(var i=0;i<newName.length;i++){ this.className=this.className.replace(new RegExp('( ?|^)'+newName[i]+'\\b'), '')}
			return this;
		}
		
	};
	
	/*
		* Script: dom.js
	*/
	var $dom=window.$dom={
		addElement:function(ele){
				
		}
	};
	
	/*
		* Script: cookie.js
	*/
	var $cookie=window.$cookie={
		read:function(objName){
			var sRE='(?:;)?'+objName+'=([^;]*);?'
			var oRE=new RegExp(sRE);
			if(oRE.test(document.cookie)){
				return decodeURIComponent(RegExp['$1']);
			}
			else{
				return null;	
			}
		},
		
		write:function(objName,objValue,objDay,sPath,sDomain,bSecure){
			var str =objName+"="+encodeURIComponent(objValue);
			if(objDay){
				var date=new Date();
				date.setTime(date.getTime() + objDay*24*60*60*1000);
				str +='; expires=' + date.toGMTString();	
			}
			if(sPath){ str+='; path='+sPath;}
			if(sDomain){ str+='; domain='+sDomain;}
			if(bSecure){ str+='; secure';}
			document.cookie=str;
			return this;
		},
		
		del:function(objName){
			$cookie.write(objName,'');
		}
		
	};
	
	/*
		* Script: ajax.js
	*/
	var $ajax=window.$ajax={
		
	};
	
//})();