(function($) {
    $.fn.videoize = function(opts) {
        
        if (!swfobject) return;
        
        $.fn.videoize.defaults = {
            plain: true, // Don't add captions or any of the classes to dress up the image. Just wait for a click.  Ignored if "immediate" is true.
            immediate: true, // Immediately transform into a video or leave the original image in place to wait for a click.
            useAltAsCaption: false, // Ignored if "plain" is true.
            hoverClass: 'hover', // Ignored if "plain" is true.
            captionClass: 'caption', // Ignored if "plain" is true.
            videoClass: 'video', // Wrapper class.
            flv: false, // Path to FLV.  If false, will look for path as metadata (requires metadata plugin).
            player: '/common-flash/in-content-video-player.swf',   // Path to Flash player.
            flashVersion: "9", // Minimum Flash version
            autoplay: false, // Automatically play 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.
        };
        var opts = $.extend({}, $.fn.videoize.defaults, opts);

        return this.each(function() {
            if (this.tagName != "IMG") return;
            
            var o = $.metadata ? $.extend({}, opts, $(this).metadata()) : opts;

            if (!o.player) return;
            
            var img = $(this);
            
            // Get the alignment setting from the image if none was passed to the javascript.
            var float = o.float;
            if (!o.float) {  
                if (img.hasClass("left")) float = "left";
                else if (img.hasClass("right")) float = "right";
                else float = "none";
            }

            if (o.immediate) Convert(this, o.player, o.playerSkin, o.autoplay, o.flv, o.flashVersion, float, o.floatClass, o.videoClass);
            else {
                $(this).hover(function() {
                    $(this).addClass(opts.hoverClass);
                }, function() {
                    $(this).removeClass(opts.hoverClass);
                }).click(function() {
                    Convert(this, o.player, o.playerSkin, o.autoplay, o.flv, o.flashVersion, float, o.floatClass, o.videoClass);
                });
            }
        });
        
        function Convert(img, player, playerSkin, playerAutoplay, flv, flashVersion, float, floatClass, videoClass) {
            img = $(img);
            var id = img.attr("id");
            if (!id) {
                id = "videoize-" + Math.floor(Math.random()*999999);
                //img.attr("id", id);
            }
            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 height = img.height();
            var width = img.width();
            
            var wrapperID = id + "-parent";
            var wrapper = $('<div id="' + wrapperID + '"></div>').css({ height: height, width: width }).addClass(videoClass);
            imgWrapper.wrap(wrapper);
            //return;
            var params = {wmode: 'transparent', scale: 'noscale', salign: 'tl'}, flashvars = {flv: flv, image: img.attr("src"), autoplay: playerAutoplay}, expressInstallSwfurl = "";
            swfobject.embedSWF(player, $.browser.msie ? wrapperID : id, width, height, flashVersion, expressInstallSwfurl, flashvars, params, attributes);
        }
    }
})(jQuery);
$(document).ready(function() {
    $("img[class*={flv:]").videoize();
});

