﻿/*photo scroll */

	var imagesScroll = function () {
            var  timer = null;		
			// 缓动类
			var Tween = {
				Quart: {
					easeOut: function(t,b,c,d){
						return -c * ((t=t/d-1)*t*t*t - 1) + b;
					}
				}
			}
			function query(cl,parent){
				var _parent =  document.getElementById(parent);
				var _childArray = [];
				_childArray = _parent.getElementsByTagName(cl);
				return _childArray;
			};
			// 默认配置
			var defaultConfig = {
				auto: true, // 是否自动切换
				tween: Tween.Quart.easeOut, // 默认缓动类
				Time: 30, // 滑动延时
				duration: 60,    // 切换时间
				stay: 6000 // 停顿时间
			};
			// 主类构造函数
			var scrollTrans = function(sid, tag, config) {
				var self = this;
				if (!(self instanceof scrollTrans)) {
					return new scrollTrans( sid, tag, config);
				} 
				self.scroller = $('#'+sid)[0];
				if (!self.scroller) { 
					return;
				} 
				self.config = (function () {
										for( var a in  (config || {})) {
											 defaultConfig[a] = config[a];
										}
										 return defaultConfig;	
									})()
				self.timer = null;
				self.childs = query(tag,sid);
				self.count = self.childs.length;
				self.index = 0;
			};
			
			
			scrollTrans.prototype = {
				constructor: scrollTrans,
				transform: function(index) {
					var self = this;   
					self.time = 0;
					 index == undefined && (index = self.index);
					 index < 0 && (index = self.count - 1) || index >= self.count && (index = 0); 
					self.begin = parseInt($(self.scroller).css('top'));
					self.target = Math.round(parseInt(self.childs[index].offsetHeight) + parseInt($(self.childs[index]).css('marginBottom')));   			
					self.change = self.target - Math.abs(self.begin);
					self.duration = self.config.duration;
					self.run();
				},
				run: function() {
					var self = this;
					clearTimeout(self.timer);
					if(self.change && self.time < self.duration){
						self.moveTo(Math.round(self.config.tween(self.time++, self.begin, self.change, self.duration)));
						self.timer = setTimeout(function(){self.run();}, self.config.Time);
				   }
					else{
						self.timer = setTimeout(function() {
							$(self.scroller).css('top','0px');	
							$(self.scroller).append(self.childs[self.index]);						           					
						  if(self.index > (self.count - 1)){
							self.index = 0;
						}
							self.next();
						}, self.config.stay);
					}
				},
				moveTo: function(i) {
					$(this.scroller).css('top',-i + 'px');
				},
				next: function() {
					this.transform(self.index);
				},
				stopScroll : function() {
				     var self = this;
					 clearTimeout(self.timer);
				},
				scroll: function() {
				    var self = this;    
					setTimeout(function(){self.transform(self.index);}, self.config.stay);
				}
			};
			return {
			    st : function(sid, tag, config){return scrollTrans(sid, tag, config);}		
			}
	 }();
	 var scroller = imagesScroll.st('scrollphoto','li');
	 scroller.scroll();
	

