
/**
 * JSON読み込み
 */
function getDelicious(tag, page)
{
	nowTag = tag;
	nowPage = page;
	$('#grid-content').empty();
	$('#page-nation').empty();
	$.post(
		"Delicious.php",
		{"tags": nowTag, "page":nowPage, "type":"bookmarks"},
		function(xml, status) {
			$(parseXML(xml)).appendTo('#grid-content');
			$(paging(nowPage, postMax)).appendTo('#page-nation');
			clickable();
		}
	);
}
/**
 * JSONパース
 */
function parseXML(xml)
{
	var result = "";
	postMax = $(xml).find("posts").attr("total");
	$(xml).find("post").each( function(){
		var url = $(this).attr("href");
		var others = $(this).attr("others");
		var hash = $(this).attr("hash");
		//var title = $(this).attr("description");
		var title = $(this).find("description").text();
		if(title.length >= 40){
			title = title.substring(0, 30) + "...";
		}
		var tags = '<div class="tags">';
		var tagsBase = $(this).attr("tag").split(" ");
		var tagsLen = tagsBase.length;
		for(var j=0; j<tagsLen; j++)
		{
			tags += '<span class="tag">' + tagsBase[j] + '</span>';
			if(j < tagsLen - 1)
			{
				tags += ' ';
			}
		}
		tags += "</div>";
		result += '<li class="post"><a href="' + url + '" target="_blank"><img src="http://capture.heartrails.com/200x130?' + url + '" width="200" height="130" class="thumb">';
		result += '<div class="title"><img src="images/icon_link.gif" width="12" height="11" class="icon_link">' + title + '</a><span class="others">&nbsp[<a href="http://delicious.com/url/' + hash + '" target="_blank"><img src="images/icon_delicious.gif" width="8" height="8" class="icon_delicious">' + others + '</a>&nbsp]</span></div><div class="dotline"></div>' + tags;
	});
	return result;
}
/**
 * 生成したリストのクリック設定。 
 */
function clickable()
{
	//ページネイション
	$(".next").mouseover(function(){
		this.style.textShadow = "none";
		this.style.cursor = "pointer";
		this.style.textDecoration = "underline";
	});
	$(".next").mouseout(function(){
		this.style.textShadow = "1px 1px 0 #fff";
		this.style.textDecoration = "none";
	});
	$(".next").click( function(){
		nowPage = nowPage + 1;
		getDelicious(nowTag, nowPage);
		return false;
	});
	$(".prev").mouseover(function(){
		this.style.textShadow = "none";
		this.style.cursor = "pointer";
		this.style.textDecoration = "underline";
		$(this).attr('style', 'text-shadow:none');
	});
	$(".prev").mouseout(function(){
		this.style.textShadow = "1px 1px 0 #fff";
		this.style.textDecoration = "none";
	});
	$(".prev").click( function(){
		nowPage = nowPage - 1;
		getDelicious(nowTag, nowPage);
		return false;
	});
	$(".link,.last").mouseover(function(){
		this.style.textShadow = "none";
		this.style.cursor = "pointer";
		this.style.textDecoration = "underline";
	});
	$(".link,.last").mouseout(function(){
		this.style.textShadow = "1px 1px 0 #fff";
		this.style.textDecoration = "none";
	});
	$(".link,.last").each(function(){
		$(this).click( function(){
			var id = parseInt($(this).text());
			nowPage = id;
			getDelicious(nowTag, nowPage);
			return false;
		});
	});
	
	//tag関連
	$(".tag").mouseover(function(){
		this.style.textShadow = "none";
		this.style.backgroundColor = "#000";
		this.style.color = "#FFF";
		this.style.cursor = "pointer";
	});
	$(".tag").mouseout(function(){
		this.style.textShadow = "1px 1px 0 #fff";
		this.style.background = "none";
		this.style.color = "#000";
	});
	$(".tag").each(function(i){
		$(this).click( function(){
			nowPage = 1;
			nowTag = $(this).text();
			getDelicious(nowTag, nowPage);
			return false;
		});
	});
	$("#grid-content").vgrid();
}
/**
 * tag読み込み
 */
function getTags()
{
	$.post(
		"Delicious.php",
		{"type": "tags"},
		function(xml, status) {
			var result = '';
			var temp = new Array();
			$(xml).find("tag").each( function(){
				var obj = new Object();
				obj.name = $(this).attr("tag");
				obj.number = $(this).attr("count");
				temp.push(obj);
			});
			temp.sort(function(a, b) {return b.number - a.number || a.name - b.name});
			var len = temp.length;
			var limit = 0;
			for(var i = 0;i < len;i++)
			{
				if(limit < 30)
				{
					result += '<span class="side"><span class="tag-nav">' + temp[i].name + '</span>(<span class="tag-number">' + temp[i].number + "</span>)</span>";
				}
				limit++;
			}
			$(result).appendTo('#tag-navigation');
			
			$(".tag-nav").mouseover(function(){
				this.style.textShadow = "none";
				this.style.backgroundColor = "#000";
				this.style.color = "#FFF";
				this.style.cursor = "pointer";
			});
			$(".tag-nav").mouseout(function(){
				this.style.textShadow = "1px 1px 0 #fff";
				this.style.background = "none";
				this.style.color = "#000";
			});
			$(".tag-nav").each(function(i){
				$(this).click( function(){
					nowPage = 1;
					nowTag = $(this).text();
					getDelicious(nowTag, nowPage);
					return false;
				});
			});
		}

	);
}

/**
 * ページネイション生成。
 */
function paging(now, max)
{
	var delta = 5;
	var perPage = 20;
	var totalPage = Math.floor(max / perPage) + 1;
	if(max <= 20)
	{
		totalPage = 1;
	}
	var page = (now) ? now : 1;
	var pager = "";
	var alpha = 0;
	var links = "";
	
	pager += '<ul>';
	if (page > 1) 
	{
		pager += '<li class="prev">&lt;&lt;</li>';
	}
	if (page <= delta) alpha = delta - page + 1;
	
	var len = Number(page) + delta + alpha;
	if(len >= totalPage) len = totalPage;
	for (var i = page - delta; i <= len; i++) 
	{
		if (i < 1) continue;
		if (i == page) 
		{
			preTag = '<li class="strong">&nbsp;&nbsp;&nbsp;';
		}
		else if(i == len)
		{
			preTag = '<li class="last">&nbsp;&nbsp;&nbsp;';
		}
		else
		{
			preTag = '<li class="link">&nbsp;&nbsp;&nbsp;';
		}
		aftTag = "&nbsp;&nbsp;&nbsp;</li>";
		links += preTag + i + aftTag;
	}
	//pager += links.substring(0,links.length-6) + '</li>';
	pager += links;
	if (page < totalPage)
	{
		pager += '<li class="next">&gt;&gt;</li> ';
	}
	pager += '</ul>';
	return pager;
}