/** 
*	YT Fetch Profile 0.1
*	By CosmicSpork
*
*	Requires JQuery
**/

var YTFetchProfile = {
	StartIndex: 1,
	PageSize: 10,
	TotalRecords: 0,
	AccName: null,
	Type: "uploads",
	Data: null,	
	ItemsPerRow: 5,
	OrderBy: "published",
	ContainerId: "YTFetchProfile",
	
	GetVideos: function(accName, type, startIndex) {	
		this.StartIndex = startIndex;
		
		if(this.StartIndex == null)
			this.StartIndex = 1;
		
		this.AccName = accName;
		
		if(type)
			this.Type = type;
			
		var url = "http://gdata.youtube.com/feeds/api/users/" + this.AccName + "/" + this.Type;
		
		var parameters = '?orderby=' + this.OrderBy + '&max-results=' + this.PageSize + '&start-index=' + this.StartIndex;
		parameters += '&alt=json-in-script&callback=YTFetchProfile.OnDataReceived&=?';

		$("#" + YTFetchProfile.ContainerId + " .Loader").show();
		$("#" + YTFetchProfile.ContainerId + " .Content").hide();

		jQuery.getJSON(url + parameters);
	},
	
	OnDataReceived: function(data) {
		if(!data) {
			this.OnDataError();
		} else {		
			YTFetchProfile.TotalRecords = parseInt(data.feed.openSearch$totalResults.$t);
			YTFetchProfile.Data = data;
		
			var recordsLength = data.feed.entry != undefined ? data.feed.entry.length : 0;
	
			$("#" + YTFetchProfile.ContainerId + " .Loader").hide();
			$("#" + YTFetchProfile.ContainerId + " .Content").show();								
			this.GenerateContent();
		}
	},
	
	OnDataError: function() {
		$("#" + YTFetchProfile.ContainerId + " .Loader").hide();
		$("#" + YTFetchProfile.ContainerId + " .Content").show();
		$("#" + YTFetchProfile.ContainerId + " .Content").html("An Error Has Occurred. The user you specified might not exist.");
	},
	
	GenerateContent: function() {
		if(this.TotalRecords > 0) {
			var table = document.createElement("table");
			table.cellPadding = 0;
			table.cellSpacing = 0;
			$(table).addClass("VideoTable");
			var entries = this.Data.feed.entry;
			var numRows = entries.length / this.ItemsPerRow;
			var curEntryIndex = 0;
			for(var r = 0; r < numRows; r++) {
				var row = document.createElement("tr");
				$(row).addClass("VideoRow");
				for(var c = 0; c < this.ItemsPerRow; c++) {
					if(entries[curEntryIndex] == undefined)
						break;
					
					var entry = entries[curEntryIndex];
					var entryBox = this.GenerateVideoBox(entry);
					var cell = document.createElement("td");
					$(cell).addClass("VideoCell").append(entryBox);					
					curEntryIndex++;
					$(row).append(cell);
					$(row).find(".Box").hover(function() {
						$(this).parent().addClass("VideoCell-Hover");
					},
					function() {
						$(this).parent().removeClass("VideoCell-Hover");
					});
				}
				$(table).append(row);
			}
			$("#" + YTFetchProfile.ContainerId + " .Content").html("").append(table);
		} else {
			$("#" + YTFetchProfile.ContainerId + " .Content").css("height","auto").html("No Videos Found");
		}
	},
	
	GenerateVideoBox: function(entry) {
		var titleText = entry.title.$t;
		
		var title = '<div class="Title">' + this.LimitString(titleText, 28) + '</div>';

		var ytIdLastSlashPos = entry.id.$t.lastIndexOf("/");
		var ytId = entry.id.$t.substring(ytIdLastSlashPos + 1, entry.id.$t.length);
		
		var imageUrl = entry.media$group.media$thumbnail[0].url;
		var image = '<div class="Image" title="' + titleText + '">'
		image += '<img src="' + imageUrl + '" alt="' + titleText + '" width="120" height="90" />';
		image += '</div>';
		
		var onClick = ' onclick="window.open(\'http://www.youtube.com/?v=' + ytId + '\');"';

		return '<div class="Box"' + onClick + '>' + image + title + '</div>';
	},
	
	LimitString: function(string, maxLength) {
		var result = string;
		if((string.length) > maxLength) {
			var newString = string.substring(0, maxLength - 3) + "...";
			result = "<span title=\""+ string +"\">" + newString + "</span>";
		}
		
		return result;
	}
}