function BluffBot() {
	this.GameState = {
		username: '',
		password: '',
		buyin: 0,
		version: '',

		my_c1 : null,
		my_c2 : null,

		bot_c1 : null,
		bot_c2 : null,

		b1 : null,
		b2 : null,
		b3 : null,
		b4 : null,
		b5 : null,

		called : 0,
		toCall : 0,
		pot : 0,

		playerstack : 0,
		botstack : 0,

		my_pos : 0
	};
}

BluffBot.prototype.serverCallBack = function(request)
{
//	alert(request.responseText);
	try	{ o = eval( "(" + request.responseText + ")" ); }
	catch (e) {
		alert("Internal error:\n"+e+"\n\nrequest.responseText:\n"+request.responseText);
		return;
	};

	document.getElementById('buttoncover').style.display = 'none';

	if (typeof(o) == 'object')
	{
		if (typeof(o['error']) != 'undefined')
			alert("BluffBot: \n"+o['error']);

		document.getElementById('raise_to').value = '';
		
		// NOTES
		if (typeof(o['notes']) != 'undefined')
		{
			document.getElementById('gamenotes').innerHTML = o['notes'];
		}

		// PLAYER ROUNDCOMMITMENT
		if (typeof(o['player']['roundCommitment']) != 'undefined')
		{
			BluffBot.GameState.called = o['player']['roundCommitment']? o['player']['roundCommitment']: '';
			document.getElementById('playerbet').innerHTML = o['player']['roundCommitment']? '$'+o['player']['roundCommitment']: '';
		}
		
		var deal_delay = 0;

		// BOT OLD ACTION (fade out)
		if (typeof(o['bot']['oldActionText']) != 'undefined')
		{
		  if (o['bot']['oldActionText'] != '')
		  {
		    document.getElementById('botaction').innerHTML = o['bot']['oldActionText']+' ';
		    Effects.fade('botaction', 100, 0, 1000, 'Effects.onFaded()');
  			deal_delay = 1000;
		  }
		}
		
		// BOT ACTION (fade in)
    if (typeof(o['bot']['actionText']) != 'undefined')
		{
		  if (o['bot']['actionText'] != '')
		  {
		    setTimeout("Effects.changeOpacity(0, 'botaction');", deal_delay);
	  		setTimeout("document.getElementById('botaction').innerHTML = o['bot']['actionText']+' ';", deal_delay+10);
  			setTimeout("Effects.fade('botaction', 0, 100, 1000, 'Effects.onFaded()');", deal_delay);
			}
			else
			{
			  setTimeout("document.getElementById('botaction').innerHTML = o['bot']['actionText']+' ';", deal_delay);
			}
		}
		
		// BOT ROUNDCOMMITMENT
		if (typeof(o['bot']['roundCommitment']) != 'undefined')
		{
		  if (o['bot']['roundCommitment'] != '' || o['bot']['actionText'] != '')
		  {
		    document.getElementById('botbet').innerHTML = '';
		    Effects.changeOpacity(0, 'botbet');
		    setTimeout("document.getElementById('botbet').innerHTML = o['bot']['roundCommitment']? '$'+o['bot']['roundCommitment']: '';", deal_delay+10);
		    setTimeout("Effects.fade('botbet', 0, 100, 1000, 'Effects.onFaded()');", deal_delay);
			}
			else
			{
			  Effects.fade('botbet', 100, 0, 1000, 'Effects.onFaded()');
			  setTimeout("document.getElementById('botbet').innerHTML = o['bot']['roundCommitment']? '$'+o['bot']['roundCommitment']: '';", deal_delay);
			}
		}

		// POT
		if (typeof(o['pot']) != 'undefined' && typeof(o['bot']['roundCommitment']) != 'undefined' && typeof(o['player']['roundCommitment']) != 'undefined')
		{
			BluffBot.GameState.pot = document.getElementById('pot').innerHTML = o['pot'];

			if (typeof(o['stage']) != 'undefined' && o['stage'] == 0) // preflop, fix post
			{
				BluffBot.GameState.pot += o['bot']['roundCommitment'] + o['player']['roundCommitment'];
			}
		}

		// PLAYER STACK
		if (typeof(o['player']['stack']) != 'undefined')
		{
			BluffBot.GameState.playerstack = document.getElementById('playerstack').innerHTML = o['player']['stack'];
		}

		/// BOT STACK
		if (typeof(o['bot']['stack']) != 'undefined')
		{
			BluffBot.GameState.botstack = document.getElementById('botstack').innerHTML = o['bot']['stack'];
		}

		// AMOUNT TO CALL
		if (typeof(o['player']['tocall']) != 'undefined')
		{
			BluffBot.GameState.toCall = o['player']['tocall'];
		}
	
		// HOLE CARDS
		if (typeof(o['player']['cards']) != 'undefined')
		{
			document.getElementById('playercard1').innerHTML = o['player']['cards']['0'];
			document.getElementById('playercard2').innerHTML = o['player']['cards']['1'];
		}

		if (typeof(o['bot']['cards']) != 'undefined')
		{
			document.getElementById('botcard1').innerHTML = o['bot']['cards']['0'];
			document.getElementById('botcard2').innerHTML = o['bot']['cards']['1'];
		}
		else
		{
			document.getElementById('botcard1').innerHTML = document.getElementById('botcard2').innerHTML = '<img src="card_bg.png">';
		}

		// BOARD
		if (typeof(o['board']) == 'undefined')
		{
			document.getElementById('board_0').innerHTML = '';
			document.getElementById('board_1').innerHTML = '';
			document.getElementById('board_2').innerHTML = '';
			document.getElementById('board_3').innerHTML = '';
			document.getElementById('board_4').innerHTML = '';
		}
		else
		{
			if ((typeof(o['board']['0']) != 'undefined') && (document.getElementById('board_0').innerHTML == ''))
			{
			  Effects.changeOpacity(0, 'board_0');
			
				setTimeout("document.getElementById('board_0').innerHTML = o['board']['0'];", 10);
				
				Effects.fade('board_0', 0, 100, deal_delay+250, 'Effects.onFaded()');
			}

			if ((typeof(o['board']['1']) != 'undefined') && (document.getElementById('board_1').innerHTML == ''))
			{
			  Effects.changeOpacity(0, 'board_1');
			
				setTimeout("document.getElementById('board_1').innerHTML = o['board']['1'];", 10);
				
				Effects.fade('board_1', 0, 100, deal_delay+250, 'Effects.onFaded()');
			}

			if ((typeof(o['board']['2']) != 'undefined') && (document.getElementById('board_2').innerHTML == ''))
			{
			  Effects.changeOpacity(0, 'board_2');
			
				setTimeout("document.getElementById('board_2').innerHTML = o['board']['2'];", 10);
				
				Effects.fade('board_2', 0, 100, deal_delay+250, 'Effects.onFaded()');
			}

			if ((typeof(o['board']['3']) != 'undefined') && (document.getElementById('board_3').innerHTML == ''))
			{
			  Effects.changeOpacity(0, 'board_3');
			
				setTimeout("document.getElementById('board_3').innerHTML = o['board']['3'];", 10);
				
				Effects.fade('board_3', 0, 100, deal_delay+500, 'Effects.onFaded()');
			}

			if ((typeof(o['board']['4']) != 'undefined') && (document.getElementById('board_4').innerHTML == ''))
			{
			  Effects.changeOpacity(0, 'board_4');
			
				setTimeout("document.getElementById('board_4').innerHTML = o['board']['4'];", 10);
				
				Effects.fade('board_4', 0, 100, deal_delay+750, 'Effects.onFaded()');
			}
		}

		// BUTTONS
		if (typeof(o['player']['tocall']) != 'undefined' && o['player']['tocall'] > 0)
		{
			document.getElementById('button_fold').style.visibility = 'visible';
			document.getElementById('button_check_or_call').value = 'Call $'+o['player']['tocall'];

			// can we raise?
			if (typeof(o['player']['stack']) != 'undefined' && o['player']['tocall'] < o['player']['stack'] && typeof(o['bot']['stack']) != 'undefined' && o['bot']['stack'] > 0)
			{
				//document.getElementById('button_bet_or_raise').style.visibility = 'visible';
				document.getElementById('button_bet_or_raise').value = 'Raise to';
				//document.getElementById('raise_to').style.visibility = 'visible';
				//document.getElementById('raisetobuttons').style.visibility = 'visible';
				document.getElementById('button_bet_or_raise').disabled = false;
				document.getElementById('raise_to').disabled = false;
				document.getElementById('raisetobuttons').disabled = false;
				document.getElementById('button_bet_or_raise').style.opacity = 1;
			  document.getElementById('raisetobuttons').style.opacity = 1;
			}
			else
			{
				/*document.getElementById('button_bet_or_raise').style.visibility = 'hidden';
				document.getElementById('raise_to').style.visibility = 'hidden';
				document.getElementById('raisetobuttons').style.visibility = 'hidden';*/
				document.getElementById('button_bet_or_raise').disabled = true;
				document.getElementById('raise_to').disabled = true;
				document.getElementById('raisetobuttons').disabled = true;
				document.getElementById('button_bet_or_raise').style.opacity = 0.5;
				document.getElementById('raisetobuttons').style.opacity = 0.5;
			}
		}
		else
		{
			document.getElementById('button_fold').style.visibility = 'hidden';
			document.getElementById('button_check_or_call').value = 'Check';
			//document.getElementById('button_bet_or_raise').style.visibility = 'visible';
			document.getElementById('button_bet_or_raise').value = 'Bet';
			//document.getElementById('raise_to').style.visibility = 'visible';
			//document.getElementById('raisetobuttons').style.visibility = 'visible';
			document.getElementById('button_bet_or_raise').disabled = false;
			document.getElementById('raise_to').disabled = false;
			document.getElementById('raisetobuttons').disabled = false;
			document.getElementById('button_bet_or_raise').style.opacity = 1;
			document.getElementById('raisetobuttons').style.opacity = 1;
		}

		// STAGE / RESTART
		if (typeof(o['stage']) != 'undefined' && o['stage'] >= 4)
		{
			document.getElementById('gamebuttons').style.display = 'none';
			document.getElementById('restartbutton').style.display = 'block';
		}
		else
		{
			document.getElementById('gamebuttons').style.display = 'block';
			document.getElementById('restartbutton').style.display = 'none';
		}

		// DEALERBUTTON
		if (typeof(o['dealerbutton']) != 'undefined')
		{
			document.getElementById('dealerbutton').className = o['dealerbutton'];
		}
		
		// Min Bet/Raise Amount
  	if (typeof(o['minraise']) != 'undefined')
	  {
		 	document.getElementById('raise_to').value = o['minraise'];
		 	try {
		    document.getElementById('raise_to').focus();
		  }
		  catch(er) { }
  	}
	}

//	alert(o);
//	alert(var_dump(o));
}


