﻿/*
* JQuery plugin - absolute positioned splash screen
* Suited for swf movies, jpg, gif and png images
* Tested on IE7, IE8, FF3
* Scripted by Internetfabriken AB 
* 2009-06-14 initial version Joacim Ahlberg
* 
**/
(function($) {
    $.fn.splashScreen = function(settings) {

        settings = jQuery.extend({
            overlayBgColor: '#000', // overlay color
            overlayOpacity: 0.6,    // overlay opacity between 0 and 1
            imageLoading: '/img/splash/loading.gif',     // link to loading anim pic
            imageArrow: '/img/splash/icon_arrow.gif',     // link to loading anim pic
            imageBtnClose: '/img/splash/icon_close.gif', // link to close button pic
            mediaFile: '/img/splash/pic_dummy.jpg', // link to pic or movie
            linkURL: '#',
            linkText: '',
            mediaFileWidth: 480,  // Movie width
            mediaFileHeight: 270, // Movie height
            splashTop: 125,     // Position of splash screen
            splashLeft: 210    // Position of splash screen
        }, settings);

        var jQueryMatchedObj = this;

        /**
        * Initializing the plugin calling the start function
        *
        * @return boolean false
        */
        function _initialize() {
            _start(this, jQueryMatchedObj); // This, in this context, refer to object (link) which the user have clicked
            return false; // Avoid the browser following the link
        }

        /**
        * Start the jQuery lightBox plugin
        *
        * @param object objClicked The object (link) whick the user have clicked
        * @param object jQueryMatchedObj The jQuery object with all elements matched
        */
        function _start(objClicked, jQueryMatchedObj) {
            // Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
            $('embed, object, select').css({ 'visibility': 'hidden' });
            // Call the function to create the markup structure; style some elements; assign events in some elements.
            _set_interface();
        }

        function _set_interface() {

            // Use correct markup for images vs Flash movies
            if (String(settings.mediaFile).toLowerCase().match("swf$") == null) {
                var mediaHTML = '<div id="splash-div"><img src="' + settings.mediaFile + '" alt="Banner" /></div>';
            } else {
                var mediaHTML = '<object id="splashMovie" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' + settings.mediaFileWidth + '" HEIGHT="' + settings.mediaFileHeight + '" id="simplemovie" ALIGN=""><PARAM NAME=movie VALUE="' + settings.mediaFile + '"><PARAM NAME=quality VALUE=medium><PARAM NAME=bgcolor VALUE=#FFFFFF><embed src="' + settings.mediaFile + '" width="' + settings.mediaFileWidth + '" height="' + settings.mediaFileHeight + '" type="application/x-shockwave-flash" scale="showall" play="true" loop="true" menu="true" wmode="Window" quality="1"></embed></object>';
            }
            // Apply the HTML markup into body tag
            //$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-top"><div id="lightbox-container-top-showtext"><img src="' + settings.imageArrow + '" alt="Arrow" /><a href="#">Visa ej igen efter start</a></div><div id="lightbox-container-top-close"><div id="lightbox-container-top-close-icon"><a href="#"><img src="' + settings.imageBtnClose + '" alt="as" /></a></div><div id="lightbox-container-top-close-text"><a href="#">Stäng fönster</a></div></div></div><div id="lightbox-container-image-box">' + mediaHTML + '<div id="lightbox-loading"><img src="' + settings.imageLoading + '" alt="loading..." /></div></div><div id="lightbox-container-bottom"><img src="' + settings.imageArrow + '" alt="Pil" /><a href="' + settings.linkURL + '">' + settings.linkText + '</a></div></div>');
            $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-top"><div id="lightbox-container-top-showtext"><img src="' + settings.imageArrow + '" alt="Arrow" /><a href="#">Visa ej igen</a></div><div id="lightbox-container-top-close"><div id="lightbox-container-top-close-icon"><a href="#"><img src="' + settings.imageBtnClose + '" alt="as" /></a></div><div id="lightbox-container-top-close-text"><a href="#">Stäng fönster</a></div></div></div><div id="lightbox-container-image-box">' + mediaHTML + '<div id="lightbox-loading"><img src="' + settings.imageLoading + '" alt="loading..." /></div></div><div id="lightbox-container-bottom">&nbsp;</div></div>');
            // Style overlay and show it
            $('#jquery-overlay').css({
                backgroundColor: settings.overlayBgColor,
                opacity: settings.overlayOpacity,
                width: "100%", // Call method for quirk mode width calculation if needed
                height: "100%" // Call method for quirk mode width calculation if needed 
            }).fadeIn();

            $('embed, object, select').css({ 'visibility': 'visible' });

            // Calculate top and left offset for the jquery-lightbox div object and show it
            $('#jquery-lightbox').css({
                top: settings.splashTop,    // Call method for top calculation if needed
                left: settings.splashLeft   // Call method for left calculation if needed
            }).show();

            // Assigning click event to close overlay
            $('#lightbox-container-top-close a').click(function() {
                _finish();
            });

            // Assigning click event to navigate to url
            //$('#lightbox-container-bottom a').click(function() {
            //    _finish();
            //});

            // Assigning click event to create no-show cookie
            $('#lightbox-container-top-showtext a').click(function() {
                $.cookie('HideSplash', "hideme", { expires: 30 });
                _finish();
            });

        }

        /**
        * Remove jQuery lightBox plugin HTML markup
        *
        */
        function _finish() {
            // Added quick fix _stop to cope with IE not stopping audio
            try {
                _stop();
            }
            catch (err) { }
            $('#jquery-lightbox').remove();
            $('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
            // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
            $('embed, object, select').css({ 'visibility': 'visible' });
        }

        // Added fix _stop to cope with IE not stopping audio
        function _stop() {
            if (window.document["splashMovie"]) {
                window.document["splashMovie"].stop();
            }
            if (navigator.appName.indexOf("Microsoft Internet") == -1) {
                if (document.embeds && document.embeds["splashMovie"])
                    document.embeds["splashMovie"].stop();
            }
            else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
            {
                if (document.getElementById("splashMovie")) {
                    document.getElementById("splashMovie").stop();
                }
            }
        }


        return this.unbind('click').click(_initialize);
    };
})(jQuery);