(function($) {
    $.fn.videoize = function(opts) {
        if (!swfobject) return;

        $.fn.videoize.defaults = {
            videoClass: 'video', // Wrapper class.
            player: '/virtua/flash/video.swf',   // Path to Flash player.
            flashVersion: "9", // Minimum Flash version
            autostart: false, // Automatically start the video
            float: false, // Shove the image/video to the left or right edge of the parent. If this is false, get the alignment from the image.
            floatClass: true, // If this is true, use a "left" and "right" class instead of an inline style for set alignment.
            target: 'body',
            useMask: true,
            maskClass: 'gallery-mask',
            maskTarget: true,
            maskOpacity: '0.8',
            indexMaskOpacity: '0.7',
            useShadows: true,
            videoZ: 50000,
            maskZ: 49999
        };
        var opts = $.extend({}, $.fn.videoize.defaults, opts);

        return this.each(function() {
            if ($(this).get(0).tagName != "A") return;

            var img = $(this).children("img:first");
            var a = $(this);

            var o = $.metadata ? $.extend({}, opts, a.metadata()) : opts;

            if (!o.player) return;

             /*if (img.length) { // We're embedding an in-content player
                $(img).load(function() {
                    var xmlURL = escape(a.attr("href") + "?xml=1&img=" + escape($(this).attr("src")) + "&h=" + $(this).height() + "&w=" + $(this).width());
                    a.replaceWith($(this).remove());
                    // Get the alignment setting from the image if none was passed to the javascript.
                    var float = o.float;
                    if (!o.float) {
                        if ($(this).hasClass("left")) float = "left";
                        else if ($(this).hasClass("right")) float = "right";
                        else float = "none";
                    }
                    Convert($(this), o.player, xmlURL, o.flashVersion, float, o.floatClass, o.videoClass, null, $(this).width(), $(this).height());
                }).each(function() {
                    if (this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) $(this).trigger("load");
                });

            } else */{ // We're overlaying the video on top of the page.
                // We don't know the dimensions of the video (since we'd ordinarily get it from the image), so we'll need to get it from the target page.

                var xmlURL = escape(a.attr("href") + "?xml=1");
                $.get(a.attr("href"), { xml: 1 }, function(data, textStatus, XMLHttpRequest) {
                    if (textStatus == "success") {

                        if (data.documentElement.tagName != "vidVars") return;
                        var docEl = data.documentElement;
                        for (var x = 0; x < docEl.childNodes.length; x++) {
                            var node = docEl.childNodes[x];
                            switch (node.tagName) {
                                case "vHeight":
                                    a.data("height", $(node).text());
                                    break;
                                case "vWidth":
                                    a.data("width", $(node).text());
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }, "xml");
                a.click(function() {
                    var vHeight = parseInt($(this).data("height"));
                    var vWidth = parseInt($(this).data("width"));
                    var vTop = 0;
                    var vLeft = 0;

                    if (!vHeight || !vWidth) return true;

                    var maskContainer = opts.maskTarget ? $(opts.target) : $("body");
                    var windowHeight = $(window).height();
                    var windowWidth = $(window).width();
                    var docHeight = $(document).height();
                    var docWidth = $(document).width();
                    var scrollTop = $(document).scrollTop();

                    vTop = (windowHeight / 2 - vHeight / 2) + scrollTop;
                    vLeft = windowWidth / 2 - vWidth / 2;

                    var videoID = "videoize-" + Math.floor(Math.random() * 999999);
                    var videoContainer = $('<div class="video-wrapper" id="wrapper-' + videoID + '"><a class="video-close" /><div id="' + videoID + '" /></div>').css({ position: 'absolute', left: vLeft, top: vTop, zIndex: opts.videoZ, height: vHeight, width: vWidth, opacity: 0 });

                    maskContainer.append(videoContainer);

                    if (opts.useMask) {
                        var mask = $('<div />').addClass(opts.maskClass);
                        var maskContainer = opts.maskTarget ? $(opts.target) : $("body");
                        mask.css({
                            position: "absolute",
                            top: maskContainer.css("padding-top"),
                            left: maskContainer.css("padding-left"),
                            opacity: "0",
                            width: maskContainer.width(),
                            height: maskContainer.height(),
                            zIndex: opts.maskZ
                        }).click(function() { $(this).fadeOut("normal", function() { $(this).remove(); $('#wrapper-' + videoID).remove(); }); });
                        maskContainer.append(mask);
                        mask.fadeTo("normal", opts.maskOpacity); //, function() { slidershow.fadeIn("fast"); });
                    }
                    videoContainer.children("a.video-close").click(function() {
                        if (opts.useMask) mask.click();
                        else videoContainer.remove();
                    }).hover(
                        function() { $(this).addClass("hover") },
                        function() { $(this).removeClass("hover"); }
                    );
                    Convert(null, o.player, xmlURL, o.flashVersion, null, o.floatClass, o.videoClass, videoID, vWidth, vHeight);
                    $("#wrapper-" + videoID).fadeTo("fast", 1);

                    return false;
                });
            }
        });

        function Convert(img, player, xmlURL, flashVersion, float, floatClass, videoClass, videoID, width, height) {
            if (img) {
                img = $(img);
                var id = img.attr("id");
                if (!id) {
                    id = "videoize-" + Math.floor(Math.random() * 999999);
                }
                var imgWrapper = $('<div id="' + id + '"></div>');
                img.wrap(imgWrapper);
                imgWrapper = $('#' + id);

                var attributes = {};
                if (floatClass && (float == "left" || float == "right")) videoClass += " " + float;
                else attributes["style"] = "float: " + float;
                videoClass = $.trim(videoClass);
                if (videoClass) attributes["class"] = videoClass;

                var wrapperID = id + "-parent";
                var wrapper = $('<div id="' + wrapperID + '"></div>').css({ "height": height, "width": width }).addClass(videoClass);
                imgWrapper.wrap(wrapper);
            } else {
                var id = videoID;
            }
            var params = { 'scale': 'noscale', 'wmode': 'transparent', 'salign': 'TL' };
            var flashvars = { xmlUrl: xmlURL };
            var expressInstallSwfurl = "";

            swfobject.embedSWF(player, id, width, height, flashVersion, expressInstallSwfurl, flashvars, params, attributes);
        }
    }
})(jQuery);

