/*************************************************************************************************************
	
	SCORE
	- set_score
	- get_score
	- init_score

*************************************************************************************************************/

SCORE = function(game) {
	SCORE.superclass.constructor.call(this, game);
}

Class(
	SCORE, OBJECT,

	"init", function(id, parent_id) {
		SCORE.superclass.init.call(this, game);
		this.game = id;
		this.callback = {
			success: this.handle_success,
			failure: this.handle_failure,
			scope: this
		};
	},

	"ajax_call", function(url) {
		YAHOO.util.Connect.asyncRequest("GET", url, this.callback, null);
	},

	"handle_success", function(o) {
		if( o.responseText === undefined ) {
			return;
		}

		var data = o.responseText.split(" ");
		var type = data[0];

		if( type == "GET_SCORE" ) {
			if( data.length < 3 ) {
				return;
			}
					
			var max = 20;

			if( !this.score_wnd ) {
				this.score_wnd = new MODAL_WINDOW("score_window");
				this.score_wnd.create(0, 0, 250, 21 + Math.min(parseInt((data.length-1)/2), max) * 19);
				this.score_wnd.setTitle("HIGH SCORE");
				this.score_wnd.center();
				YAHOO.util.Dom.setStyle(this.score_wnd.client, "font", "9pt courier new");
				YAHOO.util.Dom.setStyle(this.score_wnd.client, "color", "#ff0000");
				YAHOO.util.Dom.setStyle(this.score_wnd.client, "lineHeight", "150%");
				YAHOO.util.Dom.setStyle(this.score_wnd.client, "padding", "10px 0px");
				YAHOO.util.Dom.setStyle(this.score_wnd.client, "textAlign", "center");
			}
			else {
				this.score_wnd.show();
			}

			this.score_wnd.clearText();
			for( var i=1; i<Math.min(data.length-1, max*2); i+=2 ) {
				var number = this.get_width_string(i - parseInt(i/2), 2);
				var name = "<font style='color: #000;'>" + this.get_width_string(data[i], 14) + "</font>";
				var score = this.get_width_string(this.after_get_score(data[i+1]), 10, "RIGHT");
				result = number + "##" + name + "##" + score;
				this.score_wnd.addText(result.replace(/#/g, "&nbsp;"));
			}
			//alert(data);
		}
		else if( type == "SET_SCORE" ) {
			//alert("set score");
		}
		else if( type == "INIT_SCORE" ) {
			//alert("init score");
		}
	},
	
	"handle_failure", function(o) {
		var result = o.status + " - " + o.statusText;
		alert(result);
	},

	"set_score", function(name, score) {
		var url = "../lib/score/set_score.php?game=" + this.game + "&user_id=" + name.replace(/ /g, "_") + "&score=" + score;
		this.ajax_call(url);
	},

	"get_score", function() {
		var url = "../lib/score/get_score.php?game=" + this.game;
		this.ajax_call(url);
	},

	"init_score", function() {
		var url = "../lib/score/init_score.php?game=" + this.game;
		this.ajax_call(url);
	},

	"get_width_string", function(src, width, align) {
		if( typeof src == "number" ) {
			src = "#" + src;
			src = src.substr(1, src.length-1);
		}

		if( align == "RIGHT" ) {
			while( src.length < width ) {
				src = "#" + src;
			}
			while( src.length > width ) {
				src = src.substr(0, src.length-1);
			}
		}
		else {
			while( src.length < width ) {
				src = src + "#";
			}
			while( src.length > width ) {
				src = src.substr(0, src.length-1);
			}
		}

		return src;
	},

	"before_set_score", function(score) {
		return score;
	},

	"after_get_score", function(score) {
		return score;
	},

	"show_set_score", function(score) {
		if( !this.set_wnd ) {
			this.set_wnd = new MODAL_WINDOW("scoreset_window");
			this.set_wnd.create(0, 0, 250, 31);
			this.set_wnd.setTitle("YOUR SCORE - " + score);

			var el_txt = DW_UTIL.addInput(this.set_wnd.get_uid("txt"), this.set_wnd.get_id(), "text", "PLEASE WRITE IN ENGLISH");
			YAHOO.util.Dom.setStyle(el_txt, "font", "9pt courier new");
			YAHOO.util.Dom.setStyle(el_txt, "color", "#999");
			YAHOO.util.Dom.setStyle(el_txt, "imeMode", "disabled");
			YAHOO.util.Dom.setStyle(el_txt, "padding", "2px 2px 0px 2px");
			DW_UTIL.setBorder(el_txt, 1, "#999");
			DW_UTIL.setXYWH(el_txt, 5, 5, 183, 17);
			YAHOO.util.Event.addListener(el_txt, "click", function(e) {
				this.value = "";
				YAHOO.util.Dom.setStyle(this, "color", "#000");
			}, el_txt, true);

			var el_btn = DW_UTIL.addInput(this.set_wnd.get_uid("btn"), this.set_wnd.get_id(), "button", "OK");
			DW_UTIL.setBorder(el_btn, 1, "#999");
			DW_UTIL.setBackground(el_btn, "#eee");
			DW_UTIL.setXYWH(el_btn, 200, 5, 45, 21);
			YAHOO.util.Dom.setStyle(el_btn, "font", "bold 8pt verdana");

			this.score = this.before_set_score(score);
			this.name = el_txt;
			YAHOO.util.Event.addListener(el_btn, "click", function(e) {
				this.set_score(this.name.value, this.score);
				this.set_wnd.hide();
				this.show_get_score();
			}, this, true);
		}
		else {
			this.set_wnd.setTitle("YOUR SCORE - " + score);
			this.score = this.before_set_score(score);
			this.set_wnd.show();
		}

		this.set_wnd.center();
	},

	"show_get_score", function() {
		this.get_score();
	}
);
