/* Simple slideshow 
Creative Commons © 2007 Bryan Elliott
fordiman@gmail.com

Use notes:
	Simple to use.  Just include this script, and use the following HTML:
		<div class="slideshow" delay=8 fadetime=2>
			<img src="first/image/in/show.jpg" />
			<img src="second/image/in/show.jpg" />
			....
		</div>
	Delay and fade time are in seconds.

Code notes:
	Everything is a part of the slideShow object.
	The designer need not know any JavaScript for the intended 
		application of this object; it automatically hooks into the 
		document
	The object does not instantiate at all if the designer includes the 
		script, but doesn't invoke it via the HTML.
	The script works perfectly in Firefox and in IE 5.5-7.0 (comprising 
		90% of target browsers)
	This script does not include some proprietary mush, such as 'mootools' 
		or Flash.
	Anything but the very latest version of Opera has some ... issues.  
		Fortunately, Opera only covers a very small percentage of the 
		market.
	Konqueror and Safari (and the very latest Opera) slide well, but the 
		cross-fade doesn't work.  As a result, images fade from black.
	
License notes:
	The CC License: http://creativecommons.org/licenses/by-sa/2.5/
	The code: http://www.fordi.org/simpleSlideShow.js
	
	To be very explicit here:
		If there is a conflict with what is written here and the CC 
			License, what is written here supercedes only the 
			conflicting portion of the CC license.  In all other cases,
			both this and the CC License apply to the code.
		You may use the code, so long as it is unchanged, and as long as 
			these notices are kept intact.  No claim will be made on 
			your HTML or other code if you're using this file 
			unchanged.
		You may modify the code, but only under the condition that this 
			notice is kept intact, and if distributed, it is distributed 
			under the exact same CC License, with these same 
			additional clauses.  
		If you want to use the code in a way that is inconsistent with 
			the license and my above statements, please email me, and 
			we will discuss your intent and what compensation may or 
			may not be needed.  For clarity, compensation will only be
			needed if attribution is not to be given, or if this license
			will not be a part of the code.
*/
var slideShow = function (rootElem) {
	for (i in slideShow.members) this[i]=slideShow.members[i];
	this.root = rootElem;
	this.root.control = this;
	this.index=slideShow.instances.length;
	slideShow.instances[this.index]=this;	
	this.setupRoot();
	this.loadImages();
	this.activeImage=0;
	this.transposition=0;
	this.delay=parseFloat(this.root.getAttribute('delay'));
	var ft=parseFloat(this.root.getAttribute('fadetime'));
	if (typeof this.delay=='undefined' || isNaN(this.delay) || this.delay <= 0) this.delay=5;
	if (this.root.getAttribute('auto')!='false') this.change();
}

slideShow.instances = Array();
slideShow.members = Array();
slideShow.eventHandlers = Array();

slideShow.empty = function (a) {
	return (typeof a == 'undefined' || isNaN(a) || a <= 0);
}

slideShow.bool = function (a) {
	return (a=='true' || a==1 || a=='yes');
}

slideShow.members.getAttributes = function () {
	var rv = Array();
	rv.fadeTime = parseFloat(this.root.getAttribute('fadetime'));
	if (slideShow.empty(rv.fadeTime)) rv.fadeTime=0.5;
	rv.delay = parseFloat(this.root.getAttribute('delay'));
	if (slideShow.empty(rv.delay)) rv.delay=5;
	rv.auto = slideShow.bool(this.root.getAttribute('auto'));
	rv.shuffle = slideShow.bool(this.root.getAttribute('shuffle'));
	return rv;
}

