// JavaScript Document
function init() {
	sumContent = $("#sum").attr("value");
	$("#t3").html(sumContent);
	if (sumContent != 0) {
		sumPage = Math.ceil(sumContent/sizePage);
		$("#sumPage").html(sumPage);
		
		initPage();
		showPage();
	}
}

function initPage() {
	var htmlSpacer = "<td><img src='images/spacer.gif' width='3' height='1'/></td>";
	var htmlBegin = "<td><a href='#' onClick='backPage();'><img src='images/arrow_back.gif' width='10' height='16' border='0'/></a></td>";
	var htmlEnd = "<td><a href='#' onClick='nextPage();'><img src='images/arrow_next.gif' width='10' height='16' border='0'/></a></td>";
	
	$("#tablePagging").append(htmlBegin);
	
	for (var i = 1;i <= sumPage;i++) {
		var htmlNumber = "<td id='tdPagging" + i + "' class='gray-dark padding-content-pagging' style='font-weight:bold;'><a href='#' onClick='nextPage(gotoPage(" + (i - 1)+ "));'>" + i + "</a></td>";
		$("#tablePagging").append(htmlSpacer);
		$("#tablePagging").append(htmlNumber);
	}
	
	$("#tablePagging").append(htmlSpacer);
	$("#tablePagging").append(htmlEnd);
}

function initPageCss() {
	for (var i = 1;i <= sumPage;i++) {
		$("#tdPagging" + i).removeClass();
		if (i == currentPage) {
			$("#tdPagging" + i).addClass("blue-background white padding-content-pagging");
		} else {
			$("#tdPagging" + i).addClass("gray-dark padding-content-pagging");
		}
	}
}

function nextPage() {
	currentPage++;
	if (currentPage > sumPage) {
		currentPage = sumPage
		alert("Your current the last page!");
	} else {
		showPage();
	}
}

function backPage() {
	currentPage--;
	if (currentPage < 1) {
		currentPage = 1;
		alert("Your current the first page!");
	} else {
		showPage();
	}
}

function gotoPage(value) {
	currentPage = value;
	showPage();
}

function showPage() {
	var x = (currentPage * sizePage) - sizePage;
	var y = (currentPage * sizePage) + 1;
	
	var j = 0;
	var k = 0;
	for (var i = 1;i <= sumContent;i++) {
		if (i > x && i < y) {
			$("#" + i).css("display", "");
			j++;
		} else {
			$("#" + i).css("display", "none");
		}
	}
	
	$("#t1").html(((currentPage - 1) * sizePage) + 1);
	k = sizePage * currentPage;
	if (k > sumContent) {
		$("#t2").html(sumContent);
	} else {
		$("#t2").html(k);
	}
	
	initPageCss();
}