/**

 *@author Sam McCallum <sam@palo-verde.us>

 */



/** Load in the content via XML **/

var LexusXml = Class.create();

LexusXml.prototype = {

	initialize: function () {
		this.xml_file = '/lexus/lexus.xml';

		this.container = $$('#pastWinners .container').first();
		this.loadXmlFile();

	},

	loadXmlFile: function () {
		

		xmlReady = this.xmlReady.bind(this);

		new Ajax.Request(this.xml_file, {method: 'get', onSuccess: xmlReady});

	},

	xmlReady: function (transport) {
		var rootEl = transport.responseXML.getElementsByTagName('lexus')[0];
		this.items = rootEl.getElementsByTagName('winner');
		this.buildContent();
		startLexusCarousel();
	},
	getLittleImage: function (item) {
		var imgs = item.getElementsByTagName('images')[0];
		var littleImage = imgs.getElementsByTagName('little')[0];
		return littleImage.firstChild.nodeValue;
	},
	getBigImage: function (item) {
		var imgs = item.getElementsByTagName('images')[0];
		var bigImage = imgs.getElementsByTagName('big')[0];
		return bigImage.firstChild.nodeValue;
	},
	getAltText: function (item) {
		var txt = item.getElementsByTagName('text')[0];
		if (!txt) return '';
		if (!txt.firstChild) return '';
		if (!txt.firstChild.nodeValue) return '';
		return txt.firstChild.nodeValue;
	},

	buildContent: function () {
		this.ul = new Element('ul');

		this.items.length.times(

			function (index) {
				var li = new Element('li');
				var altText = this.getAltText(this.items[index]);
				var imgSrc = this.getLittleImage(this.items[index]);
				var img = new Element('img',{

					src: imgSrc,

					width: 99,

					height: 110,
					alt: altText

				});
				
				var bigImgSrc = this.getBigImage(this.items[index]);
				img.observe('mouseover', function (ev) {
					$('pastWinnersCallout').insert(new Element('img', {
						src: bigImgSrc,
						alt: altText,
						width: 260,
						height: 157
					}));
					//calloutFollowsMouse();
					offset = img.cumulativeOffset();
					$('pastWinnersCallout').style.top = offset.top - $('pastWinnersCallout').getHeight() + 'px';
					$('pastWinnersCallout').style.left = offset.left + 50 - $('pastWinnersCallout').getWidth() + 'px';
					$('pastWinnersCallout').show();
				});
				
				img.observe('mouseout', function () {
					$('pastWinnersCallout').hide().innerHTML = '';
				});
				var a = new Element('a',{title: altText, href:''});
				a.observe('click',function (aClickEv){aClickEv.stop();});
				a.insert(img);
				li.insert(a);
				this.ul.insert(li);
			}.bind(this)

		);
		this.container.insert(this.ul);
	}

} 


/* Manager the carousel component */
var lexusCarousel;

function startLexusCarousel() {
	lexusCarousel = new UI.Carousel("pastWinnersContent");
		

	/** Setup parents carousel **/

	lexusCarousel.watchNextButton = function (e) {

		// Check if the next button is active

		var nextClassName = "next_button" + lexusCarousel.options.disabledButtonSuffix;

		if (!e.element().hasClassName(nextClassName)) {	

			// Enabled, get current indicator number, and advance it by one

			var currentIndicator = $$("div#pastWinners div.indicator_bar div.indicator_active").first();

			lexusCarousel.setIndicator(parseInt(currentIndicator.identify().substr(currentIndicator.identify().length - 1, 1))+1);

			lexusCarousel.stopWatchingButtons();

		}

	}

	

	lexusCarousel.watchPreviousButton = function (e) {

		// Check if  the previous button is active

		var previousClassName = "previous_button" + lexusCarousel.options.disabledButtonSuffix;

		if (!e.element().hasClassName(previousClassName)) {		

			// Enabled, get current indicator number, and reverse it by one

			var currentIndicator = $$("div#pastWinners div.indicator_bar div.indicator_active").first();

			lexusCarousel.setIndicator(parseInt(currentIndicator.identify().substr(currentIndicator.identify().length - 1, 1))-1);

			lexusCarousel.stopWatchingButtons();

		}

	}

	

	var watchPreviousParentBind = lexusCarousel.watchPreviousButton.bindAsEventListener();

	var watchNextParentBind = lexusCarousel.watchNextButton.bindAsEventListener();

	

	lexusCarousel.setIndicator = function (num) {

		var num = (num == null) ? 1 : num;

		

		// clear the indicators

		$$("div#pastWinners div.indicator_bar").first().childElements().each(function (indicator) {

			indicator.className = "indicator";

		});

		

		$("past_winner_indicator_"+num).className = "indicator_active";

	}

	
	lexusCarousel.buildIndicators = function () {

		var indicators = ((lexusCarousel.container.childElements().length) / 4).ceil();

		var indicatorBar = $$("div#pastWinners div.indicator_bar").first();

		

		indicators.times(function (index) {

			indicator = new Element("div", {'class': "indicator", id: "past_winner_indicator_"+(index+1)});

			indicator.style.cursor = "pointer";

			indicatorBar.appendChild(indicator.observe("click", function (e) {

				ind = e.element();

				num = ind.identify().substr(ind.identify().length - 1,1);

				lexusCarousel.setIndicator(num);

				lexusCarousel.scrollTo((num - 1 ) * 4);

			}));

		});

		

		lexusCarousel.setIndicator();

		lexusCarousel.watchButtons();

	}

	

	lexusCarousel.watchButtons = function () {

		$(lexusCarousel.nextButton).observe("click", watchNextParentBind);

		$(lexusCarousel.previousButton).observe("click", watchPreviousParentBind);

	}

	

	lexusCarousel.stopWatchingButtons = function() {

		$(lexusCarousel.nextButton).stopObserving("click", watchNextParentBind);

		$(lexusCarousel.previousButton).stopObserving("click", watchPreviousParentBind);

	}

	

	lexusCarousel.observe("scroll:ended", function (e) {

		lexusCarousel.updateSize();

		lexusCarousel.watchButtons();

	});

	
	

	lexusCarousel.buildIndicators();
	
	if (lexusCarousel.elements.size() < 4) {

		$(lexusCarousel.nextButton).addClassName("next_button"+lexusCarousel.options.disabledButtonSuffix);

	}
	
	// disable the previous button on start manually, some IE issue..
	$(lexusCarousel.previousButton).addClassName('previous_button'+lexusCarousel.options.disabledButtonSuffix);
}



function doPastWinnersCarousel() {

	new LexusXml();
}

function calloutFollowsMouse() {
	Event.observe(document, 'mousemove', doCalloutPosition);
	$('pastWinnersCallout').show();
}

function calloutStopsFollowingMouse() {
	Event.stopObserving(document, 'mousemove', doCalloutPosition);
	$('pastWinnersCallout').hide();
}

function doCalloutPosition(e) {
	mouseX = Event.pointerX(e);
	mouseY = Event.pointerY(e);
	
	$('pastWinnersCallout').style.top = mouseY - 5 - $('pastWinnersCallout').getHeight() + 'px';
	$('pastWinnersCallout').style.left = mouseX - 5 - $('pastWinnersCallout').getWidth() + 'px';
}