(function ($) {
    jQuery.fn.extend({
        VideoPlayer: function (newopts) {
            this.each(function () {
                var $this = $(this);
                if (!$this.is(":visible") || $this.data("videoinit")) {
                    return;
                }
                $this.data("videoinit", true);

                var opts = $.extend({
                    clipUrl: "",
                    width: "",
                    height: "324",
                    autoPlay: true,
                    swfSrc: "/js/flowplayer-3.2.5.swf"
                }, newopts, $this.ParseClass(true));

                var videoEle = $this.find("div.videoContainer").hover(function () {
                    $(this).addClass("hover");
                }, function () {
                    $(this).removeClass("hover");
                });
                if (opts.width != '') {
                    videoEle.css('width', opts.width + 'px');
                }
                if (opts.height != '') {
                    videoEle.css('height', opts.height + 'px');
                }
                $this.css({ "position": "relative" });
                var playerInst,
				initPlayer = function () {
				    playerInst = $f(videoEle[0], { src: opts.swfSrc, wmode: "transparent" }, {
				        clip: {
				            url: opts.clipUrl,
				            autoPlay: opts.autoPlay,
				            tags: opts.Tags,
				            onFinish: function (clip) {
				                $this.find("div.endCard").css('visibility', 'visible');
				                videoEle.fadeOut();
				            },
				            onStart: function (clip) {
				                $this.trigger("TrackWT");
				            }
				        },
				        play: {
				            label: null,
				            replayLabel: "click to play again"
				        }
				    });
				};
                initPlayer();

                $(".replay").live("click", function () {
                    if ($(this).parents().filter(function () { return (this === $this[0]) })) {
                        videoEle.show();
                        $this.find("div.endCard").css("visibility", "hidden");
                        playerInst.play();
                    }
                });
                $(".reloadVideo").live("click", function () {
                    var _this = $(this),
					box = $this.parents("div.videoPlayer:first")[0];
                    if (_this.parents().filter(function () { return (this === box) }).length) {
                        $.post(_this.attr("href") + "&FormDialog=true", function (data) {
                            videoEle.show();
                            var newEle = $(data);
                            $.extend(opts, newEle.find("div.videoObject").ParseClass(true));
                            $this.find("div.endCard").replaceWith(newEle.find("div.endCard"));
                            $this.parents("div.videoPlayer:first").find("div.videoInfo").replaceWith(newEle.find("div.videoInfo"));

                            playerInst.play(opts.clipUrl);

                            newEle.trigger("TrackWT");
                        });
                        return false;
                    }
                });

				$this.closest(".ui-dialog").bind("dialogbeforeclose", function() {
                    playerInst.unload();
                });
            });
            return this;
        }
    });

    $.VideoPlayer = {
        init: function (sel) {
            $(sel || "div.videoObject").VideoPlayer();

        }
    };
    $(document).ready(function () {
        $.VideoPlayer.init();
    });
})(jQuery);
