
/*
	[DUCEDIS] (C)2007-2008 Ducecms Inc.
    This is a freeware, use is subject to license terms
    Script filename：flash.js

    Support: www.ducecms.com

*/

function DuceFlashBox(){
	this.totals = 0;
	this.imgs = new Array();
	this.imageindex = -1;
	this.auto = true; //true:鼠标离开索引图标后自动切换 false:不受鼠标控制
	this.click = true; //true:点击切换, false:悬停切换
	this.delta = 0.15; //偏移阀值 大于 0 且 小于 1 取值
	this.movement = true; //true:偏移滚动模式 false:MSIE滤镜效果 *非IE浏览器 值自动设为true
	this.imgzoom = 2; //图片显示模式，0:不缩放 1:自动正比缩放 2:强制缩放
	this.direct = false;
	this.showImageTimer = null;
	this.imgScrollTimer = null;
	this.btn_bgh = 15; //用于序数背景图上下偏移量
	this.btn_bgw = 0; //用于序数背景图左右偏移量
	this.target = '_blank'; //打开链接的目标窗口
	this.done = 5000; //图片切换间隔时间(毫秒)
}

var DuceFlash = new DuceFlashBox();

DuceFlashBox.prototype.setProgid = function() {
	if(!this.filters){
		this.filters = [
			//'progid:DXImageTransform.Microsoft.RandomBars( duration=1,orientation=vertical )',//细条分割 
			//'progid:DXImageTransform.Microsoft.RevealTrans(duration=0.5,transition=23 )',//随机 
			'progid:DXImageTransform.Microsoft.Spiral(duration=0.5,gridSizeX=5,gridSizeY=10)',//擦拭 
			//'progid:DXImageTransform.Microsoft.Zigzag(duration=0.25,gridSizeX=10,gridSizeY=1 )',//擦去 
			'progid:DXImageTransform.Microsoft.Fade(Overlap=1.00)',//幻影渐变
			'progid:DXImageTransform.Microsoft.Pixelate(enabled=false,duration=0.5,maxSquare=15)',//马赛克 
			'progid:DXImageTransform.Microsoft.Iris(irisStyle=cross,motion=out)',//分X展开 square
			'progid:DXImageTransform.Microsoft.Iris(irisStyle=diamond,motion=out)',//展开 
			'progid:DXImageTransform.Microsoft.GradientWipe(duration=1,gradientSize=0.5,motion=forward)',//拖尾渐变 
			'progid:DXImageTransform.Microsoft.Stretch(duration=0.5,stretchStyle=push)', //面积伸缩 
			'progid:DXImageTransform.Microsoft.Blinds(duration=0.5,bands=8,Direction=left)', //条形百页 
			'progid:DXImageTransform.Microsoft.Slide(duration=0.5,bands=1,slideStyle=swap)', //上下抽动 
			'progid:DXImageTransform.Microsoft.Slide(duration=0.5,bands=10,slideStyle=hide)', //抽动 
			'progid:DXImageTransform.Microsoft.Inset(duration=1)',//斜角轮换 
			'progid:DXImageTransform.Microsoft.RandomDissolve(duration=1)',//雪花点 
			'progid:DXImageTransform.Microsoft.Barn(duration=0.5,motion=out,orientation=vertical)',//左右分开 
			'progid:DXImageTransform.Microsoft.CheckerBoard(duration=0.5,squaresX=1,squaresY=10,Direction=right)', //区域百叶 
			'progid:DXImageTransform.Microsoft.RadialWipe(duration=1,wipeStyle=wedge)',//扇形 
			'progid:DXImageTransform.Microsoft.Wheel(duration=0.5,spokes=16)',//扇形百叶 
			'progid:DXImageTransform.Microsoft.Strips(duration=0.5,motion=rightup )',//斜角锯齿 
			'BlendTrans( duration=1 )'//幻影渐变
		];
		this.rfnum = this.filters.length - 1;
	}
	try{
		$("flash_img").style.filter = this.filters[Math.round(Math.random()*this.rfnum)];
		//$("flash_img").style.filter = this.filters[5];
	}catch(e){}
}

