var loopGetEle = function(obj,oKey){
	if (obj.className.indexOf(oKey)==-1) {
		obj = obj.parentNode;
		return loopGetEle(obj,oKey);
	} else {
		return obj;
	}
}

AE.namespace('AE.tool.slideShow');

AE.tool.slideShow = function(){
	this.defConfig = {
		wrapperId : 'slideWrap',
		slideItemClass : 'slideShowItem',
		slideItemContentClass : 'itemWrap',

		currentClass : 'current',
		otherOnCurrentClass : 'otherOnCurrent',

		currentMaxWidth : 238,
		currentMinWidth : 48,
		averageWidth : 86,

		duration : 300,
		rate : 0.2,

		switchEvent : 'mouseover',
		currentClass : 'current',
		otherOnCurrentClass : 'otherOnCurrent'
	}
};

AE.tool.slideShow.prototype = YL.merge(AE.tool.slideShow.prototype, {
	_P_KEY : {
		_root : null,
		_items : null,
		_current : null,
		_isMotion : undefined,
		_timeout : undefined,
		_resetTime : undefined
	},

	event_switch : function(e){
		var e = e || window.event;
		var el = e.target || e.srcElement;
		var ele = loopGetEle(el,'slideShowItem');
		
		var _cfg = this.defConfig;
		var _static = this._P_KEY;
		var _self = this;

		if(_static['_resetTime']){clearTimeout(_static['_resetTime'])}
		if(_static['_timeout']){clearTimeout(_static['_timeout'])}

		var fire = function(){
			for(var i=0;i<_static['_items'].length;i=i+1){
				if(_static['_items'][i] != ele){// other
					YUD.removeClass(_static['_items'][i],_cfg['currentClass']);
					YUD.addClass(_static['_items'][i],_cfg['otherOnCurrentClass']);
					YUD.getElementsByClassName(_cfg['slideItemContentClass'],'*',_static['_items'][i])[0].style.width = _cfg['currentMinWidth'] + 'px';
				}else{	//current
					YUD.removeClass(ele,_cfg['otherOnCurrentClass']);
					YUD.addClass(ele,_cfg['currentClass']);
					var myAnim = new YAHOO.util.Anim(YUD.getElementsByClassName(_cfg['slideItemContentClass'],'*',ele)[0],{
						width: {to: _self.defConfig['currentMaxWidth']}
					}, _cfg['rate']);
					myAnim.animate();
				}
			}
			YUE.on(document,'mouseover',_self.view_reset,_self,true);
		}
		_static['_timeout'] = setTimeout(fire,_cfg['duration']);
	},

	event_init : function(){
		YUE.on(this._P_KEY['_items'],this.defConfig['switchEvent'],this.event_switch,this,true);
		//YUE.on(document,'mouseover',this.view_reset,this,true);
	},

	view_reset : function(e){
		try{
			var e = e || window.event;
			var _self = this;
			var xy = YUD.getXY(get(this.defConfig['wrapperId']));
			var wh = [get(this.defConfig['wrapperId']).offsetWidth,get(this.defConfig['wrapperId']).offsetHeight];
			var area = [wh[0] + xy[0],wh[1] + xy[1]];
			var _top = document.documentElement.scrollTop;

			if((e.clientX < xy[0] || e.clientX > area[0]) || (e.clientY < (xy[1] - _top) || e.clientY > (area[1] + _top))){
				var reset = function(){
					for(var i=0;i<_self._P_KEY['_items'].length;i=i+1){
						_self._P_KEY['_items'][i].className = 'slideShowItem';
						if(window.openDatabase){
							YUD.getElementsByClassName(_self.defConfig['slideItemContentClass'],'*',_self._P_KEY['_items'][i])[0].style.width = _self.defConfig['averageWidth'] + 'px';
						}else{
							YUD.getElementsByClassName(_self.defConfig['slideItemContentClass'],'*',_self._P_KEY['_items'][i])[0].removeAttribute('style');
						}
					}
				}
				YUE.removeListener(document,'mouseover',_self.view_reset);
				this._P_KEY['_resetTime'] = setTimeout(reset, 3000);
			}
		}catch(err){
			//alert(err)
		}
	},

	view_init : function(){
		try{
			this._P_KEY['_root'] = get(this.defConfig['wrapperId']);
			this._P_KEY['_items'] = YUD.getElementsByClassName(this.defConfig['slideItemClass'],'*',this._P_KEY['_root']);
			return true;
		}catch(e){
			return false;
		}
		
	},

	init : function(userConfig){
		this.defConfig = YL.merge(this.defConfig, userConfig || {});
		if(this.view_init()){
			this.event_init();
		}
	}
});
