
var MainFeatures = new function () {
	this.heroCategory = 0;
	this.minimumHorizontalFeatureShows = 3;
	this.featureShowWidth = 206;
	this.imagesPath = '';

	this.checkTotalFeatureShows = function (totalShows) {
		if (!totalShows) totalShows = $('div.eventModule').size();

		var showScroller = (totalShows > this.minimumHorizontalFeatureShows * 2);

		document.getElementById('slidingContent').style.display = (showScroller) ? 'block' : 'none';
		document.getElementById('HeroEventGap').style.display = (showScroller) ? 'none' : 'block';

		document.getElementById('eventsDisplay').style.left = "0px";
		document.getElementById('pb-productsliderhandleimage').style.left = "0px";
	}

	this.displayFeatureShows = function (shows) {
		if (!shows || shows.length == 0) {
			// TODO: Tell people there's nothing.
			this.updateShowsContainer('', 0);
			return;
		}

		if (shows.length > this.minimumHorizontalFeatureShows) {
			var odds = [];
			var evens = [];

			for (var i = 0; i < shows.length; i++) {
				if (i % 2 == 0) odds.push(shows[i]);
				else evens.push(shows[i]);
			}

			if (odds.length < this.minimumHorizontalFeatureShows) {
				odds.push(evens.pop());
			}

			shows = odds.concat(evens);
		}

		var showHtml = '';
		var horizontalShows = this.getHorizontalShows(shows.length);

		for (var i = 0; i < shows.length; i++) {
			showHtml += '<div class="eventModule">';
			showHtml += '<a href="/shows/show.aspx?sh=' + shows[i].SoftixCode + '" title="' + escapeAttribute(shows[i].TitleNoHtml) + '" class="greybox1">';
			showHtml += '<img src="' + this.getImagePath(shows[i]) + '" alt="' + escapeAttribute(shows[i].TitleNoHtml) + '" /></a>';
			showHtml += '<div class="eventModuleCopy"><a href="/shows/show.aspx?sh=' + shows[i].SoftixCode + '" title="' + escapeAttribute(shows[i].TitleNoHtml) + '" class="event_title">';
			showHtml += '<strong>' + shows[i].Title + '</strong></a>';
			showHtml += '</div>';
			showHtml += '</div>';

			// This is for IE6.
			if (i == horizontalShows - 1) showHtml += '<br />';
		}

		this.updateShowsContainer(showHtml, shows.length);
	}

	this.displayHeroShow = function (shows) {
		var imagePath = $('event > heroImageURL:first', shows).text();

		if (!imagePath || imagePath.length == 0) {
			imagePath = '/images/1x1.gif';
		}

		$('#heroImage > img:first').attr('src', imagePath).attr('alt', $('event > navLabel:first', shows).text());
		$('#heroText > h1:first').html($('event > title:first', shows).text());
		$('#heroText > p:first').html($('event > summary:first', shows).text());
		$('#heroBuyNow').attr('href', $('event > buyNowURL:first', shows).text());
		$('#heroTellAFriend').attr('href', $('event > tellAFriend:first', shows).text());
	}

	this.getHorizontalShows = function (totalShows) {
		var horizontalShows = Math.ceil(totalShows / 2);

		if (horizontalShows < this.minimumHorizontalFeatureShows) {
			horizontalShows = this.minimumHorizontalFeatureShows;
		}

		return horizontalShows;
	}

	this.handleError = function () {
		// TODO: Display an error message of some sort
	}

	this.initialise = function (categoryID) {
		if (!categoryID) categoryID = this.heroCategory;

		this.loadHero(categoryID, true);
		this.checkTotalFeatureShows();

		$('#categoricalNav a').click(function (event) {
			event.preventDefault();

			$("#categoricalNav a").removeClass('categoricalNavItemActive');
			$(this).addClass('categoricalNavItemActive');

			var categoryID = /\?c=(\d+)/.exec(this.href)[1];

			MainFeatures.loadCategory(categoryID);
		});
	}

	this.loadCategory = function (categoryID) {
		this.loadHero(categoryID);

		Softix.PowerWeb.External.UI.Shows.ShowService.GetFeatureShows(
			categoryID,
			function (results) { MainFeatures.displayFeatureShows(results); },
			function () { MainFeatures.handleError(); });
	}

	this.loadHero = function (categoryID, load) {
		var so = new SWFObject('/flash/hero.swf', 'sotester', '635', '203', '8', '');
		so.addParam('menu', 'false');
		so.addParam('wmode', 'transparent');
		so.addVariable('xmlFeed', '/shows/ShowService.asmx/GetHeroFeed?categoryID=' + categoryID);
		var flashWritten = so.write('heroModule');

		if (!flashWritten && load) {
			$('#heroNoFlash').css('display', 'block');
		}
		
		if (!flashWritten && !load)
		{
			Softix.PowerWeb.External.UI.Shows.ShowService.GetHeroFeed(
				categoryID,
				function (results) { MainFeatures.displayHeroShow(results); },
				function () { MainFeatures.handleError(); });
		}
	}

	this.updateShowsContainer = function (html, numberOfShows) {
		var showsContainer = $get('eventsDisplay');
		showsContainer.style.width = (this.featureShowWidth * this.getHorizontalShows(numberOfShows) + 3) + 'px';
		$(showsContainer).html(html);

		this.checkTotalFeatureShows(numberOfShows);
	}

	this.getImagePath = function (show) {
		if (!show.ImageFileName || show.ImageFileName.length == 0) {
			return '/images/1x1.gif';
		}
		return this.imagesPath + show.ImageFileName;
	}

	function escapeAttribute(string) {
		if (string == null) return string;

		return string.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
	}
};

