// ----------------------- 循环显示图片的类 -------------------
/* 调用方法:
   <script type="text/javascript" language="javascript">
   var 变量名称 = new LoopImage(图片id, ...);
   变量名称.Add(图片地址, ...)
   重复上面的语句添加多个图片

   然后在图片的 src 属性中调用函数 src="javascript:变量名称.NextImage()"
*/

/* 定义一个循环显示图片的类
   imageId	-> 图片元素的 id 值
   titleId	-> 标题元素的 id 值 (可选参数)
   interval	-> 两幅图片转化间隔的时间(毫秒), 默认为 6000 (可选参数)
   transTime-> 转换图片使用的时间(秒), 默认为 2 (可选参数)
   transType-> 转换图片方式的名称, 详细资料请查阅相关资料, 默认随机转换 (可选参数)
   transParam-> 指定转换方式时, 附加的转换参数. 参数格式: 属性名称1=属性值1,属性名称2=属性值2, ... (可选参数)
*/
function LoopImage(imageId, titleId, interval, transTime, transType, transParam)
{
	var args = arguments.length;
	if (args == 0) return false;
	if (args < 2) var titleId = "";
	if (args < 3) var interval = 6000;
	if (args < 4) var transTime = 2;
	if (args < 5) var transType = null;
	if (args < 6) var transParam = null;
	this.config = {
		imageId : imageId,
		titleId : titleId,
		image : Array(),	// 储存图片地址的数组
		title : Array(),	// 储存图片标题数组
		link : Array(),		// 储存图片链接地址数组
		interval : interval,
		transTime : transTime,
		transType : transType,
		transParam : transParam
	};
}

/* 添加一个图片配置
   src -> 图片地址
   title -> 图片标题 (可选参数)
   href -> 标题连接地址, 空字符串或忽略表示没有链接 (可选参数)
*/
LoopImage.prototype.Add = function(src, text, href)
{
	var args = arguments.length;
	if (args == 0) return false;
	if (args < 2) var text = "";
	if (args < 3) var href = null;
	with(this.config)
	{
		image[image.length] = src;
		title[title.length] = text;
		link[link.length] = href;
	}
}

/* 随机取出 v1 到 v2 之间(包括 v1 和 v2)的一个整数
   v1 -> 正整数
   v2 -> 正整数, 可省略, 默认为 0
*/
LoopImage.prototype.Random = function(v1, v2)
{
	var ok = true;
	if (arguments.length == 1)
		if (typeof(v1) != "number" || v1 < 0) ok = false;
	else if (arguments.length == 2)
		if (typeof(v1) != "number" || v1 < 0 || typeof(v2) != "number" || v2 < 0) ok = false;
	else
		ok = false
	if (!ok)
	{
		alert("Random 函数需要一个或两个正整数参数");
		return false;
	}
	if (arguments.length == 1)
	{
		var v2 = v1;
		v1 = 0;
	}
	else
	{
		var temp = Math.max(v1, v2);
		v1 = Math.min(v1, v2);
		v2 = temp;
	}
	return Math.floor(Math.random() * (Math.floor(v2 - v1) + 1)) + v1;
}

