
/*
	[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 DuceFlash(){
	this.imgtotals = 0;
	this.imgs = new Array();
	this.imageindex = -1;
	this.auto = true;
	this.delta = 0.15;
	this.movement = true;
	this.imgzoom = 2;
	this.direct = false;
	this.showImageTimer = null;
	this.imgScrollTimer = null;
	this.target = '_blank';
	this.done = 6000;
}

var DuceObF = new DuceFlash();

DuceFlash.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){}
}

DuceFlash.prototype.Init = function(imgs, btn, movement) {
	if(this.showImageTimer || imgs.length<1) {
		if(imgs.length<1) $('flashtrans').style.display = 'none';
		return;
	}
	this.movement = movement || !BROWSER['ie'] ? true : false;
	this.direct = (movement && movement < 2) || !BROWSER['ie'] ? true : false;
	this.imgtotals = imgs.length;
	this.imgs = imgs;
	this.btn = btn;
	this.title = isUndefined(imgs[0].title);
	//判断是否小图模式 设置最大值 根据大图高度设置
	this.imgtotals = this.btn == 'thumb' && this.imgtotals>5 ? 5 : this.imgtotals;
	if(this.imageindex>=this.imgtotals-1) {
		this.imageindex = -1;
	}

	if(this.title){
		$('flashtrans').style.display = 'none';
	}

	if(!trim($("flash_btn").innerHTML)){
		var btnstr = '';
		var maxshows = this.btn=='thumb' ? 5 : this.imgtotals;
		for (var i=0;i<maxshows;i++){
			if(i<this.imgtotals){
				btnstr += '<a id="aimg'+i+'" onclick="DuceObF.showImage('+i+');" onMouseover="if(DuceObF.auto){DuceObF.Stop()}" onMouseout="if(DuceObF.auto){DuceObF.Start()}"';
				if(this.btn=='thumb'){
					btnstr += '><img src="' + imgs[i].img + '"></a>';
				}else if(this.btn=='num'){
					btnstr += ' style="background-position:center -'+(i*15)+'px;"></a>';
				}else {
					btnstr += '></a>';
				}
			}else{
				btnstr += '<span></span>';
			}
		}
		$("flash_btn").innerHTML = btnstr;
	}
	//是否滚动模式 否则IE中以淡出淡入模式
	if(this.movement){
		var mics = '';
		var sidewin = this.target == '_blank' ? ' target="_blank"' : '';
		for (i=0;i<this.imgtotals;i++){
			mics += this.direct ? '<td>' : '';
			mics += '<a href="'+imgs[i].href+'" class="debg"' + sidewin + ' id="flagImg' + i + '"><img id="duceimg_' + i + '" src="'+imgs[i].img+'"' + (this.imgzoom == 2 ? ' class="imgzoom"' : '') + ' onerror="try{DuceObF.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.imgtotals;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(){DuceObF.imgs[this.id.substr(8)].resized = true;}
				var a = document.createElement("a");
				a.href = this.imgs[i].href;
				if(this.target == '_blank') a.target = "_blank";
				a.className = "debg";
				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();
}

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

DuceFlash.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);
}

DuceFlash.prototype.resizeImage = function(targetIndex){
	if(targetIndex>this.imgtotals-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("DuceObF.resizeImage(" + targetIndex + ")", 10);
		return false;
	}
}

DuceFlash.prototype.showImage = function(targetIndex) {
	for(var i=0;i<this.imgtotals;i++){	
		if($("aimg"+i)){
			if(i==targetIndex||(targetIndex==this.imgtotals && i==0)){
				$("aimg"+i).className = "aimg";
			}
			else $("aimg"+i).className = "";
		}
	}		
	if(targetIndex>this.imgtotals-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;
}

DuceFlash.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);
}

DuceFlash.prototype.scrollImage = function(targetIndex) {
	this.transindex = this.imageindex;
	this.Stop('imgScrollTimer');
	var _this = this;
	this.imgScrollTimer = setInterval(function(){
		if(_this.transindex > targetIndex){
			if(_this.direct){
				var w = $("flash_img").offsetWidth;
				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{
				var h = $("flash_img").offsetHeight;
				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');
				}
			}
		}
		if(_this.transindex < targetIndex){
			if(_this.direct){
				var w = $("flash_img").offsetWidth;
				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(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);
}
