﻿/*******************************************
Name: TsoLongSlide.js
Version: 1.0
Author: TsoLong
QQ: 415802721
Email: tsolong@126.com
WebSite: http://www.tsolong.com/
Created Date: 2009-05-08
Description:
	图片滑动类
*******************************************/
var TsoLongSlide = function(){
	this.obj = null;				//滑动的对象
	this.direction = null;			//滑动方向(x 为横向滑动 y 纵向滑动)
	this.speed = 0;					//每次滑动的时间间隔(毫秒)
	this.totalStep = 10;			//滑动次数
	this.autoPlay = false;			//是否自动播放
	this.autoPlaySpeed = 1000;		//自动播放时间间隔(毫秒)
	
	this.container = null;
	this.items = [];
	this.itemsWidth = 0;
	this.itemsHeight = 0;
	
	this.startX = 0;
	this.endX = 0;
	this.currentX = 0;
	
	this.startY = 0;
	this.endY = 0;
	this.currentY = 0;
	
	this.currentIndex = 1;
	
	this.moveStatus = null;
	this.autoPlayStatus = null;
	
	this.startStep = 0;
}
TsoLongSlide.prototype = {
	init: function(config){
		for (var p in config) 
			this[p] = config[p];
		
		if (typeof(this.obj) == "string") 
			this.obj = document.getElementById(this.obj);
		
		this.container = document.all ? this.obj.childNodes[0] : this.obj.childNodes[1];
		
		var ul = this.obj.getElementsByTagName("ul");
		for (var i = 0; i < ul.length; i++) {
			this.items.push(ul[i]);
		}
		
		if (this.items.length > 0) {
			if (this.direction == "x") {
				this.itemsWidth = this.items[0].offsetWidth;
				this.container.style.width = (this.items.length * this.itemsWidth) + "px";
			}
			else 
				if (this.direction == "y") {
					this.itemsHeight = this.items[0].offsetHeight;
					this.container.style.height = (this.items.length * this.itemsHeight) + "px";
				}
			if (this.autoPlay) 
				this.play();
		}
	},
	previous: function(){
		var oThis = this;
		if (arguments[0] != "auto") 
			this.stop();
		
		if (this.moveStatus == null && this.items.length > 0) {
			if (this.direction == "x") {
				this.startX = this.currentIndex > 1 ? (this.currentIndex - 1) * this.itemsWidth : 0;
				this.endX = this.currentIndex > 1 ? this.startX - this.itemsWidth : (this.items.length - 1) * this.itemsWidth;
				this.currentX = this.startX;
				
				var v;
				if(oThis.startX < oThis.endX)
					v = oThis.endX - oThis.startX;
				else
					v = oThis.startX - oThis.endX;
				
				this.moveStatus = setInterval(function(){
					oThis.startMove(v);
				}, this.startX == 0 ? 1 : this.speed);
			}
			else 
				if (this.direction == "y") {
					this.startY = this.currentIndex > 1 ? (this.currentIndex - 1) * this.itemsHeight : 0;
					this.endY = this.currentIndex > 1 ? this.startY - this.itemsHeight : (this.items.length - 1) * this.itemsHeight;
					this.currentY = this.startY;
					
					var v;
					if(oThis.startY < oThis.endY)
						v = oThis.endY - oThis.startY;
					else
						v = oThis.startY - oThis.endY;
					
					this.moveStatus = setInterval(function(){
						oThis.startMove(v);
					}, this.startY == 0 ? 1 : this.speed);
				}
			this.currentIndex += this.currentIndex > 1 ? -1 : this.items.length - 1;
		}
	},
	next: function(){
		var oThis = this;
		if (arguments[0] != "auto") 
			this.stop();
		
		if (this.moveStatus == null && this.items.length > 0) {
			if (this.direction == "x") {
				this.endX = this.currentIndex < this.items.length ? this.currentIndex * this.itemsWidth : 0;
				this.startX = this.currentIndex < this.items.length ? this.endX - this.itemsWidth : (this.items.length - 1) * this.itemsWidth;
				this.currentX = this.startX;
				
				var v;
				if(oThis.startX < oThis.endX)
					v = oThis.endX - oThis.startX;
				else
					v = oThis.startX - oThis.endX;
				
				this.moveStatus = setInterval(function(){
					oThis.startMove(v);
				}, this.endX == 0 ? 1 : this.speed);
			}
			else 
				if (this.direction == "y") {
					this.endY = this.currentIndex < this.items.length ? this.currentIndex * this.itemsHeight : 0;
					this.startY = this.currentIndex < this.items.length ? this.endY - this.itemsHeight : (this.items.length - 1) * this.itemsHeight;
					this.currentY = this.startY;
					
					var v;
					if(oThis.startY < oThis.endY)
						v = oThis.endY - oThis.startY;
					else
						v = oThis.startY - oThis.endY;
					
					this.moveStatus = setInterval(function(){
						oThis.startMove(v);
					}, this.endY == 0 ? 1 : this.speed);
				}
			this.currentIndex += this.currentIndex < this.items.length ? 1 : 1 - this.items.length;
		}
	},
	startMove: function(v){
		this.startStep++;
		var tt = Math.ceil(TL.tween.list["exponential"]["easeInOut"](this.startStep, 0, v, this.totalStep));
		
		if (this.direction == "x") {
			if (this.currentX < this.endX) {
				this.currentX = this.startX + tt;
				if (this.currentX < this.endX)
					this.container.style.left = "-" + this.currentX + "px";
				else
					this.endMove();
			}
			else {
				this.currentX = this.startX - tt;
				if (this.currentX > this.endX)
					this.container.style.left = "-" + this.currentX + "px";
				else
					this.endMove();
			}
		}
		else 
			if (this.direction == "y") {
				if (this.currentY < this.endY) {
					this.currentY = this.startY + tt;
					if (this.currentY < this.endY)
						this.container.style.top = "-" + this.currentY + "px";
					else
						this.endMove();
				}
				else {
					this.currentY = this.startY - tt;
					if (this.currentY > this.endY)
						this.container.style.top = "-" + this.currentY + "px";
					else
						this.endMove();
				}
			}
	},
	endMove: function(){
		if (this.direction == "x") {
			this.container.style.left = "-" + this.endX + "px";
			this.startX = 0;
			this.endX = 0;
			this.currentX = 0;
		}
		else 
			if (this.direction == "y") {
				this.container.style.top = "-" + this.endY + "px";
				this.startY = 0;
				this.endY = 0;
				this.currentY = 0;
			}
		clearTimeout(this.moveStatus);
		this.moveStatus = null;
		
		this.startStep = 0;
	},
	play: function(){
		if (this.autoPlayStatus == null) {
			var oThis = this;
			this.autoPlay = true;
			this.autoPlayStatus = setInterval(function(){
				oThis.next("auto");
			}, this.autoPlaySpeed);
		}
	},
	stop: function(){
		this.autoPlay = false;
		clearTimeout(this.autoPlayStatus);
		this.autoPlayStatus = null;
	}
}


