/*
 * zCool.Marquee 1.0 - Javascript
 *
 * Copyright (c) 2009 - 2010  周柏民 (http://jscaler.appspot.com/)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2010-06-28 19:28:00 BeiJing $
 * $Revision: 33 $
 */
 
// @imports zCool.js
// @imports zCool.UI.js

// 扩展 Drag 类
(function(window, $, undefined){
	
	var DOC = window.document,
	
		ROOT = DOC.documentElement,
		
		PROP_SIZE = {left:'Width', right:'Width', up:'Height', down:'Height'},
		
		PROP_POSITION = {left:'left', right:'left', up:'top', down:'top'},
		
		POSITIVE_NEGATIVE = {left:-1, up:-1, right:1, down:1 },
		
		$UIMarquee = $.UI.defined('Marquee', function(root, settings){
			
			$.UI.call(this, root, settings);
			
			this.render();
		});
	
	// 扩展原型
	$.extend($UIMarquee.prototype, {
		
		defaultSettings: {
			scrollEnable: true,
			direction: 'left',
			duration: 500,
			interval: 2000,
			delay: 20,
			topItemIndex: 0,
			timeoutId: null
		},
		
		// 渲染组件
		render: function(){
			
			this.setSizeRelatedProp();
			this.setBody();
			this.setScroll();
		},
		
		// 设置所有和尺寸关联的属性
		setSizeRelatedProp : function(){
			
			var sizeProp = PROP_SIZE[ this.direction ];
				
			this.clientSizeProp = 'client' + sizeProp;
			this.cssSizeProp = sizeProp.toLowerCase();
			this.cssPositionProp = PROP_POSITION[ this.direction ];
			
			this.positiveNegative = POSITIVE_NEGATIVE[ this.direction ]
			
		},
		
		// 设置滚动体
		setBody: function(){
			
			this.body = $.first(this.root, '.Marquee-body');
			this.list = $.first(this.body, '.Marquee-list');
			this.items = $.children( this.list );
			// 清除中间穿插的文本节点
			$(this.list).append(this.items);
		},
		
		// 设置是否滚动
		setScroll: function(){
			
			this.bodyClientSize = this.body[ this.clientSizeProp ];
			this.listClientSize = this.list[ this.clientSizeProp ];
			
			if( this.listClientSize > this.bodyClientSize ){
			
//				$(this.body).
//					mouseenter( this.bind(this.stopScrollHandler) ).
//					mouseleave( this.bind(this.startScrollHandler) );
				
				this.listCssPositionPropValue = 0;
				
				// 获取所有项的尺寸
				this.itemsSize = this.items.map(function(el){
					return el[this.clientSizeProp];
				}, this);
				
				this.startScroll();
			}
				
		},
		
		// 开始滚动
		startScroll: function( enable ){
			
			var instance = this,
				listStyle = this.list.style,
				topItemStyle = this.items[ this.topItemIndex ].style,
				cssPositionProp = this.cssPositionProp,
				delay = this.delay,
				t = 0,
				b = this.listCssPositionPropValue,
				c = this.itemsSize[ this.topItemIndex ]*this.positiveNegative,
				d = this.duration/delay,
				method = function(t, b, c, d){
					return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
				};
			
			//
			(function(){
					
				if( t < d ){
					listStyle[ cssPositionProp ] = method(t++, b, c, d) + 'px';
					window.setTimeout(arguments.callee, delay);
				}
				else{
					
					if( instance.topItemIndex < instance.items.length - 1 ){
						instance.topItemIndex++;
						instance.listCssPositionPropValue = b + c;
						listStyle[ cssPositionProp ] = b + c + 'px';
						topItemStyle[ cssPositionProp ] = instance.listClientSize + 'px';
					}
					else{
						instance.topItemIndex = 0;
						instance.listCssPositionPropValue = 0;
						listStyle[ cssPositionProp ] = '';
						instance.items.each(function(el){
							el.style[ cssPositionProp ] = '';
						});
					}
					
					if( instance.scrollEnable ){
						instance.timeoutId = window.setTimeout(function(){
							instance.startScroll();
						}, instance.interval);
					}
				}
				
			})();
		},
		
		// 停止滚动
		stopScroll: function(){
			this.clearTimeout();
			this.scrollEnable = false;
		},
		
		// 开始滚动事件
		startScrollHandler: function(e){
			var instance = this;
			this.timeoutId = window.setTimeout(function(){
				instance.startScroll();
			}, this.interval);
		},
		
		
		// 停止滚动事件
		stopScrollHandler: function(e){
			this.stopScroll();
		}
		
	});
	
	// 扩展成员	
	/*$.extend($Stack, {
		
	});*/
	
    
})(this, zCool);

