/***********************************************
* 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.
***********************************************/

/***********************************************
Numerous edits and improvements - Scott Kuether - 1/2/2007:

-> Correct bug so it will behave appropriately if there is only one image in the array
-> Allow option to not preload all images
-> Once the rest of the page is loaded, kicks off a function to load the remaining images
-> If preloads fail, and we reach an image that isn't loaded, start over at the beginning, and
      start the load of the missing image
!!! Test and correct mouseover !!!
***********************************************/


var fadebgcolor="white";
 
////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 emptyarray = new Array();
 
var dom=(document.getElementById); //modern dom browsers
var iebrowser=document.all;

function fadeshow(containerdiv, theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder) {
this.pausecheck=pause;
this.mouseovercheck=0;
this.forcerotate=0;
this.delay=delay;
this.degree=10; //initial opacity degree (10%)
this.curimageindex=0;
this.nextimageindex=(theimages.length-1>0)? 1 : 0;
fadearray[fadearray.length]=this;
this.slideshowid=fadearray.length-1;
this.canvasbase="canvas"+this.slideshowid;
this.curcanvas=this.canvasbase+"_0";
this.theimages=theimages;
if (typeof displayorder!="undefined")
this.theimages.sort(function() {return 0.5 - Math.random();}); //thanks to Mike (aka Mwinter) :)
this.imageborder=parseInt(borderwidth);
this.postimages=new Array(); //image array
//this.imagesset=theimages.length; //preload all images
this.imagesset=2; //only preload 2 images
for (p=0;p<theimages.length;p++){
this.postimages[p]=new Image();
this.postimages[p].index=p;
if (p<this.imagesset)
	this.postimages[p].src=theimages[p][0];
}
 
var fadewidth=fadewidth+this.imageborder*2;
var fadeheight=fadeheight+this.imageborder*2;

var crossobj=iebrowser? iebrowser[containerdiv] : document.getElementById(containerdiv);
crossobj.fadeshow = this;

var captiontable='<table class="caption"><tr><td><a href="#"; onclick="javascript:fadearray['+this.slideshowid+'].forceprevimage(); return false;">&#60;prev</a></td><td class="middle"><span id="captionspan'+this.slideshowid+'">test</span></td><td class="right"><a href="#" onclick="javascript:fadearray['+this.slideshowid+'].forcenextimage(); return false;">next&#62;</a></td></tr></table>';

if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
crossobj.innerHTML='<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div class="captiondiv" id="caption'+this.slideshowid+'" style="position:absolute;bottom:-1px;left:0px;background-color: white; display:none; color:black; width:'+fadewidth+'px; filter:progid:DXImageTransform.Microsoft.alpha(opacity=75); -moz-opacity:75; -khtml-opacity:75;">'+captiontable+'</div><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0px;left:0px;z-index:1;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:0px;left:0px;z-index:1;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:'+fadebgcolor+'"></div></div>';
else
crossobj.innerHTML='<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>';

this.captiondiv=iebrowser? iebrowser["caption"+this.slideshowid] : document.getElementById("caption"+this.slideshowid);
this.captionspan=iebrowser? iebrowser["captionspan"+this.slideshowid] : document.getElementById("captionspan"+this.slideshowid);
crossobj.onmouseover=function(){
	if (this.fadeshow.pausecheck==1) {
	   this.fadeshow.mouseovercheck=1; 
	   var tempobj=iebrowser? iebrowser[this.fadeshow.curcanvas] : document.getElementById(this.fadeshow.curcanvas);
	   this.fadeshow.captiondiv.style.zIndex=tempobj.style.zIndex+2;
	   this.fadeshow.captiondiv.style.display="block";
	}
}
crossobj.onmouseout=function(){
	this.fadeshow.mouseovercheck=0;
	this.fadeshow.captiondiv.style.zIndex=-99;
}

if (iebrowser&&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) {
	if (obj.degree<100) 
	{
		obj.degree+=10;
		if (obj.tempobj.filters&&obj.tempobj.filters[0])
		{
			if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
				obj.tempobj.filters[0].opacity=obj.degree;
			else //else if IE5.5-
				obj.tempobj.style.filter="alpha(opacity="+obj.degree+")";
		}
		else 
			if (obj.tempobj.style.MozOpacity)
				obj.tempobj.style.MozOpacity=obj.degree/101;
			else 
				if (obj.tempobj.style.KhtmlOpacity)
					obj.tempobj.style.KhtmlOpacity=obj.degree/100;
	}
	else
	{
		clearInterval(fadeclear[obj.slideshowid]);

		if (obj.imagesset < obj.postimages.length)
		{
			obj.postimages[obj.imagesset].src=obj.theimages[obj.imagesset][0];
			obj.imagesset++;
		}
		
		obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1";
		obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas);

		if (obj.postimages[obj.nextimageindex].src.length < 1)
			obj.postimages[obj.nextimageindex].src=obj.theimages[obj.nextimageindex][0];

		while ((obj.nextimageindex != 0) && (obj.postimages[obj.nextimageindex].src.length<1 || !obj.postimages[obj.nextimageindex].complete))
		{
			obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0;
		}
		obj.populateslide(obj.tempobj, obj.nextimageindex);
		obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0;
		obj.timeout=setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay);
	}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
