/**
 * homepage.js - Javascript applicable to the homepage of the site
 */

// Make leadCapture object accessible globally for flash
var leadCapture;

//Make shareOptions object accessible globally for flash
var shareOptions;

// Make shareFriend object accessible globally for flash
var shareFriend;

// Make flickrChooser object accessible globally for flash
var flickrChooser;

// Make the terms object accessible globally for flash
var terms;

// Make the flashMovie instance global
var flashMovie;

// Document ready actions
$(document).ready(function()
{
	// Resize the flash to fit the window
	$(window).resize(function(){

		// Declare variables
		var width = '100%';
		var height = $(window).height() - 82;

		// Ensure width is no smaller than 900px
		if( $(window).width() < 900 )
		{
			width = '900px';
		}

		// Ensure height is no smaller than 550px
		if( height < 510 )
		{
			height = 510;
		}

		$('#bodyContainer').css('width', width);
		$('#flashContainer').css('height', height + 'px');

	});

	$(window).resize();

	//
	// Initialise the flash
	//
	if( $('#flashContainer').length > 0 )
	{
		// Embed the flash movie
		var params = {
			wmode : "transparent",
			allowscriptaccess : "always",
			allowfullscreen : false
		};

		var flashvars = {
			dataGateway : 'http://' + document.location.host + '/flashService/gateway/',
			imagePath : 'http://' + document.location.host + '',
			mainSwfPath : '/flash/encounters_1070x634.swf',
			omnitureTracking: 'live'
		};

		// If an author id is present in the query string, then this page was reached via a link in an email
		// So add a flashvar to indicate that.
		if( $.query.get('authorid').toString().length > 0 )
		{
			flashvars['sourceEmail'] = true;
		}

		// If a cid is present in the query string, then this page was reached via a banner
		// So add a flashvar to indicate that.
		if( $.query.get('cid').toString().length > 0 )
		{
			flashvars['sourceBanner'] = true;
		}
		
		var urlMatch = window.location.pathname.match('/encounter/([0-9]+)/')
		if( null != urlMatch )
		{
			var encounterId = urlMatch[1];
			flashvars['encounterId'] = encounterId;

			// Redirect to latest screen
			window.location = window.location + '#/Latest';
		}

		var attributes = {
			id: "encountersFlash"
		};

		swfobject.embedSWF("/flash/loader.swf",
			"accessibleContainer",
			"100%",
			"100%",
			"9.0.0",
			"",
			flashvars,
			params,
			attributes,
			function(e)
			{
				// Make a reference to the flash movie object
				flashMovie =  $('#flashContainer object')[0];

				if( true == e.success )
				{
					// Resize flash on load
					$(window).resize();

					// Trigger Omniture tracking for flash homepage
					omnitureTracking.home();
				}
				else
				{
					// Trigger Omniture tracking for the accessible homepage
					omnitureTracking.accessible();
				}
			}
		);
	}

	//
	// Declare variables
	//

	// LeadCapture object
	leadCapture = new LeadCapture(
		function(data)
		{
			// Pass success of lead capture back to flash movie
			flashMovie.leadCaptureCompleted(data.success);
		},
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

	// ShareFriend object
	shareFriend = new ShareFriend(
		function(data)
		{
			// Pass success of share friend back to flash movie
			flashMovie.closeShareFriend(data.success);
		},
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

	// ShareOptions object
	shareOptions = new ShareOptions(
		function()
		{
			// Show the 'Refer Friends' hover
			shareFriend.show();
		},
		function()
		{
			// Tell flash to return to the 'Create Encounters' screen
			flashMovie.goToCreate();
		},
		function()
		{
			// Tell flash that it should redirect to the 'Latest' encounters page
			flashMovie.goToLatest();
		},
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

	// FlickrChooser object
	flickrChooser = new FlickrChooser('flickrHoverContainer',
		function(data)
		{
			// Pass the image data back to flash and have it close the hover
			flashMovie.closeFlickr(data.id, data.url);
		},
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

	// Terms object
	terms = new Terms(
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

	// Tv object
	tv = new Tv(
		function()
		{
			// Tell flash that the hover's being closed
			flashMovie.closePopUp();
		}
	);

});


