//-----------------------------------------------------------
// Site JS
//-----------------------------------------------------------

var GME = function(MyName, MyMode, MyFieldID, MySpeed, MyColors, MyBoxWidth, MyBoxHeight, MyFieldWidth, MyFieldHeight) {
	return {
		// Game Vars -----------------------------------------
		Field		: null,
		ScoreField	: null,
		FieldID		: MyFieldID,
		PlayField	: null,
		State		: 'Init',
		Clicks		: 0,
		Score		: 0,
		Mode		: MyMode,
		Name		: MyName,
		Speed		: MySpeed,
		BoxWidth	: MyBoxWidth,
		BoxHeight	: MyBoxHeight,
		FieldWidth	: MyFieldWidth,
		FieldHeight : MyFieldHeight,
		Colors		: MyColors,
		//----------------------------------------------------

		// Game Functions ------------------------------------
		Init : function(Mode) {
			this.Field = document.getElementById(this.FieldID);

			if (this.Field == null) {
				this.StopGame('Error: No field.');
				return false;
			}
			this.Field.innerHTML = '';
			
			var Styles = {
				position	: ((this.Field.style.position != 'absolute') ? 'relative' : 'absolute'),
				height		: this.FieldHeight + 'px',
				width		: this.FieldWidth + 'px',
				overflow	: 'hidden'
			};
			this.ApplyStyle(this.Field, Styles);
			
			this.PlayField = [];
			for (var x = 0; x < this.FieldWidth / this.BoxWidth; x++) {
				this.PlayField[x] = [];
				for (var y = 0; y < this.FieldHeight / this.BoxHeight; y++) {
					this.PlayField[x][y] = null;
				}
			}
			this.DrawField();
			this.ScoreField = document.getElementById(this.FieldID + '_Score');
			this.ScoreField.innerHTML = 'Score: 0';
			this.ChangeState('Idle');
		},
		CheckBox : function(MyX, MyY, MyType) {
			var DoNotRemove = (!arguments[3]) ? false : true;
			var Found = 0;
			for (var x = -1; x <= 1; x++) {
				for (var y = -1; y <= 1; y++) {
					if ((x == 0 && y == 0) || (!this.PlayField[ (MyX + x) ] || !this.PlayField[ (MyX + x) ][ (MyY + y) ]) || (Math.abs(x) == 1 && Math.abs(y) == 1)) {
						continue;
					}
					if (this.PlayField[ (MyX + x) ][ (MyY + y) ].Type == MyType) {
						if (DoNotRemove) {
							return 1;
						}
						this.PlayField[ (MyX + x) ][ (MyY + y) ].parentNode.removeChild(this.PlayField[ (MyX + x) ][ (MyY + y) ]);
						this.PlayField[ (MyX + x) ][ (MyY + y) ] = false;
						Found++;
						Found += this.CheckBox((MyX + x), (MyY + y), MyType);
					}
				}
			}
			return Found;
		},
		DrawField : function() {
			if (this.State != 'Init') {
				this.StopGame('Error: can\'t draw field.');
				return false;
			}
			this.Field.style.background = 'url(/img/e1/t1/sckr/game_' + this.Random(1, 10) + '.jpg)';

			var This = this;
			this.ChangeState('Processing');
			for (var x = 0; x < this.FieldWidth / this.BoxWidth; x++) {
				for (var y = 0; y < this.FieldHeight / this.BoxHeight; y++) {
					var Top		= this.BoxHeight * y;
					var Left	= this.BoxWidth * x;

					var Box		= document.createElement('div');
						Box.className = 'Game_Box';
						Box._x = x;
						Box._y = y;
						Box.Type = this.Random(0, this.Colors.length);
						Box.onclick = function(MyEvent) {
							if (This.State != 'Idle')
								return false;

							var BoxesRemoved = This.CheckBox(this._x, this._y, this.Type);
							if (BoxesRemoved) {
								This.Score += parseInt( ( ( BoxesRemoved * ( (BoxesRemoved / 2) + 1) ) / 5) * 110 );
								This.ScoreField.innerHTML = 'Score: ' + This.Score;
								This.AnimateX();
							}
						};
					this.Field.appendChild(Box);

					var Style	= {
						top				: Top + 'px',
						left			: Left + 'px',
						position		: 'absolute',
						backgroundImage	: 'url(\'/img/e1/t1/sckr/overlay' + this.BoxWidth + 'x' + this.BoxHeight + '.png\')',
						width			: this.BoxWidth + 'px',
						cursor			: 'pointer',
						height			: this.BoxHeight + 'px',
						backgroundColor : this.Colors[Box.Type]
					};
					this.ApplyStyle(Box, Style);
					
					this.PlayField[x][y] = Box;
				}
			}
			this.ChangeState('Idle');
		},
		AnimateX : function() {
			var StillAnimating = false;
			for (var x = (this.FieldWidth / this.BoxWidth - 1); x >= 0; x--) {
				for (var y = (this.FieldHeight / this.BoxHeight - 1); y >= 0; y--) {
					if (!this.PlayField[x][y] && (this.PlayField[x-1] && this.PlayField[x-1][y])) {
						this.PlayField[x-1][y].AnimateX = true;
						StillAnimating = true;
					} else if (this.PlayField[x+1] && this.PlayField[x+1][y] && this.PlayField[x+1][y].AnimateX == true) {
						this.PlayField[x][y].AnimateX = true;
						StillAnimating = true;
					}
				}
			}
			if (StillAnimating && this.State != 'Animating')
				setTimeout(this.FieldID + '.AnimateXTimeout();', 5);
			else
				this.AnimateY();
		},
		AnimateY : function() {
			var StillAnimating = false;
			for (var x = (this.FieldWidth / this.BoxWidth - 1); x >= 0; x--) {
				for (var y = (this.FieldHeight / this.BoxHeight - 1); y >= 0; y--) {
					if (!this.PlayField[x][y] && this.PlayField[x][y-1]) {
						this.PlayField[x][y-1].AnimateY = true;
						StillAnimating = true;
					} else if (this.PlayField[x][y+1] && this.PlayField[x][y+1].AnimateY == true) {
						this.PlayField[x][y].AnimateY = true;
						StillAnimating = true;
					}
				}
			}
			if (StillAnimating && this.State != 'Animating')
				setTimeout(this.FieldID + '.AnimateYTimeout();', 5);
			else {
				this.ChangeState('Idle');
				if (this.CheckWin()) {
					alert('Super Winner!! +1000 points!');
					this.Score += 1000;
					this.ScoreField.innerHTML = 'Score: ' + this.Score;
					this.ChangeState('Init');
					this.DrawField();
				} else if (!this.CheckMoves()) {
					alert('No More Moves! -' + (this.BlocksLeft() * 100) + ' points');
					this.Score -= this.BlocksLeft() * 100;
					this.ScoreField.innerHTML = 'Score: ' + this.Score;
					this.ChangeState('Over');
				}
			}
		},
		AnimateYTimeout : function() {
			var StillAnimating = ReAnimate = false;
			for (var x = (this.FieldWidth / this.BoxWidth - 1); x >= 0; x--) {
				for (var y = (this.FieldHeight / this.BoxHeight - 1); y >= 0; y--) {
					if (this.PlayField[x][y] && this.PlayField[x][y].AnimateY == true) {
						var MyTop = parseInt(this.PlayField[x][y].style.top.replace('px', ''));
						if (MyTop >= (y * this.BoxHeight) + this.BoxHeight) {
							this.PlayField[x][y].style.top =  parseInt((y + 1) * this.BoxHeight) + 'px';
							this.PlayField[x][y].AnimateY = false;
							this.PlayField[x][y]._y = y + 1;
							this.PlayField[x][y + 1] = this.PlayField[x][y];
							this.PlayField[x][y] = false;
							ReAnimate = true;
						} else {
							this.PlayField[x][y].style.top = parseInt(MyTop + this.Speed) + 'px';
							this.ChangeState('Animating');
							StillAnimating = true;
						}
					}
				}
			}
			if (ReAnimate) {
				this.ChangeState('Processing');
				this.AnimateY();
				return;
			}
			if (StillAnimating) {
				setTimeout(this.FieldID + '.AnimateYTimeout();', 5);
			}
		},
		AnimateXTimeout : function() {
			var StillAnimating = ReAnimate = false;
			for (var x = (this.FieldWidth / this.BoxWidth - 1); x >= 0; x--) {
				for (var y = (this.FieldHeight / this.BoxHeight - 1); y >= 0; y--) {
					if (this.PlayField[x][y] && this.PlayField[x][y].AnimateX == true) {
						var MyLeft = parseInt(this.PlayField[x][y].style.left.replace('px', ''));
						if (MyLeft >= (x * this.BoxWidth) + this.BoxWidth) {
							this.PlayField[x][y].style.left =  parseInt((x + 1) * this.BoxWidth) + 'px';
							this.PlayField[x][y].AnimateX = false;
							this.PlayField[x][y]._x = x + 1;
							this.PlayField[x + 1][y] = this.PlayField[x][y];
							this.PlayField[x][y] = false;
							ReAnimate = true;
						} else {
							this.PlayField[x][y].style.left = parseInt(MyLeft + this.Speed) + 'px';
							this.ChangeState('Animating');
							StillAnimating = true;
						}
					}
				}
			}
			if (ReAnimate) {
				this.ChangeState('Processing');
				this.AnimateX();
				return;
			}
			if (StillAnimating) {
				setTimeout(this.FieldID + '.AnimateXTimeout();', 5);
			}
		},
		StopGame : function(Error) {
			alert(Error);
			this.ChangeState('Over');
		},
		CheckWin: function () {
			for (var x = 0; x < this.FieldWidth / this.BoxWidth; x++) {
				for (var y = 0; y < this.FieldHeight / this.BoxHeight; y++) {
					if (this.PlayField[x][y])
						return false;
				}
			}
			return true;
		},
		CheckMoves: function() {
			for (var x = 0; x < this.FieldWidth / this.BoxWidth; x++) {
				for (var y = 0; y < this.FieldHeight / this.BoxHeight; y++) {
					if (this.CheckBox(x, y, this.PlayField[x][y].Type, true))
						return true;
				}
			}
			return false;
		},
		BlocksLeft: function() {
			var Left = 0;
			for (var x = 0; x < this.FieldWidth / this.BoxWidth; x++) {
				for (var y = 0; y < this.FieldHeight / this.BoxHeight; y++) {
					if (this.PlayField[x][y])
						Left++;
				}
			}
			return Left;
		},
		//----------------------------------------------------

		// Misc Functions ------------------------------------
		ApplyStyle : function(MyObject, Styles) {
			for (var i in Styles) {
				MyObject.style[i] = Styles[i];
			}
		},
		Random : function(Min, Max) {
			return Min + Math.floor(Math.random() * (Max - Min));
		},
		ChangeState: function(State) {
			switch (State) {
				case 'Animating':
				case 'Idle':
				case 'Init':
				case 'Processing':
					this.State = State;
				break;

				case 'Over':
					this.State = State;
					this.SendScore('Overlay');
				break;
			}
		},
		//----------------------------------------------------

		// Scores ----------------------------------------------[TG]
		SendScore: function(Container) {
			if (!this.ScoreSubmitted) {
				if (!this.Name)
					this.Name = prompt('Please enter your Name');

				xh.loadSection(Container, '/sckr.score/?Name=' + this.Name + '&Score=' + this.Score + '&Mode=' + this.Mode);
				this.ScoreSubmitted = true;
			} else
				xh.loadSection(Container, '/sckr.score/?Mode=' + this.Mode);
		}
		//------------------------------------------------------[TG]

	};
}
//-----------------------------------------------------------