String.prototype.trim = function(){
	if(!String._specialCharPattern)
		String._specialCharPattern = /^\s+|\s+$/gi;
	String._specialCharPattern.lastIndex = 0;
	return this.replace(String._specialCharPattern,"");
}
var StyleMgr = {
	hasCSSClass:function(el,cssStyle){
		var pattern = new RegExp("\\b"+cssStyle+"\\b","g");
		if(pattern.test(el.className))
			return true;
		return false;
	},
	addCSSClass:function(el,cssStyle){
		if(!this.hasCSSClass(el,cssStyle))
			el.className += " "+cssStyle;
	},
	removeCSSClass:function(el,cssStyle){
		var pattern = new RegExp("^(.*)\\b("+cssStyle+")\\b(.*)$","g");
		if(pattern.test(el.className))
			el.className = RegExp.$1.trim()+" "+RegExp.$3.trim();
	},
	replaceCSSClass:function(el,cssStyle,newCssStyle){
		var pattern = new RegExp("^(.*)\\b("+cssStyle+")\\b(.*)$","g");
		if(pattern.test(el.className))
			el.className =  RegExp.$1+newCssStyle+RegExp.$3;
	},
	getCSSStyle:function(el,property){
		if(document.documentElement.currentStyle && el.currentStyle){
			if(property == "opacity"){
				var opacity = 100;
				try{
					opacity = el.filters["DXImageTransform.Microsoft.Alpha"].opacity;
				}catch(e){
					try{
						opacity = el.filters["alpha"].opacity;
					}catch(e){}
				}
				return opacity/100;
			}else if(property == "float"){
				return el.currentStyle["styleFloat"];
			}else{
				return el.currentStyle[property];
			}
		}else if(document.defaultView && document.defaultView.getComputedStyle){
			if(property == "float")
				property = "cssFloat";
			return document.defaultView.getComputedStyle(el,"")[property];
		}else{
			return el.style[property];
		}
	},
	setCSSStyle:function(el,property,value){
		if(el.currentStyle){
			if(property == "opacity"){
				el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+value*100+")";
				try{
					if(!el.currentStyle || !el.currentStyle.hasLayout())
						el.style.zoom = 1;
				}catch(e){}
				return;
			}
			if(property == "float")
				property = "styleFloat";
			el.style[property] = value;
		}else{
			if(property == "float")
				property = "cssFloat";
			el.style[property] = value;
		}
	}
}

var JEffect = function(target,property,from,to){
	this.target = target;
	this.property = property;
	this.from = from;
	this.to = to;
	this.unit = "";
	this.saveLength = 0;
	this.isEffecting = false;
	this.timer = {
		timer:null,
		start:0
	};
	this.constraints = {
		lpf:0,
		length:20,
		ipf:20
	};
	this.onComplete = null;
}
JEffect.CONSTRIANT_FROM = 0;
JEffect.CONSTRIANT_TO = 1;
JEffect.CONSTRIANT_PROPERTY = 2;
JEffect.CONSTRIANT_FRAMELENGTH = 3;
JEffect.CONSTRIANT_TARGET = 4;
JEffect.CONSTRIANT_UNIT = 5;
JEffect.prototype.setConstriant = function(c,v){
	if(c == JEffect.CONSTRIANT_FROM){
		this.from = v;
	}else if(c == JEffect.CONSTRIANT_TO){
		this.to = v;
	}else if(c == JEffect.CONSTRIANT_PROPERTY){
		this.property = v;
	}else if(c == JEffect.CONSTRIANT_FRAMELENGTH){
		this.constraints.length = v;
	}else if(c == JEffect.CONSTRIANT_TARGET){
		this.target = v;
	}else if(c == JEffect.CONSTRIANT_UNIT){
		this.unit = v;
	}else{
		throw new Error("Illegal Argument");	
	}
}
JEffect.prototype.analyze = function(){
	this.constraints.lpf = Math.abs(this.from-this.to)/this.constraints.length;
}
JEffect.prototype.start = function(){
	if(this.isEffecting)
		return;
	this.isEffecting = true;
	var _this = this;
	this.saveLength = this.from;
	this.timer.timer = setInterval(function(){
		_this._count();
	},this.constraints.ipf);
}
JEffect.prototype.pause = function(){
	if(this.isEffecting){
		clearInterval(this.timer.timer);
		this.timer.timer = null;
		this.timer.start = 0;
		this.isEffecting = false;			
	}
}
JEffect.prototype._count = function(){
	var f = this.from;
	var t = this.to;
	var p = this.constraints.lpf;
	if(t>f){
		if(this.saveLength+p>=t){
			this.target[this.property] = this.to+this.unit;
			clearInterval(this.timer.timer);
			this.timer.timer = null;
			this.timer.start = 0;
			this.isEffecting = false;
			if(this.onComplete != null){
				this.onComplete.call(this);;
			}			
		}else{
			this.target[this.property] = this.saveLength + p + this.unit;
			this.saveLength = this.saveLength + p;
		}
	}else{
		if(this.saveLength-p<=t){
			this.target[this.property] = this.to+this.unit;
			clearInterval(this.timer.timer);
			this.timer.timer = null;
			this.timer.start = 0;
			this.isEffecting = false;
			if(this.onComplete != null){
				this.onComplete.call(this);;
			}			
		}else{
			this.target[this.property] = this.saveLength - p + this.unit;
			this.saveLength = this.saveLength - p;
		}
	}
}

