/* ========================================================================== */
/* = YUI-Based Animated Page Banner (object prototype) ====================== */
/* = YUI Libraries Required:                                                = */
/* =	yahoo-dom-event.js                                                  = */
/* =	animation-min.js                                                    = */
/* ========================================================================== */
function YuiPageBanner(oWrapper, bannerImages, animDuration, animEasing) {
	/* ====================================================================== */
	/* = INSTANCE VALUES ==================================================== */
	this.oWrapper = oWrapper;
	this.bannerImages = bannerImages;
	this.currentIndex = 0;
	this.nextIndex = 0;
	this.previousIndex = 0;
	/* Get the banner 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 banner 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 Banner Image */
	this.SetNextBanner = function() {

		// first set next index
		this.currentIndex = this.nextIndex;
		if ( this.currentIndex == this.bannerImages.length - 1 ) {
			this.nextIndex = 0;
		} else {
			this.nextIndex++;
		}

		// now set previous index
		if (this.currentIndex == 0) {
			this.previousIndex = this.bannerImages.length - 1;
		} else {
			this.previousIndex = this.currentIndex - 1;
		}

		YAHOO.util.Dom.setStyle(this.oWrapper, 'backgroundImage', "url('" + this.bannerImages[this.nextIndex].src + "')");
	}

	this.SetPreviousBanner = function() {

		// first set previous index
		this.currentIndex = this.previousIndex;
		if ( this.currentIndex == 0 ) {
			this.previousIndex = this.bannerImages.length - 1;
		} else {
			this.previousIndex--;
		}

		// now set next index
		if (this.currentIndex == this.bannerImages.length - 1) {
			this.nextIndex = 0;
		} else {
			this.nextIndex = this.currentIndex + 1;
		}

		YAHOO.util.Dom.setStyle(this.oWrapper, 'backgroundImage', "url('" + this.bannerImages[this.previousIndex].src + "')");
	}


	/* Animation Start Function */
	this.AnimateBanner = 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.bannerImages[index].href.length > 0 ) {
			this.parent.oLink.setAttribute("href", bannerImages[index].href);
			if ( this.parent.bannerImages[index].target.length > 0 ) {
				this.parent.oLink.setAttribute("target", this.parent.bannerImages[index].target);
			}
		}
		this.parent.oImage.setAttribute("src", this.parent.bannerImages[index].src);
		YAHOO.util.Dom.setStyle(this.parent.oImage, 'opacity', '1');
		this.parent.SetNextBanner();
	}

	/* Animation End Previous Function */
	this.AnimateEndPrevious = function() {
		var index = this.parent.previousIndex;

		if ( this.parent.bannerImages[index].href.length > 0 ) {
			this.parent.oLink.setAttribute("href", bannerImages[index].href);
			if ( this.parent.bannerImages[index].target.length > 0 ) {
				this.parent.oLink.setAttribute("target", this.parent.bannerImages[index].target);
			}
		}
		this.parent.oImage.setAttribute("src", this.parent.bannerImages[index].src);
		YAHOO.util.Dom.setStyle(this.parent.oImage, 'opacity', '1');
		this.parent.SetPreviousBanner();
	}
	/* = FUNCTIONS ========================================================== */
	/* ====================================================================== */


	/* ====================================================================== */
	/* = INITIALIZE ========================================================= */
	/* Initialize Banner/Indexes */
	this.SetNextBanner();
	/* 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 ========================================================= */
	/* ====================================================================== */
}
