function setDateFormat ( date_obj ) {
	
	var mm = date_obj.getMonth() + 1;
	var dd = date_obj.getDate() ; 
	
	var date_str = ( mm < 10 ? "0" + mm : mm ) + "." + ( dd < 10 ? "0" + dd : dd );
	return date_str;
}

function SearchNavigation( rank_date, year, month, day ) {

	var pre_date = new Date(year, month - 1, parseInt(day, 10) - 1 );
	var next_date = new Date(year, month - 1, parseInt(day, 10) + 1 );
	var now_date = new Date();

	var s_date = "04.10";
	var e_date = "04.30";

	pre_date = setDateFormat( pre_date );
	next_date = setDateFormat( next_date );
	now_date = setDateFormat( now_date );

	var sOutput = "";
	if ( pre_date < s_date ) {
		sOutput += "<li class=\"prev\">" + pre_date + "</li>";
	} else {
		sOutput += "<li class=\"prev\" onmouseover=\"this.className='prev_on';\" onmouseout=\"this.className='prev';\"><a href=\"\" onclick=\"DailyRanking('" + pre_date + "');return false;\">" + pre_date + "</a></li>";
	}

	// realtime update
	if ( now_date == rank_date ) {
		sOutput += "<li class=\"today\"><a href=\"\" onclick=\"DailyRanking('" + rank_date + "');return false;\">" +  year + "." + month + "." + day + "</a></li> ";
	} else {
		sOutput += "<li class=\"today\"><a href=\"javascript:void(0);\">" +  year + "." + month + "." + day + "</a></li> ";
	}

	if ( next_date > now_date || next_date > e_date ) {
		sOutput += "<li class=\"next\">" + next_date + "</li>";
	} else {
		sOutput += "<li class=\"next\" onmouseover=\"this.className='next_on';\" onmouseout=\"this.className='next';\"><a href=\"\" onclick=\"DailyRanking('" + next_date + "');return false;\">" + next_date + "</a></li>";
	}

	$("date_tab").innerHTML = sOutput;
}

function Draw( EVENT_JSON ) {

	var sOutput = "";
	EVENT_JSON.each(function(item, idx) {
		sOutput += " \
		<li class=\"num" + item.ranking + "\" onmouseover=\"this.style.backgroundColor='#000'\" onmouseout=\"this.style.backgroundColor='#262305'\"> \
			<span class=\"id\"><a href=\"/user/" + item.id + "\" target=\"_blank\">" + item.id + "</a></span> <span class=\"num\"><em>" + item.cnt + "</em> °³</span> \
		</li>";
	});

	$('rank_list').innerHTML = sOutput;
}

function DailyRanking( rank_date ) {

	var rDate = rank_date.split(".",2);
	var year = new Date().getYear();
	var month = rDate[0];
	var day = rDate[1];

	var url = "/event/ajax/ranking";
	var param = "date=" + year + "-" + month + "-" + day ;

	new Empas.Ajax.Request(url, {
		parameters: param,
		onComplete: function(transport) {
			// ¼øÀ§ ±×¸®±â
			var response = transport.responseText;
			if (response!="") {
				eval(response);
				Draw(EVENT_JSON);
				SearchNavigation( rank_date, year, month, day );
			}
		},
		onFailure: function(transport) {
			window.alert("¼øÀ§ °Ë»ö ½ÇÆÐ");
		}
	});

	return false;
}
