
/*
	[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.direct = false;
	this.showImageTimer = null;
	this.imgScrollTimer = null;
	this.done = typeof imagechange_done == 'undefined' ? 6000 : imagechange_done;
}

var DuceObF = new DuceFlash();

DuceFlash.prototype.setProgid = function() {
	if(!this.filters){
		this.filters = [
			'progid:dximagetransform.microsoft.wipe(gradientsize=1.0,wipestyle=4, motion=forward)',
			'progid:DXImageTransform.Microsoft.Fade(Overlap=1.00)',
			'progid:DXImageTransform.Microsoft.Pixelate(maxsquare=8)'
		];
	}
	try{
		$("flash_img").style.filter = this.filters[Math.round(Math.random()*2)];
	}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 || !is_ie ? true : false;
	this.direct = (movement && movement < 2) || !is_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>9 ? 9 : 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 = '';
		for (i=0;i<this.imgtotals;i++){
			mics += '<a href="'+imgs[i].href+'" class="debg" target="_blank"><img src="'+imgs[i].img+'"></a>';	
		}
		if(this.direct) $("flash_img").style.whiteSpace = 'nowrap';
		$("flash_img").innerHTML = mics;
	}

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

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

DuceFlash.prototype.Stop = function (timer){
	if(isUndefined(timer)){
		timer = 'showImageTimer';
	}
	clearInterval(this[timer]);
	this[timer] = null;
}

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.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").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) {
	is_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=="OBJECT"){
			$(imgId).rewind();
			$(imgId).Play();
		}
	}else{
		var pos = this.imgs[targetIndex].img.lastIndexOf(".");
		if(this.imgs[targetIndex].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[targetIndex].img+'" />\
				<param name="quality" value="high" />\
				<embed src="'+this.imgs[targetIndex].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[targetIndex].img;
			var a = document.createElement("a");
			a.href = this.imgs[targetIndex].href;
			a.target = "_blank";
			a.className = "debg";
			a.id = imgId;
			a.appendChild(img);
			$("flash_img").appendChild(a);
		}
	}
	this.filterPlay && $("flash_img").filters && $("flash_img").filters[0].Play(duration=2);
}

DuceFlash.prototype.scrollImage = function(targetIndex) {
	_this = this;
	this.transindex = this.imageindex;
	this.Stop('imgScrollTimer');
	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);
}
