﻿(function ($) {
    $.fn.Video = function (filelocation, options) {
        var defaults = {
            "flashplayerlocation": "/Scripts/JQuery/JWPlayer/player.swf",
            "wmode": "opaque",
            "allowscriptaccess": "always",
            "allownetworking": "all",
            "allowfullscreen": "true",
            "volume": "80",
            "bufferlength": "5",
            "quality": "false",
            "hidelogo": "false",
            "plugins": "",
            "shuffle": "false",
            "width": "475",
            "height": "380",
            "isInWideScreenFormat": false,
            "isAudioFile": false,
            link: true,
            "linktarget": "_self",
            "controlbar": "bottom",
            "image": "",
            "captionsfile": ""
        };
        var options = $.extend(defaults, options);
        return this.each(function () {
            if (options.isInWideScreenFormat) {
                options.height = 292;
            }
            if (options.isAudioFile) {
                options.height = 100;
            }
            var flashVars = {
                "flashplayer": options.flashplayerlocation,
                "wmode": options.wmode,
                "allowscriptaccess": options.allowscriptaccess,
                "allownetworking": options.allownetworking,
                "allowfullscreen": options.allowfullscreen,
                "volume": options.volume,
                "bufferlength": options.bufferlength,
                "quality": options.quality,
                "logo.hide": options.hidelogo,
                "shuffle": options.shuffle,
                "width": options.width,
                "height": options.height,
                "link": options.link,
                "controlbar": options.controlbar,
                "linktarget": options.linktarget,
                "duration": options.duration,
                "file": filelocation,
                "image": options.image
            };
            if (options.captionsfile != "") {
                flashVars["plugins"] = {
                    "captions-2": {
                        file: options.captionsfile,
                        fontsize: "14"
                    },
                    "sharing-3": {
                        code: "%3Cembed%20src%3D%22" + encodeURIComponent("http://" + window.location.hostname + filelocation) + "%22%20width%3D%22475%22%20height%3D%22" + options.height + "%22%20allowfullscreen%3D%22true%22%20%2F%3E",
                        link: "http://" + window.location.hostname + options.link
                    }
                };
            }
            else {
                flashVars["plugins"] = {
                    "sharing-3": {
                        code: "%3Cembed%20src%3D%22" + encodeURIComponent("http://" + window.location.hostname + filelocation) + "%22%20width%3D%22475%22%20height%3D%22" + options.height + "%22%20allowfullscreen%3D%22true%22%20%2F%3E",
                        link: "http://" + window.location.hostname + options.link
                    }
                };
            }
            jwplayer($(this).attr("id")).setup(flashVars);
        });
    };
})(jQuery);
