/*********************************************************************************
* Ultimate Fade-In Slideshow (v1.5): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
*
* 17/05/2006: Modified by Jens-Uwe Korff (juk), www.CreativeSpirits.de
* 05/01/2008: Updated for Monica, www.CreativeSpirits.info
* 14/09/2008: Modified to fade captions in sync with images.
* 15/09/2008: Updated to work with Opera 9.5, Google Chrome, Safari 3.1 PC
**********************************************************************************/

/* Customisation area */
//var fadebgcolor="#000000";
//var fadebg = "#adadad url(/css/images/tile.jpg)";
var fadebg = "transparent";
var stopSlideShow = false; // juk. Initially, don't stop slide show

function exitSlideShow(idLastCaptionShown) { // juk
// Called when user clicks on 'Stop slideshow' button.
	document.getElementById('caption_'+idLastCaptionShown).style.display = 'none';	// Hide caption of last image shown before user clicked on 'stop'
	document.getElementById('caption_'+(idLastCaptionShown-1)).style.display = 'none';	// Hide caption of second last image shown before user clicked on 'stop'
	document.getElementById('mainImage').style.display = 'inline';	// Show static image containing div
	document.getElementById('caption_'+imageToLoad).style.display = 'block';		// Show caption
	document.getElementById('caption_'+imageToLoad).style.opacity = 1;
	document.getElementById('caption_'+imageToLoad).style.KhtmlOpacity = 1;
	document.getElementById('caption_'+imageToLoad).style.filter = 'alpha(opacity=100)';
	document.getElementById('master0').style.display = 'none';		// Hide slide show containing div
	// Now clear all intervals set by routine 'rotateimage':
	for ( i=0; i < fadeclear.length; i++ ) {
		clearInterval(fadeclear[i]);								// Not clearing these might result in old intervals firing after 'stop' was pressed
	}
}

function prepareSlideShow() { // juk
	// Called when user clicks on 'Start slideshow' button.
	document.getElementById('mainImage').style.display = "none";	// Hide static image containing div
	document.getElementById('caption_'+imageToLoad).style.display = "none";		// Hide caption
	document.getElementById('master0').style.display = "inline"; 	// Show containing div
	document.getElementById('canvas0_0').style.zIndex = 0;			// Reset z-indices. Required for consecutive start/stops of slide show,
	document.getElementById('canvas0_1').style.zIndex = 0;			// otherwise the sequence gets mixed up because last stop wasn't in sync.
}

/*	Invoke as follows:

		<script type="text/javascript">// <![CDATA[
		//new fadeshow(IMAGES_ARRAY_NAME, slideshow_width, slideshow_height, borderwidth, delay, pause on mouseover (0=no, 1=yes), optionalRandomOrder)
		new fadeshow(fadeimages, 165, 200, 0, 2000, 1, "R")
		</script>
		
	For start/stop buttons or links:
	
		<a href="#" onclick='new fadeshow(fadeimages, 365, 400, 0, 2000, 1, "R");'>Start slideshow</a>
		<a href="#" onclick='stopSlideShow=true;'>Stop slideshow</a>

*/
 
////NO need to edit beyond here/////////////
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById); //modern dom browsers
var is_iexplorer = document.all;
var is_opera = window.opera;
if ( !is_opera && !is_iexplorer ) { // Opera and IE do not know 'navigator.vendor' and break. See http://www.quirksmode.org/js/detect.html
	var is_safari = (navigator.vendor.indexOf('Apple') > -1 );
	var is_chrome = (navigator.vendor.indexOf('Google') > -1 );
	var is_konqueror = (navigator.vendor.indexOf('KDE') > -1 );
}

var postcaptions=new Array() // juk