var TL = {}
TL.tween = {};
TL.tween.list = {
	linear: function(t, b, c, d){
		return c * t / d + b;
	},
	quadratic: {
		easeIn: function(t, b, c, d){
			return c * (t /= d) * t + b;
		},
		easeOut: function(t, b, c, d){
			return -c * (t /= d) * (t - 2) + b;
		},
		easeInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) 
				return c / 2 * t * t + b;
			return -c / 2 * ((--t) * (t - 2) - 1) + b;
		}
	},
	cubic: {
		easeIn: function(t, b, c, d){
			return c * (t /= d) * t * t + b;
		},
		easeOut: function(t, b, c, d){
			return c * ((t = t / d - 1) * t * t + 1) + b;
		},
		easeInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) 
				return c / 2 * t * t * t + b;
			return c / 2 * ((t -= 2) * t * t + 2) + b;
		}
	},
	quartic: {
		easeIn: function(t, b, c, d){
			return c * (t /= d) * t * t * t + b;
		},
		easeOut: function(t, b, c, d){
			return -c * ((t = t / d - 1) * t * t * t - 1) + b;
		},
		easeInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) 
				return c / 2 * t * t * t * t + b;
			return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
		}
	},
	quintic: {
		easeIn: function(t, b, c, d){
			return c * (t /= d) * t * t * t * t + b;
		},
		easeOut: function(t, b, c, d){
			return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
		},
		easeInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) 
				return c / 2 * t * t * t * t * t + b;
			return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
		}
	},
	sinusoidal: {
		easeIn: function(t, b, c, d){
			return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
		},
		easeOut: function(t, b, c, d){
			return c * Math.sin(t / d * (Math.PI / 2)) + b;
		},
		easeInOut: function(t, b, c, d){
			return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
		}
	},
	exponential: {
		easeIn: function(t, b, c, d){
			return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
		},
		easeOut: function(t, b, c, d){
			return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
		},
		easeInOut: function(t, b, c, d){
			if (t == 0) 
				return b;
			if (t == d) 
				return b + c;
			if ((t /= d / 2) < 1) 
				return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
			return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	},
	circular: {
		easeIn: function(t, b, c, d){
			return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
		},
		easeOut: function(t, b, c, d){
			return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
		},
		easeInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) 
				return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
			return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
		}
	},
	elastic: {
		easeIn: function(t, b, c, d, a, p){
			if (t == 0) 
				return b;
			if ((t /= d) == 1) 
				return b + c;
			if (!p) 
				p = d * .3;
			if (!a || a < Math.abs(c)) {
				a = c;
				var s = p / 4;
			}
			else 
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
		},
		easeOut: function(t, b, c, d, a, p){
			if (t == 0) 
				return b;
			if ((t /= d) == 1) 
				return b + c;
			if (!p) 
				p = d * .3;
			if (!a || a < Math.abs(c)) {
				a = c;
				var s = p / 4;
			}
			else 
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
		},
		easeInOut: function(t, b, c, d, a, p){
			if (t == 0) 
				return b;
			if ((t /= d / 2) == 2) 
				return b + c;
			if (!p) 
				p = d * (.3 * 1.5);
			if (!a || a < Math.abs(c)) {
				a = c;
				var s = p / 4;
			}
			else 
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			if (t < 1) 
				return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
			return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
		}
	},
	back: {
		easeIn: function(t, b, c, d, s){
			if (s == undefined) 
				s = 1.70158;
			return c * (t /= d) * t * ((s + 1) * t - s) + b;
		},
		easeOut: function(t, b, c, d, s){
			if (s == undefined) 
				s = 1.70158;
			return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
		},
		easeInOut: function(t, b, c, d, s){
			if (s == undefined) 
				s = 1.70158;
			if ((t /= d / 2) < 1) 
				return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
			return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
		}
	},
	bounce: {
		easeIn: function(t, b, c, d){
			return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
		},
		easeOut: function(t, b, c, d){
			if ((t /= d) < (1 / 2.75)) {
				return c * (7.5625 * t * t) + b;
			}
			else 
				if (t < (2 / 2.75)) {
					return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
				}
				else 
					if (t < (2.5 / 2.75)) {
						return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
					}
					else {
						return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
					}
		},
		easeInOut: function(t, b, c, d){
			if (t < d / 2) 
				return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
			else 
				return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
		}
	}
}