BluffBot.prototype.serverCall = function(o) {

	document.getElementById('buttoncover').style.display = 'block';
	var postBody = '';

	for (var i in o)
	{
		postBody += i+"="+encodeURIComponent(o[i])+"&";
	}

	var o2 = {'postBody': postBody};
	o2.onComplete = this.serverCallBack;
	new ajax('/online/handle.php', o2);
}

BluffBot.prototype.newGame = function()
{
	this.serverCall({'c': 'restart', 'buyin': this.GameState.buyin, 'username': this.GameState.username, 'password': this.GameState.password, 'version': this.GameState.version});
	return false;
}

BluffBot.prototype.fold = function()
{
	this.serverCall({'c': 'fold'});
	return false;
}

BluffBot.prototype.checkOrCall = function()
{
	this.serverCall({'c': 'check_or_call', 'amount': this.GameState.toCall});
	return false;
}

BluffBot.prototype.betOrRaise = function()
{
	this.serverCall({'c': 'bet_or_raise', 'amount': document.getElementById('raise_to').value});
	return false;
}

BluffBot.prototype.setRaiseTo = function(factor) 
{
	var to = factor == 'max'? (this.GameState.called + this.GameState.playerstack) : Math.round((this.GameState.pot + this.GameState.toCall) * factor) + this.GameState.toCall + this.GameState.called;
	if (parseInt(to) > this.GameState.called + this.GameState.playerstack)
		to = this.GameState.called + this.GameState.playerstack;

	document.getElementById('raise_to').value = to;

	return false;
}





