
YAHOO.DP.WINDOW.title_color = "#369";
YAHOO.DP.WINDOW.border_color = "#036";
YAHOO.DP.WINDOW.icon_color1 = "#036";
YAHOO.DP.WINDOW.icon_color2 = "#fff";
YAHOO.DP.WINDOW.color = "#d4d0c8";

var beginner = { col: 9, row: 9, mine: 10, name: "mine_sweeper_beginner" };
var normal = { col: 16, row: 16, mine: 40, name: "mine_sweeper_normal" };
var expert = { col: 30, row: 16, mine: 99, name: "mine_sweeper_expert" };

MINESWEEPER = function(id) {
	this.id = id;

	this.init = function() {
		YAHOO.namespace("MINESWEEPER");
		YAHOO.namespace.prev_click = -1;
		YAHOO.namespace.curr_click = -1;
		YAHOO.namespace.prev = new Date().getTime();
		YAHOO.namespace.curr = new Date().getTime();
		this.preload();
	}

	this.smile_create = function() {
		var smile_color = "#ccc";
		var el = DW_UTIL.addDiv(this.get_uid("smile"), this.wnd.get_id());
		DW_UTIL.setXYWH(el, 0, 0, this.map.width, 26);
		DW_UTIL.setBackground(el, smile_color);
		DW_UTIL.setBorder(el, this.map.border_size, smile_color);
		YAHOO.util.Dom.setStyle(el, "textAlign", "center");
		YAHOO.util.Dom.setStyle(el, "padding", "2px 0px");

		this.smile = document.createElement("img");
		this.smile_set_image("start");
		DW_UTIL.setBorder(this.smile, 1, "#999");
		el.appendChild(this.smile);

		this.led_timer = new LED_TIMER("mine_timer", this.get_uid("smile"));
		this.led_timer.create(this.map.width - 49, 0);

		this.led_score = new LEDS("mine_score", this.get_uid("smile"));
		this.led_score.create(1, 0);

		YAHOO.util.Event.addListener(this.smile, "mousedown", this.smile_click, this, true);
	}

	this.smile_set_image = function(key) {
		if( key != "start" && key != "finish" && key != "fail" && key != "click" ) {
			return;
		}
		this.smile.src = DW_UTIL.getPreload(key);
	}

	this.smile_click = function(e) {
		this.game_start();
	}

	this.get_uid = function(id) {
		return this.id + "." + id;
	}
/*
	this.add_menu = function() {
		this.wnd.addMenu("about", function() {
			CREDIT.show();
		});

		this.wnd.addMenu("high score", function() { 
			this.score.show_get_score(); 
			this.wnd.menu.hide();
		}, this);

		this.wnd.addMenu("beginner", function() { 
			if( this.col == beginner.col && this.row == beginner.row && this.mine_count == beginner.mine ) {
				return;
			}
			document.location.href = "../beginner/index.htm";
		}, this);

		this.wnd.addMenu("normal", function() { 
			if( this.col == normal.col && this.row == normal.row && this.mine_count == normal.mine ) {
				return;
			}
			document.location.href = "../normal/index.htm";
		}, this);

		this.wnd.addMenu("expert", function() { 
			if( this.col == expert.col && this.row == expert.row && this.mine_count == expert.mine ) {
				return;
			}
			document.location.href = "../expert/index.htm";
		}, this);
	}
*/
	this.create = function(level) {
		this.col = level.col;
		this.row = level.row;
		this.mine_count = level.mine;

		this.score = new SCORE(level.name);
		this.score.before_set_score = function(score) { return 999 - score; }
		this.score.after_get_score = function(score) { return 999 - score + " sec"; }

		this.wnd = new WINDOW(this.get_uid("window"));
		this.wnd.create(0, 0, 100, 100);
		this.wnd.setTitle("me2Minesweeper");
		//this.add_menu();

		this.map = new CELLMAP(this.get_uid("tilemap"), this.wnd.get_id());
		this.map.cell.width = 15;
		this.map.cell.height = 15;
		this.map.cell.border_size = 1;
		this.map.border_color = "#ccc";
		this.map.create(this.col, this.row);

		var w = this.map.width + this.map.border_size * 2;
		var h = this.map.height + 33 + this.map.border_size * 2;
		this.wnd.setWH(w, h);
		this.wnd.center();

		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				cell.parent = this;
//				YAHOO.util.Event.addListener(cell.get_id(), "click", this.on_click, cell, true);
//				YAHOO.util.Event.addListener(cell.get_id(), "dblclick", this.on_dblclick, cell, true);
				YAHOO.util.Event.addListener(cell.get_id(), "mousedown", this.on_mousedown, cell, true);
				YAHOO.util.Event.addListener(cell.get_id(), "mouseup", this.on_mouseup, cell, true);
				YAHOO.util.Event.addListener(cell.get_id(), "mouseout", this.on_mouseout, cell, true);
			}
		}

		this.smile_create();
		this.set_game_position();
		YAHOO.util.Event.addListener(window, "resize", this.set_game_position, this, true);
	}

	this.destroy = function() {
		DW_UTIL.safeDestroy(this.get_uid("smile"), this.wnd.get_id());
		this.map.destroy();
		this.wnd.destroy();
	}

	this.set_game_position = function(e) {
		DW_UTIL.setXY(this.get_uid("smile"), 0, 0)
		DW_UTIL.setXY(this.map.get_uid("container"), 0, 33);
	}

	this.preload = function() {
		DW_UTIL.setPreload("0", "../image/0.bmp");
		DW_UTIL.setPreload("1", "../image/1.bmp");
		DW_UTIL.setPreload("2", "../image/2.bmp");
		DW_UTIL.setPreload("3", "../image/3.bmp");
		DW_UTIL.setPreload("4", "../image/4.bmp");
		DW_UTIL.setPreload("5", "../image/5.bmp");
		DW_UTIL.setPreload("6", "../image/6.bmp");
		DW_UTIL.setPreload("7", "../image/7.bmp");
		DW_UTIL.setPreload("8", "../image/8.bmp");

		DW_UTIL.setPreload("open", "../image/open.bmp");
		DW_UTIL.setPreload("close", "../image/close.bmp");
		DW_UTIL.setPreload("bomb", "../image/bomb.bmp");
		DW_UTIL.setPreload("mine", "../image/mine.bmp");
		DW_UTIL.setPreload("flag", "../image/flag.bmp");
		DW_UTIL.setPreload("wrong", "../image/wrong.bmp");

		DW_UTIL.setPreload("start", "../image/start.gif");
		DW_UTIL.setPreload("click", "../image/click.gif");
		DW_UTIL.setPreload("finish", "../image/finish.gif");
		DW_UTIL.setPreload("fail", "../image/fail.gif");
	}

	this.game_start = function() {
		this.first_click = true;
		this.is_finish = false;
		this.is_success = false;
		this.time_start = null;
		this.time_finish = null;
		this.led_timer.init();
		this.led_score.set_number(this.mine_count);
		this.smile_set_image("start");

		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				cell.setImage(DW_UTIL.getPreload("close"));
				cell.is_flag = false;
				cell.is_open = false;
				cell.is_mine = false;
				cell.mine_count = 0;
			}
		}
	}

	this.game_finish = function() {
		this.is_finish = true;
		this.time_finish = new Date().getTime();
		var score = parseInt((this.time_finish - this.time_start) / 1000);
		this.led_timer.stop();
		this.led_timer.set_value(score);

		if( this.is_success ) {
			this.show_all_flag();
			//this.score.show_set_score(score);
			DPMe2Login.show();
			YAHOO.MINESWEEPER.score = score;
		}
		else {
			this.show_all_mine();
		}
	}

	this.show_all_flag = function() {
		this.led_score.set_number(0);
		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				if( cell.is_mine ) {
					cell.setImage(DW_UTIL.getPreload("flag"));
				}
			}
		}
	}

	this.show_all_mine = function() {
		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				if( !cell.is_open && !cell.is_flag && cell.is_mine ) {
					cell.setImage(DW_UTIL.getPreload("mine"));
				}
				else if( cell.is_flag && !cell.is_mine ) {
					cell.setImage(DW_UTIL.getPreload("wrong"));
				}
			}
		}
	}
	
	this.on_click = function(e) {
		var parent = this.parent;

		if( this.is_flag || parent.is_finish ) {
			return;
		}

		if( parent.first_click ) {
			parent.first_click = false;
			parent.add_mine(this.x, this.y);
			parent.cal_mine();
			parent.time_start = new Date().getTime();
			parent.led_timer.start();
		}

		if( this.is_mine ) {
			this.setImage(DW_UTIL.getPreload("bomb"));
			parent.game_finish();
		}
		else {
			parent.open(this.x, this.y);
		}

		if( parent.is_clear() ) {
			parent.game_finish();
		}
	}

	this.on_dblclick = function(e) {
		var parent = this.parent;

		if( parent.is_finish ) {
			return;
		}

		if( this.is_open && this.mine_count == parent.get_flag_count(this.x, this.y) ) {
			if( parent.check_dblclick(this.x, this.y) ) {
				parent.open_recursively(this.x, this.y);
			}
			else {
				parent.game_finish();
			}
		}

		if( parent.is_clear() ) {
			parent.game_finish();
		}
	}

	this.on_mousedown = function(e) {
		var parent = this.parent;

		if( parent.is_finish ) {
			return;
		}

		YAHOO.MINESWEEPER.click = null;

		if( e.button == 0 || e.button == 2 ) {
			YAHOO.MINESWEEPER.prev_click = YAHOO.MINESWEEPER.curr_click;
			YAHOO.MINESWEEPER.curr_click = e.button;
			YAHOO.MINESWEEPER.prev = YAHOO.MINESWEEPER.curr;
			YAHOO.MINESWEEPER.curr = new Date().getTime();
			if( YAHOO.MINESWEEPER.prev_click + YAHOO.MINESWEEPER.curr_click == 2 && Math.abs(YAHOO.MINESWEEPER.curr - YAHOO.MINESWEEPER.prev) < 100 ) {
				YAHOO.MINESWEEPER.click = "DBLCLICK";
			}
		}

		if( !YAHOO.MINESWEEPER.click ) {
			if( e.button == 0 || e.button == 1 ) {
				YAHOO.MINESWEEPER.click = "LCLICK";
			}
			else if( e.button == 2 ) {
				YAHOO.MINESWEEPER.click = "RCLICK";
			}
			else if( e.button == 3 ) {
				YAHOO.MINESWEEPER.click = "DBLCLICK";
			}
		}

		if( e.button != 2 ) {
			parent.smile_set_image("click");
		}

		if( YAHOO.MINESWEEPER.click == "LCLICK" ) {
			if( this.is_open || this.is_flag || parent.is_finish ) {
				return;
			}

			if( parent.first_click ) {
				parent.first_click = false;
				parent.add_mine(this.x, this.y);
				parent.cal_mine();
				parent.time_start = new Date().getTime();
				parent.led_timer.start();
			}

			if( this.is_mine ) {
				this.setImage(DW_UTIL.getPreload("bomb"));
				this.is_open = true;
				parent.game_finish();
			}
			else {
				parent.open(this.x, this.y);
			}

			if( parent.is_clear() ) {
				parent.game_finish();
			}
		}
		else if( YAHOO.MINESWEEPER.click == "RCLICK" ) {
			if( this.is_open ) {
				return;
			}

			if( !this.is_flag && parent.led_score.get_value() ) {
				this.is_flag = true;
				parent.led_score.decrease();
				this.setImage(DW_UTIL.getPreload("flag"));
			}
			else if( this.is_flag ) {
				this.is_flag = false;
				parent.led_score.increase();
				this.setImage(DW_UTIL.getPreload("close"));
			}
		}
		else if( YAHOO.MINESWEEPER.click == "DBLCLICK" ) {
			if( !this.is_open ) {
				return;
			}

			if( this.mine_count == parent.get_flag_count(this.x, this.y) ) {
				if( parent.check_dblclick(this.x, this.y) ) {
					parent.open_recursively(this.x, this.y);
				}
				else {
					parent.game_finish();
				}
			}

			if( parent.is_clear() ) {
				parent.game_finish();
			}
		}
	}

	this.on_mouseup = function(e) {
		var parent = this.parent;

		if( parent.is_finish && parent.is_success ) {
			parent.smile_set_image("finish");
		}
		else if( parent.is_finish && !parent.is_success ) {
			parent.smile_set_image("fail");
		}
		else {
			parent.smile_set_image("start");
		}
	}

	this.on_mouseout = function(e) {
		var parent = this.parent;

		if( parent.is_finish && parent.is_success ) {
			parent.smile_set_image("finish");
		}
		else if( parent.is_finish && !parent.is_success ) {
			parent.smile_set_image("fail");
		}
		else {
			parent.smile_set_image("start");
		}
	}

	this.is_clear = function() {
		var count = 0;
		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				if( cell.is_open && !cell.is_mine ) {
					count++;
				}
			}
		}

		if( this.col * this.row - count == this.mine_count ) {
			this.is_success = true;
			return true;
		}
		else {
			return false;
		}
	}

	this.check_dblclick = function(x, y) {
		for( var i=-1; i<=1; i++ ) {
			for( var j=-1; j<=1; j++ ) {
				if( i == 0 && j == 0 ) {
					continue;
				}
				else {
					var cell = this.map.getCell(x+i, y+j);
					if( cell && !cell.is_open && !cell.is_flag && cell.is_mine ) {
						cell.is_open = true;
						cell.setImage(DW_UTIL.getPreload("bomb"));
						return false;
					}
				}
			}
		}

		return true;
	}

	this.open = function(x, y) {
		var cell = this.map.getCell(x, y);
//		DW_UTIL.alert(cell, cell.is_open, cell.is_mine, cell.is_flag, cell.mine_count);

		if( !cell || cell.is_open || cell.is_mine || cell.is_flag ) {
			return;
		}

		cell.is_open = true;

		if( cell.mine_count ) {
			cell.setImage(DW_UTIL.getPreload(cell.mine_count));
			return;
		}
		else {
			cell.setImage(DW_UTIL.getPreload("0"));
			this.open_recursively(x, y);
		}
	}

	this.open_recursively = function(x, y) {
		for( var i=-1; i<=1; i++ ) {
			for( var j=-1; j<=1; j++ ) {
				if( i == 0 && j == 0 ) {
					continue;
				}
				else {
					this.open(x+i, y+j);
				}
			}
		}
	}

	this.add_mine = function(x, y) {
		for( var count=0; count<this.mine_count; ) {
			var rnd_x = Math.floor(Math.random() * this.col);
			var rnd_y = Math.floor(Math.random() * this.row);

			if( x == rnd_x && y == rnd_y ) {
				continue;
			}

			var cell = this.map.getCell(rnd_x, rnd_y);
			if( !cell.is_mine ) {
				cell.is_mine = true;
				count++;
			}
		}
	}

	this.cal_mine = function() {
		for( var x=0; x<this.col; x++ ) {
			for( var y=0; y<this.row; y++ ) {
				var cell = this.map.getCell(x, y);
				if( !cell.is_mine ) {
					cell.mine_count = this.get_mine_count(x, y);
				}
			}
		}
	}

	this.get_mine_count = function(x, y) {
		var count = 0;
		for( var i=-1; i<=1; i++ ) {
			for( var j=-1; j<=1; j++ ) {
				var cell = this.map.getCell(x+i, y+j);
				if( i == 0 && j == 0 ) {
					continue;
				}
				else if( cell && cell.is_mine ) {
					count++;
				}
			}
		}
		return count;
	}

	this.get_flag_count = function(x, y) {
		var count = 0;
		for( var i=-1; i<=1; i++ ) {
			for( var j=-1; j<=1; j++ ) {
				var cell = this.map.getCell(x+i, y+j);
				if( i == 0 && j == 0 ) {
					continue;
				}
				else if( cell && cell.is_flag ) {
					count++;
				}
			}
		}
		return count;
	}
}
