/********************************
Public members
********************************/

var	carousel = {}; // the main carousel object itself

/********************************
Initiation function
********************************/

/*
Initiation function.
*/

carousel.init = function()
	{
		// public members
		this.currentItem = 0;

		// private members
		this._nLeftItem; // item index to the left
		this._nRightItem; // item index to the right

		if (this.items != undefined)
		{
			this._configItems();
			this._display();
			this.show();
		}
	}

/********************************
Public methods
********************************/

/*
Informs the carousel to collect the html content from the specified html elements.

	@contentItems:Array				list of html ids to collect the content from
*/

carousel.collectContent = function(contentItems)
	{
		this.items = [];
		for (var i = 0; i < contentItems.length; i++) this.items[i] = {content:document.getElementById(contentItems[i]).innerHTML};
	}

/*
Default:visible.
*/

carousel.show = function(show)
	{
		show = (show == false) ? "hidden" : "visible";
		$("#carouselContainer").css("visibility", show);
	}

/*
Moves to the next item in the carousel.
*/

carousel.nextItem = function()
	{
		this.currentItem += 1;
		if (this.currentItem >= this.items.length) this.currentItem = 0;
		this._display();
	}

/*
Moves to the previous item in the carousel.
*/

carousel.prevItem = function()
	{
		this.currentItem -= 1;
		if (this.currentItem < 0) this.currentItem = this.items.length - 1;
		this._display();
	}

/*
Moves to the specified item in the carousel.

	@index:Number				index position to move to
*/

carousel.setItem = function(index)
	{
		this.currentItem = index;
		if (this.currentItem < 0) this.currentItem = this.items.length - 1;
		this._display();
	}

/*
Causes the current media being played to stop by replacing it with an empty container.
*/

carousel.stopMedia = function()
	{
		$("#carouselContainer #carouselFlashContentContainer").html("<div id='carouselFlashContent'></div>");
	}

/*
Causes the current media being to play by re-displaying it.
*/

carousel.startMedia = function()
	{
		this._display();
	}

/********************************
Private functions
********************************/

/*
Sets all data for all items in the carousel.
*/

carousel._configItems = function()
	{
		this.items[0].name = "home";
		this.items[0].background = "images/home_bg.png";
		this.items[0].backgroundLeft = "images/home_bg_left.png";
		this.items[0].backgroundRight = "images/home_bg_right.png";
		this.items[0].flashPath = "flash/content_loader.swf";
		this.items[0].flashWidth = "520";
		this.items[0].flashHeight = "364";
		this.items[0].flashX = "206";
		this.items[0].flashY = "16";
		this.items[0].flashvars = {type:"flv", path:"../videos/nlp.flv", autoPlay:"false", staticImage:"home", skin:"flash/SkinOverPlayStopSeekMuteVol.swf"};

		this.items[1].name = "games";
		this.items[1].background = "images/games_bg.png";
		this.items[1].backgroundLeft = "images/games_bg_left.png";
		this.items[1].backgroundRight = "images/games_bg_right.png";

		this.items[2].name = "nessyFair";
		this.items[2].background = "images/nessy_fair_bg.png";
		this.items[2].backgroundLeft = "images/nessy_fair_bg_left.png";
		this.items[2].backgroundRight = "images/nessy_fair_bg_right.png";
		this.items[2].flashPath = "flash/content_loader.swf";
		this.items[2].flashWidth = "520";
		this.items[2].flashHeight = "364";
		this.items[2].flashX = "206";
		this.items[2].flashY = "16";
		this.items[2].flashvars = {type:"flv", path:"../videos/nessy_fair.flv", autoPlay:"false", staticImage:"nessyfair", skin:"flash/SkinOverPlayStopSeekMuteVol.swf"};

		this.items[3].name = "inUse";
		this.items[3].background = "images/in_use_bg.png";
		this.items[3].backgroundLeft = "images/in_use_bg_left.png";
		this.items[3].backgroundRight = "images/in_use_bg_right.png";

		this.items[4].name = "rules";
		this.items[4].background = "images/rules_bg.png";
		this.items[4].backgroundLeft = "images/rules_bg_left.png";
		this.items[4].backgroundRight = "images/rules_bg_right.png";
		this.items[4].flashPath = "flash/content_loader.swf";
		this.items[4].flashWidth = "520";
		this.items[4].flashHeight = "364";
		this.items[4].flashX = "206";
		this.items[4].flashY = "16";
		this.items[4].flashvars = {type:"flv", path:"../videos/er_ir_ur_rule.flv", autoPlay:"false", staticImage:"rules", skin:"flash/SkinOverPlayStopSeekMuteVol.swf"};
	}

/*
Applies the html to the central container for display.
*/

carousel._buildContent = function()
	{
		$("#carouselContainer .centralContent").html(this.items[this.currentItem].content);
		carousel._buildFlashContent();
	}

/*
Returns the current browser location without any page anchors or queries.

	@location:String			location to search
	@return:String				stripped down location
*/

carousel.getCurrentLocation = function(location)
	{
		var newLocation = String(location).split("?")[0];
		return newLocation.split("#")[0];
	}

/*
Applies the flash embedding part of the html to the central container for display.
Creates a new empty flash content div in the container for loading flash into, then loads the appropriate move.
*/

