// ----------------------- 循环显示图片的类 -------------------
/* 调用方法:
   首先包含这个文件: <script type="text/javascript" language="javascript" src="javascript/loopimage.js"></script>
   <script type="text/javascript" language="javascript">
   var 变量名称 = new LoopImage();

   变量名称.属性 = 值
   重复上面的语句设置需要的属性值(属性可以参考下面的说明)

   变量名称.Add(图片地址, ...)
   重复上面的语句添加多个图片

   然后在图片的 src 属性中调用函数 src="javascript:变量名称.NextImage()"
*/

/* 定义一个循环显示图片的类 */
function LoopImage()
{
	this.imageId = null;		// 图片元素的 id 值
	this.imageWidth = null;		// 图片的宽度
	this.imageHeight = null;	// 图片的高度
	this.titleId = null;		// 标题元素的 id 值 (可选)
	this.linkId = null;		// 连接元素的 id 值 (可选)
	this.contentId = null;		// 内容元素的 id 值 (可选)
	this.interval = 5000;		// 两幅图片转化间隔的时间(毫秒), 默认为 6000 (可选)
	this.transTime = 2;			// 转换图片使用的时间(秒), 默认为 2 (可选) - 此时间包含在 interval 中
	this.transType = null;		// 转换图片方式的名称, 详细资料请查阅相关资料, 默认随机转换 (可选)
	this.transParam = null;		// 指定转换方式时, 附加的转换参数. 参数格式: 属性名称1=属性值1,属性名称2=属性值2, ... (可选)
	this.loopImageTimer = null;
	this.image = Array();	// 储存图片地址的数组
	this.title = Array();	// 储存图片标题的数组
	this.link = Array();	// 储存图片链接地址的数组
	this.content = Array();	// 储存内容的数组
}
/* 
 添加一个图片配置
 src -> 图片地址
 title -> 图片标题 (可选参数)
 href -> 标题连接地址, 空字符串或忽略表示没有链接 (可选参数)
*/
LoopImage.prototype.Add = function(src, titleText, href, contentText)
{
	var args = arguments.length;
	if (args == 0) return false;
	if (args < 2) var title = "";
	if (args < 3) var href = "";
	if (args < 4) var content = "";
	with(this)
	{
		image[image.length] = src;
		title[title.length] = titleText;
		link[link.length] = href;
		content[content.length] = contentText;
	}
}
/*
 随机取出 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)
	{
		if (imageId == null)
		{
			alert("没有指定 LoopImage 对象中图片元素的 id");
			return;
		}

		var eImage = document.getElementById(imageId);
		var eTitle = document.getElementById(titleId);
		var eContent = document.getElementById(contentId);
		var eLink = document.getElementsByName(linkId);
		if (!eLink) eLink[0] = document.getElementById(linkId);
		if (eImage)
		{
			if (imageWidth != null) eImage.style.width = imageWidth;
			if (imageHeight != null) eImage.style.height = imageHeight;
			if (document.all)
			{
				if (index != undefined && image.length <= 1) return;
				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();
			}
			/*
			if (imageWidth != null && imageHeight != null)
			{
				eImage.onload = function() {
					this.removeAttribute("height"); this.removeAttribute("width");
					if (this.height / this.width > imageHeight / imageWidth)
						this.height = Math.min(imageHeight, this.height);
					else
						this.width = Math.min(imageWidth, this.width);
				}
			};
			*/
			eImage.src = image[index];

			// 改变标题
			if (eTitle)
				eTitle.innerHTML = title[index];

			// 改变连接地址
			if (eLink)
			{
				for (var i = 0; i < eLink.length; i++)
				{
					if (link[index])
					{
						eLink[i].onclick = function(){};
						eLink[i].href = link[index];
					}
					else
					{
						eLink[i].onclick = function(){return false;};
						eLink[i].href = "#";
					}
				}
			}
			// 改变内容
			if (eContent)
				eContent.innerHTML = content[index];
			// 开始转换图片
			if (document.all) eImage.filters[typeName].play();
		}
		var temp = this;
		loopImageTimer = setTimeout(function(){temp.NextImage(index); temp = null;}, interval);
	}	// with(this)
}