﻿/**
*  Plugin which renders the YouTube channel videos list to the page
*  @author:  H. Yankov (hristo.yankov at gmail dot com)
*  @version: 1.0.0 (Nov/27/2009)
*    http://yankov.us
*  
*  Modified my Dan Hounshell (Jan/2010) to work for favorites or 
*  uploads feeds and simplified output 
*/

var __mainDiv;
var __preLoaderHTML;
var __opts;

function __jQueryYouTubeChannelReceiveData(data_obj) {
    var cnt = 0;

    jQuery.each(data_obj.data.items, function (i, e) {
        if (cnt < __opts.numberToDisplay) {
            /*var parts = e.id.$t.split('/');
            var videoId = parts[parts.length - 1];*/
            var videoId = e.id;
            var link = e.player["default"];
            //alert(link);
            var out = '<div class="youtube-video"><table border=0 width="100%">';
            out += '<td width="75" align="center">';
            out += '<a href="' + link + '" target="_blank"><img src="http://i.ytimg.com/vi/' + videoId + '/2.jpg" width=60 height=45></a>';
            out += '</td><td rowspan="2">';
            out += '<a href="' + link + '" target="_blank">' + e.title + '</a>';
            out += '</td></tr>';
            out += '<tr><td align="center">';
            out += 'Views: ' + ((typeof e.viewCount === "undefined") ? "0" : e.viewCount);
            out += '</td></tr>';
            out = out + '</table></div>';

            __mainDiv.append(out);
            cnt = cnt + 1;
        }
    });

    // Open in new tab?
    if (__opts.linksInNewWindow) {
        jQuery(__mainDiv).find("li > a").attr("target", "_blank");
    }

    // Remove the preloader and show the content
    jQuery(__preLoaderHTML).remove();
    __mainDiv.show();
}

(function ($) {
    $.fn.youTubeChannel = function (options) {
        var videoDiv = $(this);

        $.fn.youTubeChannel.defaults = {
            userName: null,
            channel: "favorites", //options are favorites or uploads
            loadingText: "Loading...",
            numberToDisplay: 3,
            linksInNewWindow: true,
            hideAuthor: false
        }

        __opts = $.extend({}, $.fn.youTubeChannel.defaults, options);

        return this.each(function () {
            if (__opts.userName != null) {
                videoDiv.append("<div id=\"channel_div\"></div>");
                __mainDiv = $("#channel_div");
                __mainDiv.hide();

                __preLoaderHTML = $("<p class=\"loader\">" +
                    __opts.loadingText + "</p>");
                videoDiv.append(__preLoaderHTML);

                // TODO: Error handling!
                /*$.ajax({
                    url: "http://gdata.youtube.com/feeds/base/users/" +
                        __opts.userName + "/" + __opts.channel + "?alt=json",
                    cache: true,
                    dataType: 'jsonp',
                    success: __jQueryYouTubeChannelReceiveData
                });*/

                $.ajax({
                    url: "http://gdata.youtube.com/feeds/api/users/ZoN26TV/uploads?max-results=16&v=2&orderby=published&alt=jsonc",
                    cache: true,
                    dataType: 'jsonp',
                    success: __jQueryYouTubeChannelReceiveData
                });
            }
        });
    };
})(jQuery);
