// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function setActiveStyleSheet(title) {
  var i, a, main;
//  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	var links=$$('#head link');
//	var start=new Date();
	links.each (function (a) 
	{
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  });
	// var end=new Date();
	// alert (end-start);
	createCookie("game_size", title, 365);
}

window.onload = function(e) {
  var cookie = readCookie("game_size");
  var theme = cookie ? cookie : "default";
	$('game_size').value=theme;
  setActiveStyleSheet(theme);
}

/* cookies */

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

var Piece = Class.create({
	
	initialize: function(x)
	{
		this.code=x;
	},
	html: function(options)
	{
		if (!options) options={};
		color=this.code[0];
		img=color+this.ranks_to_names()[this.code[1]];
		title=this.tooltips()[this.code[1]];
		src="<image src='/images/pieces/" + img + ".png' piece='" + this.code + "' class='mypiece piece' from='" + options["from"] + "' title='" + title +"' />";
		return src;
	},
	ranks_to_names: function()
	{
    return {"B":"bomb", "F":"flag", "1":"marshal", "2":"general", "3":"colonel", "4":"major",
      "5":"captain", "6":"lieutenant", "7":"sergeant", "8":"miner", "9":"scout", "S":"spy",
			"U":"unknown", "N":"nuke" };
	},
	tooltips: function()
	{
		return {
			"B":"Bombs are stationary, blow up attackers, and remain on the board after battle." +
			"\nException: Miners (8) can defuse and remove bombs.",
			"S":"The spy is the weakest piece but if attacking first can kill the Marshal (1).",
			"F":"If your flag is captured, the game is over.",
			"9":"Scouts may move across multiple squares (along a straight line) in a single turn.",
			"8":"Miners defuse bombs.",
			"1":"The marshal - your strongest piece.",
			"2":"The general - second in command of your forces."
		}
	}
	
});

var Poller = Class.create({
	initialize: function(id,turns)
	{
		this.game_id=id;
		this.turns=turns;
	},
	start: function()
	{
		this.loop=new PeriodicalExecuter(this.poll.bind(this), 3);
	},
	stop: function()
	{
		if (this.loop)
			this.loop.stop();
	},
	poll: function()
	{
		params="&turns="+this.turns;
		new Ajax.Request('/games/' + this.game_id + '/poll', {asynchronous:true, evalScripts:true, method:'get', parameters:params});
	}
})