/**
 *  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) {
//alert(data.feed.entry)
results = 0;
     var cnt = 0;
	 // Check if any results
	 if(data.feed.entry){
     $.each(data.feed.entry, function(i, e) {
			//alert(e.title.$t)	
         if (cnt < __opts.numberToDisplay && e.title.$t != '') {
			//alert(e.id.$t)
			
			 results++;
	 
             var parts = e.id.$t.split('/');
             var videoId = parts[parts.length-1];
			 var largeWidth = parseInt(__opts.imageWidth)+4;
			 var reflect = '';
			 if (__opts.reflect == 1) {
                 reflect = 'reflect';;
             }
			 var ytTitle = addslashes(e.title.$t.replace( /"([^"]*)"/g, "" ));
             var out = '<span class="video" id="'+videoId+'"><a rel="' + 
                  videoId + '" class="ytLink" id="'+ytTitle+'"><img style="margin-bottom:4px;" onmouseover="ToolTipBox(\'<strong>Click to view</strong><br/>'+ytTitle+'\', 200); this.style.marginTop=\'4px\'; document.getElementById(\''+videoId+'\').style.paddingBottom=\'0px\';" onmouseout="UnTip();this.style.marginTop=\'auto\'; document.getElementById(\''+videoId+'\').style.paddingBottom=\'4px\';" class="'+reflect+'" src="http://i.ytimg.com/vi/' + 
                  videoId + '/2.jpg" width="'+__opts.imageWidth+'"/></a>';
				  //'<span rel="' + videoId + '" class="ytLink" title="'+e.title.$t+'">' + e.title.$t + '</span><br/>';
				  
			 
				  
				  
			//var out = '<div class="video"><a rel="'+ videoId + '" class="ytLink" title="'+e.title.$t+'">';
			//	out+='<img src="http://i.ytimg.com/vi/'+videoId+ '/2.jpg" width="'+__opts.imageWidth+'" align="left" style="margin-right:6px;"/>' + e.title.$t + '';
			//	out+='</a><br />'
				  
				  
             if (!__opts.hideAuthor) {
                 out = out + 'Author: ' + e.author[0].name.$t + '';
             }
             out = out + '</span>';
             __mainDiv.append(out);
             cnt = cnt + 1;
			 
			 
         }
		 
     });
	 } else {
            
	//if (results < 1){
		var out = '<div class="video">No Results</div>';  
		 __mainDiv.append(out);
		//alert('No Video Results');
	//	}
	 }
    // Open in new tab?
    if (__opts.linksInNewWindow) {
        $(__mainDiv).find("li > a").attr("target", "_blank");
    }
    
	
	$("img.reflect").reflect({/* Put custom options here */});
	
    // Remove the preloader and show the content
    $(__preLoaderHTML).remove();
    __mainDiv.show();

	/* !CREATE VIDEO PLAYER */
	$("a.ytLink").each(function(){
		$(this).css("cursor", "pointer");
		$(this).click(function(){
			ytVid = $(this).attr("rel");
			ytTitle = $(this).attr("id");
			//alert("yo "+ytVid);
			
			//$("#youtubevideos").prepend("<div id='ytPlayer'><a href='#' id='ytplrCls'>[x] CLOSE</a><div id='ytPlayerInr'><object width='640' height='385'><param name='movie' value='http://www.youtube.com/v/"+ytVid+"&hl=en_US&fs=1&'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+ytVid+"&hl=en_US&fs=1&' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='640' height='385'></embed></object></div></div>");
			//$("#ytPlayer").draggable({cancel:"#ytPlayerInr", containment: '#innerBody', scroll: false });
			//$("#ytplrCls").click(function(){
			//	$("#ytPlayer").remove();
			//});
			//alert("http://www.youtube.com/v/"+ytVid+"&hl=en_US&fs=1&")
			
			if(fb_tab!=1){ 
				$.colorbox({innerWidth:"680px", innerHeight:"420px",  opacity:0.7,html:"<div id='ytPlayer'>"+ ytTitle+"<div id='ytPlayerInr'><object width='640' height='385'><param name='movie' value='http://www.youtube.com/v/"+ytVid+"&hl=en_US&fs=1&'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+ytVid+"&hl=en_US&fs=1&' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='640' height='385'></embed></object></div></div>"});
			} else { 
				wopen ("facebook_tab/youtube_popup.php?fb_popup&v="+ytVid+"&t="+ytTitle+"","youtubewindow",680,420);
			}
			
		});
	});
	
}

           
(function($) {
    $.fn.youTubeChannel = function(options) {
        var videoDiv = $(this);

        $.fn.youTubeChannel.defaults = {
           //userName: null,
           // channel: "uploads", //options are favorites or uploads
           loadingText: "<img src='images/active_refresh.gif' width='16' height='16' align='absmiddle' /> Loading Videos"
           // numberToDisplay: 3,
           // linksInNewWindow: true,
           //hideAuthor: true
        }

        __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 = $("<div style='text-align:center;' class=\"loader\">" + 
                    __opts.loadingText + "</div>");
                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
                });
            }
        });
    };
})(jQuery);
