// Marocco is copyright 2008, 2009 Felix Pleşoianu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var marocco = {
	container: null,
	clock_interval: 5000,
	starting_sticks: 10,
	is_vertical: false,
	color_list: ['red', 'orange', 'yellow', '#00ff00', 'magenta', 'cyan'],
	total_time: 0,
	playing: false,
	paused: false,
	onstartgame: function () {},
	onpausegame: function () {},
	onresumegame: function () {},
	onstopgame: function () {},
	ongameover: function () {},

	start_game: function() {
		try {
			window.clearInterval(this.stick_timer);
		} catch (e) {
			// Ignore.
		}

		this.onstartgame();

		this.container.innerHTML = '';
		this.sticks = [];
		for (var i = 0; i < this.starting_sticks; i++) {
			this.drop_stick();
		}

		this.start_clock();
		this.total_time = 0;
		this.playing = true;
		this.paused = false;
	},

	pause_game: function() {
		this.stop_clock();
		this.paused = true;
		this.onpausegame();
	},

	resume_game: function() {
		if (this.playing && this.paused) {
			this.start_clock();
			this.paused = false;
			this.onresumegame();
		}
	},

	stop_game: function () {
		this.stop_clock();
		this.playing = false;
		this.onstopgame();
	},

	gameover: function () {
		this.stop_clock();
		this.playing = false;
		this.ongameover();
	},


	drop_stick: function () {
		var stick = document.createElement('div');
		this.container.appendChild(stick);
		this.sticks.push(stick);
		//stick.innerHTML = '&nbsp;';
		stick.style.position = 'absolute';
		stick.style.borderRight =
			stick.style.borderBottom =
				'1px solid black';
		stick.style.backgroundColor = this.pick(this.color_list);

		var position = Math.round(Math.random() * 95) + '%';
		if (this.is_vertical) {
			stick.style.width = '4%';
			stick.style.height = '99%';
			stick.style.left = position;
		} else {
			stick.style.width = '99%';
			stick.style.height = '4%';
			stick.style.top = position;
		}
		this.is_vertical = !this.is_vertical;

		stick.onclick = function () {
			if (marocco.is_top_stick(this)) {
				marocco.remove_stick(this);
			}
		}
	},

	pick: function (options) {
		return options[Math.floor(Math.random() * options.length)];
	},

	remove_stick: function (stick) {
		if (!this.playing) return;
		if (this.is_top_stick(stick)) {
			this.sticks.pop();
			this.container.removeChild(stick);
			if (this.sticks.length == 0) {
				this.gameover();
			}
			this.is_vertical = !this.is_vertical;
		}
	},

	is_top_stick: function (stick) {
		return this.sticks[this.sticks.length - 1] == stick;
	},

	start_clock: function () {
		if (this.clock_interval > 0) {
			this.stick_timer = window.setInterval(
				function () { marocco.drop_stick(); },
				this.clock_interval)
		}
		this.start_time = new Date();
	},

	stop_clock: function () {
		try {
			window.clearInterval(this.stick_timer);
		} catch (e) {
			// Ignore.
		}
		var stop_time = new Date();
		this.total_time +=
			Math.round((stop_time - this.start_time) / 1000);
	}
};


window.onload = function () {
	function msgbox(message) {
		var msgbox = document.getElementById('msgbox');
		msgbox.innerHTML = message;
		msgbox.style.display = 'block';
		window.setTimeout(
			function () { msgbox.style.display = 'none'; },
			3000);
	}

	marocco.container = document.getElementById('gameboard');
	marocco.onstartgame = function () {
		document.getElementById('header').style.display = 'none';
		document.getElementById('helpdlg').style.display = 'none';
		document.getElementById('setdlg').style.display = 'none';
	}
	marocco.onpausegame = function () {
		document.getElementById('pausemsg').style.display = 'block';
		marocco.container.style.display = 'none';
	}
	marocco.onresumegame = function () {
		document.getElementById('pausemsg').style.display = 'none';
		marocco.container.style.display = 'block';
	}
	marocco.onstopgame = function () {
		msgbox('Game cancelled');
		this.container.innerHTML = '';
		document.getElementById('header').style.display = 'block';
	}
	marocco.ongameover = function () {
		msgbox("Congrats!\nYour time: "+this.total_time+" seconds!");
		document.getElementById('header').style.display = 'block';
	}

	document.getElementById('playcmd').onclick = function () {
		if (marocco.paused) {
			marocco.resume_game();
			document.getElementById('pausecmd')
				.style.borderColor = 'black';
		} else {
			marocco.start_game();
		}
		return false;
	};
	document.getElementById('pausecmd').onclick = function () {
		if (!marocco.playing) return false;
		if (marocco.paused) {
			marocco.resume_game();
			this.style.borderColor = 'black';
		} else {
			marocco.pause_game();
			this.style.borderColor = 'red';
		}
		return false;
	};
	document.getElementById('stopcmd').onclick = function () {
		if (marocco.paused) {
			marocco.resume_game();
			document.getElementById('pausecmd')
				.style.borderColor = 'black';
		}
		marocco.stop_game();
		return false;
	};

	document.getElementById('helpcmd').onclick = function () {
		var helpdlg = document.getElementById('helpdlg');
		if (helpdlg.style.display != 'block') {
			helpdlg.style.display = 'block';
			this.style.borderColor = 'red';
		} else {
			helpdlg.style.display = 'none';
			this.style.borderColor = 'black';
		}
		return false;
	};
	document.getElementById('setcmd').onclick = function () {
		var setdlg  = document.getElementById('setdlg');
		if (setdlg.style.display != 'block') {
			setdlg.style.display = 'block';
			this.style.borderColor = 'red';
		} else {
			setdlg.style.display = 'none';
			this.style.borderColor = 'black';
		}
		return false;
	};
	document.getElementById('closecmd').onclick = function () {
		window.close();
		return false;
	};

	document.getElementById('helpdlg').onclick = function () {
		this.style.display = 'none';
		document.getElementById('helpcmd').style.borderColor = 'black';
	}

	document.getElementById('easycmd').onclick = function () {
		marocco.clock_interval = 5000;
		marocco.starting_sticks = 10;
	}

	document.getElementById('mediumcmd').onclick = function () {
		marocco.clock_interval = 3000;
		marocco.starting_sticks = 10;
	}

	document.getElementById('hardcmd').onclick = function () {
		marocco.clock_interval = 1500;
		marocco.starting_sticks = 10;
	}

	document.getElementById('pick50cmd').onclick = function () {
		marocco.clock_interval = 0;
		marocco.starting_sticks = 50;
	}

	document.getElementById('pick250cmd').onclick = function () {
		marocco.clock_interval = 0;
		marocco.starting_sticks = 250;
	}
	
	if (widget) {
		document.getElementById('homepage-link').onclick = function () {
			getURL(this.href);
			return false;
		};
		
		document.getElementById('license-link').onclick = function () {
			getURL(this.href);
			return false;
		};
	}
}
