(function($) {
var pluginName = 'video';
var defaults = {
    'joinPanelId': null,
    'width': 820,
    'height': 460
};
var methods = {
    init : function(options) {
        if (typeof(jwplayer)==='undefined') {
            return(false);      // No JW Player is available
        }
        options = $.extend(defaults,options);
        var joinPanel = $('#'+options.joinPanelId);
        return(this.each(function(){
            var $this = $(this);
            var video = $this.attr('data-video');
            if (!video) {
                return(true);
            }
            var thumb = null;
            var img = $('img',$this).first();
            if (img) {
                thumb = img.attr('src');
            }
            var id = 'video'+Math.round(Math.random()*10000);
            var container = $('<div>');
            var vWrap = $('<div>').attr('id','v_'+id).appendTo(container);
            $('<div>').attr('id',id).appendTo(vWrap);
            if (joinPanel) {
                joinPanel.clone().attr('id','jp_'+id).hide().appendTo(container);
            }
            $this.empty().append(container);
            var config = {
                'id': id,
                'width': options.width,
                'height': options.height,
                'src': video,
                'thumb': thumb
            };
            jwplayer(config.id).setup({
                'flashplayer': 'player/player.swf',
                'id': config.id,
                'width': config.width,
                'height': config.height,
                'dock': false,
                'autostart': false,
                'allowFullScreen': false,
                'hasVersion': 9,
                'bgcolor': '#000000',
                'wmode': 'transparent',
                'modes': [
                    {type: 'flash', src: 'player/player.swf'},
                    {type: 'html5'}
                ],
                'skin': 'player/skin/lioness.zip',
                'file': config.src,
                'image': config.thumb,
                'provider': 'http',
                events: {
                    onComplete: function() {
                        var jp = $('#jp_'+this.id);
                        if (jp) {
                            $('#v_'+this.id).hide();
                            jp.show();
                        }
                    }
                }
            });
       }));
    },
    destroy : function() {
        return(this.each(function(){
            var $this = $(this);
            var data = $this.data(pluginName);
            $(window).unbind('.'+pluginName);
            data[pluginName].remove();
            $this.removeData(pluginName);
        }));
    }
};

$.fn[pluginName] = function(method) {
    if (methods[method]) {
        return(methods[method].apply(this,Array.prototype.slice.call(arguments,1)));
    } else if ((typeof(method)==='object') || (!method)) {
        return(methods.init.apply(this,arguments));
    } else {
        $.error('Method '+method+' does not exist on jQuery.'+pluginName);
    };
};

})(jQuery);

