/*
 * Youtube Chromeless Video Plugin
 * http://www.viget.com/
 *
 * Copyright (c) 2010 Trevor Davis
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://jquery.org/license
 *
 * @version 0.2
 */
(function(d) {
    d.fn.ytchromeless = function() {
        var p = {
            videoWidth: "640",
            videoHeight: "360",
            videoIdBase: "ytplayer",
            params: {
                allowScriptAccess: "always",
                wmode: "transparent"
            }
        };
        return this.each(function(q) {
            var e = d.extend(p, e),
            g = d(this),
            r = g.attr("href"),
            f = g.attr("id") || e.videoIdBase + q,
            s = r.substr(31),
            a = g.wrap('<div class="video-player"></div>').parent(),
            h = d('<div class="video-controls"></div>').appendTo(a);
            d('<div class="video"></div>').prependTo(a).attr("id", f);
            var m, n, o, i, j, k, b;
            a.bind({
                togglePlay: function() {
                    a.togglePlay()
                },
                play: function() {
                    a.play()
                },
                pause: function() {
                    a.pause()
                },
                toggleMute: function() {
                    a.toggleMute()
                },
                mute: function() {
                    a.mute()
                },
                unMute: function() {
                    a.unMute()
                },
                seek: function() {
                    a.seek()
                },
                update: function() {
                    a.update()
                },
                cue: function() {
                    b.cueVideoById(s)
                }
            });
            a.togglePlay = function() {
                j.hasClass("playing") ? a.trigger("pause") : a.trigger("play");
                return false
            };
            a.play = function() {
                b.playVideo();
                j.removeClass("paused").addClass("playing").attr("title", "Parar")
            };
            a.pause = function() {
                b.pauseVideo();
                j.removeClass("playing").addClass("paused").attr("title", "Tocar")
            };
            a.toggleMute = function() {
                i.hasClass("muted") ? a.trigger("unMute") : a.trigger("mute");
                return false
            };
            a.mute = function() {
                b.mute();
                i.addClass("muted").attr("title", "Ligar Som")
            };
            a.unMute = function() {
                b.unMute();
                i.removeClass("muted").attr("title", "Desligar Som")
            };
            a.seek = function(c) {
                c = Math.round(b.getDuration() * c);
                b.seekTo(c, false)
            };
            a.update = function() {
                if (b && b.getDuration) {
                    if (b.getPlayerState() === 1) a.play();
                    else b.getPlayerState() === 0 && a.pause();
                    if (b.getVideoBytesLoaded() > -1) {
                        var c = b.getVideoBytesLoaded() / b.getVideoBytesTotal() * 100;
                        o.css("width", c + "%")
                    }
                    if (b.getCurrentTime() > 0) {
                        c = b.getCurrentTime() / b.getDuration() * 100;
                        n.css("left", c + "%")
                    }
                }
            };
            onYouTubePlayerReady = function(c) {
                var l = d(document.getElementById(c)).parent();
                setInterval(function() {
                    l.trigger("update")
                },
                250);
                l.trigger("cue")
            };
            a.init = function() {
                swfobject.embedSWF("http://www.youtube.com/apiplayer?&enablejsapi=1&playerapiid=" + f, f, e.videoWidth, e.videoHeight, "8", null, null, e.params, {
                    id: f
                },
                function() {
                    b = document.getElementById(f)
                });
                a.addControls()
            };
            a.addControls = function() {
                j = d("<a/>", {
                    href: "#",
                    "class": "play-pause",
                    text: "Tocarar/Pausar",
                    title: "Tocar",
                    click: function() {
                        a.trigger("togglePlay");
                        return false
                    }
                }).appendTo(h);
                i = d("<a/>", {
                    href: "#",
                    "class": "volume",
                    text: "Volume",
                    title: "Desligar Som",
                    click: function() {
                        a.trigger("toggleMute");
                        return false
                    }
                }).appendTo(h);
                g.addClass("view-youtube").attr("title", "Ver no Youtube").html("Ver no Youtube").appendTo(h);
                k = d("<div/>", {
                    "class": "status",
                    click: function(c) {
                        c = c.pageX - k.offset().left;
                        var l = k.width();
                        a.seek(c / l)
                    }
                }).appendTo(h);
                m = d('<div class="bar"></div>').appendTo(k);
                o = d('<div class="loaded"></div>').appendTo(m);
                n = d('<span class="indicator"></span>').appendTo(m)
            };
            a.init()
        })
    }
})(jQuery);
