﻿function magicImageObject(p, u, t) {
	var me = this;

	this.image = new Image();
	me.image.src = p;
	if(typeof(u)!='string') {
		this.Href = null;
	} else if(u.length==0) {
		this.Href = null;
	} else {
		this.Href = u;
	}
	if(typeof(t)!='string') {
		this.target = '_self';
	} else {
		t = t.replace(/(^\s*)|(\s*$)/g, '');
		if(t.length==0) {
			this.target = '_self';
		} else {
			this.target = t.toLowerCase();
		}
	}
}

/*
oID网页中放置幻灯容器的ID
magicArray幻灯的对象数组
w最大宽度	不是数字时为不限
h最大高度	不是数字时为不限	宽、高必须设置一个，否则没有切换效果
t切换间隔时间 单位：毫秒  小于1000毫秒将不自动切换图片
sp按钮对象定位	不是数字时为不浮动按钮对象
spck按钮切换图片方式	true为鼠标指上换图		false为点击换图
sbt是否显示按钮对象
ish按钮提示内容		可为任何字符串
isp提示内容插入位置		0为前 1为后
*/
function magicImage(oID, magicArray, w, h, t, sp, spck, sbt, ish, isp) {
	var Width, Height, TimeOut, Spear, bOverButtonChange, showButton, insertString, insertPosition;
	if(typeof(w)!='number') {
		Width = null;
	} else if(w<50) {
		Width = 300;
	} else {
		Width = parseInt(w);
	}
	if(typeof(h)!='number') {
		Height = null;
	} else if(h<50) {
		Height = 300;
	} else {
		Height = parseInt(h);
	}
	if(typeof(t)!='number') {
		TimeOut = 6000;
	} else if(t<1000) {
		TimeOut = null;
	} else {
		TimeOut = parseInt(t);
	}
	if(typeof(sp)!='number') {
		Spear = null;
	} else if(sp<0||sp>20) {
		Spear = 10;
	} else {
		Spear = parseInt(sp);
	}
	if(typeof(spck)!='boolean') {
		bOverButtonChange = true;
	} else {
		bOverButtonChange = spck;
	}
	if(typeof(sbt)!='boolean') {
		showButton = true;
	} else {
		showButton = sbt;
	}
	if(ish==null) {
		insertString = '';
	} else {
		insertString = ish;
	}
	if(isp==0 || isp==1) {
		insertPosition = isp;
	} else {
		insertPosition = 0;
	}
	if(magicArray.length==1) TimeOut = null;
	var imgIndex, filterIndex, timeSpan;
	var imgCtrl, imgUrl, imgBox, btBox;
	var me = this;

	this.SetObject = function() {
		if(imgIndex>magicArray.length - 1) imgIndex = 0;
		
		imgCtrl.src = magicArray[imgIndex].image.src;
		if(magicArray[imgIndex].Href!=null) imgUrl.href = magicArray[imgIndex].Href;
		if(magicArray[imgIndex].target!='_self') imgUrl.target = magicArray[imgIndex].target;
	}
	
	this.ChangeImage = function(tIndex) {
		if(imgIndex==tIndex) return;
		window.clearInterval(timeSpan);

		var thisBT
		if(showButton) thisBT = document.getElementById(oID + '_bt_' + imgIndex);
		try {
			imgBox.filters[0].Apply();
		} catch(e) {}
		
		imgIndex = tIndex;
		me.SetObject();
		
		try {
			imgBox.filters[0].play();
		} catch(e) {}
		
		if(filterIndex<23) {
			filterIndex += 1;
		} else {
			filterIndex = 0;
		}
		try {
			imgBox.filters[0].Transition = filterIndex;
		} catch(e) {}
		
		if(showButton) {
			thisBT.className = oID + '_bt';
			thisBT = document.getElementById(oID + '_bt_' + imgIndex);
			thisBT.className = oID + '_bt_over';
		}
		
		if(TimeOut!=null) timeSpan = window.setInterval(function(){me.ChangeImage(imgIndex + 1)}, TimeOut);
	}
	
	this.Init = function() {
		var bbox = document.getElementById(oID);
		if(bbox==null) return;
		if(magicArray.length==0) return;
				
		var s = '';
		s += '<div id="' + oID + '_imgBox" style="' + (Width!=null ? 'width:' + Width + 'px; ' : '') + (Height!=null ? 'height:' + Height + 'px; ' : '') + 'overflow:hidden; clear:both; filter:progid:DXImageTransform.Microsoft.RevealTrans()"><a id="' + oID + '_imgUrl"><img id="' + oID + '_imgCtrl" border="0" class="' + oID + '_Img" /></a></div>';
		if(showButton) {
			s += '<div id="' + oID + '_btBox"' + (Spear!=null ? ' style="position:absolute"' : '') + '>' + (insertPosition==0 ? insertString : '') + '<table cellspacing="2" border="0" cellpadding="0"><tr>';
			for (var i=0; i<magicArray.length; i++) s += '<td id="' + oID + '_bt_' + i + '" class="' + oID + '_bt" imgID="' + i + '">' + (i + 1).toString() + '</td>';
			s += '</tr></table>' + (insertPosition==1 ? insertString : '') + '</div>';
		}
		bbox.innerHTML = s;
		
		imgCtrl = document.getElementById(oID + '_imgCtrl');
		if(TimeOut!=null) {
			imgCtrl.onmouseover = function() {
				window.clearInterval(timeSpan);
			}
			imgCtrl.onmouseout = function() {
				timeSpan = window.setInterval(function(){me.ChangeImage(imgIndex + 1)}, TimeOut);
			}
		}
		imgUrl = document.getElementById(oID + '_imgUrl');
		imgBox = document.getElementById(oID + '_imgBox');
		if(showButton) btBox = document.getElementById(oID + '_btBox');
		
		if(showButton && magicArray.length>1) {
			for (var i=0; i<magicArray.length; i++) {
				if(bOverButtonChange) {
					document.getElementById(oID + '_bt_' + i).onmouseover = function() {
						me.ChangeImage(parseInt(this.innerHTML) - 1);
					}
				} else {
					document.getElementById(oID + '_bt_' + i).onclick = function() {
						me.ChangeImage(parseInt(this.innerHTML) - 1);
					}
				}
			}
		} else {
			try {
				btBox.style.display = 'none';
			} catch(ex) {}
		}
		
		imgIndex = 0;
		filterIndex = 0;
		me.SetObject();
		if(showButton) {
			document.getElementById(oID + '_bt_' + imgIndex).className = oID + '_bt_over';
			if(Spear!=null) {
				btBox.style.top = imgBox.offsetTop + imgBox.offsetHeight - btBox.offsetHeight - Spear;
				btBox.style.left = imgBox.offsetLeft + imgBox.offsetWidth - btBox.offsetWidth - Spear;
			}
		}
		
		if(TimeOut!=null) {
			window.clearInterval(timeSpan);
			timeSpan = window.setInterval(function(){me.ChangeImage(imgIndex + 1)}, TimeOut);
		}
	}
	
	me.Init();
}

