// Contains classes that control the carousel on the home page.

// A class to contain information for a carousel item.
function CarouselDataItem (initObj) {
	this.imageSrc = '';
	this.imageDescription = '';
	this.imageLink = '';
	this.text = '';
	
	this.anchorWrapper = null; // This is a reference to an object that wraps an anchor Element that when clicked causes the item to be displayed.
	
	try {
		this.imageSrc = initObj.imageSrc;
		this.imageDescription = initObj.imageDescription;
		this.imageLink = initObj.imageLink;
		this.text = initObj.text;
	}
	catch (e) {
		alert(e);
	}
}

// A wrapper class for CarouselDataItems and related presentation logic.
function CarouselCollection () {
	this.items = new Array();
	this.pointer = 0;
	
	// Add an item to the carousel.
	this.add = function (dataItem) {
		this.items.push(dataItem);
	}
	
	// Treat as private.
	/** Set internal pointer to a random position.
	 *  @return null
	 */
	this.randomizePointer = function () {
		this.pointer = Math.floor(Math.random() * this.items.length);
		return null;
	}
	
	this.getLength = function () {
		return this.items.length;
	}
	
	// Treat as private.
	this.display = function () {
		try {
			$('carousel_image').writeAttribute(
				{
					'src': this.items[this.pointer].imageSrc,
					'alt': this.items[this.pointer].imageDescription
				}
			);
			$('carousel_text').innerHTML = this.items[this.pointer].text
			
			if (this.items[this.pointer].imageLink != "") {
				$('carousel_imageLink').writeAttribute(
					{ 
						'href': this.items[this.pointer].imageLink
					}
				);
				
				$('carousel_imageLink').removeClassName('cursorDefault');
				$('carousel_imageLink').addClassName('cursorPointer');
				
				//console.log('I linked.');
			}
			else {
				$('carousel_imageLink').writeAttribute(
					{
						'href':  "javascript:void(0);"
					}
				);

				$('carousel_imageLink').removeClassName('cursorPointer');
				$('carousel_imageLink').addClassName('cursorDefault');
				
				//console.log("I didn't link.");
			}
		}
		catch (e) {
			//console.log(e);
		}

		return null;
	}
	
	this.show = function (index) {
		this.items[this.pointer].anchorWrapper.makeNormal();
		
		this.pointer = index;
		this.display();

		this.items[this.pointer].anchorWrapper.makeActive();
		
		return null;
	}
	
	this.showNext = function () {

		var newPointer = 0;
		if (this.pointer + 1 == this.items.length) newPointer = 0;
		else newPointer = this.pointer + 1;
		
		this.show(newPointer);
		
		return null;
	}
	
	this.showPrevious = function () {

		var newPointer = 0;
		if (this.pointer == 0 ) newPointer = this.items.length -1;
		else newPointer = this.pointer - 1;
		
		this.show(newPointer);
		
		return null;
	}
	
	// Initialize the display of the carousel.
	this.init = function () {
		// Set the internal pointer of the carousel to a random item.
		this.randomizePointer();


		// Create item arrows and links and bind click events.
		var leftArrow = Element('a', {href: "javascript:void(0);"}).update("&laquo;");
		Event.observe(leftArrow, 'click', this.showPrevious.bind(this));
		
		
		$('carousel_itemLinks').insert('&#160;');
		
		$('carousel_itemLinks').insert(leftArrow);
		for (var i = 0; i < this.items.length; i++) {
			var numberLink = Element('a', {href: "javascript:void(0);"}).update(i + 1);
			Event.observe(numberLink, 'click', this.show.bind(this, i));
			this.items[i].anchorWrapper = new AnchorWrapper(numberLink);
			
			$('carousel_itemLinks').insert(numberLink);
			$('carousel_itemLinks').insert('&#160;');
		}

		var rightArrow = Element('a', {href: "javascript:void(0);"}).update("&raquo;");
		Event.observe(rightArrow, 'click', this.showNext.bind(this));

		$('carousel_itemLinks').insert(rightArrow);


		// Show the initial item.
		this.items[this.pointer].anchorWrapper.makeActive();
		this.display();
		
		return null;
	}	
}


// This Class wraps an anchor, stores it's default style info, and defines methods that provide effect.
function AnchorWrapper (anchorElement) {
	this.anchor = anchorElement;
	this.defaultStyle = Object.clone(anchorElement.style);
	
	this.makeNormal = function () {
		this.anchor.style.color = this.defaultStyle.color;
		this.anchor.style.textDecoration = this.defaultStyle.textDecoration;
		this.anchor.style.fontWeight = this.defaultStyle.fontWeight;
	}
	
	this.makeActive = function () {
		this.anchor.style.color = "#000000";
		this.anchor.style.textDecoration = "none";
		this.anchor.style.fontWeight = "bold";
	}
}