(function ($) {
    $.fn.videobox = function (settings) {

        if (typeof settings == 'undefined')
            settings = {};
        $.videobox.show($(this[0]), settings);
        return this;
    };

    $.videobox = {

        defaults: {
            resizeDuration: 400,  // Duration of height and width resizing (ms)
            initialWidth: 250,    // Initial width of the box (px)
            initialHeight: 250,    // Initial height of the box (px)
            width: 425,    // Default width of the box (px)
            height: 350,  // Default height of the box (px)
            autoplay: false,
            flvplayer: '/kodakgcg/kodakgcgpages/scripts/flvplayer.swf',
            overlayTemplate: '<div id="lbOverlay_video"></div>',
            contentTemplate: '<div id="lbCenter" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);"></div>',
            bottomContentTemplate: '<div id="lbBottomContainer" ><div id="lbBottom"><a id="lbCloseLink" href="#"></a><div id="lbCaption_video"></div><div id="lbNumber"></div><div style="clear: both;"></div></div></div>'
        },

        uiElements: {},

        init: function (options) {

            $.videobox.defaults = jQuery.extend(options, $.videobox.defaults);

            $('body').click(function (event) {

                var el = $(event.target).get(0);

                if (typeof el != 'undefined' && el)
                    el = $(event.target).parents('a').length > 0 ? $(event.target).parents('a').get(0) : el;


                if (el.tagName.toLowerCase() == 'a') {
                    return $.videobox.show($(el), $.videobox.defaults);
                }
            });

            $.videobox.createUI();
        },

        close: function () {
            var overlay = $.videobox.uiElements.overlay
			, center = $.videobox.uiElements.content
			, bottomContainer = $.videobox.uiElements.bottom;

            overlay.fadeTo("fast", 0, function () { $(this).css('display', 'none'); /*$.videobox.destroy();*/ });
            center.css('display', 'none');
            bottomContainer.css('display', 'none');
            center.html('');

            $('object, embed').each(function () {
                $(this).css('visibility', 'visible');
            });

            return false;
        },

        destroy: function () {

            $.videobox.uiElements.overlay.remove();
            $.videobox.uiElements.content.remove();
            $.videobox.uiElements.bottom.remove();
        },

        createUI: function () {
            var options = $.videobox.defaults;

            /* Create the overlay */
            $.videobox.uiElements.overlay = $(options.overlayTemplate).hide().click($.videobox.close).appendTo('body');
            $.videobox.uiElements.content = $(options.contentTemplate).css({
                width: options.initialWidth + 'px',
                height: options.initialHeight + 'px',
                display: 'none'
            })
		 															.appendTo('body');
            $.videobox.uiElements.bottom = $(options.bottomContentTemplate)
		 									.hide()
											.find('#lbCloseLink')
												.click($.videobox.close)
											.end()
											.appendTo('body');
        },

        setupUI: function (obj, width, height) {

            /* copy data from teh show method */
            $.extend(this, obj);

            insert = function () {
                var obj = $.videobox;

                var content = obj.uiElements.content;
                var bottomContainer = obj.uiElements.bottom;

                content.css('background', '#fff');
                obj.flash ? obj.so.write(content.attr("id")) : content.html(obj.other);
                bottomContainer.css({ 'top': (obj.top + content.height() + 10) + "px", 'left': content.css('left'), 'width': obj.width + 'px' });
                bottomContainer.slideDown('slow');
            };

            /* create the ui if it has not been already created */
            if (typeof ($.videobox.uiElements.overlay) == 'undefined')
                createUI();

            if (typeof (this.title) != 'undefined')
                $.videobox.uiElements.bottom.find('#lbCaption_video').html(this.title);

            $.videobox.uiElements.overlay.css({ 'top': $(window).scrollTop() + 'px', 'height': $(window).height() + 'px' });
            this.width = width;
            this.height = height;
            this.top = $(window).scrollTop() + (($(window).height() / 2) - (height / 2)) - 25;
            this.left = (($(window).width() / 2) - (width / 2));

            $('#lbCenter').css({ top: this.top + 'px'
						   		, left: this.left + 'px'
								, display: 'none'
								, background: '#fff url(css/loading.gif) no-repeat center'
								, height: this.height + 'px'
								, width: this.width + 'px'
            });

            // hide all the flash objects in the page
            $('object, embed').each(function () {
                $(this).css('visibility', 'hidden');
            });
            //done hiding the flash elements

            $('#lbOverlay_video').css('display', 'block').fadeTo("fast", 0.8);

            $('#lbCenter').fadeIn('slow', function () { insert(); });
        },


        show: function (el, options) {

            this.options = options;

            var link = el.attr('href');

            if (link.length == 0) {
                return true; /* so the default action for the event executes */
            }

            if (el.attr('title'))
                this.title = el.attr('title');
            else if (el.attr('alt'))
                this.title = el.attr('alt');

            var relAttr = el.attr('rel');
			relAttr = !relAttr ? relAttr='':relAttr;
            var width = $.videobox.defaults.width;
            var height = $.videobox.defaults.height;
            var autoplay = $.videobox.defaults.autoplay;

            if (relAttr.toLowerCase().indexOf('leavemealone') > -1) {
                return true;
            }

            if (relAttr.toLowerCase().indexOf('vidbox') >= 0) {
                var aDim = relAttr.match(/[0-9]+/g);
                width = (aDim && (aDim[0] > 0)) ? aDim[0] : $.videobox.defaults.width;
                height = (aDim && (aDim[1] > 0)) ? aDim[1] : $.videobox.defaults.height;

                if (relAttr.match(/autoplay/ig))
                    autoplay = true;
            }

            $.videobox.getVideoHtml(link, width, height, autoplay);

            if (this.isValid) {
                $.videobox.setupUI(this, width, height);
                //$('body').append(this.so.getSWFHTML());
                return false;
            }
            else
                return true;
        },

        getVideoHtml: function (sLinkHref, width, height, autoplay) {

            this.isValid = true;
            if (sLinkHref.match(/youtube\.com\/watch/i)) {
                this.flash = true;
                var hRef = sLinkHref;
                var videoId = hRef.split('=');
                this.videoID = videoId[1];
                this.so = new SWFObject("http://www.youtube.com/v/" + this.videoID, "flvvideo", width, this.options.height, "0");
                this.so.addParam("wmode", "transparent");
            }
            else if (sLinkHref.match(/metacafe\.com\/watch/i)) {
                this.flash = true;
                var hRef = sLinkHref;
                var videoId = hRef.split('/');
                this.videoID = videoId[4];
                this.so = new SWFObject("http://www.metacafe.com/fplayer/" + this.videoID + "/.swf", "flvvideo", width, height, "0");
                this.so.addParam("wmode", "transparent");
            }
            else if (sLinkHref.match(/google\.com\/videoplay/i)) {
                this.flash = true;
                var hRef = sLinkHref;
                var videoId = hRef.split('=');
                this.videoID = videoId[1];
                this.so = new SWFObject("http://video.google.com/googleplayer.swf?docId=" + this.videoID + "&hl=en", "flvvideo", width, height, "0");
                this.so.addParam("wmode", "transparent");
            }
            else if (sLinkHref.match(/ifilm\.com\/video/i)) {
                this.flash = true;
                var hRef = sLinkHref;
                var videoId = hRef.split('video/');
                this.videoID = videoId[1];
                this.so = new SWFObject("http://www.ifilm.com/efp", "flvvideo", width, height, "0", "#000");
                this.so.addVariable("flvbaseclip", this.videoID + "&");
                this.so.addParam("wmode", "transparent");
            }
            else if (sLinkHref.match(/\.mov/i)) {
                this.flash = false;
                if (navigator.plugins && navigator.plugins.length) {
                    this.other = '<object id="qtboxMovie" type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="' + sLinkHref + '" width="' + width + '" height="' + height + '"><param name="src" value="' + sLinkHref + '" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="' + autoplay + '" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
                } else {
                    this.other = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="' + width + '" height="' + height + '" id="qtboxMovie"><param name="src" value="' + sLinkHref + '" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="' + autoplay + '" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
                }
            }
            else if (sLinkHref.match(/\.wmv/i) || sLinkHref.match(/\.asx/i)) {
                this.flash = false;
                this.other = '<object NAME="Player" WIDTH="' + width + '" HEIGHT="' + height + '" align="left" hspace="0" type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><param NAME="URL" VALUE="' + sLinkHref + '"><param><param NAME="AUTOSTART" VALUE="' + autoplay + '"></param><param name="showControls" value="true"></param><embed WIDTH="' + width + '" HEIGHT="' + height + '" align="left" hspace="0" SRC="' + sLinkHref + '" TYPE="application/x-oleobject" AUTOSTART="false"></embed></object>'
            }
            else if (sLinkHref.match(/\.flv/i)) {
                this.flash = true;
                this.so = new SWFObject(this.defaults.flvplayer + "?file=" + sLinkHref, "flvvideo", width, height, "0", "#000");
                this.so.addVariable("autostart", autoplay);
                this.so.addParam("allowFullScreen", "true");
            }
            else if (sLinkHref.match(/\.swf/i)) {
                this.flash = true;
                this.so = new SWFObject(sLinkHref, "flvvideo", width, height, "0", "#000");
                this.so.addVariable("autostart", autoplay);
            }
            else {
                this.isValid = false;
            }

        }
    }
    $(function () { $.videobox.init({}) });

})(jQuery);