/*
movement 值 0: 渐变 1: 左右滚动 2: 上下滚动
btn 值 'num': 背景序数图标 'thumb': 缩略图
*/
DuceFlashBox.prototype.Init = function(btn, movement) {
	this.totals = this.imgs.length;
	if(this.showImageTimer || this.totals<1) {
		return;
	}
	this.movement = movement || !BROWSER['ie'] ? true : false;
	this.direct = (movement && movement < 2) || !BROWSER['ie'] ? true : false;
	this.btn = btn;
	this.title = isUndefined(this.imgs[0].title);
	//判断是否小图模式 设置最大值 根据大图高度设置
	this.totals = this.btn == 'thumb' && this.totals > 5 ? 5 : this.totals;
	if(this.imageindex >= this.totals-1) {
		this.imageindex = -1;
	}

	if(!trim($("flash_btn").innerHTML)){
		var btnstr = '';
		var maxshows = this.btn=='thumb' ? 5 : this.totals;
		for (var i=0;i<maxshows;i++){
			if(i<this.totals){
				btnstr += '<a id="aimg'+i+'" ';
				btnstr += this.click ? 'onclick="DuceFlash.showImage('+i+');" ' : '';
				btnstr += 'onMouseover="if(DuceFlash.auto){DuceFlash.Stop()}';
				btnstr += this.click ? '' : ';DuceFlash.showImage('+i+');';
				btnstr += '" onMouseout="if(DuceFlash.auto){DuceFlash.Start()}"';
				if(this.btn=='thumb'){
					btnstr += '><img src="' + imgs[i].img + '"></a>';
				}else if(this.btn=='num'){
					btnstr += ' style="background-position:-'+(i*this.btn_bgw)+'px -'+(i*this.btn_bgh)+'px;"></a>';
				}else {
					btnstr += '></a>';
				}
			}else{
				btnstr += '<span></span>';
			}
		}
		$("flash_btn").innerHTML = btnstr;
	}
	
	if(this.movement){
		var mics = '';
		var sidewin = this.target == '_blank' ? ' target="_blank"' : '';
		for (i=0;i<this.totals;i++){
			mics += this.direct ? '<td>' : '';
			mics += '<a ';
			mics += trim(this.imgs[i].href) ? 'href="'+this.imgs[i].href+'"'+sidewin : '';
			mics += ' class="aimgbox"' + ' id="flagImg'+i+ '"><img id="duceimg_'+i+'" src="'+this.imgs[i].img+'"' + (this.imgzoom == 2 ? ' class="imgzoom"' : '') + ' onerror="try{DuceFlash.imgs[' + i + '].resized=true;}catch(e){}"></a>';
			mics += this.direct ? '</td>' : '';
		}
		if(this.direct) {
			mics = '<table><tr>' + mics + '</tr></table>';
		}
		$("flash_img").innerHTML = mics;
	}else{
		for (i=0;i<this.totals;i++){
			var imgId = "flagImg"+i;
			var pos = this.imgs[i].img.lastIndexOf(".");
			if(this.imgs[i].img.substr(pos+1).substr(0,3).toLowerCase()=="swf"){
				$("flash_img").innerHTML += '\
					<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" id="'+imgId+'">\
					<param name="movie" value="' + this.imgs[i].img + '" />\
					<param name="quality" value="high" />\
					<embed src="' + this.imgs[i].img + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>\
					</object>';
			}else{
				var img = new Image();
				img.src = this.imgs[i].img;
				img.id = 'duceimg_' + i;
				if(this.imgzoom == 2) img.className = "imgzoom";
				img.onerror = function(){DuceFlash.imgs[this.id.substr(8)].resized = true;}
				var a = document.createElement("a");
				if(trim(this.imgs[i].href)){
					a.href = this.imgs[i].href;
					if(this.target == '_blank') a.target = "_blank";
				}
				a.className = "aimgbox";
				a.id = imgId;
				a.appendChild(img);
				$("flash_img").appendChild(a);
			}
		}
	}
	this.done = this.done>=3000 ? this.done + 1000 : 6000;

	this.showImage(this.imageindex+1);
	this.Start();
}

DuceFlashBox.prototype.Start = function (){
	var _this = this;
	this.showImageTimer = setInterval(function(){
		var stat = _this.showImage(_this.imageindex + 1);
		if(!stat){_this.Stop();}	
	}, _this.done);
}

DuceFlashBox.prototype.Stop = function (timer){
	if(isUndefined(timer)) timer = 'showImageTimer';
	clearInterval(this[timer]);
	this[timer] = null;
	if(this.imgzoom == 1 && this.movement) {
		this.resizeImage(this.imageindex+1);
	}
}

