var simpleGalleries = [];

function simpleGalleryFader(initContainer, initPager) {
	var galleryImages = [];
	var blobs = [];
	var currentlyshowing = null;
	var currentblob = null;
	var crossfadetimeout = null;
	var gallerytitle = null;
	var gallerydescription = null;
	var gallerylink = null;
	var foo = this;
	
	this.replaceImages = function(newtitle, newdescription, newlink, newimages) {
		clearTimeout(crossfadetimeout);
		var x = 0;
		for (x in galleryImages) {
			if (galleryImages[x].style) {
				this.changeOpacity(galleryImages[x], 0);
			}
		}
		if (currentblob) {
			currentblob.src = "/images/blob-off.gif";
		}
		var x = 0;
		for (x in newimages) {
			if (galleryImages[x]) {
				galleryImages[x].src = newimages[x];
			}
		}
		if (galleryImages[0]) {
			galleryImages[0].onload = function() {
				var fade = new YAHOO.util.Anim(galleryImages[0], { opacity: { from: 0, to: 1 }}, 1.5, YAHOO.util.Easing.easeBoth);
				fade.animate();
				setTimeout(foo.crossfade, 5000);
			}
			currentlyshowing = galleryImages[0];
		}
		//currentlyshowing = null;
		//this.changeOpacity(galleryImages[0], 100);
		//currentlyshowing = galleryImages[0];
		//foo.crossfade();
		//setTimeout(foo.crossfade, 5000);
		
		if (gallerytitle) {
			gallerytitle.innerHTML = newtitle;
		}
		if (gallerydescription) {
			gallerydescription.innerHTML = newdescription;
		}
		if (gallerylink) {
			gallerylink.src = newlink;
			if (newlink == "") {
				gallerylink.style.display = "none";
			} else {
				gallerylink.style.display = "block";
			}
		}
	}
	
	this.changeOpacity = function(el, value) {
		if (document.all) {
			el.style["filter"] = "alpha(opacity="+value+")";
		} else {
			el.style["opacity"] = value/100;
		}
	}
	
	this.crossfade = function() {
		clearTimeout(crossfadetimeout);
		if (currentlyshowing) {
			var fade = new YAHOO.util.Anim(currentlyshowing, { opacity: { from: 1, to: 0 }}, 1.5, YAHOO.util.Easing.easeBoth);
			fade.animate();
		}
		if (currentblob) {
			currentblob.src = "/images/blob-off.gif";
		}
		var x = 0;
		for (x in galleryImages) {
			if (galleryImages[x] == currentlyshowing) {
				if ((parseInt(x)+1) > (galleryImages.length-1)) {
					//wrap around to beginning
					currentlyshowing = galleryImages[0];
					currentlyshowing.style.display = "block"; //make sure its actually showing!
					currentblob = blobs[0];
					currentblob.src = "/images/blob-on.gif";
					var fade = new YAHOO.util.Anim(currentlyshowing, { opacity: { from: 0, to: 1 }}, 1.5, YAHOO.util.Easing.easeBoth);
					fade.animate();
				} else {
					currentlyshowing = galleryImages[parseInt(x)+1];
					currentlyshowing.style.display = "block"; //make sure its actually showing!
					currentblob = blobs[parseInt(x)+1];
					currentblob.src = "/images/blob-on.gif";
					var fade = new YAHOO.util.Anim(currentlyshowing, { opacity: { from: 0, to: 1 }}, 1.5, YAHOO.util.Easing.easeBoth);
					fade.animate();
				}
				if (currentlyshowing.alt != "" && gallerytitle) {
					gallerytitle.innerHTML = currentlyshowing.alt;
				}
				if (currentlyshowing.title != "" && gallerydescription) {
					gallerydescription.innerHTML = currentlyshowing.title;
				}
				if (currentlyshowing.rel != "" && gallerylink) {
					gallerylink.src = currentlyshowing.rel;
				}
				break;
			}
		}
		crossfadetimeout = setTimeout(foo.crossfade, 5000);
	}
	
	this.init = function(container, initPager) {
		//alert("initing gallery");
		var arrElements = container.getElementsByTagName('img');
		for (var i = 0; i < arrElements.length; i++) {
			if (arrElements[i].className != null) {
				myRE = new RegExp("hp_recentwork_large_image", "i");
				if (arrElements[i].className.search(myRE) != -1) {
					if (i == 0) {
						currentlyshowing = arrElements[i];
						arrElements[i].style.display = "block";
					}
					galleryImages.push(arrElements[i]);
					if (arrElements[i].style.display != "none") {
						currentlyshowing = arrElements[i];
					} else {
						foo.changeOpacity(arrElements[i], 0);
					}
				}
				
			}		
		}
		
		var arrElements=document.getElementsByTagName("*");
		for (var i = 0; i < arrElements.length; i++) {
			if (arrElements[i].className != null) {
				myRE2 = new RegExp("gallery_title", "i");
				myRE3 = new RegExp("gallery_description", "i");
				myRE4 = new RegExp("gallery_link", "i");
				if (arrElements[i].className.search(myRE2) != -1) { //title
					gallerytitle = arrElements[i];
				}
				if (arrElements[i].className.search(myRE3) != -1) { //description
					gallerydescription = arrElements[i];
				}
				if (arrElements[i].className.search(myRE4) != -1) { //link
					gallerylink = arrElements[i];
				}	
			}
		}
		
		if (initPager) {
			initPager.innerHTML = "";
			var x = 0;
			for (x=0;x<galleryImages.length;x++) {
				var blob = document.createElement("img");
				if (x == parseInt(0)) {
					blob.src = "/images/blob-on.gif";
					currentblob = blob;
				} else {
					blob.src = "/images/blob-off.gif";
				}
				initPager.appendChild(blob);
				blobs.push(blob);
			}
		}
		
		//alert(galleryImages.length);
		crossfadetimeout = setTimeout(foo.crossfade, 5000);
	}
	
	if (initContainer) {
		this.init(initContainer, initPager);	
	}
}
