//トップのランキングタブ
$(function(){
	$("ul.rankingField li:not("+$("ul.rankingTab li a.selected").attr("href")+")").hide();
	getCategoryRank($("ul.rankingTab li a.selected").attr("category"),$("ul.rankingTab li a.selected").attr("href"));
	$("ul.rankingTab li a").click(function(){
		$("ul.rankingTab li a").removeClass("selected");
		$(this).addClass("selected");
		$("ul.rankingField li").hide();
		$($(this).attr("href")).show();
		getCategoryRank($(this).attr('category'),$(this).attr('href'));
		return false;
	});
});


function getCategoryRank(category,id)
{
	$(id).html('<span class="loadingAnime"><img src="/js/loading.gif"></span>');
	var html = "";
	$.getJSON("/ajax/top/" + '?category=' + category + '&rand='+ Math.random(),
	function(data){
		var box = $(id);
		$.each(data, function (i,item) {
			item.title = item.title.replace(/&/g, "&amp;");
			item.title = item.title.replace(/"/g, "&quot;");
			item.title = item.title.replace(/</g, "&lt;");
			item.title = item.title.replace(/>/g, "&gt;");
			
			item.text = item.text.replace(/&/g, "&amp;");
			item.text = item.text.replace(/"/g, "&quot;");
			item.text = item.text.replace(/</g, "&lt;");
			item.text = item.text.replace(/>/g, "&gt;");

			item.title = truncate(item.title,24);
			item.text = truncate(item.text,80);
			item.text = autoLink(item.text);
			item.rank = i + 1;
			html += template(tpStr,item);
		});
		box.html(html);
	});
}

var tpStr = '\
<div class="rankArea">\
<span class="video12090 imgL"><a href="/video/#{contents_id}"><img src="/movie/l_#{thum_path}" alt="#{title}"/></a></span>\
<img src="img/top/icon_no#{rank}.png" width="40" height="30" alt="No.#{rank}" class="rankingIcon" />\
<dl>\
<dt class="title"><img src="/img/top/ranking_no#{rank}.gif" width="45" height="17" alt="NO.#{rank}" /><a href="/video/#{contents_id}">#{title}</a></dt>\
<dd>\
<p><a href="/userpage/#{uid}">#{user_name}</a></p>\
<span class="user3030 imgL"><a href="/userpage/#{uid}"><img src="/images/ss_#{image}" alt="#{user_name}"/></a></span>\
#{text}\
</dd>\
<dt class="icon">\
<span class="icon_play" title="再生回数">#{play}</span>\
<span class="icon_clap" title="拍手回数">#{ovation}</span>\
<span class="icon_favorite" title="お気に入り数">#{mylist}</span>\
<span class="icon_comment" title="コメント数">#{comment}</span>\
</dt>\
</dl>\
</div>\
';

/** 
 * 文字数調節
 */
function truncate(str,max)
{
	if(str.length > max){
		return str.substring(0,max) + "…";
	}
	return str;
}

/** 
 * 自動リンク
 */
function autoLink(string)
{
	return string.replace(/\/(\w+)\/(\d+)/gi,'<a href="/$1/$2">/$1/$2</a>');
}