﻿var see=function(s,sp){
	var see=document.getElementById("see")
	see.innerHTML = sp ? (s+sp+see.innerHTML) : s;
}
var slideMenu2=function(_containerEl_){
	var $=function(s){return(typeof(s)=="string"?document.getElementById(s):s);},
		uA=navigator.userAgent.toLowerCase(),
		msie=/msie/.test(this.uA)&&!/opera/.test(this.uA),
		rmCls=function(el,c){
			if(el=$(el)){
				var _c=(el.className || "").split(/\s+/),str="";
				for(var i in _c){if(_c[i]!=c) str+=" "+_c[i];}
				el.className=str;
			}
			return el;
		},
		addCls=function(el,c){
			if(el=$(el)){rmCls(el,c).className+=" "+c};
			return el;
		},
		hasCls=function(el,_c){
			var el=$(el),c=(el.className || "").split(/\s+/), l=c.length;
			while(l--){if(c[l]==_c) return true;};
			return false;
		},
		$C=function(dom,c){
			if(typeof(c)!="string") return c;
			var cObject=$(dom || document.body).childNodes,i=0,rsArray=new Array();
			for(;i<cObject.length;i++)if(cObject[i].nodeType==1 && hasCls(cObject[i],c)){rsArray=rsArray.concat(cObject[i]);}
			return rsArray;
		},
		$T=function(dom,t){
			if(typeof(t)!="string") return t;
			return $(dom || document).getElementsByTagName(t);
		},
		$Str=function(s){
			return typeof(s)=="string";
		},
		addEvent=function(element,type,handler){
			if (element.addEventListener) {
				element.addEventListener(type, handler, false);
			}else{
				var old=element["on" + type];
				element["on" + type]=function(event){
					if (typeof(old)=="function") old.call(this,event);
					handler.call(this,event);
				}
			}
			return element;
		},
		dgFn=function(fn,obj){
			return function(){fn.apply(obj,Array.prototype.slice(arguments))};
		};
		
	return{
		_durationInt:10,_intervalTime:10,
		_containerEl:$(_containerEl_),_boxListEl:null,_titleListEl:null,_bodyListEl:null,
		_boxCount:0,
		_conWidth:0,_maxBoxWidth:0,_minBoxWidth:0,_theoryWidth:0,_averangeWidth:0,
		_unCurrWidth:0,
		_currBoxEl:null,
		_currIndexInt:0,_firstIndexInt:0,
		_onClass:'on',_evenType:'click',_slider:"body",
		_callbackFn:null,
		_unMinBox:{},
		ID:"ID"+Math.random(),
		setDuration:function(n){
			this._durationInt=parseInt(n) || this._durationInt;
			return this;
		},
		setIntervalInt:function(n){
			this._intervalTime=parseInt(n) || this._intervalTime;
			return this;
		},
		setContainer:function(s){
			this._containerEl=$(s || this._containerEl);
			return this;
		},
		setFirst:function(f){
			this._firstIndexInt=parseInt(f) || this._firstIndexInt;
			return this;
		},
		setEvent:function(e){
			this._evenType=e || this._evenType;return this;
		},
		setSlider:function(s){
			this._slider=s || this._slider;
			return this;
		},
		_getElements:function(s,from){
			if(typeof(s)!="string") return s;
			from=$(from || this._containerEl);
			var _px=s.substr(0,1);
			return _px=="." ? $C(from,s.substr(1)):
				( _px=="#" ? $(s.substr(1)) : $T(from,s));
		},
		setBoxList:function(s,from){
			this._boxListEl=this._getElements(s || "dl",from);
			return this;
		},
		setTitleList:function(s,from){
			this._titleListEl=this._getElements(s || "dt",from);
			return this;
		},
		setBodyList:function(s,from){
			this._bodyListEl=this._getElements(s || "dd" ,from);
			return this;
		},
		getWidth:function(){
			this.boxWidth=this.titleWidth=this.bodyWidth=0;
			with(this){
				boxWidth=parseInt((_boxListEl && _boxListEl[0])?_boxListEl[0].offsetWidth:0);
				titleWidth=parseInt((_titleListEl && _titleListEl[0])?_titleListEl[0].offsetWidth:0);
				bodyWidth=parseInt((_bodyListEl && _bodyListEl[0])?_bodyListEl[0].offsetWidth:0);
				return [boxWidth,titleWidth,bodyWidth];
			}
		},
		maxWidth:function(w){
			this._maxBoxWidth= w || this._maxBoxWidth;
			if(this._maxBoxWidth==0)
				this._maxBoxWidth=this._slider=="box" ? this.getWidth()[0] : this.getWidth()[2];
			return this;
		},
		minWidth:function(w){
			this._minBoxWidth= w || this._minBoxWidth;
			if(this._minBoxWidth==0)
				this._minBoxWidth=this._slider=="box" ? this.getWidth()[1] : 0;
			return this;
		},
		callback:function(fn){
			this._callbackFn=typeof(fn)=="function" ? fn : function(){};
			return this;
		},
		build:function(con,_firstIndexInt_,du,time,max,min,box,nw){
			this.setContainer(con).setDuration(du).setIntervalInt(time)
				.setBoxList().setTitleList().setBodyList()
				.maxWidth(max).minWidth(min).setFirst(_firstIndexInt_);
			this._slicerListEl=this[(this._slider=="box"?"_box":"_body")+"ListEl"];

			var i=0;			
			with(this){
				_boxCount=_boxListEl.length;//item count
				if(_firstIndexInt && _firstIndexInt>_boxCount) _firstIndexInt=1;
				_theoryWidth=_maxBoxWidth+_minBoxWidth*(_boxCount-1);

				//for _firstIndexInt==0
				// average width of each item.
				_averangeWidth=_theoryWidth/_boxCount;				
				//one is spread,and the last item's width
				_unCurrWidth=_minBoxWidth || Math.floor((_theoryWidth-_maxBoxWidth)/(_boxCount-1));
				
				for(i=0;i<_boxCount;i++){
					var el=_slicerListEl[i];
					el["index"+this.ID]=i;
					if(_firstIndexInt>0 && i==_firstIndexInt-1){
						_currIndexInt=_firstIndexInt-1;
						addCls(el,_onClass);
						el.style.width=_maxBoxWidth+"px";
					}else if(_firstIndexInt>0 && i!=_firstIndexInt-1){			
						el.style.width=_minBoxWidth+"px";
					}else{
						el.style.width=_averangeWidth+'px';
					}
					attachElementEvent(i);
				}
			}
			//this.m["timer"]=window.setInterval(function(){that.slide(that.sa[that.co])},this.t);
			//this._auto();
			return this;
		},
		timer:function(_indexInt_){
			with(this){
				if(_currIndexInt==_indexInt_) return;
				var cls=_onClass;
				rmCls(_boxListEl[_currIndexInt],cls);
				rmCls(_titleListEl[_currIndexInt],cls);
				rmCls(_bodyListEl[_currIndexInt],cls);
				_currIndexInt=_indexInt_;
				addCls(_boxListEl[_indexInt_],cls);
				addCls(_titleListEl[_indexInt_],cls);
				addCls(_bodyListEl[_indexInt_],cls);
				var that = this;
				clearInterval(_containerEl["timer"+ID]);
				_containerEl["timer"+ID]=setInterval(function(){that.slide(_indexInt_);},that._intervalTime);		
			}		
		},
		attachElementEvent:function(index){
			var that=this,
			actionFn=function(event){
				that.timer.call(that,index);
				//clearInterval(that._containerEl["auto_timer"]);
			}
			addEvent(this._boxListEl[index],this._evenType,actionFn);
			
			if(false/*auto run*/){
				var mout=this.m.onmouseout;
				this.m.onmouseout=function(event){
					if(typeof mout=="function") mout.call(this,event);
					that._auto(that.cuIndex);
				}
			}
		},
		index:function(o){
			if(typeof(o["index"+this.ID])!="undefined") return o["index"+this.ID];
			with(this){
				var l=_boxCount;
				while(l--){
					if(_boxListEl[l]==o) return l;
				}
			}
		},
		slide:function(index){
			var el=this._slicerListEl[index],
			runTimeWidth=parseInt(el.style.width,'10'),
			newRunWidth=0,fixWidth=0,
			unFinish=function(u){
				for(var i in u){
					if(u[i]!=null) return [i,u[i]];
				}
				return null;
			}
			if(runTimeWidth<this._maxBoxWidth || unFinish(this._unMinBox)!=null){
				var otherBoxTotalWidth=0,i=0;
				for(;i<this._boxCount;i++){
					if(this._slicerListEl[i]!=el){
						var otherBox,otherBoxWidth;
						var otherBoxStep=1;//constrict step. the most miniature step is 1;
						otherBox=this._slicerListEl[i];//item not current spreading
						otherBoxWidth=parseInt(otherBox.style.width,'10');
						
						if(otherBoxWidth>this._minBoxWidth){
							otherBoxStep=Math.floor((otherBoxWidth-this._minBoxWidth)/this._durationInt);
							otherBoxStep=(otherBoxStep>0)?otherBoxStep:1;
							//constrict the box
							otherBoxWidth-=otherBoxStep;
							otherBox.style.width=otherBoxWidth+'px';
						}
						this._unMinBox["u"+i] =otherBoxWidth>this._minBoxWidth ? otherBox : null;						
						otherBoxTotalWidth+=otherBoxWidth;//otherBoxTotalWidth
					}
				}
				newRunWidth=this._theoryWidth-otherBoxTotalWidth;
				el.style.width=(this._theoryWidth-otherBoxTotalWidth)+'px';
			}else{
				if(runTimeWidth>this._maxBoxWidth) el.style.width=this._maxBoxWidth+'px';
				clearInterval(this._containerEl["timer"+this.ID]);
			}
		},
		see:function(s){
			var SE=this[s || "_theoryWidth"];
			alert(typeof SE=="function" ? SE.call(this) : SE );
			return this;
		},
		auto:function(at,f){
			at===false? ( this.first(f),this.at=null ) :(this.first(f || 1), (this.at=at || 3600) );
			return this;
		},
		_auto:function(index){
			if(!this.at) return;
			var that=this;
			var ct=index || that.sl-1;
			var fn=function(){
				ct=++ct>=that.l? 0 : ct;
				//see(ct,",");
				that._timer(that.sa[ct]);
			}
			clearInterval(this.m["auto_timer"]);
			this.at ? ( this.m["auto_timer"]=setInterval(fn,this.at) ) : (null);
			return true;
		}
	}
};

var attacheStart=function(){
	slideMenu2("accordion").setSlider("").setFirst(3).build();
}