(function ($) {
    $.fn.xcomMediaSlider = function (options) {
        var static_defaults = {
            auto_slide_interval: 0
        };

        var global_opts = $.extend(static_defaults, $.fn.xcomMediaSlider.defaults, options);

        return this.each(function () {
            var self = $(this);

            var opts = $.meta ? $.extend({}, global_opts, self.data()) : global_opts;

            init(self, opts);
        });
    };

    function init(context, opts) {
        // Find the active item
        var active = context.find(opts.item_selector + ".active");
        if (active.size() == 0) {
            active = context.find(opts.item_selector + ":first");
            active.addClass("active");
        }

        // Setup navigate to prev/next media handler
        context.find('.media-slider-nav div').click(function () {
            stop_interval(context, opts);

            switch_media(context, opts, $(this).find("a[href^=#prev]").size() > 0);

            return false;
        });

        // Show/hide navigation
        context.find(".media-slider-nav div").hover(function () {
            $(this).find("a:first").show();
        },
    function () {
        $(this).find("a:first").hide();
    });

        if (opts.auto_slide) {
            opts.auto_slide_interval = window.setInterval(function () {
                switch_media(context, opts, false);
            }, opts.auto_slide_wait_time);
        }

        // Make sure that the video tag is initialized if exists.
        init_video(context, opts, active);
    }


    function stop_interval(context, opts) {
        if (opts.auto_slide && opts.auto_slide_interval != 0) {
            window.clearInterval(opts.auto_slide_interval);
            opts.auto_slide_interval = 0;
        }
    }

    // Init video tag if exists
    function init_video(context, opts, item) {
        if (item.find('video').size() > 0) {
            if (!item.data('xcom-media-slider-video-initialized')) {
                item.find('video').VideoJS();
                var nav = context.find(".media-slider-nav:first").clone(true);
                item.find(".video-js-box").append(nav);
                nav.show();
                nav.css('zIndex', '0');

                item.find(".vjs-big-play-button").click(function () {
                    stop_interval(context, opts);
                });

                /*item.find('video')[0].addEventListener('ended', function() {
                alert('ended');
                }, false);
                item.find('video')[0].addEventListener('pause', function() {
                alert('pause');
                }, false);*/

                if ($.browser.msie) {
                    $.fn.xcomMediaSlider._objectMap[item.find('object').attr('id')] = function (dir) {
                        stop_interval(context, opts);
                        switch_media(context, opts, dir == 'prev');
                    };
                }


                item.data('xcom-media-slider-video-initialized', true);
            }

            //item.find(".vjs-load-progress").css('width', '0%');
            //item.find(".vjs-play-progress").css('width', '0%');
            item.find(".vjs-spinner").css('display', 'none');

            context.find(".media-slider-nav:first").hide();
        }
        else {
            // Make sure that the nav is visible if there is more than one image in the slider.
            if (context.find(opts.item_selector).size() > 1)
                context.find(".media-slider-nav:first").show();

            //context.find(".media-slider-nav:first").css('backgroundColor', 'red').css('zIndex', '9999999');
        }
    }

    // Switch to next/prev media
    function switch_media(context, opts, direction_prev) {
        var next = null;

        var active = context.find(opts.item_selector + ".active");
        if (active.find("video").size() > 0) {
            if ($.browser.msie) {
                var video = active.find("object")[0];
                try { video.enterPosterState(); } catch (e) { }
            }
            else {
                var video = active.find("video")[0].player;
                trace(video);
                video.pause();
                video.currentTime(0);
                video.showPoster();
                active.find(".vjs-big-play-button").show();
            }
        }


        if (direction_prev) {
            next = active.prev(opts.item_selector);

            if (next.size() == 0)
                next = context.find(opts.item_selector + ':last');
        }
        else {
            next = active.next(opts.item_selector);

            if (next.size() == 0)
                next = context.find(opts.item_selector + ':first');
        }

        if (opts.can_slide_media(next, opts, opts.auto_slide_interval != 0)) {
            context.find(opts.item_selector).removeClass('active').hide();
            next.addClass('active').fadeIn("slow");

            init_video(context, opts, next);
        }
    }

    $.fn.xcomMediaSlider.switchMedia = function (oid, dir) {
        var switch_fn = $.fn.xcomMediaSlider._objectMap[oid];
        switch_fn(dir);
        return true;
    };

    $.fn.xcomMediaSlider._objectMap = {};

    $.fn.xcomMediaSlider.defaults = {
        item_selector: 'img',
        auto_slide: false,
        auto_slide_wait_time: 5000,
        can_slide_media: function (context, opts, is_auto_slide) {
            return context[0].complete;
        }

    };
})(jQuery);

