var board = {	
	'.field' : function(element) {
		element.onmouseover = function() {
			this._mark();				
		}
		element.onmouseout = function() {
			this._unmark();
		}
		element._mark = function() {
			if (null != $('game').ActiveField)
				$($('game').ActiveField)._unmark();

			$('game').ActiveField = this.id;
			this.style.border = '1px solid #ff0000';
		}
		element._unmark = function() {
			if (this.id != $('game').ActiveField)
				$($('game').ActiveField)._unmark();

			$('game').ActiveField = null;
			this.style.border = '1px solid #afafaf';
		}
		element.setField = function(Value) {
			if (0 <= Value <= 9) {
				this.src = '/img/tiles40x40/' + Value + 'r.gif';
				return true;
			}
			return false;
		}
		element.getX = function() {
			if (null == this.X)
				this.X = parseInt(this.id.split('-')[1]); 
			return this.X;
		}
		element.getY = function() {
			if (null == this.Y)
				this.Y = parseInt(this.id.split('-')[2]);
			return this.Y;
		}
	},
	'#claim' : function(element) {
		element.router = function(message) {
			this.innerHTML = message.set;
			/*
			new Ajax.Updater('claim', '/Index/ads', {
			  method: 'get',
			  evalScripts: true
			});
			*/
		}
	},
	'#skyad' : function(element) {
		element.router = function(message) {
			
			this.update();
		}
		
		element.update = function() {

			new Ajax.Updater('skyad', '/Index/ads', {
			  evalScripts: true,
			  insertion: Insertion.Bottom
			});
		}
	},
	'#gamefinish' : function(element) {
		element.router = function(message) {
			this.innerHTML = message.set;
			this.style.display = 'block';
		}
	},
	'#game' : function(element) {
		element.ActiveField = null;
		
		element.router = function(message) {
			if(false != message['set']) {
				$('FIELD-' + message.set.x + '-' + message.set.y).setField(message.set.value);				
			}
			else {
				alert(message['error']);
			}
		}

		element.setField = function(Value) {

			new Ajax.Request(
				'/Sudoku/Set', {
		  			method: 'get',
					parameters: {'x': $(this.ActiveField).getX(), 'y': $(this.ActiveField).getY(), 'value': Value},
					onSuccess: function(transport) {
						var response = transport.responseText.evalJSON();
						for (id in response) {
							$(id).router(response[id]);
						}
		    		},
					onFailure: function(){
					}
				}
			);
		}

		element._onkeydown = function(key) {
			
			if (this.ActiveField != null)
			{
				if (key==46 || key==48 || key==96 || key==110)
					return this.setField(0);
				if (key>48 && key<58)
					return this.setField(key-48);
				if (key>96 && key<106)
					return this.setField(key-96);
			}

			if (key == 37) {
				if (null == this.ActiveField)
					return $('FIELD-1-1')._mark();

				if ((Y = $(this.ActiveField).getY()) > 1) {
					X = $(this.ActiveField).getX();
					$(this.ActiveField)._unmark();
					return $('FIELD-' + X + '-' + (Y-1))._mark();
				}
			}
			if (key == 38) {
				if (null == this.ActiveField)
					return $('FIELD-1-1')._mark();

				if ((X = $(this.ActiveField).getX()) > 1) {
					Y = $(this.ActiveField).getY();
					$(this.ActiveField)._unmark();
					return $('FIELD-' + (X-1) + '-' + Y)._mark();
				}
			}
			if (key == 39) {
				if (null == this.ActiveField)
					return $('FIELD-1-2')._mark();

				if ((Y = $(this.ActiveField).getY()) < 9) {
					X = $(this.ActiveField).getX();
					$(this.ActiveField)._unmark();
					return $('FIELD-' + X + '-' + (Y+1))._mark();
				}
			}
			if (key == 40) {
				if (null == this.ActiveField)
					return $('FIELD-2-1')._mark();

				if ((X = $(this.ActiveField).getX()) < 9) {
					Y = $(this.ActiveField).getY();
					$(this.ActiveField)._unmark();
					return $('FIELD-' + (X+1) + '-' + Y)._mark();
				}
			}
		}
	}
};
Attitude.append(board);