/*
		.me2rankbox { margin: 0 0px; float: left; background: #eeeeee; padding: 5px; width: 190px; height: px; overflow: hidden; border-bottom: solid 1px #d1d1d1; border-top: solid 1px #ffffff; font: 10pt 'gill sans mt', 'Trebuchet MS'; }
		.me2rankbox:hover { background: #ffffff; }
		.me2rank { float: left; padding-top: 10px; width: 30px; font: 15pt 'gill sans mt', 'Trebuchet MS'; text-align: center; margin-right: 10px; }
		.me2profile { float: left; width: 48px; height: 48px; border: solid 1px #000000; }
		.me2id { float: left; width: 80px; padding: 6px 10px 0 10px; text-decoration: none; overflow: hidden; }
		.me2score { float: left; width: 80px; padding: 0 10px 5px 10px; text-decoration: none; overflow: hidden; }
*/

DPMe2Rank = {
	container: null,

	ids: null,

	score: null,

	index: 0,

	create: function() {
		if( !DPMe2Rank.container ) {
			DPMe2Rank.container = new WINDOW('dp.me2ranking');
			DPMe2Rank.container.create(0, 0, 400, 310, 'CLOSE');
			DPMe2Rank.container.setTitle("Ranking");
			DPMe2Rank.container.center();
			
			YAHOO.util.Dom.setStyle(DPMe2Rank.container.container, "zIndex", "999");
			DPMe2Rank.container.close = DPMe2Rank.hide;

			DPMe2Rank.ids = ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'];
			DPMe2Rank.score = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000];

			for( var i = 0; i < 10; i++ ) {
				DPMe2Rank.container.addHTML(DPMe2Rank.getRankString(i + 1, DPMe2Rank.ids[i], DPMe2Rank.score[i]));
			}

			DPMe2Rank.hide();
		}
	},

	batchUpdate: function(ids, score) {
		if( !ids || !score ) {
			return;
		}

		while( ids.length < 10 ) {
			ids.push('-');
		}
		while( score.length < 10 ) {
			score.push(10000);
		}

		DPMe2Rank.ids = ids;
		DPMe2Rank.score = score;

		for( var i = 0; i < 10; i++ ) {
			DPMe2Rank.setRankString(i + 1, DPMe2Rank.ids[i], DPMe2Rank.score[i]);
		}
	},

	updateImage: function() {
		if( !DPMe2Rank.index ) {
			DPMe2Rank.index = 1;
		}

		if( DPMe2Rank.index < 0 || DPMe2Rank.index > 10 ) {
			return;
		}

		var id = YAHOO.util.Dom.get('me2id' + DPMe2Rank.index).innerHTML;
		//var id = DPMe2Rank.ids[DPMe2Rank.index - 1];
		DPFileExist.query('http://me2day.net/images/user/' + id + '/profile.png');
		DPFileExist.onSuccess = function(o) {
			eval('var image = ' + o.responseText);
			YAHOO.util.Dom.get('me2profile' + DPMe2Rank.index).src = image.url;
			if( DPMe2Rank.index >= 10 ) {
				DPMe2Rank.index = 0;
				return;
			}
			else {
				DPMe2Rank.index++;
				DPMe2Rank.updateImage();
			}
		}
		DPFileExist.onFailure = function(o) {
			DPMe2Rank.index == 0;
		}
	},

	clear: function() {
		var ids = ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'];
		var score = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000];
		DPMe2Rank.batchUpdate(ids, score);
	},

	setRank: function(id, score) {
		var rank = DPMe2Rank.getRank(score);

		if( rank > 10 ) {
			return;
		}

		for( var i = 10; i > rank; i-- ) {
			DPMe2Rank.ids[i - 1] = DPMe2Rank.ids[i - 2];
			DPMe2Rank.score[i - 1] = DPMe2Rank.score[i - 2];
			DPMe2Rank.setRankString(i, DPMe2Rank.ids[i - 1], DPMe2Rank.score[i - 1]);
		}
		DPMe2Rank.ids[rank - 1] = id;
		DPMe2Rank.score[rank - 1] = score;
		DPMe2Rank.setRankString(rank, id, score);
	},

	getRank: function(score) {
		for( var i = 10; i > 0; i-- ) {
			if( score >= DPMe2Rank.score[i - 1] ) {
				return i + 1;
			}
		}

		return 1;
	},

	setRankString: function(rank, id, score) {
		if( (score !=0 && !score) || rank < 1 || rank > 10 ) {
			return;
		}

		var image = (id == '-') ? 'http://ideapool.co.kr/me2day/minesweeper/none.gif' : 'http://me2day.net/images/user/' + id + '/profile.png';
		var href = (id == '-') ? '#' : 'http://me2day.net/' + id;
		var sec = (id == '-') ? '-' : score + 'sec';
		YAHOO.util.Dom.get('me2rank' + rank).innerHTML = rank;
		//YAHOO.util.Dom.get('me2profile' + rank).src = image;
		YAHOO.util.Dom.get('me2id' + rank).href = href;
		YAHOO.util.Dom.get('me2id' + rank).innerHTML = id;
		YAHOO.util.Dom.get('me2score' + rank).innerHTML = sec;
	},

	getRankString: function(rank, id, score) {
		var image = (id == '-') ? 'http://ideapool.co.kr/me2day/minesweeper/none.gif' : 'http://me2day.net/images/user/' + id + '/profile.png';
		var href = (id == '-') ? '#' : 'http://me2day.net/' + id;
		var sec = (id == '-') ? '-' : score + 'sec';
		return "<div class='me2rankbox' id='me2rankbox" + rank + "'>" 
			 + "   <div class='me2rank' id='me2rank" + rank + "'>" + rank + "</div>"
			 + "   <img class='me2profile' id='me2profile" + rank + "' src='" + image + "' />"
			 + "   <div class='me2id'><a id='me2id" + rank + "' href='" + href + "'>" + id + "</a></div>"
			 + "   <div class='me2score' id='me2score" + rank + "'>" + sec + "</div>"
			 + "</div>";
	},

	handleKey: function(e) {
		if( e.keyCode != 13 && e.keyCode != 27 ) {
			return;
		}
	},

	show: function() {
		DPModal.show();
		DPMe2Rank.container.show();
	},

	hide: function() {
		DPModal.hide();
		DPMe2Rank.container.hide();
	}
};