carousel._buildFlashContent = function()
	{
		var currLocation = carousel.getCurrentLocation(window.location); // current browser location

		$("#carouselContainer #carouselFlashContentContainer").html("<div id='carouselFlashContent'></div>");

		// add lightbox controls to the section buttons

		if (this.items[this.currentItem].name == "home")
		{
			/*
			The basic HTML version hides the get nuggets flash image as it displays beyond the scroll area.
			Here, if JS is running, we show the flash image.
			*/
			$("#nuggetsFlash").css("display", "block");

			$("#innerContentHome a.watchNow").css("display", "none");

			$("#moreInfoHome").click(function(event){
				this.href = currLocation + "#popupHome";
				event.preventDefault();
			});

			$("#moreInfoHome").fancybox({
				'titleShow': false,
				'onStart': stopMedia,
				'onClosed': startMedia
			});
		}

		if (this.items[this.currentItem].name == "games")
		{
			$("#moreInfoGames").click(function(event){
			 this.href = currLocation + "#popupGames";
			 event.preventDefault();
			});

			$("#moreInfoGames").fancybox({
				'titleShow': false,
				'onStart': stopMedia,
				'onClosed': startMedia
			});

			// container is already being positioned in javascript popup version
			$(".standaloneContentGames").css("left", 0);
			$(".standaloneContentGames").css("top", 0);

			$("#playGorillaTickler").click(function(event){
				//this.href = "flash/gorilla_tickler_loader.swf?gameDir=flash/&exitButton=false";
				this.href = currLocation + "#gorillaTicklerGame";
				event.preventDefault();
			});

			// the 'type' value forces the lightbox to recognice the loaded element as a SWF because we are adding a query string
			$("#playGorillaTickler").fancybox({
				'titleShow': false,
				//'width': 800,
				//'height': 600,
				//'type': 'swf',
				'onStart': stopMedia,
				'onClosed': startMedia
			});

			$("#playJigsore").click(function(event){
			 //this.href = "flash/jigsore_loader.swf?gameDir=flash/&exitButton=false";
			 this.href = currLocation + "#jigsoreGame";
			 event.preventDefault();
			});

			$("#playJigsore").fancybox({
				'titleShow': false,
				//'width': 800,
				//'height': 600,
				//'type': 'swf',
				'onStart': stopMedia,
				'onClosed': startMedia
			});
		}

		if (this.items[this.currentItem].name == "nessyFair")
		{
			$("#innerContentNessyFair a.watchNow").css("display", "none");
		}

		if (this.items[this.currentItem].name == "inUse")
		{
			$("#moreInfoInUse").click(function(event){
			 this.href = currLocation + "#popupInUse";
			 event.preventDefault();
			});

			$("#moreInfoInUse").fancybox({
				'titleShow': false,
				'onStart': stopMedia,
				'onClosed': startMedia
			});
		}

		if (this.items[this.currentItem].name == "rules")
		{
			$("#innerContentRules a.watchNow").css("display", "none");

			$("#moreInfoRules").click(function(event){
			 this.href = currLocation + "#popupRules";
			 event.preventDefault();
			});

			$("#moreInfoRules").fancybox({
				'titleShow': false,
				'onStart': stopMedia,
				'onClosed': startMedia
			});
		}

		// position the container
		if (this.items[this.currentItem].flashX != undefined)
		{
			// add 135px to the x coord as it is relative to the outer container
			$("#carouselContainer #carouselFlashContentContainer").css("left", (parseInt(this.items[this.currentItem].flashX) + 135).toString() + "px");
		}

		if (this.items[this.currentItem].flashY != undefined)
		{
			$("#carouselContainer #carouselFlashContentContainer").css("top", this.items[this.currentItem].flashY + "px");
		}

		// load the swf according to params
		if (this.items[this.currentItem].flashPath != "" && this.items[this.currentItem].flashPath != undefined)
		{
			swfobject.embedSWF(
				this.items[this.currentItem].flashPath,
				"carouselFlashContent",
				(this.items[this.currentItem].flashWidth == undefined) ? "100" : this.items[this.currentItem].flashWidth,
				(this.items[this.currentItem].flashHeight == undefined) ? "100" : this.items[this.currentItem].flashHeight,
				"9.0.124",
				"",
				(this.items[this.currentItem].flashvars == undefined) ? {} : this.items[this.currentItem].flashvars,
				{},
				{wmode:"window", salign:"lt"}
			);
		}
}

/*
Sets the index positions of the two adjacent items.
*/

carousel._setAdjacentIndexes = function()
	{
		_nLeftItem = (this.currentItem == 0) ? this.items.length - 1 : this.currentItem - 1;
		_nRightItem = (this.currentItem == this.items.length - 1) ? 0 : this.currentItem + 1;
	}

/*
Sets the backgrounds for each item on display.
*/

carousel._setBackgrounds = function()
	{
		$("#carouselContainer .centralContent").css("background-image", "url(" + this.items[this.currentItem].background + ")");
		$("#carouselContainer .leftContent").css("background-image", "url(" + this.items[_nLeftItem].backgroundLeft + ")");
		$("#carouselContainer .rightContent").css("background-image", "url(" + this.items[_nRightItem].backgroundRight + ")");
	}


/*
Initiates the display of all items in the carousel according to the current items index value.
*/

carousel._display = function()
	{
		this._setAdjacentIndexes();
		this._setBackgrounds();
		this._buildContent();
	}
