﻿// Constant parament
var csDelay=5000;
var csSlstep=50;
var csTimestep=50;
// JScript File
var ie4=(document.all)?true:false;
var dom=(document.getElementById&&navigator.userAgent.indexOf("Opera")==-1);
var DLA=document.layers;
//Public Things
var SLAR=new Array()
function getIt(el){
    if (typeof(el)=='object') return el;
    if (typeof(el)=='string'){
		if (dom) return document.getElementById(el);
		if (ie4) return eval('document.all.'+el);
	}
    return null;
}
function Opt(a,b){
	return (typeof a=='undefined')?b:a;
}
function Opts(o,i,b){
	if (typeof o =='undefined'){
		return b;
	}else if (typeof o[i] =='undefined'){
		return b;
	}else{
		return o[i];
	}
}
var $extend = Object.extend = function(){
	var args = arguments;
	if (!args[1]) args = [this, args[0]];
	for (var property in args[1]) args[0][property] = args[1][property];
	return args[0];
};

var JClass = function(properties){
	var klass = function(){
		return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;
	};
	$extend(klass, this);
	klass.prototype = properties;
	klass.constructor = JClass;
	return klass;
};


//Class
var JSSlideshow = new JClass({
    initialize: function(opts) {
		//var opts = theoptions;
		if (opts.images.length < 1) {
			return; 
		}
		SLAR[SLAR.length]=this
		this.slideid=SLAR.length-1
		//this.slideid = 'SL' + parseInt(Math.random() * 10000) + 'SL';
		this.imgnum = opts.images.length;	
		//if (typeof opts.israndom!="undefined") opts.images.sort(function() {return 0.5 - Math.random();})
		this.images = new Array();
		for (i=0;i<this.imgnum;i++){
			this.images[i]= new Array();
			this.images[i].src   = opts.images[i];
			this.images[i].alt   =  Opts(opts.alts,i,'');
			this.images[i].ilink =  Opts(opts.ilinks,i,'') 
			this.images[i].target=  Opts(opts.targets,i,'_self')
			this.images[i].title =  Opts(opts.titles,i,'');
			this.images[i].desc  =  Opts(opts.descs,i,'');
			this.images[i].thumb =  Opts(opts.thumbs,i,i+1);
		}
									   
		this.isnostart= Opt(opts.noauto,null);
		this.oslide   = getIt(opts.slidehover);
		
		this.width= parseInt(Opt(opts.size[0],this.oslide.style.width));
		this.height= parseInt(Opt(opts.size[1],this.oslide.style.height));	
		this.isthumbtext=Opt(opts.isthumbtext,false);
		this.oslidebar   = getIt(opts.slidebar);	
		this.progress   = getIt(opts.progress);
		this.otitle =getIt(opts.objtitle);
		this.odesc  =getIt(opts.objdesc);
		this.delay  =parseInt(Opt(opts.delay,csDelay));
		if ( this.delay<1000 ) this.delay=1000;
		this.step  =parseInt(Opt(opts.step,csSlstep));
		this.timestep  =parseInt(Opt(opts.timestep,csTimestep));
		if (this.timestep * this.step>=this.delay) {
			//this.timestep= this.step= 50;
		}
		//effecter: [fading]/ [moving, direction]/ [rotation, direction]
		this.trans  =Opt(opts.effect,'fading');
		this.direction  =Opt(opts.moving_direction,'right');
		//effect for title and description
		this.other_trans= Opt(opts.desc_effect,'fading');
		this.pause  = parseInt(Opt(opts.pause,0));
		this.pausemouse=0;
		
		if (this.pause==1){
			var obj=this;
			this.oslide.onmouseover=function(){obj.pausemouse=1; }
			this.oslide.onmouseout=function(){obj.pausemouse=0; }
		}
		this.clickpause  = parseInt(Opt(opts.clink_pause,0));
		this.clickstop=Opt(opts.noauto,0);//0;
		this.cur_index =0;
		//this.pre_index =null;
		this.cur_sdiv='img_' + this.slideid + '_0';
		//Show bar link
		//alert(this.oslidebar)
		if (this.oslidebar){
			if (this.imgnum<=1) {
				this.oslidebar.style.display="none";
			} else{
			 	thisul = document.createElement('ul');
				thisul.id='ulbar_' + this.slideid;
			 	for (i=0 ; i<this.imgnum; i++){
					li = document.createElement('li');
					li.id='thb_' + i ;
					a = document.createElement('a');
					a.id='thba_' + i ;
					a.name='thban_' + i;
					if (isNaN(parseInt(this.images[i].thumb)) && !this.isthumbtext){
						a.innerHTML ='<img src="' + this.images[i].thumb + '"  />';
						//alert(1);
					}else{
						a.innerHTML = this.images[i].thumb;
						//alert(2);
					}
					if (i==0) a.className="active";
					var obj=this;
					a.onclick = function() {
						if (obj.isdoing) { /*alert('please wait...'); */return;}
						clearTimeout(obj.timeout);
						obj.cur_index= this.id.replace('thba_','');
						obj.clickstop=obj.clickpause;
						//alert(this.id.replace('thba_','') + " --" + obj.cur_index + ' -- '  + this.cur_index);
						obj.preshow();
					};
					li.appendChild(a); //a.injectInside(li);
					thisul.appendChild(li) //.injectInside(thisul);
				}
				this.oslidebar.appendChild(thisul) //thisul.injectInside(this.oslidebar);
			}
		}
		this.start();
	},
	
	start:function(){
		var slideinner=''
		var margin='';
		if (this.trans.indexOf('fading')!=-1){
			margin +=" filter:alpha(opacity=0);-moz-opacity:0.02; "
		} 
		if (this.trans.indexOf('moving')!=-1){
			
			if (this.direction=='right'){
				margin +='margin-left:-' + this.width + 'px';
			}else if (this.direction=='left') {
				margin +=' margin-left:' + this.width + 'px';
			}else if (this.direction=='up') {
				margin +=' margin-top:' + this.height + 'px';
			}else if (this.direction=='down') {
				margin +=' margin-top:-' + this.height + 'px';
			}
			
		}else{
		}
		
		
		slideinner='<div id="img_' + this.slideid + '_0" style="position:absolute; ' 
			+ ' width:' + this.width + 'px; height:' + this.height + 'px; ' + margin + '; "></div>' 
			+ '<div id="img_' + this.slideid + '_1"  style="position:absolute; width:' 
			+ this.width + 'px; height:' + this.height + 'px;' + margin + '; "></div>';			
		//alert(slideinner)
		this.oslide.innerHTML=slideinner;
		//For title and desc
		if (this.otitle){
			var titleinner='<div id="title_' + this.slideid + '_0" style="position:absolute;"></div>' 
			+ '<div id="title_' + this.slideid + '_1"  style="position:absolute; "></div>';	
			this.otitle.innerHTML=titleinner;
		}
		if (this.odesc){
			var decsinner='<div id="desc_' + this.slideid + '_0" style="position:absolute; width:100%;left:0;"></div>' 
			+ '<div id="desc_' + this.slideid + '_1"  style="position:absolute;width:100%;left:0;"></div>';	
			this.odesc.innerHTML=decsinner;
		}
		
		this.preshow();
		if (this.pause==9)this.pausemouse=1;
	},
	
	preshow:function(){
		//var obj=SLAR[this.slideid ];
		//alert(this.pausemouse + ":" + obj.pausemouse);
		if (this.pausemouse==1){			
			this.timeoutpause = setTimeout("SLAR[" + this.slideid + "].preshow();", 50);
			return;
		}
		
		var curdiv = getIt(this.cur_sdiv);
		if (this.trans.indexOf('moving')!=-1){
			if (this.direction.indexOf('right')!=-1) curdiv.style.marginLeft='-' + (this.width) + 'px';
			if (this.direction.indexOf('left')!=-1) curdiv.style.marginLeft= (this.width) + 'px';
			if (this.direction.indexOf('down')!=-1) curdiv.style.marginTop='-' + (this.height) + 'px';
			if (this.direction.indexOf('up')!=-1) curdiv.style.marginTop= (this.height) + 'px';
			curdiv.style.zIndex="2";
			var other= (this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';
			var objOut =getIt(other);
			objOut.style.marginLeft='0px';
			objOut.style.zIndex="1";
		}
		
		var curtitle = getIt(this.cur_sdiv.replace("img_","title_"))
		var curdesc = getIt(this.cur_sdiv.replace("img_","desc_"))
		//for link bar
		var cura = getIt('ulbar_' + this.slideid);		
		if (cura){
			var lisa=cura.getElementsByTagName("LI");
			for (i=0; i<lisa.length; i++) {
				if (lisa[i].id=='thb_' + this.cur_index){
					lisa[i].className='active';
				}else{
					lisa[i].className='';
				}
			}
		}		
		var slidehtml='';
		var i=Opt(this.cur_index,0);
		if (i>=this.imgnum) i=0;
		if (this.images[i].ilink!='')slidehtml  ='<a href="' + this.images[i].ilink + '" target="' + this.images[i].target + '">';
			slidehtml +='<img src="' + this.images[i].src + '" border="0" alt="' + this.images[i].alt 
			+ '" title="' + this.images[i].alt + '">';// width="' + this.width + '" height="' + this.height + '">';
		if (this.images[i].ilink!='')slidehtml += '</a>'
		curdiv.innerHTML=slidehtml;
		if (curtitle) curtitle.innerHTML= this.images[i].title;
		if (curdesc) curdesc.innerHTML= this.images[i].desc;
		this.isdoing=true;
		if (this.progress)	this.progress.style.display='';
		if (this.trans=='movingfading'){
			eval('this.fading(0)');
			eval('this.moving(0)');
			
		}else{
			eval('this.' + this.trans + '(0)');
		}

	},

	fading:function(opacity) {		
		//var obj=SLAR[this.slideid ];		
		var objIn = getIt(this.cur_sdiv);		
		var other= (this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';
		var objOut =getIt(other);
		//alert(objOut.id)
    	if (opacity <= 100) {
			this.setOpacity(objIn, opacity);
			this.setOpacity(objOut,100 - opacity);
			if (this.other_trans=='fading') this.fadingOther(opacity)
			opacity += parseInt(100/this.step);
			this.timeoutslide = setTimeout("SLAR[" + this.slideid + "].fading("+opacity+");", this.timestep);
		} else{
			this.isdoing=false;
			if (this.progress)	this.progress.style.display='none';
			clearTimeout(this.timeoutslide)
			clearTimeout(this.timeout)
			this.cur_sdiv=(this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';
			this.cur_index= (this.cur_index<this.imgnum-1)?this.cur_index+1:0;
			if (this.clickstop==0){
				this.timeout = setTimeout("SLAR[" + this.slideid + "].preshow()", this.delay);
			}
		}
	},
	moving:function() {		
			
		var objIn = getIt(this.cur_sdiv);		
		var other= (this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';
		var objOut =getIt(other);		
		var theMargin = parseInt(objIn.style.marginLeft)		
		if (this.direction.indexOf('up')!=-1 || this.direction.indexOf('down')!=-1) theMargin = parseInt(objIn.style.marginTop)		
		//alert(this.direction + ":" + theMargin);
		if (Math.abs(theMargin) > 0) {				
		  	var magin = parseInt(this.width /(this.step +1));	
			if (this.direction.indexOf('up')!=-1 || this.direction.indexOf('down')!=-1) magin = parseInt(this.height/this.step);	
			if (this.direction.indexOf('up')!=-1 || this.direction.indexOf('left')!=-1) magin=-magin;
			//if (this.direction.indexOf('up')!=-1 || this.direction.indexOf('down')!=-1){
			if (this.direction.indexOf('up')!=-1){
				objIn.style.marginTop =(parseInt(objIn.style.marginTop)+magin)<0?0:(parseInt(objIn.style.marginTop)+magin) + 'px';
				if (this.direction.indexOf('both')!=-1){
					objOut.style.marginTop =(parseInt(objIn.style.marginTop)-this.height  ) + 'px';
				}
			}else if (this.direction.indexOf('down')!=-1){
				objIn.style.marginTop =(parseInt(objIn.style.marginTop)+magin)>0?0:(parseInt(objIn.style.marginTop)+magin) + 'px';
				if (this.direction.indexOf('both')!=-1){
					objOut.style.marginTop =(this.height  + (parseInt(objIn.style.marginTop))) + 'px';
				}
			} else if (this.direction.indexOf('right')!=-1){				
				objIn.style.marginLeft =(parseInt(objIn.style.marginLeft)+magin)>0?0:(parseInt(objIn.style.marginLeft)+magin) + 'px';
				if (this.direction.indexOf('both')!=-1){
					objOut.style.marginLeft =(this.width  + (parseInt(objIn.style.marginLeft))) + 'px';
				}
				
			}else if (this.direction.indexOf('left')!=-1){
 				objIn.style.marginLeft =((parseInt(objIn.style.marginLeft)+magin)<0?0:(parseInt(objIn.style.marginLeft)+magin)) + 'px';
				if (parseInt(objIn.style.marginLeft)<Math.abs(magin) && parseInt(objIn.style.marginLeft)>0){objIn.style.marginLeft='0px';}
				if (this.direction.indexOf('both')!=-1){
					objOut.style.marginLeft =(parseInt(objIn.style.marginLeft)-this.width) + 'px';
				}
				//alert('objIn.style.marginLeft' + objIn.style.marginLeft + ' objOut.style.marginLeft' + objOut.style.marginLeft)
			}
					
			
			//var opacity = parseInt((Math.abs(parseInt(objIn.style.marginLeft) *100))/(this.width));	
			//if (this.direction.indexOf('up')!=-1 || this.direction.indexOf('down')!=-1)
			//opacity = parseInt((Math.abs(parseInt(objIn.style.marginTop) *100))/(this.height));
				//alert(opacity);
			//if (this.other_trans=='fading') this.fadingOther(100- opacity)				
			this.timeoutslide = setTimeout("SLAR[" + this.slideid + "].moving();", this.timestep);
		} else{
				this.isdoing=false;
				if (this.direction.indexOf('right')!=-1 ){
					objIn.style.marginLeft= '0px';			
					objOut.style.marginLeft= '-' + this.width + 'px';
				}
				if (this.direction.indexOf('left')!=-1 ){
					objIn.style.marginLeft= '0px';			
					//objOut.style.marginLeft= this.width + 'px';
					//alert(objIn.style.marginLeft);
				}
				if (this.direction.indexOf('down')!=-1 ){
					objIn.style.marginTop= '0px';
					objOut.style.marginTop= '-' + this.height + 'px';
				}
				if (this.direction.indexOf('up')!=-1 ){
					objIn.style.marginTop= '0px';
					objOut.style.marginTop=  this.height + 'px';
				}
				
				if (this.other_trans=='fading') this.fadingOther(100)	
				clearTimeout(this.timeoutslide)
				clearTimeout(this.timeout)
				this.cur_sdiv=(this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';
				this.cur_index= (this.cur_index<this.imgnum-1)?this.cur_index+1:0;
				if (this.clickstop==0){
					this.timeout = setTimeout("SLAR[" + this.slideid + "].preshow()", this.delay);
				}
			}
		
	},
	fadingOther:function(opacity){
		var titleIn = getIt(this.cur_sdiv.replace("img_","title_"))
		var descIn = getIt(this.cur_sdiv.replace("img_","desc_"))
		var other= (this.cur_sdiv=='img_' + this.slideid + '_0')?'img_' + this.slideid + '_1':'img_' + this.slideid + '_0';		
		var titleOut = getIt(other.replace("img_","title_"))
		var descOut = getIt(other.replace("img_","desc_"))
		//alert(objOut.id)
    	if (opacity <= 100) {			
			if (titleIn){
				this.setOpacity(titleIn, opacity);
				this.setOpacity(titleOut,100 - opacity);
			}
			if (descIn){
				this.setOpacity(descIn, opacity);
				this.setOpacity(descOut,100 - opacity);
			}			
		}
	},
	setOpacity:function(obj,opacity) {
		opacity = (opacity == 100)?99.999:opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")";   // IE/Win
		obj.style.KHTMLOpacity = opacity/100;   // Safari<1.2, Konqueror
		obj.style.MozOpacity = opacity/100;   // Older Mozilla and Firefox
		obj.style.opacity = opacity/100;  // Safari 1.2, newer Firefox and Mozilla, CSS3
	}
	

});


function setOpacity(objin, objout ,opacity) {
		other_opacity = 100- opacity;
		other_opacity = (other_opacity == 100)?99.999:other_opacity;
		opacity = (opacity == 100)?99.999:opacity;
		
		objout.style.filter = "alpha(opacity:"+opacity+")";   // IE/Win
		objout.style.KHTMLOpacity = opacity/100;   // Safari<1.2, Konqueror
		objout.style.MozOpacity = opacity/100;   // Older Mozilla and Firefox
		objout.style.opacity = opacity/100;  // Safari 1.2, newer Firefox and Mozilla, CSS3
		
		objout.style.filter = "alpha(opacity:"+opacity+")";   // IE/Win
		objout.style.KHTMLOpacity = opacity/100;   // Safari<1.2, Konqueror
		objout.style.MozOpacity = opacity/100;   // Older Mozilla and Firefox
		objout.style.opacity = opacity/100;  // Safari 1.2, newer Firefox and Mozilla, CSS3
		
}

