function tvPlay(url) {
	$f('tvPlayer').play(url);
	location.hash = url;
	$('body').scrollTop(0);
}

function tvPlaylistAdd(url) {
	tvPlaylist.push(url);
}

function tvPlayNext(sv) {
	tvPlayCur = tvPlayCur + 1;
	url = tvPlaylist[tvPlayCur];
	tvPlay(url);
}

function videoDiv(o, i) {
	html = '<div class="vidln' + (i%2 ? 2 : '') + '" id="tvPlaylist' + o.id  + '">';
	html += '<div class="lft">' + i + '</div>';
	html += '<a href="javascript:tvPlay(\'' + o.file + '\')"><img src="http://s.transmissia.net/video/tmb/' + o.poster + '" alt="" style="float: left;"/>' + o.name + '</a>';
	html += '</div>';
	return html;
}

function tvLoadCat(cat) {
	$.get('/ajax/tv?cat=' + cat + '&limit=15', function(a) {
		tvPlaylist = new Array();
		html = '<div style="margin: 5px; text-align: right;"><a href="/tv/" style="text-decoration: none; font-weight: bold; color: white;">к главному плейлисту</a></div>';
		n = 1;
		for(var i in a) {
		    html += videoDiv(a[i], n++);
			tvPlaylistAdd(a[i].file);
		}
		$('#tvPlaylist').html(html);
		tvPlay(a[0].file);
	}, 'json');
}

function tvPageCat(cat, nav) {
	catDiv = 'tvPageCat' + cat.capitalize();
	switch(nav) {
		case 'bck':
			if(tvCurPage == 0) break;
			tvCurPage--;
		break;
		case 'fwd':
			tvCurPage++;
		break;
		default: break;
	}
	ajaxUrl = '/ajax/tv?cat=' + cat + '&page=' + tvCurPage;
	$.get(ajaxUrl, function(a) {
		html = '';
		n = 1;
		for(var i in a) {
		    html += videoDiv(a[i], n++);
		}
		$('#' + catDiv).html(html);
	}, 'json');
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