slideShow.members.changeImage = function () {
	this.Images[this.lastImage].style.zIndex=-1;
	this.Images[this.activeImage].style.zIndex=0;
	this.Images[this.activeImage].style.visibility='visible';
	slideShow.setOpacity(this.Images[this.activeImage],0);
	this.transposition=0;
	var ft=parseFloat(this.root.getAttribute('fadetime'));
	setTimeout("slideShow.instances["+this.index+"].crossFade();", ft*100);
}
slideShow.members.crossFade = function () {
	var ft=parseFloat(this.root.getAttribute('fadetime'));
	if (typeof ft=='undefined' || isNaN(ft) || ft<=0) ft=0.5;
	this.transposition+=5/ft;
	slideShow.setOpacity(this.Images[this.activeImage],this.transposition/100);
	if (this.transposition>=100) {
		this.Images[this.lastImage].style.visibility='hidden';
		this.fadeTimer=0;		
	} else setTimeout("slideShow.instances["+this.index+"].crossFade();", 25);
		
}
slideShow.members.advance = function () {
	this.lastImage=this.activeImage;
	this.activeImage=(this.activeImage+1)%this.Images.length;
	this.changeImage();
	setTimeout("slideShow.instances["+this.index+"].advance();", this.delay*1000);
}
slideShow.members.change = function () {
	if (this.root.getAttribute('auto')!='false')
		setTimeout("slideShow.instances["+this.index+"].advance();", this.delay*1000);
}
slideShow.members.setupRoot = function () {
	this.root.style.position='relative';
	this.root.style.display='block';
	this.root.style.overflow='hidden';
	this.root.style.backgroundColor='#000000';
}
slideShow.members.loadImages = function () {
	var Imgs = this.root.getElementsByTagName('img');
	this.Images=new Array();
	var ct=0;
	for (var i=0; i<Imgs.length; i++) {
		this.Images[ct]=Imgs[i];
		this.Images[ct].control=this;
		this.Images[ct].style.position='absolute';
		this.Images[ct].style.top=0;
		this.Images[ct].style.left=0;
		if (ct!=0) {
			this.Images[ct].style.visibility='hidden';
			this.Images[ct].style.display='';
			this.Images[ct].style.zIndex=0;
			if (!this.Images[ct].complete) 		
			slideShow.attach(this.Images[ct],'load',slideShow.eventHandlers.imageLoad);
			else slideShow.imageLoad(this.Images[ct]);
		}else {
			if (!this.Images[ct].complete) 		
				slideShow.attach(this.Images[ct],'load',slideShow.eventHandlers.imageFirstLoad);
			else slideShow.imageFirstLoad(this.Images[ct]);
		}
		ct++;
	}
	
}
slideShow.imageLoad = function (img) {
	//Fix this later to account for aspect ratio.
	img.width = img.offsetWidth;
	img.height = img.offsetHeight;
}
slideShow.imageFirstLoad = function (img) {
	img.width = img.offsetWidth;
	img.height = img.offsetHeight;
	img.control.root.style.width=parseInt(img.offsetWidth)+"px";
	img.control.root.style.height=parseInt(img.offsetHeight)+"px";
}
slideShow.eventHandlers.imageFirstLoad = function (e) {
	if (window.event) var src=window.event.srcElement;
	else var src=this;
	slideShow.imageFirstLoad(this);
}
slideShow.eventHandlers.imageLoad = function (e) {
	if (window.event) var src=window.event.srcElement;
	else var src=this;
	slideShow.imageLoad(this);
}
slideShow.eventHandlers.docLoad=function () {
	var Divs = document.getElementsByTagName('div');
	for (var div=0; div<Divs.length; div++) 
		if (Divs[div].className.toLowerCase().search("slideshow")!=-1) new slideShow(Divs[div]);
}
slideShow.setOpacity=function(obj, opacity){
	if (opacity==0&&obj.style.visibility !="hidden") obj.style.visibility="hidden";
	else if (obj.style.visibility != "visible") obj.style.visibility="visible";
	if (window.ActiveXObject) obj.style.filter="alpha(opacity="+opacity*100+")";
	else obj.style.opacity=opacity+'';
}
slideShow.attach=function(obj,event,func) {
	if (obj.addEventListener) return obj.addEventListener(event,func,false);
	if (obj.attachEvent) return obj.attachEvent('on'+event,func);
	return false;
}
slideShow.attach(window,'load',slideShow.eventHandlers.docLoad);