DuceFlashBox.prototype.resizeImage = function(targetIndex){
	if(targetIndex>this.totals-1) targetIndex = 0;
	if(this.imgs[targetIndex].resized || !$('duceimg_' + targetIndex)) {
		$('flashimg_loading').style.display = "none";
		return;
	}

	var obj = $('duceimg_' + targetIndex);
	var iw = obj.offsetWidth;
	var ih = obj.offsetHeight;

	if(iw && ih){
		this.imgs[targetIndex].resized = true;
		var aw = this.imagebox['w'] ? this.imagebox['w'] : obj.parentNode.offsetWidth;
		var ah = this.imagebox['h'] ? this.imagebox['h'] : obj.parentNode.offsetHeight;
		if(iw/ih > aw/ah){
			obj.style.height = ah + 'px';
			obj.style.marginLeft = Math.floor(-(obj.offsetWidth-aw)/2) + 'px';//左右居中
		}else{
			obj.style.width = aw + 'px';
			if(targetIndex) obj.style.marginTop = Math.floor(-(obj.offsetHeight-ah)/2) + 'px';//上下居中
		}
		$('flashimg_loading').style.display = "none";
		return true;
	}else{
		$('flashimg_loading').style.display = this.imageindex == targetIndex ? "block" : "none";
		setTimeout("DuceFlash.resizeImage(" + targetIndex + ")", 10);
		return false;
	}
}

DuceFlashBox.prototype.showImage = function(targetIndex) {
	for(var i=0;i<this.totals;i++){	
		if($("aimg"+i)){
			if(i==targetIndex||(targetIndex==this.totals && i==0)){
				$("aimg"+i).className = "aimg";
			}
			else $("aimg"+i).className = "";
		}
	}		
	if(targetIndex>this.totals-1) targetIndex = 0;
	if(!this.imgs[targetIndex]) return false;	
	this.filterPlay = targetIndex == this.imageindex || this.imageindex<0 ? false : true;

	if(this.imgzoom == 1) this.resizeImage(targetIndex);

	if(this.movement){
		this.scrollImage(targetIndex);
	}else{
		this.transImage(targetIndex);
	}

	if($("flash_title") && this.imgs[targetIndex].title){
		this.filterPlay && $("flash_show_ctl_msg").filters && $("flash_show_ctl_msg").filters[0].Apply();
		$("flash_title").href = this.imgs[targetIndex].href;
		$("flash_title").innerHTML = this.imgs[targetIndex].title;
		$("flash_title").title = this.imgs[targetIndex].title;
		if(this.target == '_blank') $("flash_title").target = "_blank";
		this.filterPlay && $("flash_show_ctl_msg").filters && $("flash_show_ctl_msg").filters[0].Play();
	}
	
	this.imageindex = targetIndex;
	return true;
}

/*MSIE滤镜效果函数*/
DuceFlashBox.prototype.transImage = function(targetIndex) {
	BROWSER['ie'] && this.filterPlay && this.setProgid();
	this.filterPlay && $("flash_img").filters && $("flash_img").filters[0].Apply();
	for(i=0; i<$("flash_img").childNodes.length; i++){
		$("flash_img").childNodes[i].style.display = "none";
	}
	var imgId = "flagImg"+targetIndex;
	if($(imgId)){
		$(imgId).style.display = "block";
		if($(imgId).tagName.toLowerCase() == "object"){
			$(imgId).rewind();
			$(imgId).Play();
		}
	}
	this.filterPlay && $("flash_img").filters && $("flash_img").filters[0].Play(duration=1);
}

/*偏移滚动函数*/
DuceFlashBox.prototype.scrollImage = function(targetIndex) {
	this.Stop('imgScrollTimer');
	var _this = this;
	this.imgScrollTimer = setInterval(function(){
		if(_this.direct){
			var w = $("flash_img").offsetWidth;
			if($("flash_img").scrollLeft > targetIndex * w){
				if($("flash_img").scrollLeft - targetIndex * w > 1){
					$("flash_img").scrollLeft -= ($("flash_img").scrollLeft - targetIndex * w)* _this.delta;
				}else {
					$("flash_img").scrollLeft = targetIndex * w;
					_this.Stop('imgScrollTimer');
				}
			}else{
				if(targetIndex * w - $("flash_img").scrollLeft > 0){
					$("flash_img").scrollLeft += (targetIndex * w - $("flash_img").scrollLeft)* _this.delta+1;
				}else {
					$("flash_img").scrollLeft = targetIndex * w;
					_this.Stop('imgScrollTimer');
				}
			}
		}else{
			var h = $("flash_img").offsetHeight;
			if($("flash_img").scrollTop > targetIndex * h){
				if($("flash_img").scrollTop - targetIndex * h > 1){
					$("flash_img").scrollTop -= ($("flash_img").scrollTop - targetIndex * h)* _this.delta;
				}else {
					$("flash_img").scrollTop = targetIndex * h;
					_this.Stop('imgScrollTimer');
				}
			}else{
				if(targetIndex * h - $("flash_img").scrollTop > 0){
					$("flash_img").scrollTop += (targetIndex * h - $("flash_img").scrollTop)* _this.delta+1;
				}else {
					$("flash_img").scrollTop = targetIndex * h;
					_this.Stop('imgScrollTimer');
				}
			}
		}
	}, 10);
}
