var p=0;
var mp=0;

function prcheck()
{
	if ($('input#u').val() == null || $('input#u').val() == '') {
		searchOff();
		return;
	}

	searchOn();

	$.ajax({
		'type': 'POST',
		'url': '/pr.php',
		'dataType': 'text',
		'data': {
			'u': $('input#u').val(),
			'p': $('input#p').attr('checked')
		},
		'success': function(rank) {
			prwrite(rank);
		}
	});
}

function prwrite(rank)
{
	$('#pr').html('<h1 class="pr">' + rank + '</h1><h2 class="pr">/10</h2><br /><img src="images/pr' + rank + '.gif" />');

	searchOff();

	if ($('input#p').val() == 1) {
		list();
	}
}

function searchOn()
{
	$('#pr').css('display', 'none');
	$('#p_i').css('display', 'block');
}

function searchOff()
{
	$('#pr').css('display', 'block');
	$('#p_i').css('display', 'none');
}

function list()
{
	listOn();
	
	$.ajax({
		'type': 'POST',
		'url': '/list.php',
		'dataType': 'html',
		'success': function(list) {
			window.setTimeout(lstwrite(list), 0);
		}
	});
}

function lstwrite(list)
{
	listOff();
	
	$('#list').html(list);
}

function listOn()
{
	$('#l_i').css('display', 'block');
}

function listOff()
{
	$('#l_i').css('display', 'none');
}

function navi(p)
{
	var pager='';
	

	if (0 < p) {
		pager+='<a href="#" onclick="javascript:prevPage();return false;">&laquo; 前へ</a>';
	} else {
		pager+='&laquo; 前へ';
	}

	pager+='&nbsp;&nbsp;&nbsp;';

	if ((p+1) * parseInt($('select#rc').val(),10) < mp) {
		pager+='<a href="#" onclick="javascript:nextPage();return false;">次へ &raquo;</a>';
	} else {
		pager+='次へ &raquo;';
	}
	
	$('#header_pager').html(pager);
	$('#bottom_pager').html(pager);
}

function nextPage()
{
	p++;
	summary(p);
}

function prevPage()
{
	p--;
	summary(p);
}

function pointPage(o)
{
	summary(o);
}

function summary(o)
{
	p=o;
	
	summaryOn();
	
	$.ajax({
		'type': 'POST',
		'url': '/l.php',
		'dataType': 'xml',
		'data': {
			'o': o,
			'rc': $('select#rc').val()
		},
		'success': function(xml) {
			window.setTimeout(function(){smrywrite(xml);}, 0);
		}
	});
}

var xpos=0;

function smrywrite(xml)
{
	var item=xml.getElementsByTagName('item');
	if (item.length <= 0) {
		return true;
	}

	xpos=0;
	window.setTimeout(function(){smrywriting(xml);}, 0);
}

function smrywriting(xml)
{
	if (xpos==0) {
		var c=[];
		c.push('<table width="100%" border="0" cellspacing="0" cellpadding="0" id="publish_list">');
		c.push('</table>');
		$('#summary').html(c.join(''));
	}

	var item=xml.getElementsByTagName('item');
	var c=[];
	for (xpos=0; xpos < item.length; xpos++) {
		c.push('<tr>');
		c.push('<td class="thumbnail"><img src="/images/mpr' + item[xpos].getAttribute('rank') + '.gif" width="60" height="60" /></td>');
		c.push('<td class="url"><a href="');
		c.push(item[xpos].getAttribute('url') + '" target="_blank" style="background:url(http://favicon.qfor.info/f/' + item[xpos].getAttribute('url') + ') left center no-repeat;padding:1px 0 1px 18px;display:block;">');
		c.push(item[xpos].getAttribute('title') + '</a>');
		c.push('<span class="ent_date">' + item[xpos].getAttribute('ent_date') + '</span><br />');
		c.push('<div class="description">' + item[xpos].getAttribute('description') + '</div>');
		c.push('<div class="status">');
		c.push('<a href="http://ja.webscouter.net/info.phtml?u=' + item[xpos].getAttribute('url') + '" target="_blank"><img src="http://jaicon.webscouter.net/icon.phtml?u=' + item[xpos].getAttribute('url') + '" alt="Web スカウター" border="0" width="80" height="15"></a>');
		c.push(' &nbsp; ');
		c.push('<a href="http://b.hatena.ne.jp/entry/' + item[xpos].getAttribute('url') + '" target="_blank"><img src="http://b.hatena.ne.jp/images/append.gif" width="16" height="12" /></a><a href="http://b.hatena.ne.jp/entry/' + item[xpos].getAttribute('url') + '" target="_blank"><img src="http://b.hatena.ne.jp/entry/image/' + item[xpos].getAttribute('url') + '" height="13" /></a>');
		c.push(' &nbsp; ');
		c.push('<a href="http://clip.livedoor.com/redirect?link=' + item[xpos].getAttribute('url') + '" target="_blank"><img src="http://parts.blog.livedoor.jp/img/cmn/clip_16_12_w.gif" width="16" height="12" /></a><a href="http://clip.livedoor.com/page/' + item[xpos].getAttribute('url') + '" target="_blank"><img src="http://image.clip.livedoor.com/counter/' + item[xpos].getAttribute('url') + '" height="12" /></a>');
		c.push('</div></td>');
		c.push('</tr>');
	}

	$('#publish_list').html($('#publish_list').html() + c.join(''));
	
	var total=xml.getElementsByTagName('total');
	mp=total[0].getAttribute('count');
	
	var rc=parseInt($('select#rc').val(),10);
	var last_p=((p*rc)+rc);
	if (mp < last_p) last_p=mp;
	$('#total_count').html('<strong>' + mp + '</strong> 件中 <strong>' + ((p*rc)+1) + '</strong> - <strong>' + last_p + '</strong> 件目を表示');
	
	navi(p);

	summaryOff();
}

function summaryOn()
{
	$('#s_i').css('display', 'inline');
}

function summaryOff()
{
	$('#s_i').css('display', 'none');
}

