/*
* jTube
* http://jtube.monkeecreate.com
*
* jQuery Youtube API Feed Plugin
*
* Developed by John Hoover <john@defvayne23.com>
* Another project from monkeeCreate <http://monkeecreate.com>
*
* Version 1.3.6 - Last updated: May 25, 2010
*/
(function ($) {
    $.extend({
        jTube: function (i) {
            var i = $.extend({
                user: '',
                userType: 'uploads',
                search: '',
                feed: '',
                playlist: '',
                format: 'flash',
                order: 'published',
                time: 'all_time',
                limit: 5,
                page: 1,
                success: function (a, b) {}, error: function (a) {}
            }, i);
            var j = 'http://gdata.youtube.com/feeds/';
            var k = this;
            var l = '';
            if (i.user != '') j += 'api/users/' + i.user + '/' + i.userType + '?';
            else if (i.search != '') j += 'api/videos?q=' + i.search + '&';
            else if (i.feed != '') j += 'api/standardfeeds/' + i.feed + '?';
            else if (i.playlist != '') j += 'api/playlists/' + i.playlist + '?';
            else {
                i.error("No feed choices given.");
                return false
            }
            j += 'alt=json-in-script';
            j += '&max-results=' + i.limit;
            j += '&start-index=' + (((i.page * i.limit) - i.limit) + 1);
            j += '&orderby=' + i.order;
            j += '&time=' + i.time;
            if (i.format == "mpeg") j += '&format=6';
            else if (i.format == "h263") j += '&format=1';
            else j += '&format=5';
            j += '&callback=?';
            $.getJSON(j, function (d) {
                if (d != '' && d != null) {
                    if (i.user != '' && i.userType == 'playlists') {
                        var e = [];
                        $(d.feed.entry).each(function () {
                            var a = {
                                title: this.title.$t,
                                description: this.yt$description.$t,
                                id: this.yt$playlistId.$t,
                                link: this.link[1].href,
                                published: new Date(this.published.$t)
                            };
                            e[e.length] = a
                        });
                        var f = e
                    } else if (i.user != '' && i.userType == 'subscriptions') {
                        var g = [];
                        $(d.feed.entry).each(function () {
                            var a = {
                                title: this.title.$t,
                                username: this.yt$username.$t,
                                link: this.link[1].href,
                                thumbnail: this.media$thumbnail.url,
                                published: new Date(this.published.$t),
                                updated: new Date(this.updated.$t)
                            };
                            g[g.length] = a
                        });
                        var f = g
                    } else if (i.user != '' && i.userType == 'contacts') {
                        var h = [];
                        $(d.feed.entry).each(function () {
                            var a = {
                                username: this.yt$username.$t,
                                status: this.yt$status.$t,
                                link: this.link[1].href
                            };
                            h[h.length] = a
                        });
                        var f = h
                    } else {
                        var f = [];
                        $(d.feed.entry).each(function () {
                            var b = [];
                            $(this.category).each(function (a) {
                                if (a != 0) {
                                    b[a - 1] = this.term
                                }
                            });
                            var c = {
                                title: this.title.$t,
                                link: this.link[0].href,
                                categories: b,
                                author: {
                                    name: this.author[0].name.$t,
                                    link: this.author[0].uri.$t
                                }, 
								published: this.published.$t
                            };
                            if (this.media$group.media$description) {
                                c.description = this.media$group.media$description.$t
                            }
                            if (this.media$group.media$thumbnail) {
                                c.thumbnail = this.media$group.media$thumbnail[3].url
                            }
                            videoFormats = [];
                            $(this.media$group.media$content).each(function () {
                                videoFormats[this.yt$format] = this.url
                            });
                            if (i.format == "mpeg") c.video = videoFormats[6];
                            else if (i.format == "h263") c.video = videoFormats[1];
                            else c.video = videoFormats[5];
                            if (this.media$group.yt$duration) {
                                duration = this.media$group.yt$duration.seconds;
                                hours = 0;
                                minutes = 0;
                                seconds = 0;
                                while (duration >= 3600) {
                                    hours = hours + 1;
                                    duration = duration - 3600
                                }
                                while (duration >= 60) {
                                    minutes = minutes + 1;
                                    duration = duration - 60
                                }
                                seconds = duration;
                                if (seconds < 10) seconds = '0' + seconds;
                                c.length = minutes + ':' + seconds;
                                if (hours > 0) c.length = hours + ':' + c.length
                            }
                            if (this.yt$statistics) c.views = this.yt$statistics.viewCount;
                            f[f.length] = c
                        })
                    }
                    pages = Math.ceil(d.feed.openSearch$totalResults.$t / i.limit);
                    i.success(f, pages)
                } else {
                    i.error("Bad request.")
                }
            });
            return this
        }, jTubeEmbed: function (a, b) {
            var b = $.extend({
                width: 290,
                height: 250,
                autoplay: true,
                fullscreeen: false,
                related: true,
                loop: false,
                keyboard: true,
                genie: false,
                border: false,
                highdef: true,
                start: 0
            }, b);
            var c = a + "?";
            c += 'autoplay=' + (b.autoPlay ? 1 : 0);
            c += '&fs=' + (b.fullscreen ? 1 : 0);
            c += '&rl=1' + (b.related ? 1 : 0);
            c += '&loop=1' + (b.loop ? 1 : 0);
            c += '&disablekb=0' + (b.keyboard ? 0 : 1);
            c += '&egm=1' + (b.genie ? 1 : 0);
            c += '&border=1' + (b.border ? 1 : 0);
            c += '&hd=1' + (b.highdef ? 1 : 0);
            c += '&start=' + b.start;
            var d = '<object width="' + b.width + '" height="' + b.height + '">';
            d += '<param name="movie" value="' + c + '"</param>';
            d += '<param name="allowScriptAccess" value="always"></param>';
            if (b.fullscreen == true) d += '<param name="allowFullScreen" value="true"></param>';
            d += '<embed src="' + c + '"';
            d += ' type="application/x-shockwave-flash"';
            if (b.fullscreen == true) d += ' allowfullscreen="true"';
            d += ' allowscriptaccess="always"';
			d += ' wmode="opaque"';
            d += ' width="' + b.width + '" height="' + b.height + '">';
            d += ' </embed>';
            d += '</object>';
            return d
        }
    })
})(jQuery);
