﻿function onYouTubePlayerReady(playerId) {
    this.simpelOnline.youtube.playerLoaded();
}

this.simpelOnline = {
    youtube: {
        player: null
        , timeSlider: null
        , volumeSlider: null
        , timer: null
        , videos: []
        , progress1: null
        , progress2: null
        , tSliding: false
        , init: function(v, s) {
            if (v)
                this.cue(v, s);

            /*this.timeSlider = $("div#youtube ul.buttons li.timeslider").slider({
                min: 0
                , max: 100
                , animate: true
                , step: 1
                , value: 0
                , slide: function(e, u) {
                    this.tSliding = true;
                    $("div#youtube ul.buttons li.time").html(this.formatSecond(u.value) + " / " + this.formatSecond(this.duration()));
                } .createDelegate(this)
                , stop: function(e, u) {
                    this.tSliding = false;
                    this.seekTo(u.value);
                } .createDelegate(this)
            });

            $("div#youtube ul.buttons li.timeslider").prepend("<div class=\"ui-slider-range ui-slider-range-min progress1\"></div><div class=\"ui-slider-range ui-slider-range-min progress2\"></div>");

            this.progress1 = $("div#youtube ul.buttons li.timeslider div.progress1");
            this.progress2 = $("div#youtube ul.buttons li.timeslider div.progress2");

            $("div#youtube ul.buttons li a.button").bind("click", function(e) {
                var p = $(e.currentTarget).parent();

                if (p.hasClass('pause') === true) {
                    this.pause();
                }
                else if (p.hasClass('play') === true) {
                    this.play();
                }
                else if (p.hasClass('stop') === true) {
                    this.stop();
                }
                else if (p.hasClass('maximize') === true || p.hasClass('restore') === true) {
                    clearTimeout(this.timer);

                    if (p.hasClass('restore') === true) {
                        var t = $("div#youtube").css({
                            position: null
                            , top: null
                            , left: null
                            , height: null
                            , width: null
                        }).find("div.player");

                        t.css({
                            width: null
                            , height: null
                        });

                        p.removeClass('restore').addClass('maximize');
                    }
                    else {
                        var d = $(document);
                        var w = d.width();
                        var h = d.height();

                        var t = $("div#youtube").css({
                            position: "absolute"
                            , top: "0px"
                            , left: "0px"
                            , width: w
                            , height: h
                        }).find("div.player");

                        t.css({
                            width: w
                            , height: h - 100
                        });

                        p.removeClass('maximize').addClass('restore');
                    }
                }
                else if (p.hasClass('mute') === true) {
                    this.mute();
                }
                else if (p.hasClass('unmute') === true) {
                    this.unMute();
                }
                else if (p.hasClass('volume') === true) {
                    this.setVolume(p.children("a").attr("volume"));
                }

                return false;
            } .createDelegate(this));*/

            var params = {
                allowScriptAccess: "always"
            };

            var atts = {
                id: "youtubePlayer"
            };

            swfobject.embedSWF(
                "http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer"
                , "youtubePlayer"
                , "100%"
                , "100%"
                , "8"
                , null
                , null
                , params
                , atts
            );
        }
        , playerLoaded: function() {
            this.player = $("#youtubePlayer");

            this.player = this.player[0];

            this.player.addEventListener("onStateChange", "simpelOnline.youtube.setState");
            this.player.addEventListener("onError", "simpelOnline.youtube.error");

            var muted = this.player.isMuted();
            this.setVolume(this.volume());

            if (muted === true)
                this.mute();

            if (this.videos.length > 0) {
                this.load(this.videos.pop());
            }
        }
        , load: function(o) {
            if (this.player) {
                this.player.loadVideoById(o.video, o.seconds || 0);
            }
        }
        , cue: function(v, s) {
            this.videos.push({
                video: v
                , seconds: s
            });
        }
        , error: function(error) {
            alert(error);
        }
        , setState: function(state) {
            $("div#youtube p.state").html(state);

            var t = $("div#youtube ul.buttons li:first");

            switch (state) {
                case -1:
                    /*unstarted*/
                    break;
                case 0:
                    /*ended*/
                    t.removeClass('pause').addClass('play')
                    if (this.videos.length > 0) {
                        this.load(this.videos.pop());
                    }
                    break;
                case 1:
                    /*playing*/
                    t.removeClass('play').addClass('pause')
                    this.changeValues();
                    break;
                case 2:
                    /*paused*/
                    t.removeClass('pause').addClass('play')
                    break;
                case 3:
                    /*buffering*/
                    break;
                case 5:
                    /*cued*/
                    break;
            }

        }
        , changeValues: function() {
            clearTimeout(this.timer);

            if (this.tSliding === false) {
                var c = this.currentTime();
                var d = this.duration();
                var t = this.bytesTotal();
                var l = this.bytesLoaded();
                var s = this.startBytes();

                var r1 = (s + l) / t;
                var r2 = c / d;
                var ml = s / t;

                if (this.duration() < 0) {
                    c = 0;
                    d = 100;
                }

                this.timeSlider.slider("option", "max", d);
                this.timeSlider.slider("value", c);

                this.progress2.css("width", ((r2 - ml) * 100) + "%").css("left", (ml * 100) + "%");
                this.progress1.css("width", ((r1 - ml) * 100) + "%").css("left", (ml * 100) + "%");

                $("div#youtube ul.buttons li.time").html(this.formatSecond(c) + " / " + this.formatSecond(d));
            }

            /*this.timer = setTimeout(function() {
                this.changeValues();
            } .createDelegate(this), 500);*/
        }
        , play: function() {
            if (this.player) {
                this.player.playVideo();
            }
        }
        , pause: function() {
            if (this.player) {
                this.player.pauseVideo();
            }
        }
        , stop: function() {
            if (this.player) {
                this.player.stopVideo();
            }
        }
        , seekTo: function(seconds) {
            if (this.player) {
                this.player.seekTo(seconds, true);
            }
        }
        , mute: function() {
            if (this.player) {
                $("div#youtube ul.buttons li.mute").removeClass("mute").addClass("unmute");
                $("div#youtube ul.buttons li.volumeContainer").addClass("disabled");
                this.player.mute();
            }
        }
        , unMute: function() {
            if (this.player) {
                $("div#youtube ul.buttons li.unmute").removeClass("unmute").addClass("mute");
                $("div#youtube ul.buttons li.volumeContainer").removeClass("disabled");
                this.player.unMute();
            }
        }
        , setVolume: function(volume) {
            if (this.player) {
                this.unMute();
                this.player.setVolume(volume);

                $("div#youtube ul.buttons li.volume").removeClass("active");
                $("div#youtube ul.buttons li.volume:lt(" + (volume / 10) + ")").addClass("active");
            }
        }
        , clearVideo: function() {
            if (this.player) {
                this.player.clearVideo();
            }
        }
        , state: function() {
            if (this.player) {
                return this.player.getPlayerState();
            }
        }
        , bytesLoaded: function() {
            if (this.player) {
                return this.player.getVideoBytesLoaded();
            }
        }
        , bytesTotal: function() {
            if (this.player) {
                return this.player.getVideoBytesTotal();
            }
        }
        , startBytes: function() {
            if (this.player) {
                return this.player.getVideoStartBytes();
            }
        }
        , duration: function() {
            if (this.player) {
                return this.player.getDuration();
            }
        }
        , currentTime: function() {
            if (this.player) {
                return this.player.getCurrentTime();
            }
        }
        , embedCode: function() {
            if (this.player) {
                return this.player.getEmbedCode();
            }
        }
        , videoUrl: function() {
            if (this.player) {
                return this.player.getVideoUrl();
            }
        }
        , volume: function() {
            if (this.player) {
                return this.player.getVolume();
            }
        }
        , formatSecond: function(s) {
            var m = s / 60;
            s = s % 60;

            return parseInt(m) + (s < 10 ? ":0" : ":") + parseInt(s);
        }
    }
}

