$(function() {

	rollTimer = setInterval(goRoll,rollTime);

	
	$(".plus_btn").css("cursor","pointer").click(function() {
		$(".bottom_area").show();
	});
	$(".closebtn").css("cursor","pointer").click(function() {
		$(".bottom_area").hide();
	});

	$(".index_top_box").hover(
		function() {
			clearInterval(rollTimer);
		}
		,
		function() {
			rollTimer = setInterval(goRoll,rollTime);
		}
	);
	
	$(".box_p").css("cursor","pointer").click(function() {
		location.href = "/pages/earth/save_people.asp";
	});

	$("[savepeople]").css("cursor","pointer").click(function() {
		var direction = $(this).attr("savepeople");
		if (direction == "pre") {
			if (savePopleCurrentPage == 1) { return; }
			else {
				savePopleCurrentPage = savePopleCurrentPage - 1;
				saveActionprocess(savePopleCurrentPage,'4');
				$("[savepeople='next']").show();
			}
		} else {
			if (savePoplecnt < 3) { return; }
			else {
				savePopleCurrentPage = savePopleCurrentPage + 1;
				saveActionprocess(savePopleCurrentPage,'4');
			}
		}
	});

	$("[newsIndex]").hover(
		function() {
			if ($(this).attr("newsIndex") == newsIndex) { return; }
			else {
				$(this).attr("src",$(this).attr("src").replace(".gif","_on.gif"));
			}
		}
		,
		function() {
			if ($(this).attr("newsIndex") == newsIndex) { return; }
			else {
				$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"));

			}
		}
	);

	$("[newsIndex]").click(function() {
		$("div[id ^='boardList']").hide();
		$("#boardList_"+$(this).attr("newsIndex")).show();
		$("[newsIndex='1']").attr("src",$("[newsIndex='1']").attr("src").replace("_on.gif",".gif"));
		$("[newsIndex='2']").attr("src",$("[newsIndex='2']").attr("src").replace("_on.gif",".gif"));
		$("[newsIndex='3']").attr("src",$("[newsIndex='3']").attr("src").replace("_on.gif",".gif"));
		$(this).attr("src",$(this).attr("src").replace(".gif","_on.gif"));
		newsIndex = $(this).attr("newsIndex");
	});



	$(".bottom_area > ul > li ").hover(
		function() {
			$(this).css("cursor","pointer");
			if ($(this).attr("class") != "closebtn") {
				if (rollNum-1 == -1) {
					$(this).children("img").attr("src",$(this).children("img").attr("src").replace(".gif","_on.gif"));
				} else {
					if ($(".bottom_area img:eq("+(rollNum-eventImgCnt)+")").attr("src") == $(this).children("img").attr("src")) { return; }
					else {
						$(this).children("img").attr("src",$(this).children("img").attr("src").replace(".gif","_on.gif"));
					}
				}
			}
		}
		,
		function() {
			if ($(this).attr("class") != "closebtn") {
				if (rollNum-1 == -1) {
					$(this).children("img").attr("src",$(this).children("img").attr("src").replace("_on.gif",".gif"));
				} else {
					if ($(".bottom_area img:eq("+(rollNum-eventImgCnt)+")").attr("src") == $(this).children("img").attr("src")) { return; }
					else {
						$(this).children("img").attr("src",$(this).children("img").attr("src").replace("_on.gif",".gif"));
					}
				}
			}
		}
	);


	$(".bottom_area > ul > li > img").click(function() {

		if ($(this).parent("li").attr("class") == "closebtn")	 { return; }
		if ($(this).attr("rollIndex") == rollNum)	 { return; }
		else {
			$(this).attr("src", $(this).attr("src").replace("_on.gif",".gif"));
			for (i=0; i <= totalRollImgCnt; i++) {
				if ($(this).attr("rollIndex") == rollNum) { break; }
				$("#imgboxList > img:eq(0)").css("width","0");
				$("#imgboxList > img:eq(0)").hide();
				$("#imgboxList > img:eq(1)").show();
				$("#imgboxList > img:eq(1)").css("width","946");
				
				$("#imgboxList").append($("#imgboxList > img:eq(0)"));
				$(".bottom_area img").each(function() {
					$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"))
				});
		
				$(".bottom_area img").each(function() {
					$(this).attr("rollIndex") == $("#imgboxList > img:eq(0)").attr("rollIndex") ?
						$(this).attr("src",$(this).attr("src").replace(".gif","_on.gif"))
						:
						"";
				});
				rollNum = rollNum + 1;
				if (totalRollImgCnt < rollNum) {
					rollNum = 0;
				}
			}
		}

	});

	$(".rollLeft").click(function() {
		rollDirection("left");
	});
	$(".rollRight").click(function() {
		goRoll();
	});

});

function goRoll(){ 
	$("#imgboxList > img:eq(0)").animate({width:0},rollSpeed);
	$("#imgboxList > img:eq(1)").show();
	$("#imgboxList > img:eq(1)").animate({width:946},rollSpeed,function() {
		$("#imgboxList > img:eq(0)").hide();
		$("#imgboxList").append($("#imgboxList > img:eq(0)"));

		$(".bottom_area img").each(function() {
			$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"))
		});
		
		$(".bottom_area img").each(function() {
			$(this).attr("rollIndex") == $("#imgboxList > img:eq(0)").attr("rollIndex") ?
				$(this).attr("src",$(this).attr("src").replace(".gif","_on.gif"))
				:
				"";
		});

		rollNum = rollNum + 1;
		if (totalRollImgCnt < rollNum) {
			rollNum = 0;
		}
		
	});

}

function rollDirection(direction) {
	if (direction == "left") {
		$("#imgboxList").prepend($("#imgboxList > img:last"));
		$("#imgboxList > img:eq(1)").animate({width:0},rollSpeed);
		$("#imgboxList > img:eq(0)").show();
		$("#imgboxList > img:eq(0)").animate({width:946},rollSpeed,function() {
			$("#imgboxList > img:eq(1)").hide();

			$(".bottom_area img").each(function() {
				$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"))
			});
		
			$(".bottom_area img").each(function() {
				$(this).attr("rollIndex") == $("#imgboxList > img:eq(0)").attr("rollIndex") ?
					$(this).attr("src",$(this).attr("src").replace(".gif","_on.gif"))
					:
					"";
			});
			rollNum = rollNum - 1;
			if (rollNum < 0) {
				rollNum = 0;
			}
		});
	}
}



function saveActionprocess(nextPage,listline) {
		$.ajax({
			type:"GET",
			url:"/common/api/saveAction.asp?process=indexmoreCmt&saveCategory=1001&page=" + nextPage + "&listline=" + listline,
			error: function() { alert("처리실패");},
			success: function(msg) {
				savePoplecnt = msg.split("/|/")[1];
				savePoplecnt < 3 ? $("[savepeople='next']").hide() : "";
				if (savePoplecnt == -1) {
					savePopleCurrentPage = savePopleCurrentPage - 1
					return;
				}

				$("#indexSaveList").hide();
				$("#indexSaveList").html(msg.split("/|/")[0]);
				$("#indexSaveList").fadeIn(500);
				$(".box_p").css("cursor","pointer").click(function() {
					location.href = "/pages/earth/save_people.asp";
				});
				savePoplecnt = msg.split("/|/")[1];
				savePoplecnt < 3 ? $("[savepeople='next']").hide() : "";
			}
		});
	}


