var MEDIA_ID;

function buildComment(comment) {
	return $('<div/>').addClass('comment').addClass('clearfix').append($('<div/>').addClass('avatar').append($('<a/>').attr('href', comment.user.profile).attr('title', comment.user.username).append($('<img/>').attr('src', comment.user.avatarSrc)))).append($('<div/>').addClass('body').append($('<p/>').addClass('posted').html('Posted by <a href="'+comment.user.profile+'" title="'+comment.user.username+'">'+comment.user.username+'</a> '+comment.createdAgo+':')).append(comment.body))
}

function loadMedia(id) {
	MEDIA_ID = id;
	
	var url = DYNAMIC_BASE+'/cuetv/load-media?mediaId='+id;
	$.get(
		url,
		function (d) {
			var media = eval('('+d+')');
			
			$('div#featured-video').html('');
			
			// player
			
			$('div#featured-video').append($('<div/>').attr('id', 'video-player'));
			
			var so = new SWFObject("http://stream.zoopy.com/video-onsite.swf", "videodetail", media.width, media.height, "9", "#000000");
			so.addParam("flashvars", "id="+media.id);
			so.addParam("allowfullscreen", "true");
			so.addParam("allowscriptaccess", "always");
			so.addParam("wmode", "transparent");
			so.write("video-player");
			
			// meta
			
			$('div#featured-video').append($('<div/>').addClass('meta').addClass('clearfix').append($('<div/>').addClass('avatar').append($('<a/>').attr('href', media.user.profile).attr('title', media.user.username).append($('<img/>').attr('src', media.user.avatarSrc)))).append($('<div/>').addClass('posted').append($('<h2/>').html(media.title)).append($('<span/>').html('Posted by <a href="" title="" style="background-image:url('+STATIC_BASE+'/img/flags/'+media.user.country.code+'.png)">'+media.user.username+'</a> '+media.createdAgo))));
			
			// description
			
			$('div#featured-video').append($('<div/>').addClass('description').html(media.description));
			
			// views
			
			$('div#featured-video').append($('<div/>').addClass('views').html(media.views+' view'+(media.views == '1' ? '' : 's')));			
			
			// comments
			
			$('div#comment-list').html('');
			
			$('div#comment-list').append($('<h3/>').html(media.commentCount+' comment'+(media.commentCount == '1' ? '' : 's')));
			
			for (i in media.comments) {
				var comment = media.comments[i];
				$('div#comment-list').append(buildComment(comment));
			}
		}
	);
	
}

function loadPagination(page) {
	var url = DYNAMIC_BASE+'/cuetv/load-pagination?page='+page;
	$.get(
		url,
		function (d) {
			var data = eval('('+d+')');
			
			// media
			
			$('div#more-videos div.thumbs').html('');

			for (i in data.results) {
				var media = data.results[i];				
				
				if (typeof MEDIA_ID == 'undefined') {
					loadMedia(media.id);
				}
				
				var imgA = $('<a/>').attr('id', 'more-videos-'+media.id).attr('href', '#').attr('title', media.title).append($('<img/>').attr('src', media.thumbSrc)).click(function (e) {
					stop(e);
					$(this).blur();
					var id = $(this).attr('id').replace('more-videos-', '');
					loadMedia(id);
				});
				var titleA = $('<a/>').addClass('title').attr('id', 'more-videos-title-'+media.id).attr('href', '#').attr('title', media.title).html(media.titleTrunk).click(function (e) {
					stop(e);
					$(this).blur();
					var id = $(this).attr('id').replace('more-videos-title-', '');
					loadMedia(id);
				});
				var created = $('<span/>').html(media.createdAgo);
				var user = $('<a/>').addClass('user').attr('href', media.user.profile).attr('title', media.user.username).html(media.user.usernameTrunk);
				var div = $('<div/>').append(imgA).append(titleA).append(created).append(user);
				$('div#more-videos div.thumbs').append(div);
			}
			
			// pagination
			
			$('div#more-videos div.pagination').html('');
			
			for (i = 1; i < data.pages; i ++) {
				if (i == data.page) {
					var span = $('<span/>').html(i);
					$('div#more-videos div.pagination').append(span).append(' ');
				}
				else {
					var a = $('<a/>').attr('href', '#').attr('title', i).html(i).click(function (e) {
						stop(e);
						$(this).blur();
						loadPagination(parseInt($(this).html()));
					});
					$('div#more-videos div.pagination').append(a).append(' ');
				}
			}		
			
			$('div#more-videos div.pagination').prepend($('<span/>').html('Page: '));
		}	
	);
}

function submitComment(e) {
	stop(e);
	var body = $('textarea#new-comment').val();
	
	if (typeof MEDIA_ID != 'undefined' && body != '') {
		
		var url = DYNAMIC_BASE+'/cuetv/submit-comment';		
		$.post(
			url,
			{ mediaId: MEDIA_ID, body: body },
			function (d) {
				var comment = eval('('+d+')');
				$('div#comment-list').find('h3').after(buildComment(comment));	
				$('textarea#new-comment').val('');
				$('div#comment-list h3').html(comment.commentCount+' comment'+(comment.commentCount == '1' ? '' : 's'));
			}		
		)
		
	}
}

$(function () {
	loadPagination(1);
	$('input#new-comment-submit').click(submitComment);
});