/* 显示下一幅图片
   index -> 当前图片的索引值 (可选参数)
*/
LoopImage.prototype.NextImage = function(index)
{
	with(this.config)
	{
		var eImage = document.getElementById(imageId);
		var eTitle = document.getElementById(titleId);
		if (eImage)
		{
			if (document.all)
			{
				if (index == undefined || ++index == image.length) var index = 0;

				var filterStr = "Barn|BlendTrans|Blinds|CheckerBoard|Fade|GradientWipe|Inset|Iris|" + 
								"Pixelate|RadialWipe|RandomBars|RandomDissolve|RevealTrans|" +
								"Slide|Spiral|Stretch|Strips|Wheel|Zigzag";	// 有效的滤镜
				var type = transType;
				var params = transParam;
				// 检查转换滤镜名称是否有效
				if (filterStr.indexOf(type) == -1)
				{
					// 名称无效则随机选取一种, 并忽略 transParam 参数
					var filters = filterStr.split("|");
					type = new Array();
					for (var i = 0; i < filters.length; i++) type[type.length] = filters[i];
					// 添加 "RevealTrans" 转换效果的几率
					//for (var i = 0; i < 22; i++) type[type.length] = "RevealTrans";
					// 随机选择一个转换效果
					type = type[this.Random(type.length - 1)];
				}
				else
				{
					// 名称有效, 取得转换参数
					if (typeof(transParam) == "string")
					{
						params = {};
						var temp = transParam.split(",");
						for (var i = 0; i < temp.length; i++)
						{
							var tmp = temp[i].split("=");
							if (tmp.length != 2)
							{
								alert("转换滤镜的参数不正确");
								return false;
							}
							params[tmp[0].substr(0, 1).toUpperCase() + tmp[0].substr(1).toLowerCase()] = isNaN(parseInt(tmp[1])) ? tmp[1] : parseInt(tmp[1]);
						}
					}
					// 储存参数, 以便下次直接使用
					transParam = params;
				}
				var enabled = type != "Pixelate" ? "true" : "false";	// Pixelate 滤镜必须先把 enabled 属性设为 false
				var typeName = type != "BlendTrans" && type != "RevealTrans" ? "DXImageTransform.Microsoft." + type : type;
				// 为图片添加没有的转换滤镜
				if (!eImage.filters[typeName])
					eImage.style.filter += (type != "BlendTrans" && type != "RevealTrans" ? " progid:" : " ") + typeName + "(enabled=" + enabled + ")";
				// 设置转换时间
				eImage.filters[typeName].duration = transTime;
				// 如果没有指明转换参数, 则设置每种滤镜的默认转换参数
				if (params == null)
				{
					params = {};
					switch (type)
					{
						case "Barn" :
							params.Motion = this.Random(1) == 0 ? "in" : "out";	// 从外显示还是从内显示
							params.Orientation = this.Random(1) == 0 ? "vertical" : "horizontal";	// 横向显示还是纵向显示
							break;
						case "Blinds" :
							params.Bands = this.Random(1, 20);	// 百叶窗的窗格条数, 最大 100
							var direction = ["down", "up", "right", "left"];
							params.Direction = direction[this.Random(direction.length - 1)];	// 百叶窗开关的方向
							break;
						case "CheckerBoard" :
							params.SquaresX = this.Random(2, 24);	// 横向条数, 大于等于 2
							params.SquaresY = this.Random(2, 20);	// 纵向条数, 大于等于 2
							var direction = ["down", "up", "right", "left"];
							params.Direction = direction[this.Random(direction.length - 1)];	// 网格推拉的方向
							break;
						case "Fade" :
							params.Overlap = this.Random(10) / 10;	// 转换进程中源内容和目标内容都被显示的比例, 0 - 1.0
							break;
						case "GradientWipe" :
							params.GradientSize = this.Random(10) / 10;	// 图像内容被梯度渐隐条覆盖的百分比, 0 - 1.0
							params.Motion = this.Random(1) ? "forward" : "reverse";	// 图像出现方向是依据 WipeStyle 特性的设定, 还是取反
							params.WipeStyle = this.Random(1);	// 渐隐滚动条的方向
							break;
						case "Iris" :
							var style = ["PLUS", "DIAMOND", "CIRCLE", "CROSS", "SQUARE", "STAR"];
							params.IrisStyle = style[this.Random(style.length - 1)];	// 剪切轮廓的外形
							params.Motion = this.Random(1) == 0 ? "in" : "out";	// 从外显示还是从内显示
							break;
						case "Pixelate" :
							params.MaxSquare = this.Random(2, 50);	// 矩形色块的最大宽度, 2 - 50
							break;
						case "RadialWipe" :
							var style = ["CLOCK", "WEDGE", "RADIAL"];
							params.WipeStyle = style[this.Random(style.length - 1)];	// 擦除方式
							break;
						case "RandomBars" :
							params.Orientation = this.Random(1) == 0 ? "vertical" : "horizontal";	// 横向显示还是纵向显示
							break;
						case "RevealTrans" :
							params.Transition = 23;	// 随机显示
							break;
						case "Slide" :
							params.Bands = this.Random(1, 100);	// 多少滑条被抽离
							var style = ["HIDE", "PUSH", "SWAP"];
							params.SlideStyle = style[this.Random(style.length - 1)];	// 滑条抽离效果的方式
							break;
						case "Spiral" :
							params.GridSizeX = this.Random(1, 32);	// 横向盘旋次数
							params.GridSizeY = this.Random(1, 32);	// 纵向盘旋次数
							break;
						case "Stretch" :
							var style = ["SPIN", "HIDE", "PUSH"];
							params.StretchStyle = style[this.Random(style.length - 1)];	// 拉伸变形转换的方式
							break;
						case "Strips" :
							var motion = ["leftdown", "leftup", "rightdown", "rightup"];
							params.Motion = motion[this.Random(motion.length - 1)];	// 换新内容从哪一个角开始
							break;
						case "Wheel" :
							params.Spokes = this.Random(2, 20);	// 风车叶轮数目
							break;
						case "Zigzag" :
							params.GridSizeX = this.Random(1, 32);	// 横向盘旋次数
							params.GridSizeY = this.Random(1, 32);	// 纵向盘旋次数
							break;
					}
				}
				// 设置滤镜参数
				for (param in params) eval("eImage.filters[typeName]." + param + " = params[param];");
				// 应用滤镜
				eImage.filters[typeName].apply();
			}

			var styleCursor = link[index] == null ? "auto" : "hand";
			var clickFunction = link[index] == null ? null : function() {window.open(link[index]);};

			eImage.src = image[index];
			eImage.style.cursor = styleCursor;
			eImage.onclick = clickFunction;
			if (eTitle)
			{
				eTitle.innerHTML = title[index];
				eTitle.style.cursor = styleCursor;
				eTitle.onclick = clickFunction;
			}
			clickFunction = null;
			// 开始转换
			if (document.all) eImage.filters[typeName].play();
		}
		var temp = this;
		_loopImageTheTimer = setTimeout(function(){temp.NextImage(index); temp = null;}, interval);
	}
}