/*  Prototype JavaScript framework, version 1.4.0
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
 *
/*--------------------------------------------------------------------------*/

//note: stripped down version of prototype, to be used with moo.fx by mad4milk (http://moofx.mad4milk.net).

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

Object.extend = function(destination, source) {
	for (var property in source) destination[property] = source[property];
	return destination;
};

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
};

if (!Array.prototype.forEach){
	Array.prototype.forEach = function(fn, bind){
		for(var i = 0; i < this.length ; i++) fn.call(bind, this[i], i);
	};
}

Array.prototype.each = Array.prototype.forEach;

String.prototype.camelize = function(){
	return this.replace(/-\D/gi, function(match){
		return match.charAt(match.length - 1).toUpperCase();
	});
};

var $A = function(iterable) {
	var nArray = [];
	for (var i = 0; i < iterable.length; i++) nArray.push(iterable[i]);
	return nArray;
};

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
};

if (!window.Element) var Element = {};

Object.extend(Element, {

	remove: function(element) {
		element = $(element);
		element.parentNode.removeChild(element);
	},

	hasClassName: function(element, className) {
		element = $(element);
		return !!element.className.match(new RegExp("\\b"+className+"\\b"));
	},

	addClassName: function(element, className) {
		element = $(element);
		if (!Element.hasClassName(element, className)) element.className = (element.className+' '+className);
	},

	removeClassName: function(element, className) {
		element = $(element);
		if (Element.hasClassName(element, className)) element.className = element.className.replace(className, '');
	}

});

document.getElementsByClassName = function(className){
	var elements = [];
	var all = document.getElementsByTagName('*');
	$A(all).each(function(el){
		if (Element.hasClassName(el, className)) elements.push(el);
	});
	return elements;
};





//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};


// Fading effect from Effects.js

Effects = {};

Effects.fade = function(id, opacStart, opacEnd, duration, callback)
{
	//Effects.changeOpacity(0, id);
	var speed = Math.round(duration/100);
	var timer = 0;

	if(opacStart > opacEnd)
	{
		for(var i=opacStart; i>=opacEnd; i--)
		{
			setTimeout("Effects.changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +"')", (timer*speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd)
	{
		for(var i=opacStart; i<=opacEnd; i++)
		{
			setTimeout("Effects.changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +"')", (timer*speed));
			timer++;
		}
	}
}

Effects.changeOpacity = function(opacity, id, endPoint, callback)
{
	var _style = document.getElementById(id).style;
    _style.opacity = (opacity / 100);
    _style.MozOpacity = (opacity / 100);
    _style.KhtmlOpacity = (opacity / 100);
    _style.filter = "alpha(opacity=" + opacity + ")";
	
	if(opacity == endPoint && callback != null)
	{
		eval(callback);
	}
}

Effects.onFaded = function()
{
	// Add callback code here
}
