//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Ghost Scroll | 跟随滚动条幽灵般呼吸移动的元素
// @require YAHOO.util.Anim
// PS.支持纵向
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
AE.namespace('AE.app');

AE.app.ghostScroll = function(){
 this.config = {
 		target				:	get("with-scroll"),	// 幽灵化目标
 		fullOpacity		:	1,									// 满载透明度
 		targetOpacity	:	0,									// 目标透明度
 		hideTime			:	0.5,								// 隐藏时间(s)
 		showTime			:	0.5,								// 再次显示时间(s)
  	ghostTime			:	0.6,								// 潜行时间(s)
  	bottom				:	200,								// 距离可视区域底部(px)
  	maxBottom			:	294									// 距离页面内容高度底部极限距离(px)
 };
 
 this.temp = {
 		hideAnimLock : false,				// 隐藏动画中锁定
 		showAnimTimeout : null			// 显示等待 Timtout ID
 };

 return this;
};

AE.app.ghostScroll.prototype = {
		
		// 滚动触发
		scrolling : function(){
			var _self = this,config = this.config,temp = this.temp;
			// 隐
			if(temp.hideAnimLock == false && YAHOO.util.Dom.getStyle(config.target,"opacity") > config.targetOpacity){
				var hideAnim = new YAHOO.util.Anim(config.target,{
					opacity:{to:config.targetOpacity}
				},config.hideTime);
				
				hideAnim.onComplete.subscribe(function(){
					temp.hideAnimLock = false;
				});
				hideAnim.animate();
				
				temp.hideAnimLock = true;
			};
			
			// 显
			clearTimeout(temp.showAnimTimeout);
			temp.showAnimTimeout = setTimeout(function(){
				if(YAHOO.util.Dom.getStyle(config.target,"opacity") > config.targetOpacity)return;
				var showAnim = new YAHOO.util.Anim(config.target,{
					opacity:{to:config.fullOpacity}
				},config.showTime);
				
				// 目标Y值
				toY = (YAHOO.util.Dom.getDocumentScrollTop()+YAHOO.util.Dom.getViewportHeight() - config.bottom);
				if(toY > YAHOO.util.Dom.getDocumentHeight()-config.maxBottom){
					toY = YAHOO.util.Dom.getDocumentHeight()-config.maxBottom;
				}
				YAHOO.util.Dom.setStyle(config.target, "top", toY + 'px');
				showAnim.animate();
			},config.ghostTime*1000);

		},
		
		// 事件绑定
		defineEvents : function(){
			var _self = this,config = this.config,temp = this.temp;
				YAHOO.util.Event.on(window,"scroll",function(){
					_self.scrolling();
				});
		},
		
		// 初始化
		init : function(customConfig){
			this.config = YL.merge(this.config,customConfig||{});
			var _self = this,config = this.config,temp = this.temp;
			_self.defineEvents();
			if(AE.bom.isIE){ CollectGarbage(); }
			return _self;
		}
};
