(function($) {
    $.fn.swfBox = function(settings) {

        // DEFAULT SETTINGS
        settings = jQuery.extend({

            swfWidth: '420', 							            // (string) Width of movie
            swfHeight: '341',                                       // (string) Height of movie

            // Imagery surrounding the swf
            imageLoading: '/Assets/SiteImages/Overlay/swfLoading.gif',  // (string) Path to loading image
            imageClose: '/Assets/SiteImages/Overlay/swfClose.gif', 	    // (string) Path to close button

            // Overlay defaults
            overlayBgColor: '#666', 						        // (string) Default background colour hex
            overlayOpacity: 0.85, 							        // (integer) Opacity of the overlay. Values from 0 to 1.0 in 0.1 increments

            // Display values for the display container
            containerPadding: 10, 					                // (integer) Border-width
            containerResizeSpeed: 400, 				                // (integer) Resize speed
            containerBgColour: '#000' 				                // (string)  Background colour for 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, #commonBar img').css({ 'visibility': 'hidden' });
            
            // Make sure something is available for us to use
            // Get the href of the link - this is the swf we need to load
            if (selectedObject.getAttribute('href').length > 0) {
                swfToLoad = selectedObject.getAttribute('href');
            } else {
                return false;
            }

            // Set up the additional HTML that's required
            setupHtml(swfToLoad);
            setupSwf();
        };
        // END swfBoxStart function

        // START setupHtml function - wrap the swf and set up the overlay structure
        function setupHtml(swfToLoad) {

            $('body').append('<div id="swfLoadContainer"><img src="' + settings.imageLoading + '" alt="Loading" width="32" height="32" /></div><div id="overlayContainer"></div><div id="swfContainer"><object width="420" height="341"><param name="movie" value="' + swfToLoad + '" /><param name="allowFullScreen" value="true" /><embed src="' + swfToLoad + '" type="application/x-shockwave-flash" allowfullscreen="true" width="420" height="341" /></object></div><div id="overlayButtonContainer"><img src="' + settings.imageClose + '" alt="Close" width="72" height="23" /></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 deltaHeight = (($(window).height() - settings.swfHeight) / 2);
            var deltaWidth = ($('body').width() - settings.swfWidth) / 2;

            $('#swfLoadContainer').css({
                top: deltaHeight + '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: deltaHeight + $(window).scrollTop() + 'px',
                left: deltaWidth + 'px',
                width: settings.swfWidth + 'px',
                height: settings.swfHeight + 'px'
            }).show();

            $('#overlayButtonContainer').css({
                top: deltaHeight + $(window).scrollTop() + settings.swfHeight + 'px',
                left: (($('body').width() - settings.swfWidth) + 10) + '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, #overlayButtonContainer').click(function() {
                endOverlay();
                return false;
            });

            // If the window is resized, then reset
            $(window).resize(function() {
                var deltaHeight = (($(window).height() - settings.swfHeight) / 2);
                var deltaWidth = ($('body').width() - settings.swfWidth) / 2;

                $('#overlayContainer').css({
                    width: Math.max($(document).width(), $(window).width()),
                    height: Math.max($(document).width(), $(window).width())
                });

                $('#swfContainer').css({
                    top: deltaHeight + 'px',
                    left: 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() {
            // Show the loading animation while the swf is loading
            $('#swfLoadContainer').show();

            // Show the swf
            showSwf();
        };
        // END setupSwf function

        // START showSwf function - finally! Write the swf to the screen
        function showSwf() {
            // Hide the loading image
            $('#swfLoadContainer').hide();

            // Make sure keyboard users can close the overlay
            enableKeyClose();

            // Show the overlay swf
            $('#swfContainer').show();

        };
        // 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, #commonBar img').css({ 'visibility': 'visible' });
        };
        // END endOverlay function

        return this.unbind('click').click(initialise);
    };
})(jQuery);