/* ========================================================================== */
/* = YUI-Based Image Slide Show (object prototype) ========================== */
/* = YUI Libraries Required:                                                = */
/* =	yahoo-dom-event.js                                                  = */
/* =	animation-min.js                                                    = */
/* ========================================================================== */
function YUISlideShow(oWrapper, slideImages, animDuration, animEasing) {
	/* ====================================================================== */
	/* = INSTANCE VALUES ==================================================== */
	this.oWrapper = oWrapper;
	this.slideImages = slideImages;
	this.currentIndex = 0;
	this.nextIndex = 0;
	this.previousIndex = 0;
	/* Get the slide link pointer */
	for ( var i = 0; i < this.oWrapper.childNodes.length; i++ ) {
		if ( this.oWrapper.childNodes[i].tagName == 'A' ) {
			this.oLink = this.oWrapper.childNodes[i];
		}
	}
	/* Get the slide image pointer */
	for ( var i = 0; i < this.oLink.childNodes.length; i++ ) {
		if ( this.oLink.childNodes[i].tagName == 'IMG' ) {
			this.oImage = this.oLink.childNodes[i];
		}
	}
	/* = INSTANCE VALUES ==================================================== */
	/* ====================================================================== */


	/* ====================================================================== */
	/* = FUNCTIONS ========================================================== */
	/* Set The Index Values/Next Slide Image */
	this.SetNextSlide = function() {

		// first set next index
		this.currentIndex = this.nextIndex;
		if ( this.currentIndex == this.slideImages.length - 1 ) {
			this.nextIndex = 0;
		} else {
			this.nextIndex++;
		}

		// now set previous index
		if (this.currentIndex == 0) {
			this.previousIndex = this.slideImages.length - 1;
		} else {
			this.previousIndex = this.currentIndex - 1;
		}

		YAHOO.util.Dom.setStyle(this.oWrapper, 'backgroundImage', "url('" + this.slideImages[this.nextIndex].src + "')");
	}

	this.SetPreviousSlide = function() {

		// first set previous index
		this.currentIndex = this.previousIndex;
		if ( this.currentIndex == 0 ) {
			this.previousIndex = this.slideImages.length - 1;
		} else {
			this.previousIndex--;
		}

		// now set next index
		if (this.currentIndex == this.slideImages.length - 1) {
			this.nextIndex = 0;
		} else {
			this.nextIndex = this.currentIndex + 1;
		}

		YAHOO.util.Dom.setStyle(this.oWrapper, 'backgroundImage', "url('" + this.slideImages[this.previousIndex].src + "')");
	}


	/* Animation Start Function */
	this.AnimateSlides = function(direction) {
		if ( this.oLink.getAttribute("href") && this.oLink.getAttribute("href").length > 0 ) {
			this.oLink.removeAttribute("href");
		}
		if ( this.oLink.getAttribute("target") && this.oLink.getAttribute("target").length > 0 ) {
			this.oLink.removeAttribute("target");
		}

		if (direction=="previous") {
			this.animOutPrevious.animate();
		} else {
			this.animOutNext.animate();
		}
	}

	/* Animation End Next Function */
	this.AnimateEndNext = function() {
		var index = this.parent.nextIndex;

		if ( this.parent.slideImages[index].href.length > 0 ) {
			this.parent.oLink.setAttribute("href", slideImages[index].href);
			if ( this.parent.slideImages[index].target.length > 0 ) {
				this.parent.oLink.setAttribute("target", this.parent.slideImages[index].target);
			}
		}
		this.parent.oImage.setAttribute("src", this.parent.slideImages[index].src);
		YAHOO.util.Dom.setStyle(this.parent.oImage, 'opacity', '1');
		this.parent.SetNextSlide();
	}

	/* Animation End Previous Function */
	this.AnimateEndPrevious = function() {
		var index = this.parent.previousIndex;

		if ( this.parent.slideImages[index].href.length > 0 ) {
			this.parent.oLink.setAttribute("href", slideImages[index].href);
			if ( this.parent.slideImages[index].target.length > 0 ) {
				this.parent.oLink.setAttribute("target", this.parent.slideImages[index].target);
			}
		}
		this.parent.oImage.setAttribute("src", this.parent.slideImages[index].src);
		YAHOO.util.Dom.setStyle(this.parent.oImage, 'opacity', '1');
		this.parent.SetPreviousSlide();
	}
	/* = FUNCTIONS ========================================================== */
	/* ====================================================================== */


	/* ====================================================================== */
	/* = INITIALIZE ========================================================= */
	/* Initialize Slides/Indexes */
	this.SetNextSlide();
	/* Animation Config */
	this.animOutNext = new YAHOO.util.Anim(this.oImage, { opacity: { from: 1, to: 0 } }, animDuration, YAHOO.util.Easing[animEasing]);
	this.animOutNext.parent = this; /* preserve a parent reference for when calling the onComplete function */
	this.animOutNext.onComplete.subscribe(this.AnimateEndNext);

	this.animOutPrevious = new YAHOO.util.Anim(this.oImage, { opacity: { from: 1, to: 0 } }, animDuration, YAHOO.util.Easing[animEasing]);
	this.animOutPrevious.parent = this; /* preserve a parent reference for when calling the onComplete function */
	this.animOutPrevious.onComplete.subscribe(this.AnimateEndPrevious);
	/* = INITIALIZE ========================================================= */
	/* ====================================================================== */
}