
/*
	[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.timer = new Array();
	this.direct = false; //默认值请勿改动
	this.imagebox = {'w':320, 'h':240}; //默认值请勿改动
	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.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.timer['showImage'] || this.totals<1) {
		if(this.totals<1 && $('flashtrans')){
			$('flashtrans').style.filter = 'Alpha(opacity=20)';
			$('flashtrans').style.opacity = '0.2';
		}
		return;
	}
	$('flash_img').style.backgroundImage = 'none';

	this.movement = movement || !BROWSER.ie ? true : false;
	this.direct = this.movement && movement != 2 ? 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.direct){
		var table = document.createElement('table');
		var row = table.insertRow(-1);
		var jsimgbox;
	}
	else{
		var jsimgbox = $("flash_img");
	}

	for (i=0;i<this.totals;i++){
		if(this.direct){
			jsimgbox = row.insertCell(-1);
		}
		var pos = this.imgs[i].img.lastIndexOf(".");
		if(this.imgs[i].img.substr(pos+1).substr(0,3).toLowerCase()=="swf"){
			jsimgbox.innerHTML += AC_FL_RunContent(
					'id', "flagImg"+i, 
					'src', this.imgs[i].img, 
					'width', this.imagebox['w'], 
					'height', this.imagebox['h'],
					'wmode', 'transparent'
			);
		}else{
			jsimgbox.appendChild(this.creatImageObj(i));
		}
	}

	if(this.direct)	$("flash_img").appendChild(table);

	this.imgObj = [];
	for (i=0;i<this.totals;i++){this.sImage(i);} // 获取图片的尺寸

	this.done = this.done>=3000 ? this.done + 1000 : 6000;
	this.Start();
}

DuceFlashBox.prototype.creatImageObj = function(i){
	var _this = this;
	var img = new Image();
	img.src = this.imgs[i].img;
	img.id = 'duceimg_' + i;
	if(this.imgzoom == 2){
		img.className = "imgzoom";
	}
	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 = "flagImg"+i;
	a.appendChild(img);

	return a;
}

DuceFlashBox.prototype.sImage = function(i){
	var img = new Image();
	this.imgObj[i] = img;
	var _this = this;
	if(BROWSER.ie){
		img.onreadystatechange = function () {
            if (this.readyState == "complete" || this.readyState=='loaded'){
				_this.imgs[i].w = this.width;
				_this.imgs[i].h = this.height;
				_this.resizeImage(i);
            }
        };
	}
	else{
        img.onload = function () {
            if (this.complete == true){
				_this.imgs[i].w = this.width;
				_this.imgs[i].h = this.height;
				_this.resizeImage(i);
			}
        }
	}
	img.onerror = function(){
		_this.imgs[i].resized = -1;
	}

	this.imgObj[i].src = this.imgs[i].img;
}

DuceFlashBox.prototype.Start = function(){
	var _this = this;
	if(this.imageindex == -1) {
		this.showImage(0);
	}
	this.timer['showImage'] = setInterval(function(){
		var stat = _this.showImage(_this.imageindex+1);
		if(!stat){_this.Stop();}	
	}, _this.done);
}

DuceFlashBox.prototype.Stop = function(timer){
	if(typeof timer == 'undefined'){
		timer = 'showImage';
	}
	clearInterval(this.timer[timer]);
	this.timer[timer] = null;
}

DuceFlashBox.prototype.resizeImage = function(i){
	if(i > this.totals - 1) i = 0;
	var imgId = 'duceimg_' + i;
	if(this.imgzoom != 1 || this.imgs[i].resized || !$(imgId)){
		if(this.imgs[i].resized == -1){
			$(imgId).style.marginLeft = '-1000px';
		}
		return;
	}

	var zw = this.imgs[i].w;
	var zh = this.imgs[i].h;
	if(zw>1 && zh>1){
		this.imgs[i].resized = true;
		var aw = this.imagebox['w'] ? this.imagebox['w'] : $(imgId).parentNode.offsetWidth;
		var ah = this.imagebox['h'] ? this.imagebox['h'] : $(imgId).parentNode.offsetHeight;
		if(zw/zh > aw/ah){
			$(imgId).style.height = ah + 'px';
			$(imgId).style.marginLeft = Math.floor(-(ah*zw/zh-aw)/2) + 'px';//左右居中
		}else{
			$(imgId).style.width = aw + 'px';
			$(imgId).style.marginTop = Math.floor(-(aw*zh/zw-ah)/2) + 'px';//上下居中
		}
	}else{
		setTimeout("DuceFlash.resizeImage(" + i + ")", 20);
	}
}

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.movement){
		this.scrollImage(targetIndex);
	}else{
		this.resizeImage(targetIndex);
		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('imgScroll');
	var _this = this;
	this.timer['imgScroll'] = 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('imgScroll');
				}
			}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('imgScroll');
				}
			}
		}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('imgScroll');
				}
			}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('imgScroll');
				}
			}
		}
	}, 10);
}

