function getYouTubeInfo(video_link, livello_titolo, livello_descrizione, livello_info) {
		$.ajax({
				url: "http://gdata.youtube.com/feeds/api/videos/"+video_link+"?v=2&alt=json",
				dataType: "jsonp",
				success: function (data) { parseresults(data, livello_titolo, livello_descrizione, livello_info); }
		});
}

function getYouTubeInfoPage(video_link, livello_titolo, livello_descrizione, livello_info) {
		$.ajax({
				url: "http://gdata.youtube.com/feeds/api/videos/"+video_link+"?v=2&alt=json",
				dataType: "jsonp",
				success: function (data) { parseresults_page(data, livello_titolo, livello_descrizione, livello_info); }
		});
}

function parseresults(data, livello_titolo, livello_descrizione, livello_info) {
		var title = data.entry.title.$t;
		var description = data.entry.media$group.media$description.$t;
		var viewcount = data.entry.yt$statistics.viewCount;
		var author = data.entry.author[0].name.$t;
		var duration = data.entry.media$group.yt$duration.seconds;
		var duration = data.entry.media$group.yt$duration.seconds;
		var published = data.entry.published.$t;
		//var logo = '<img src="img/icone/youtube_small.png" align="absmiddle" />';
		
		pub_a = published.substr(0,10);
		pub_b = published.substr(11,8);
		pub = pub_a.split("-");
		
		$('#'+livello_titolo).html(title);
		$('#'+livello_descrizione).html( (description.length > 60 ? description.substr(0,57)+'...' : description) );
		$('#'+livello_info).html('' + pub[2] + '/' + pub[1] + '/' + pub[0] + ' ' + pub_b + '<br />' + check(duration) + ' min. | <b>' + viewcount + ' visualizzazioni</b>');
		//$('#YTextrainfo').html('<b>Author</b>: ' + author + '<br/><b>Views</b>: ' + viewcount);
		//getComments(data.entry.gd$comments.gd$feedLink.href + '&max-results=50&alt=json', 1);
}

function parseresults_page(data, livello_titolo, livello_descrizione, livello_info) {
		var title = data.entry.title.$t;
		var description = data.entry.media$group.media$description.$t;
		var viewcount = data.entry.yt$statistics.viewCount;
		var author = data.entry.author[0].name.$t;
		var duration = data.entry.media$group.yt$duration.seconds;
		var duration = data.entry.media$group.yt$duration.seconds;
		var published = data.entry.published.$t;
		
		pub_a = published.substr(0,10);
		pub_b = published.substr(11,8);
		pub = pub_a.split("-");
		
		$('#'+livello_titolo).html(title);
		$('#'+livello_descrizione).html(str_replace('\n','<br />',description));
		$('#'+livello_info).html('<span style="float:right; font-size:25px">' + viewcount + '</span>pubblicato:<br />' + pub[2] + '/' + pub[1] + '/' + pub[0] + ' ' + pub_b);
		
		$('#dove_sei').append( (title.length > 70 ? title.substr(0,70)+'...' : title) );
		document.title += ' | ' + title;
		
		getComments(data.entry.gd$comments.gd$feedLink.href + '&max-results=50&alt=json', 1);
}

function getComments(commentsURL, startIndex) {
		$.ajax({
				url: commentsURL + '&start-index=' + startIndex,
				dataType: "jsonp",
				success: function (data) {
				$('#YU_com_page').append('<div class="titolo_pagina" style="margin-bottom:5px;">Commenti YouTUBE</div>');
				var conteggio = 0;
				$.each(data.feed.entry, function(key, val) {
						if(conteggio == 1){
							$('#YU_com_page').append('<div class="YU_commenti_utente_2"><p><span>Autore:</span> <a href="http://www.youtube.com/comment_search?username='+val.author[0].name.$t+'" target="_blank">' + val.author[0].name.$t + '</a></p>' + val.content.$t + '</div>');
							conteggio = 0;
						} else {
							$('#YU_com_page').append('<div class="YU_commenti_utente"><p><span>Autore:</span> <a href="http://www.youtube.com/comment_search?username='+val.author[0].name.$t+'" target="_blank">' + val.author[0].name.$t + '</a></p>' + val.content.$t + '</div>');
							conteggio+=1;
						}
				});
				if ($(data.feed.entry).size() == 50) { getComments(commentsURL, startIndex + 50); }
				}
		});
}

function check(secT){
	secondi = secT % 60;
	minuti = ((secT - secondi)/60) % 60;
	//ore = (secT - secondi - (minuti * 60)) / 3600 % 3600;
	
	//return ore + ":" + minuti + ":" + secondi;
	return minuti + ":" + secondi;
} 