function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder, initRun){
	this.fadeheight = fadeheight; // added by juk, necessary for vertical image centering
	stopSlideShow = false; // juk
	fadearray= Array(); // juk, clear cache array to always start with index '0'. Very important, so we can reference divs (see routines above)
	fadeclear= Array(); // juk, clear cache array
	this.pausecheck=pause
	this.mouseovercheck=0
	this.delay=delay
	this.degree=10 //initial opacity degree (10%)
	this.curimageindex=0
	this.nextimageindex=1
	fadearray[fadearray.length]=this
	this.slideshowid=fadearray.length-1
	this.canvasbase="canvas"+this.slideshowid
	this.curcanvas=this.canvasbase+"_0"
//	if (typeof displayorder!="undefined")
	if (displayorder == "R") // juk
		theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
	this.theimages=theimages
	this.imageborder=parseInt(borderwidth+5)
	//preload images:
	this.postimages=new Array()
	for (p=0;p<theimages.length;p++){
		this.postimages[p]=new Image()
		this.postimages[p].src=theimages[p][0]
		postcaptions[p] = document.getElementById('caption_'+(p+1)) // juk
	}

	var fadewidth=fadewidth+this.imageborder*2
	var fadeheight=fadeheight+this.imageborder*2


	if (initRun) { // juk
		// Prepare code in page, but don't start slide show:
		if (is_iexplorer&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
//			document.write('<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:'+fadebgcolor+'"></div></div>')
			document.write('<div id="master'+this.slideshowid+'" style="position:absolute;top:0;left:0;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background:'+fadebg+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background:'+fadebg+'"></div></div>') // juk
		else
			document.write('<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>')
		document.getElementById('master0').style.display = 'none'; // juk. Don't display on init run
		return; // juk. Don't show slideshow on init run, leave start to user
	}

	if (is_iexplorer&&dom||dom) { //if IE5+ or modern browsers such as Firefox
		this.startit()
	} else {
		this.curimageindex++
		setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
	}
}

//function fadepic(obj){
function fadepic(obj, oCaption, oCaptionLast){
	if (stopSlideShow) { // juk
		// User clicked on 'Stop slideshow' => set timeout which prepares stop:
		setTimeout("exitSlideShow("+obj.nextimageindex+")", 10);
	}
	if (obj.degree<100){
		obj.degree+=10

		obj.tempobj.style.opacity = obj.degree/100;
		if ( is_konqueror ) obj.tempobj.style.KhtmlOpacity = obj.degree/100;
		if ( is_iexplorer ) obj.tempobj.style.filter = 'alpha(opacity=' + obj.degree + ')';
		
		oCaption.style.display="block";									// juk. Show next caption
		oCaption.style.opacity = obj.degree/100;
		if ( is_konqueror ) oCaption.style.KhtmlOpacity = obj.degree/100;
		if ( is_iexplorer ) oCaption.style.filter = 'alpha(opacity=' + obj.degree + ')';
		
		oCaptionLast.style.opacity = (100-obj.degree)/100;				// juk. Hide previous caption
		if ( is_konqueror ) oCaptionLast.style.KhtmlOpacity = obj.degree/100;
		if ( is_iexplorer ) oCaptionLast.style.filter = 'alpha(opacity=' +(100-obj.degree)+ ')';

	} else {
		clearInterval(fadeclear[obj.slideshowid])
		obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
		obj.tempobj=is_iexplorer? is_iexplorer[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
		obj.populateslide(obj.tempobj, obj.nextimageindex)
		obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
		setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
	}
}

fadeshow.prototype.populateslide=function(picobj, picindex){
	var imageHeight = this.postimages[picindex].height;	// juk. Get image height
	var canvasHeight = this.fadeheight;					// juk. Get canvas height
	var paddingTop = (canvasHeight - imageHeight) / 2;	// juk. Now calculate the padding needed to center image vertically
	var slideHTML=""
	if (this.theimages[picindex][1]!="") //if associated link exists for image
		slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
//	slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
	slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px" style="padding-top: '+paddingTop+'px">' //juk
	if (this.theimages[picindex][1]!="") //if associated link exists for image
		slideHTML+='</a>'
	picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(){
	if (this.pausecheck==1) { //if pause onMouseover enabled, cache object
		var cacheobj=this
	}
	if (this.mouseovercheck==1) {
		setTimeout(function(){cacheobj.rotateimage()}, 100)
	}
	else if (is_iexplorer&&dom||dom){
		this.resetit()
		var crossobj=this.tempobj=is_iexplorer? is_iexplorer[this.curcanvas] : document.getElementById(this.curcanvas)
		crossobj.style.zIndex++
		var prevIndex=((this.curimageindex-1)<0)? this.postimages.length-1 : (this.curimageindex-1); // juk
//		fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
		fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"], postcaptions["+this.curimageindex+"], postcaptions["+prevIndex+"])",50)// juk
		this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
		this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
	} else {
		var ns4imgobj=document.images['defaultslide'+this.slideshowid]
		ns4imgobj.src=this.postimages[this.curimageindex].src
		this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
	}
}
 
fadeshow.prototype.resetit=function(){
	this.degree=10
	var crossobj=is_iexplorer? is_iexplorer[this.curcanvas] : document.getElementById(this.curcanvas)
	crossobj.style.opacity = this.degree/100;
	if ( is_konqueror ) crossobj.style.KhtmlOpacity = this.degree/100;
	if ( is_iexplorer ) crossobj.style.filter = 'alpha(opacity=' + this.degree + ')';
}
 
 
fadeshow.prototype.startit=function(){
	var crossobj=is_iexplorer? is_iexplorer[this.curcanvas] : document.getElementById(this.curcanvas)
	this.populateslide(crossobj, this.curimageindex)
	if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
		var cacheobj=this
		var crossobjcontainer=is_iexplorer? is_iexplorer["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
		crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
		crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
	}
	this.rotateimage()
}
