/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(function() {
	var url = location.href;
	var target = url.match("^https?:\/\/" + location.hostname + "/(.+)/");
	if (navigator.userAgent.indexOf('iPhone') > 0 || navigator.userAgent.indexOf('iPad') > 0 || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {
		if(target[1] == 'top') {
			actionSmartPhone(4);
		} else {
			actionSmartPhone(3);
		}
	} else {
		$("#news .content").css('overflow-y', 'auto');
		$("#news .content").css('height', '100px');
		if(target[1] == 'top') {
			$("#news .content").css('margin-right', '15px');
		}
		$("#news .content").show();
	}

});
function actionSmartPhone(value) {
	cnt = value;
	flg = 1;
	speed = 500;
	select = 0;
	move = 0;
	max = $("#news .content tr").length;
	minHeight = 26;
	$("#news .content").show();
	set_top = $("#news .content").offset().top;
	$("#news .content").css("position", "relative");
	$("#news .content").css("overflow", "hidden");
	setHeight();
	$("#news .content table").css("position", "absolute");
	$("#news .btn").css("display","block");
	checkBtn ();

	$("#news .btn .up img").click(function(){
		if (select > 0 && flg == 1) {
			changeNews(-1*cnt);
		}
	});
	$("#news .btn .down img").click(function(){
		if(select < (max-cnt) && flg == 1){
			changeNews(cnt);
		}
	});
}
function changeNews(value){
	flg = 0;
	select=select+value;
	setHeight();
	move += set_top-($("#news .content tr").eq(select).offset().top);

	$("#news .content table").animate({"top": move},speed,function(){flg = 1;checkBtn();});
}
function checkBtn () {
	if (select <= 0) {
		$("#news .btn .up img").css("cursor", "default");
		$("#news .btn .up img").fadeTo(0, 0.6);
	} else {
		$("#news .btn .up img").css("cursor", "pointer");
		$("#news .btn .up img").fadeTo(0, 1);
	}
	if (select >= (max-cnt)) {
		$("#news .btn .down img").css("cursor", "default");
		$("#news .btn .down img").fadeTo(0, 0.6);
	} else {
		$("#news .btn .down img").css("cursor", "pointer");
		$("#news .btn .down img").fadeTo(0, 1);
	}
}
function setHeight() {
	var hei = 0;
	for (var i=select; i<$("#news .content tr").length && i<select+cnt; i++) {
		hei += $("td",$("#news .content tr").eq(i)).eq(0).innerHeight();
	}
	if (hei < minHeight*cnt) {
		hei = minHeight*cnt;
	}
	$("#news .content").css("height", hei+"px");
}