var SlideEffect = function(UI,option){
	this.UI = UI;
	this.option = option;
	this.UIContainerWidth = 0;
	this.UITabsWidth = 0;
	this.onSlide = null;
	this.onBeforeSlide = null;
	this.onAfterSlide = null;
	this.effect = null;
	this.init();
}
SlideEffect.LEFT = 0;
SlideEffect.RIGHT = 1;
SlideEffect.SLIDE_INTERVAL = 60;
SlideEffect.prototype = {
	init:function(){
		var _this = this;
		this.validate();
		this.effect = new JEffect(this.UI.containerUI,"scrollLeft",0,0);
		this.effect.onComplete = function(){
			if(_this.onAfterSlide)
				_this.onAfterSlide.call(this);
		}
	},
	validate:function(){
		this.UIContainerWidth = this.UI.containerUI.offsetWidth;
		this.UITabsWidth = 0;
		var bothMargin = 0;
		for(var i=0,len=this.UI.tabsUI.length;i<len;i++){
			if(0 == i)
				bothMargin = parseInt(StyleMgr.getCSSStyle(this.UI.tabsUI[i],"marginLeft")) + parseInt(StyleMgr.getCSSStyle(this.UI.tabsUI[i],"marginRight"));
			this.UITabsWidth += this.UI.tabsUI[i].offsetWidth + bothMargin;
		}
	},
	hasHeadSpace:function(){
		return this.UI.containerUI.scrollLeft>0;
	},
	hasTailSpace:function(){
		return this.UITabsWidth - this.UI.containerUI.scrollLeft - this.UIContainerWidth > 0;
	},
	slideToScale:function(x){
		var _this = this;
		if(x == this.UI.containerUI.scrollLeft)
			return;
		if(this.onBeforeSlide)
			this.onBeforeSlide.call(this);
		this.effect.setConstriant(JEffect.CONSTRIANT_FROM,this.UI.containerUI.scrollLeft);
		this.effect.setConstriant(JEffect.CONSTRIANT_TO,x);
		this.effect.analyze();
		this.effect.start();
	},
	slideToHead:function(){
		this.slideToScale(0);
	},
	slideToTail:function(){
		this.slideToScale(this.UITabsWidth-this.UIContainerWidth);
	},
	getCurrentScale:function(){
		return this.UI.containerUI.scrollLeft;
	},
	setScale:function(scale){
		this.UI.containerUI.scrollLeft = scale;
	}
}

var JTabPanel = function(){
	this.onStyle = "on";
	this.tabChangedListeners = [];
	this.activeTabIndex = -1;
	this.tabCtrls = [];
	this.tabContents = [];
}
JTabPanel.prototype = {
	setActiveTab:function(index){
		if(index<0||index>this.tabCtrls.length-1)
			return;//break
		var elapsedIndex = this.activeTabIndex;
		//if(index == elapsedIndex)
		//	return;//break;
		if(elapsedIndex != -1){
			this._setActiveTabUI(elapsedIndex,false);
		}
		this._setActiveTabUI(index,true);
		this.activeTabIndex = index;
		this.fireTabChanged(elapsedIndex,index);
	},
	_setActiveTabUI:function(index,active){
		if(active){
			if(!StyleMgr.hasCSSClass(this.tabCtrls[index],this.onStyle))
				StyleMgr.addCSSClass(this.tabCtrls[index],this.onStyle);
			this.tabContents[index].style.display = "block";
		}else{
			if(StyleMgr.hasCSSClass(this.tabCtrls[index],this.onStyle))
				StyleMgr.removeCSSClass(this.tabCtrls[index],this.onStyle);
			this.tabContents[index].style.display = "none";
		}
	},
	addTab:function(tabCtrl,tabContent){
		var _this=this;
		tabCtrl.setAttribute("jtabpanelindex",this.tabCtrls.length);
		tabCtrl.onclick = function(){
			var index = parseInt(this.getAttribute("jtabpanelindex"));
			_this.setActiveTab(index);
		}
		this.tabCtrls.push(tabCtrl);
		this.tabContents.push(tabContent);
	},
	addTabChangeListener:function(l){
		if(typeof l == "function"){
			this.tabChangedListeners.push(l);
		}
	},
	fireTabChanged:function(elapsedIndex,activeIndex){
		for(var i=0,len=this.tabChangedListeners.length;i<len;i++){
			this.tabChangedListeners[i].call(this,elapsedIndex,activeIndex);
		}
	},
	getActiveTabIndex:function(){
		return this.activeTabIndex;
	}
}

var Cookie = {
	setCookie:function(name,value,expires,path,domain,secure){
		if(!name || !value)
			return;
		var candidate = [];
		candidate.push(name+"="+encodeURIComponent(value));
		if(expires)
			candidate.push("expires="+expires.toGMTString());	
		if(path)
			candidate.push("path="+path);	
		if(domain)
			candidate.push("domain="+domain);
		if(secure)
			candidate.push("secure");
		document.cookie = candidate.join(";");	
	},
	getCookie:function(name){
		var res = "(?:; )?"+name+"=([^;]*);?";
		var re = new RegExp(res);
		if(re.test(document.cookie))
			return decodeURIComponent(RegExp["$1"]);
		else
			return null;	
	},
	deleteCookie:function(name,path,domain){
		this.setCookie(name,"",new Date(0),path,domain);
	}
}

function setTheme(tid){
	document.body.className = tid;
	Cookie.setCookie("theme_id",tid,new Date(new Date().getTime() + 31*24*60*60*1000));
}

function addBookmark(url,name){
	if(document.all){
		window.external.addFavorite(url,name);
	}else if(window.sidebar){
		window.sidebar.addPanel(name,url,"");
	}else{
	}
}

function setHomePage(url, o){
	if(document.all){
		o.style.behavior='url(#default#homepage)';
		o.setHomePage(url);
	}else if (window.sidebar){
		if(window.netscape){
			try{ 
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
			}catch (e){}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage',url);
	}else{
	}
	return false;
}



