/*
* Change out the video that is playing
*/
var maxItems = 24;
var onPage = 12;
var totalPages = Math.ceil(maxItems/onPage);
var actualPage = 1;
var firstLoad = 1;
var nowPlaying = "";

google.load("swfobject", "2.1");
// Loads the selected video into the player.
function loadVideo(videoID) {
	if(ytplayer) {
	  nowPlaying = videoID;
	  ytplayer.loadVideoById(videoID);
	  loadComments(videoID);
	  loadRelativeVideo(videoID);
	  loadVideoName(videoID,'.ytVideoName');
	  $(".ytFeedItem").each(function (){
		  $(this).removeClass("nowPlaying");
	  });
	  $("#video"+videoID).addClass("nowPlaying");
	  $.scrollTo( '#content-inside',500, {easing:'jswing'} );
	}
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
	alert("An error occured of type:" + errorCode);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("ytPlayer");
	ytplayer.addEventListener("onError", "onPlayerError");
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer(videoID,w,h) {
	// The video to load
	var videoID = videoID
	// Lets Flash from another domain call JavaScript
	var params = { allowScriptAccess: "always", wmode:'transparent', allowFullScreen:true };
	// The element id of the Flash embed
	var atts = { id: "ytPlayer" };
	// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
	swfobject.embedSWF("http://www.youtube.com/v/" + videoID + 
					   "?version=3&enablejsapi=1&playerapiid=player1&autoplay=1&fs=1", 
					   "yPlayer", w, h, "9", null, null, params, atts);
					   
	loadComments(videoID);
	loadRelativeVideo(videoID);
}

function loadPager(){
	var pages = "";
	var selectedPage = "";
	for(c= 1;c<totalPages+1; c++){
		if(c==actualPage) { selectedPage = "ytPageSelected"; } else { selectedPage = "";  };
		pages += '<a onClick="changeYtPage('+c+')" class="ytPage '+selectedPage+'" id="ytPage'+c+'">'+c+'</a>';
	}
	$(".ytPager").html(pages);
};

function changeYtPage(page){
	actualPage = page;
	loadYtPage();
	loadPager();
}

function loadRelativeVideo(videoID){
	
		$.jTube({
			request: 'video',
			requestValue: videoID,
			requestOption: 'related',
			limit: 6,
			page: 1,
			success: function(videos) {
				$('.videoRelatives').html('');
				$(videos).each(function() {
						var videoID = gup('v',this.link);
						html = '<div style="background:url(/show_image.php?filename='+this.thumbnail+'&width=30&height=30&modify=) no-repeat left center; padding:5px 10px 5px 40px;">\n';
						html += '\t<a onClick="loadVideo(\''+videoID+'\')">'+this.title+'</a> - '+this.length+'\n';
						/*html += '<br><span class="label">Category:</span> '+this.category+'\n';
						html += '<br><span class="label">Keywords:</span> '+this.keywords+'\n';*/
						html += '</div>\n';
					$('.videoRelatives').append(html);
				});
			},
			error: function(error) {
				$('#videoRelatives').html(error);
			}
		});
}

function loadComments(videoID){
	$.jTube({
			request: 'video',
			requestValue: videoID,
			requestOption: 'comments',
			limit: 6,
			page: 1,
			success: function(videos) {
				$('.videoComments').html('');
				$(videos).each(function() {
					html = '<div>\n';
					html += '\t<a href="http://youtube.com/user/'+escape(this.author)+'">'+this.author+'</a>\n';
					html += '<br><span class="label">Published:</span> '+(this.published.getMonth() + 1)+'/'+this.published.getDate()+'/'+this.published.getFullYear()+'\n';
					html += '<br>'+this.comment+'\n';
					html += '</div>\n';
					$('.videoComments').append(html);
				});
			},
			error: function(error) {
				$('.videoComments').html(error);
			}
		});
};

function loadVideoName(videoID,to){
	$.jTube({
			request: 'video',
			requestValue: videoID,
			requestOption: 'info',
			limit: 1,
			page: 1,
			success: function(video) {
				$(to).html(video.title);
			},
			error: function(error) {
				$(to).html(error);
			}
		});
}

function loadYtPage(){
	
	$(".jTube").each(function (){
	var id = $(this).attr("id");
	var name = $(this).attr("title");
	var out = "";

	$.jTube({
				request: 'user',
				requestValue: name,
				requestOption: 'uploads',
				limit: onPage,
				page: actualPage,
				success: function(videos) {
					var count = 0;
					var playVideo;
					var playingCls;
					$('#+id').html('');
					$(videos).each(function() {
						count = count + 1;
						var videoID = gup('v',this.link);
						if(firstLoad==1){  
						if(count==1) {playingCls = " nowPlaying"; }else{ playingCls = "";};
						} else {
						if(videoID==nowPlaying) {playingCls = " nowPlaying"; }else{ playingCls = "";}
						}
						if(count==1) {playVideo = videoID; playVideoName=this.title;}
						
						out += '<div onClick="loadVideo(\''+videoID+'\')" id="video'+videoID+'" class="ytFeedItem'+playingCls+'" style="background-image:url(/show_image.php?filename='+this.thumbnail+'&width=60&height=45&modify=c);">\n';
						out += '\t<h3 class="ytFeedItemNadpis">'+this.title+'</h3>\n';
						/*html += '<p class="ytFeedItemPopis"><span class="label">Popis:</span> '+this.description+'</p>\n';*/
						out += '<span>Délka: '+this.length+'</span> | <span>Zobrazeno: '+this.views+'x</span> \n';
						out += '</div>\n';
						
					});	
					
					$('#userUploads').html(out);	
					if(firstLoad==1){ $(".ytVideoName").html(playVideoName); firstLoad = 0; nowPlaying = playVideo;
					loadPlayer(playVideo,$("#yPlayer").width(),$("#yPlayer").innerHeight());
					}
					
					loadPager();
						
				},
				error: function(error) {
					$('#'+id).html(error);
					
				}
			});	
	});
	
}
 
$(document).ready(function(){
		 
	loadYtPage();
	
	$(".jTubeLast").each(function (){
	var id = $(this).attr("id");
	var name = $(this).attr("title");
	
	$.jTube({
				request: 'user',
				requestValue: name,
				requestOption: 'uploads',
				limit: 1,
				page: 1,
				success: function(videos) {
					var count = 0;
					var playVideo;
					var playVideoName;
					$('#+id').html('');
					$(videos).each(function() {
						count = count + 1;
						var videoID = gup('v',this.link);
						if(count==1) {playVideo = videoID;}
						html = '<a href="/jesenik-tv/"><img src="'+this.thumbnail+'" width="210" height="140"></a><br><br>\n';
						html += '\t<h3 class="ytFeedItemNadpis"><a href="/jesenik-tv/">'+this.title+'</a></h3>\n';
						/*html += '<p class="ytFeedItemPopis"><span class="label">Popis:</span> '+this.description+'</p>\n';*/
						html += '<span>Délka: '+this.length+'</span> | <span>Zobrazeno: '+this.views+'x</span> \n';
						html += '<br><br>\n';
						$('#userUploads').append(html);	
					});	

					
						
				},
				error: function(error) {
					$('#'+id).html(error);
					
				}
			});	
	});	
		
});

	function gup(name,odkaz)
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec(odkaz);
	  if( results == null )
		return "";
	  else
		return results[1];
	}	
