/*
 * zCool.Stack.Slide_accordion 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
// @imports zCool.Stack.js

// 扩展 Drag 类
(function(window, $, undefined){

	// 快捷获取
	var document = window.document,
	
		ROOT = document.documentElement,
	
		$UIStackSlide_accordion = $.UI.Stack.defined('Slide_accordion', function(root, settings){
			
			$.UI.Stack.call(this, root, settings);
			
			$( this.getPages() )
				[ this.triggerPageType ]
					( this.bind( this.activedPageMouseenter ) );
			
			this.autoplay && this.play();
		});
	
	// 扩展原型
	$.extend($UIStackSlide_accordion.prototype, {
		
		defaultSettings: {
			
			// Boolean 缺省自动循环播放
			autoplay: true,
			
			// 切换间隔
			interval: 3000,
			
			// 缺省为缩略图
			tabView: 'none',
			
			triggerPageType: 'mouseenter'
			
		},
		
		// 渲染组件
		render: function(){
			
			this.setPages();
			this.renderPages();
		},
		
		// 渲染组件
		renderPages: function(){
			
			var pages = this.getPages(),
				length = pages.length,
				pagesRootWidth = this.pagesRoot.offsetWidth,
				pageWidth = pages[0].offsetWidth,
				average = (pagesRootWidth - pageWidth) / (length - 1);
				
			pages.each(function( page, i ){
				$(page).css({
					left: i * average + 'px',
					zIndex: length - i
				})
				[this.activedIndex == i ? 'addClass' : 'removeClass']
				(this.pageActivedClass);
			}, this);
		},
		
		// 停留在激活的标签页
		activedPageMouseenter: function(e){
			this.pageActive(e.currentTarget);
			$(e.currentTarget).mouseleave( this.bind( this.activedPageMouseleave ) );
			this.stop();
		},
		
		// 离开激活的标签页
		activedPageMouseleave: function(e){
			$(e.currentTarget).noMouseleave( this.activedPageMouseleave );
			this.play();
		},
		
		play: function(){
			
			var _this = this;
			
			this.timeoutId = window.setTimeout(function(){
				
				_this.indexActive( _this.activedIndex + 1 );
				
				_this.timeoutId = window.setTimeout( arguments.callee, _this.interval );
				
			}, this.interval);
		},
		
		indexActive : function( activeIndex ){
			
			var pages = this.getPages(), 
				pagesLength = pages.length;
				
			activeIndex >= pagesLength && (activeIndex = 0);
				
			if( activeIndex !== this.activedIndex ){
				
				$( pages[ this.activedIndex ] ).removeClass( this.pageActivedClass );
				
				$( pages[ activeIndex ] ).addClass( this.pageActivedClass );
				
				this.activedIndex = activeIndex;
				
				pages.each( activeIndex ? function( page, i ){
					page.style.zIndex =  pagesLength + (i < activeIndex ? i : i > activeIndex ? -i : pagesLength - 1);
				} : function( page, i ){
					page.style.zIndex = pagesLength - i;
				});
			}
		},
		
		stop: function(){
			this.clearTimeout();
		}
		
	});
	
	// 扩展成员	
	/*$.extend($Slide, {
		
	});*/
    
})(this, zCool);

