/*  JavaScript SudokuTime, version 1.0.0
 *  (c) 2007-2008 Zone Projects Ltd
 *--------------------------------------------------------------------------*/
var bb;
var active_cell;
var CommandProcessor = new commandProcessor();
 
document.observe('dom:loaded' , function() {
	bb = new Board(3);
	bb.Build();
	var cmd = new NewPuzzle_Command(6);
	CommandProcessor.execute( cmd );
}); 

 function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;

}

function MainMenu_ButtonClicked()
{
	//$("board").style.visibility = "hidden";
	$("options").hide();
	$("titular_buttons").style.visibility = "hidden";
	$("candidate_buttons").style.visibility = "hidden";
	$("switch_candidates_titular_btn").style.visibility = "hidden";
	
	var board_dimensions = $("board").getDimensions();
	$("mainmenu").style.position = "absolute";
	$("mainmenu").style.width = board_dimensions.width + "px";
	$("mainmenu").style.height = board_dimensions.height + "px";
	$("board").style.visibility = "hidden";
	$("mainmenu").show();
	$("check_button").hide();
	$("hint_button").hide();
	$("undo_button").hide();
	$('new_st_puzzle_difficulty_level_div').hide();
}
function Options_ButtonClicked()
{
	$("titular_buttons").style.visibility = "hidden";;
	$("candidate_buttons").style.visibility = "hidden";;
	$("switch_candidates_titular_btn").style.visibility = "hidden";
	
	$("mainmenu").hide();
	var board_dimensions = $("board").getDimensions();
	$("options").style.position = "absolute";
	$("options").style.width = board_dimensions.width + "px";
	$("options").style.height = board_dimensions.height + "px";
	$("board").style.visibility = "hidden";
	$("options").show();
	$("check_button").hide();
	$("hint_button").hide();
	$("undo_button").hide();
	$('new_st_puzzle_difficulty_level_div').hide();
}
function GameBoard_ButtonClicked()
{
	$("board").style.visibility = "";
	$("mainmenu").hide();
	$("options").hide();
	$("check_button").show();
	$("hint_button").show();
	$("undo_button").show();
	$("titular_buttons").style.visibility = "";
	$("candidate_buttons").style.visibility = "";
	$("switch_candidates_titular_btn").style.visibility = "";
	
	Element.hide($('uploadpuzzle'));
	$('new_st_puzzle_difficulty_level_div').hide();
}


function TitularBtn_clicked(btn, uservalue_to_set)
{
	if (!($(btn).hasClassName('tb-disabled')))
	{
		var tableIndex = bb.GetActiveCell_TableIndex();
		
		var Command_SetUserValue = new SetUserValue_Command( tableIndex,  uservalue_to_set);
		CommandProcessor.execute( Command_SetUserValue );
	}
}


function ToggleCandidateBtn_clicked(btn, candidate_to_toggle)
{
	if (!($(btn).hasClassName('cb-disabled')))
	{
		var tableIndex = bb.GetActiveCell_TableIndex();
		
		if (candidate_to_toggle)
		{
			var TurnOnOffValue = bb.IsCandidateOn( tableIndex, candidate_to_toggle );
			var Command_SetCandidateValue = new SetCandidateValue_Command( tableIndex,  candidate_to_toggle, !TurnOnOffValue);
			CommandProcessor.execute( Command_SetCandidateValue );
		}
		else
		{
			var Command_DeleteCandidates = new DeleteCandidates_Command( tableIndex, bb.Cells[tableIndex].candidates);
			CommandProcessor.execute( Command_DeleteCandidates );
		}
	}
}


function ToggleCandidateBtn(candidate_btn_id, TurnOn)
{
	var candidate_btn = document.getElementById('cb' + candidate_btn_id);
	if (TurnOn) // changed
	{
		if (candidate_btn.className != 'cb-down')
		{
			candidate_btn.className = 'cb-down';
		}
	}
	else
	{
			if (candidate_btn.className != 'cb-up')
			{
				candidate_btn.className = 'cb-up';
			}
	}
}


/*
Colin Mollenhour - iframesubmit.js
Adapted from pork.iframe.js (http://beta.zapguide.nl/iframe/)

new Form.IframeSubmit(form);
*/

Form.IframeSubmit = Class.create({
initialize: function(form, options){
    this.options = Object.extend({
      onComplete: null,
      container: null,
      insertion: Element.update
    },options || {});
    this.form = form;
    this.uniqueId = new Date().getTime();
    this.iframeName = 'iframe_'+this.uniqueId;
    Form.IframeSubmit.iframeLoaders[this.uniqueId] = this;
    this.transport = this.getTransport();
    form.target= this.iframeName;
    form.setAttribute("target", this.iframeName);
    form.submit();
  },
  onStateChange: function(){
    this.transport = $(this.iframeName);
    var aa = this.transport;
    var doc = Try.these(
      function () { // for FF
        var contents = aa.contentDocument.body.innerHTML;
        //aa.contentDocument.body.close();
        return contents;
      },
      function () { // For NS6
        var contents = aa.contentDocument.document.body.innerHTML;
        aa.contentDocument.document.close();
        return contents;
      },
      function () { // For IE5.5 and IE6
        var contents = aa.contentWindow.document.body.innerHTML;
        aa.contentWindow.document.close();
        return contents;
      },
      function () { // for IE5
        var contents = aa.document.body.innerHTML;
        aa.document.body.close();
        return contents;
      },
      function () { // for really nasty browsers
        return window.frames[this.iframeName].document.body.innerText;
      });
    
    this.transport.responseText = doc;
    if (this.options.onComplete){ setTimeout(function(){ this.options.onComplete(this.transport,false); }.bind(this), 10); }
    if (this.options.container){ setTimeout(function(){ this.options.insertion(this.options.container,this.transport.responseText,false); }.bind(this), 10); }
  },
  getTransport: function(){
    var divElm = document.createElement('DIV');
    divElm.style.position = "absolute";
    divElm.style.top = "0";
    divElm.style.marginLeft = "-10000px";
    if(navigator.userAgent.indexOf('MSIE') > 0 && navigator.userAgent.indexOf('Opera') == -1){// switch to the crappy solution for IE
     divElm.innerHTML = '<iframe name="'+this.iframeName+'" id="'+this.iframeName+'" src="about:blank" onload="setTimeout(function(){Form.IframeSubmit.iframeLoaders['+this.uniqueId+'].onStateChange()},20);"></iframe>';
    }else{
      var frame = document.createElement("iframe");
      frame.setAttribute("name", this.iframeName);
      frame.setAttribute("id", this.iframeName);
      frame.addEventListener("load",  function(){ this.onStateChange(); }.bind(this), false);
      divElm.appendChild(frame);
    }
    document.getElementsByTagName("body").item(0).appendChild(divElm);
  }
});
Form.IframeSubmit.iframeLoaders = {};
 
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 var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}