/**
 * jQuery overlay plugin - swf first then to extend for images etc.
 * I still need to code to add the swfObject parameters as optional settings
*/

(function($) { 
	$.fn.swfBox = function(settings) {
		
		// DEFAULT SETTINGS
		settings = jQuery.extend({
			// Default swf
			swfToLoad:			'Flash/APOverlay/apOverlay.swf',	// (string) A default swf file
			swfVersion:			'8',								// (string) The version of Flash that's required for this swf
			swfWidth:			'900',								// (string) This default is set for FGW homepage Flash
			swfHeight:			'600',								// (string) This default is set for FGW homepage Flash
			
			// Overlay defaults
			overlayBgColor: 	'#181236',								// (string) Default background colour hex
			overlayOpacity:		0.85,								// (integer) Opacity of the overlay. Values from 0 to 1.0 in 0.1 increments
			
			// Imagery surrounding the swf
			imageLoading:		'Images/Overlay/swfLoading.gif',	// (string) Path to loading image
			imageClose:			'Images/Overlay/swfClose.gif',		// (string) Path to close button
			
			// Display values for the display container
			containerPadding:				10,						// (integer) Border-width
			containerResizeSpeed:			400,					// (integer) Resize speed
			containerBgColour:				'#000',					// (string)  Background colour for the container
			containerAdditionalTopOffset:	52						// (integer) (px) Additional top offset to position the container
			
		},settings);
		// END DEFAULT SETTINGS
	
		// Caching the jQuery object with all elements matched
		var jQueryMatchedObj = this; // this = the jQuery object that we've just set up using either the default settings above or the parameters from the call
		
		// START initialise function
		// Start everything off by calling the swfBoxStart function and making sure the link doesn't open elsewhere
		function initialise (){
			swfBoxStart(this, jQueryMatchedObj); // this = The object that fired the event to call this plugin
			return false;
		};
		// END initialise function
		
		// START swfBoxStart function - the main function for the plugin
		function swfBoxStart(selectedObject, jQueryMatchedObj) {
			// Overlays in IE can cause issues with some items so let's hide them
			$('embed, object, select').css({'visibility': 'hidden'});			
			
			// Set up the additional HTML that's required
			setupHtml();
			setupSwf(settings.swfToLoad);
			
			// Make sure something is available for us to use
			if (jQueryMatchedObj.length == 1) {
				
				// Get the href of the link - this is the swf we need to load
				// Hard coded for now
				
				//if (selectedObject.getAttribute('href').length > 0) {
				//	swfToLoad = selectedObject.getAttribute('href');
				//} else {
					//swfToLoad = settings.swfToLoad;
				//}
			}
		};
		// END swfBoxStart function
		
		// START setupHtml function - wrap the swf and set up the overlay structure
		function setupHtml() {
			
			$('body').append('<div id="swfLoadContainer"><img src="' + settings.imageLoading + '" alt="Loading" width="32" height="32" /></div><div id="overlayContainer"></div><div id="swfContainer"><img src="Flash/APOverlay/overlayNonFlash.gif" alt="Big Ticket. Small Fare." width="867" height="383" style="margin-top: 60px;" /></div><div id="overlayButtonContainer"><a href="default.aspx" id="closeButton" class="clearfix"><img src="Flash/APOverlay/return.gif" alt="Return to website" width="143" height="31" /></a><a href="BuyTickets.aspx" class="clearfix"><img src="Flash/APOverlay/buyNow.gif" alt="Buy Now" width="100" height="32" id="forwardButton" /></a></div>');
			
			// Show the overlay container
			$('#overlayContainer').css({
				width: Math.max($(document).width(), $(window).width()),
				height: Math.max($(document).height(), $(window).height()),
				backgroundColor: settings.overlayBgColor,
				opacity: settings.overlayOpacity
			}).fadeIn();
			
			// Show the swf container
			var offsetTop	= settings.containerAdditionalTopOffset + $('body').scrollTop();
			
			var bodyWidth = $('body').width();
			var offsetLeft = $('#mainNavigation').offset().left;
			var deltaWidth = bodyWidth % settings.swfWidth;
			
			$('#swfLoadContainer').css({
				top:	offsetTop + 'px'
			}).show();
			
			// Position this offset.left and subtract the difference between the width of the page and the width of teh container
			$('#swfContainer').css({
				top:	offsetTop + 'px',
				left:	$('#siteWelcome h1').offset().top - (offsetLeft - deltaWidth) + 'px',
				width:	settings.swfWidth + 'px',
				height:	settings.swfHeight + 'px'
			}).show();
			
			// Need to sort this once I know what the offset top should be.
			$('#overlayButtonContainer').css({
				top:	offsetTop + 'px'
			}).show();
			
			// Allow clicking anywhere on the page to close the overlay
			$('#overlayContainer, #swfContainer').click(function() {
				endOverlay();
			});
			
			// Clicking on the load container should stop the link action, too.
			$('#swfLoadContainer, #closeButton').click(function() {
				endOverlay();
				return false;
			});
			
			// If the window is resized, then reset
			$(window).resize(function() {
				offsetTop	= settings.containerAdditionalTopOffset + $('body').scrollTop();

				bodyWidth = $('body').width();
				offsetLeft = $('#mainNavigation').offset().left;
				deltaWidth = bodyWidth % settings.swfWidth;
				
				$('#overlayContainer').css({
					width: Math.max($(document).width(), $(window).width()),
					height: Math.max($(document).width(), $(window).width())
				});
				
				$('#swfContainer').css({
					top:	offsetTop + 'px',
					left:	$('#siteWelcome h1').offset().top - (offsetLeft - deltaWidth) + 'px'
				});
			});			
		};
		// END setupHtml function
		
		// START enableKeyClose function
		function enableKeyClose() {
			$(document).keydown(function(objEvent) {
				endOverlay();
			});
		};
		// END enableKeyClose function
		
		// START setupSwf function - set the swf details using swfObject
		function setupSwf(swfToLoad) {
			// Show the loading animation while the swf is loading
			$('#swfLoadContainer').show();
			
			// Set up swfObject - At some point I need to code the parameters as settings
			var theSwfObject = new SWFObject(swfToLoad, "swfId", settings.swfWidth, settings.swfHeight, settings.swfVersion, settings.containerBgColour);
			theSwfObject.addParam("wmode", "transparent");
			theSwfObject.addParam("align", "middle");
			
			// Show the swf
			showSwf(theSwfObject);
		};
		// END setupSwf function
		
		// START showSwf function - finally! Write the swf to the screen
		function showSwf(theSwfObject) {
			// Hide the loading image
			$('#swfLoadContainer').hide();
			
			// Make sure keyboard users can close the overlay
			enableKeyClose();
			
			// Show the overlay swf
			theSwfObject.write("swfContainer");
			
		};
		// END showSwf function
		
		// START endOverlay function
		function endOverlay() {
			$('#swfLoadContainer').remove();
			$('#swfContainer').remove();
			$('#overlayButtonContainer').remove();
			$('#overlayContainer').fadeOut(function() {$('#overlayContainer').remove();});
			
			// Make the elements we had to remove visible again
			$('embed, object, select').css({'visibility': 'visible'});
		};
		// END endOverlay function
			
		return this.unbind('click').click(initialise);
	};
})(jQuery);
