/**
 * This class will display left and right banners. It will make an AJAX call to @ajaxURL.
 * It then will write the banner to homePageLeftBannerLocation and
 * homePageRightBannerLocation in @writeToLeftElementId and @writeToRightElementId.
 *
 * @params writeToLeftElementId, writeToRightElementId - id tag of element which we are going to place the banners in.
 * @param ajaxURL - url to controller that is responsible for creating the list of banners. * *
 * @return NA.
 */
function SplitBanners(writeToLeftElementId,writeToRightElementId, ajaxURL) {
	this._banners = new Array(0);
	this._bannersLength = -1;
	this._currentBannerIndex = -1;

	this._ajaxURL = ajaxURL;

	this._hasError = false;
	this._errorMessage = "";
	this._interval = null;
	this._defaultWidth = 800;
	this._defaultHeight = 150;

	/**
	 * Initial function called to create the AJAX request, when it succeeds, it calls
	 * first, which will determine if we have any banners and set the counter to the
	 * appropriate starting banner. Once our request is complete, we call create, which
	 * will write the new banner to the DOM.
	 */
	this.init = function() {
		var _nocache = new Date();
		new Ajax.Request(ajaxURL, {
			method: "post",
			parameters: "nocache=" + _nocache.getTime(),
			//Get First Banner
			onSuccess: this.first.bindAsEventListener(this),
			//Write Banner to DOM
			onComplete: this.create.bindAsEventListener(this),
			onFailure: function(transport) {
				this._hasError = true;
				this._errorMessage = "Failure in init - ajax call.";
			},
			onException: function(transport) {
				this._hasError = true;
				this._errorMessage = "Exception in init - ajax call.";
			}
		});
	};

	/**
	 * This method will write the current banner to the dom.
	 */
	this.create =  function(evt) {
		if(this._bannersLength > 0) {
			//Grab Left banner
			var _copartLeftBanner = this._banners[this._currentBannerIndex];
			//Grab Right banner
			var _copartRightBanner = this._banners[this._currentBannerIndex+1];

			//Set container width
			$(writeToLeftElementId).style.width  	= "400px"
			$(writeToRightElementId).style.width 	= "400px"

			//Set Container height
			$(writeToLeftElementId).style.height  	= "112px"
			$(writeToRightElementId).style.height  	= "112px"

			//Set display
			$(writeToLeftElementId).style.display  	= "block";
			$(writeToRightElementId).style.display 	= "block";

			//Set overflow
			$(writeToLeftElementId).style.overflow  = "hidden";
			$(writeToRightElementId).style.overflow = "hidden";

			//If the left and right banners path are empty
			if (!_copartLeftBanner.path.length > 0 && !_copartRightBanner.path.length > 0)
			{
				$("homePageBannerLocation").style.display="none";
			}

			var anchor;
			//Left Banner
			if(_copartLeftBanner.path.length > 0)
			{
				var leftBannerType = _copartLeftBanner.path.substring(_copartLeftBanner.path.length,_copartLeftBanner.path.length-3);
				if (leftBannerType.toUpperCase() == "SWF")
				{
					var so = new SWFObject(_copartLeftBanner.path, "def-lft", this.finalWidth(_copartLeftBanner.w), this.finalHeight(_copartLeftBanner.h), "7", "#FFFFFF");
		    		so.addParam("wmode", "transparent");
		    		so.write('homePageLeftBannerLocation');

				}
				else
				{
					$("leftFlash").style.display="none";
					//Check if the current banner is supposed to hyperlink
					if(_copartLeftBanner.url.length > 0) {
						//Check if the hyperlink should open a new window
						if(_copartLeftBanner.newWindow == "true") {

							//Create the link tag for the banner for new window
							anchor = $E({ tag: 'a', href: _copartLeftBanner.url, target: "_blank", id: writeToLeftElementId + 'Link' });
						}
						else {
							//Create the link tag for the banner
							anchor = $E({ tag: 'a', href: _copartLeftBanner.url, target: "_self", id: writeToLeftElementId + 'Link' });
						}
						//Create the image tag for the banner
						var img = $E({
							tag:	'img',
							src:	this.createBannerPath(_copartLeftBanner.path),
							id:		writeToLeftElementId + 'Img',
							height: _copartLeftBanner.h ,
							width: _copartLeftBanner.w
						});
						//Add the Image to the hyperlink tag
						anchor.appendChild(img);
						//Add the hyperlink tag to the element specified
						$(writeToLeftElementId).appendChild(anchor);
					}
					//There is no hyperlink for the banner
					else {
						//Create the image tag for the banner
						var img = $E({
							tag:	'img',
							src:	this.createBannerPath(_copartLeftBanner.path),
							id:		writeToLeftElementId + 'Img',
							height: _copartLeftBanner.h ,
							width: _copartLeftBanner.w
						});
						//Add the img tag to the element specified
						$(writeToLeftElementId).appendChild(img);
					}
				}
			}
			else
			{
				$("homePageLeftBannerLocation").style.display="none";
			}
			//Right Banner
			if(_copartRightBanner.path.length > 0)
			{
				var rightBannerType = _copartRightBanner.path.substring(_copartRightBanner.path.length,_copartRightBanner.path.length-3);
				if (rightBannerType.toUpperCase() == "SWF")
				{
					var so1 = new SWFObject(_copartRightBanner.path, "def-rgt", this.finalWidth(_copartRightBanner.w), this.finalHeight(_copartRightBanner.h), "7", "#FFFFFF");
		    		so1.addParam("wmode", "transparent");
		    		so1.write('homePageRightBannerLocation');

				}
				else
				{
					$("rightFlash").style.display="none";
					//Check if the current banner is supposed to hyperlink
					if(_copartRightBanner.url.length > 0) {
						//Check if the hyperlink should open a new window
						if(_copartRightBanner.newWindow == "true") {
							//Create the link tag for the banner for new window
							anchor = $E({ tag: 'a', href: _copartRightBanner.url, target: "_blank", id: writeToRightElementId + 'Link' });
						}
						else {
							//Create the link tag for the banner
							anchor = $E({ tag: 'a', href: _copartRightBanner.url, target: "_self", id: writeToRightElementId + 'Link' });
						}
						//Create the image tag for the banner
						var img = $E({
							tag:	'img',
							src:	this.createBannerPath(_copartRightBanner.path),
							id:		writeToRightElementId + 'Img',
							height: _copartRightBanner.h ,
							width: _copartRightBanner.w
						});
						//Add the Image to the hyperlink tag
						anchor.appendChild(img);
						//Add the hyperlink tag to the element specified
						$(writeToRightElementId).appendChild(anchor);
					}
					//There is no hyperlink for the banner
					else {
						//Create the image tag for the banner
						var img = $E({
							tag:	'img',
							src:	this.createBannerPath(_copartRightBanner.path),
							id:		writeToRightElementId + 'Img',
							height: _copartRightBanner.h ,
							width: _copartRightBanner.w
						});
						//Add the img tag to the element specified
						$(writeToRightElementId).appendChild(img);
					}
				}
			}
			else
			{
				$("homePageRightBannerLocation").style.display="none";
			}
		}
	};

	/**
	 * This method will read the AJAX response and extract the JSON object. This method sets
	 * the banner count and collection. If no banners exist or the node that we are going to
	 * populate with the banners does not exist, we set the error.
	 */
	this.first = function(transport) {
		//Eval the AJAX Response and get the JSON object

		var jsonBanners = transport.responseText.evalJSON();

		//Set banners count
		this._bannersLength = jsonBanners.banners.length;

		//Instantiate banners collection
		this._banners = new Array(this._bannersLength);
		for(var i = 0; i < this._bannersLength; i++) {
			//Add banner to collection
			this._banners[i] = jsonBanners.banners[i];
		}

		//No Banners.
		if (this._bannersLength < 1) {
			this._hasError = true;
			this._errorMessage = "There are no banners to display.";
		}
		//Element to write to is null.
		else if ($(writeToLeftElementId) === null || $(writeToLeftElementId) === undefined) {
			this._hasError = true;
			this._errorMessage = "Element with id: " + writeToLeftElementId + " doesn't exist.";
		}
		else if ($(writeToRightElementId) === null || $(writeToRightElementId) === undefined) {
			this._hasError = true;
			this._errorMessage = "Element with id: " + writeToRightElementId + " doesn't exist.";
		}
		else {
			// We're good, start at the first banner.
			this._currentBannerIndex = 0;
		}
	};

	this.finalWidth = function(bannerWidth) {
		if(bannerWidth != null) {
			return bannerWidth;
		}
		else {
			return this._defaultWidth;
		}
	};

	this.finalHeight = function(bannerHeight) {
		if(bannerHeight != null) {
			return bannerHeight;
		}
		else {
			return this._defaultHeight;
		}
	};

	this.createBannerPath = function(bannerPath) {
		if(bannerPath != null) {
			var normalizedPath = bannerPath.toUpperCase();
			if(normalizedPath.indexOf('HTTP://') >= 0) {
				return bannerPath;
			}
			else {
				return global.contextRoot + bannerPath;
			}
		}
	}

	/**
	 * This method will let you know if we have an error and what it was.
	 *
	 * @return Error -  hasError (boolean) and errorMsg (String).
	 */
	this.error = function() {
		return { hasError: this._hasError, errorMsg: this._errorMessage };
	};

	//////////////////////////
	// Getters and Setters
	//////////////////////////
	this.getId = function() {
		return this._id;
	};

	this.getCurrentBannerIndex = function() {
		return this._currentBannerIndex;
	};

	this.setCurrentBannerIndex = function(val) {
		this._currentBannerIndex = val;
	};

	this.getBannersLength = function() {
		return this._bannersLength;
	};

	this.getBanners = function() {
		return this._banners;
	};

}