picobj.index=picindex;
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.theimages[picindex][0]+'" border="'+this.imageborder+'px">';
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML+='</a>';
picobj.innerHTML=slideHTML;
}


fadeshow.prototype.rotateimage=function(){
//if pause onMouseover and mouseover then cache object
if (this.pausecheck==1 && this.mouseovercheck==1 && this.forcerotate==0) {
var cacheobj=this;
this.timeout=setTimeout(function(){cacheobj.rotateimage()}, 100); }
else {
this.captionspan.innerHTML = this.theimages[(this.postimages.length+this.nextimageindex-1) % this.postimages.length][3];
if (iebrowser&&dom||dom){
this.resetit();
var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
crossobj.style.zIndex++;
fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50);
this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_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;
this.forcerotate=0;
}
 
fadeshow.prototype.resetit=function(){
this.degree=10;
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
if (crossobj.filters&&crossobj.filters[0]){
if (typeof crossobj.filters[0].opacity=="number") //if IE6+
crossobj.filters(0).opacity=this.degree;
else //else if IE5.5-
crossobj.style.filter="alpha(opacity="+this.degree+")";
}
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=this.degree/101;
else if (crossobj.style.KhtmlOpacity)
crossobj.style.KhtmlOpacity=this.degree/100;
else if (crossobj.style.opacity&&!crossobj.filters)
crossobj.style.opacity=this.degree/101;
}
 
 
fadeshow.prototype.startit=function(){
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas);
this.populateslide(crossobj, this.curimageindex);
this.rotateimage();
}

fadeshow.prototype.forcenextimage=function(){
	clearInterval(fadeclear[this.slideshowid]);
	clearTimeout(this.timeout);
	this.forcerotate=1;
	this.rotateimage();
}

fadeshow.prototype.forceprevimage=function(){
	clearInterval(fadeclear[this.slideshowid]);
	clearTimeout(this.timeout);
	this.forcerotate=1;
	var tempobj=iebrowser? iebrowser[this.nextcanvas] : document.getElementById(this.nextcanvas);
	this.nextimageindex=(this.postimages.length+tempobj.index-2) % this.postimages.length;
	this.populateslide(tempobj, this.nextimageindex);
	this.nextimageindex=this.nextimageindex+1 % this.postimages.length;
	this.rotateimage();
}

/* Son of SuckerFish */
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
// Only needed for IE
if (window.attachEvent) window.attachEvent("onload", sfHover);


/* Carve out a percentage of an array */
function carvearray(sourcearray, startpercent, endpercent) {
var len=sourcearray.length;
var start=(startpercent < 1 || startpercent > 99) ? 0 : Math.round(startpercent/100*len);
var end=(endpercent < 1 || endpercent > 99) ? len-1 : Math.round(endpercent/100*len);
return(sourcearray.slice(start,end));
}

/* Eliminate duplicates from array */
function eliminatedups(sourcearray) {
var noDupArray = new Array();

sourcearray = sourcearray.sort(sortfirststring); 
var compVar = sourcearray[sourcearray.length-1][0];
var x = 0;
for (i=0;i<sourcearray.length;i++)
	if (sourcearray[i][0] != compVar)
	{
		noDupArray[x++] = sourcearray[i];
		compVar = sourcearray[i][0];
	}
return(noDupArray);
}

function sortfirststring(a,b) {
   if(a[0] > b[0]) 
      return 1;
   if(a[0] < b[0]) 
      return -1; 
   return 0;
}
