/*
* 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 ($) {
    $.fn.ytchromeless = function (o) {
    
        //Initial configuration
        var config = {
            videoWidth: '630',
            videoHeight: '354',
            videoIdBase: 'ytplayer',
            params: {
                allowScriptAccess: 'always',
                wmode: 'transparent'
            }
        };

        return this.each(function (i) {


            // initial var setup

            var options = $.extend(config, o),

            // set jQuery objects
            $link = $(this),

            // set variables          
            url = $link.attr('href'),
            videoId = $link.attr('id') || options.videoIdBase + i,
            ytVideoId = url.substr(31),

            // new DOM elements
            $video = $link.wrap('<div class="video-player"></div>').parent(),
            $poster = $('<div id="youtube_poster"></div>').appendTo($video),
            $controls = $('<div class="video-controls"></div>').appendTo($video), $toReplace = $('<div class="video"></div>').prependTo($video).attr('id', videoId),
            $bar,
            $indicator,
            $loaded,
            $mute,
            $play,
            $seek,

            // set up the special player object
            player;

            // bind public methods upfront 
            $video.bind({

                // playing, pausing, muting, 
                'togglePlay': function () { $video.togglePlay(); },
                'play': function () { $video.play(); },
                'pause': function () { $video.pause(); },
                'pauseVideo': function () { player.pauseVideo(); },
                'toggleMute': function () { $video.toggleMute(); },
                'mute': function () { $video.mute(); },
                'unMute': function () { $video.unMute(); },
                'seek': function () { $video.seek(); },

                // initializing and revising the player
                'update': function () { $video.update(); },
                'cue': function () { player.cueVideoById(ytVideoId, 0, 'hd720'); },
                'load': function () {
                    player.loadVideoById(ytVideoId);
                    player.pauseVideo();
                }

            });


            // control methods

            // function fired when the play/pause button is hit
            $video.togglePlay = function () {
                if ($play.hasClass('playing')) {
                    $video.trigger('pause');
                } else {
                    $video.trigger('play');
                }
                return false;
            };

            // play the video
            $video.play = function () {
                player.playVideo();
                setTimeout("document.getElementById('youtube_poster').style.display = 'none'", 500);
                $play.removeClass('paused').addClass('playing').attr('title', 'Pause');
            };

            // pause
            $video.pause = function () {
                player.pauseVideo();
                $play.removeClass('playing').addClass('paused').attr('title', 'Play');
            };

            // function fired when the mute/unmute button is hit
            $video.toggleMute = function () {
                if ($mute.hasClass('muted')) {
                    $video.trigger('unMute');
                } else {
                    $video.trigger('mute');
                }
                return false;
            };

            // mute the video
            $video.mute = function () {
                player.mute();
                $mute.addClass('muted').attr('title', 'Un-Mute');
            };

            // unmute
            $video.unMute = function () {
                player.unMute();
                $mute.removeClass('muted').attr('title', 'Mute');
            };

            //Seek to a position in the video
            $video.seek = function (seekPosition) {
                var seekToPosition = Math.round(player.getDuration() * seekPosition);
                player.seekTo(seekToPosition, false);
            };



            // player init and update methods

            //Update the video status
            $video.update = function () {

                if (player && player.getDuration) {

                    if (player.getPlayerState() === 1) {
                        $video.play();
                    } else if (player.getPlayerState() === 0) {
                        $video.pause();
                    }

                    if (player.getVideoBytesLoaded() > -1) {
                        var loadedAmount = (player.getVideoBytesLoaded() / player.getVideoBytesTotal()) * 100;
                        $loaded.css('width', loadedAmount + '%');
                    }

                    if (player.getCurrentTime() > 0) {
                        var videoPosition = (player.getCurrentTime() / player.getDuration()) * 100;
                        $indicator.css('left', videoPosition + '%');
                    }

                }

            };


            // the youtube movie calls this method when it loads
            // DO NOT CHANGE THIS METHOD'S NAME
            onYouTubePlayerReady = function (videoId) {

                var $videoRef = $(document.getElementById(videoId)).parent();

                $videoRef.trigger('cue');

                setInterval(function () {
                    $videoRef.trigger('update');
                }, 250);

            };



            // init methods

            // the embed!
            $video.init = function () {

                if (window.location.protocol == "https:") {
                    window.location.href = "http:" + window.location.hostname + window.location.pathname
                }

                swfobject.embedSWF(
            'http://www.youtube.com/apiplayer?&enablejsapi=1&hd=1&playerapiid=' + videoId,
            videoId,
            options.videoWidth,
            options.videoHeight,
            '8',
            null,
            null,
            options.params,
            { id: videoId },
            function () {
                player = document.getElementById(videoId);
            }
          );

                $video.addPoster();
                $video.addControls();

            };

            $video.addPoster = function () {
                $posterlink = $('<a/>', {
                    href: '#',
                    text: 'Play',
                    title: 'Play',
                    id: 'poster_play',
                    click: function () {
                        $video.trigger('play');
                        return false;
                    }
                }).appendTo($poster);
            };
            // add controls
            $video.addControls = function () {

                //Play and pause button
                $play = $('<a/>', {
                    href: '#',
                    'class': 'play-pause',
                    text: 'Play/Pause',
                    title: 'Play',
                    click: function () {
                        $video.trigger('togglePlay');
                        return false;
                    }
                }).appendTo($controls);

                //Play and pause button
                $mute = $('<a/>', {
                    href: '#',
                    'class': 'volume',
                    text: 'Volume',
                    title: 'Mute',
                    click: function () {
                        $video.trigger('toggleMute');
                        return false;
                    }
                }).appendTo($controls);

                //View on YouTube
                $link
    		    .addClass('view-youtube')
    		    .attr('title', 'View on YouTube')
    		    .html('Play/View on YouTube')
    		    .appendTo($controls);

                //Play and pause button
                $seek = $('<div/>', {
                    'class': 'status',
                    click: function (e) {
                        var skipTo = e.pageX - $seek.offset().left,
                          statusWidth = $seek.width();
                        $video.seek(skipTo / statusWidth);
                    }
                }).appendTo($controls);

                $bar = $('<div class="bar"></div>').appendTo($seek);
                $loaded = $('<div class="loaded"></div>').appendTo($bar);
                $indicator = $('<span class="indicator"></span>').appendTo($bar);

            };

            $video.init();

        });

    };

})(jQuery);

function Client() {
}

Client.prototype.mobileClients = [
	"midp",
	"240x320",
	"blackberry",
	"netfront",
	"nokia",
	"panasonic",
	"portalmmm",
	"sharp",
	"sie-",
	"sonyericsson",
	"symbian",
	"windows ce",
	"benq",
	"mda",
	"mot-",
	"opera mini",
	"philips",
	"pocket pc",
	"sagem",
	"samsung",
	"sda",
	"sgh-",
	"vodafone",
	"xda",
	"webos",
	"ipod",
	"ipad",
	"iphone",
	"android",
	"msie 6",
	"msie 5"
];

Client.prototype.isMobileClient = function (userAgent) {
    userAgent = userAgent.toLowerCase();
    for (var i in this.mobileClients) {
        if (userAgent.indexOf(this.mobileClients[i]) != -1) {
            return true;
        }
    }
    return false;
}

var client = new Client();

$(document).ready(function () {

    if (!client.isMobileClient(navigator.userAgent)) {
        $('a.video-link').ytchromeless();
    } else {
        $('#youtube_embed').show();
        $('#youtube_container').hide();
    }
});
