var hand_converter = new Class({
	initialize: function(container){
		this.game_parts = new Array('preflop','flop','turn','river');
		this.raw_hand;
		this.hc_status = {
			i:0,
			play:false,
			step_by_step:false,
			game_part:0,
			i_game_part:new Array(),
			game_finisehd:false,
			notify:true,
			players:new Hash(),
			text_version_loaded:false
		};
		this.config = {
			'delay':2800,
			'container':container,
			'hand_converter_url':'/modules/hand_converter/hand_converter.php'
		}
		this.preferences = new Hash.Cookie('hc_novopoker_settings',{duration:365});
		this.current_player_el = null;
	},
	load: function(hand){
		this.raw_hand = hand;
		this.create_table();
		//If user pref is text version switch to it
		if(this.preferences.get('hc_show_text_hand')){
			this.init_text_version();
			this.toggle_text_version();
		}else{
			this.init_full_version();
		}
	},
	init: function(hand_json,hand_text){
		this.hc_status.full_version_loaded = true;
		this.hand=hand_json;
		this.init_table();
		this.reset_table();
		this.get_odds();
		this.play_pause(false);//Play the hand with delay
	},
	init_full_version: function(){
		if(!this.hc_status.full_version_loaded){
			new Request.JSON({
								url: this.config.hand_converter_url,
								method:'post',
								onComplete:function(resJSON,resText){
									this.init(resJSON,resText)
								}.bind(this)
							}).send('hand='+escape(this.raw_hand));
		}
	},
	init_text_version: function(){
		if(!this.hc_status.text_version_loaded){
			new Request.HTML({
								url: this.config.hand_converter_url,
								method:'post',
								onComplete:function(resTree,resEl,resHtml){
									this.html_hand = resHtml;
									this.hc_status.text_version_loaded = true;
									this.fill_text_version(resHtml)
								}.bind(this)
							}).send('hand='+escape(this.raw_hand)+'&format=html');
		}
	},
	set_odds: function(values){
		var player = null;
		var player_id = 0;
		for(var i = 0;i<10;i++){
			player = this.hc_status.players[i];
			if(player && !player.fold){
				var player_el = this.hc_el.players[i]
				this.set_player_odds(player_el,parseFloat(values[player_id].ev/10));
				player_id++;
			}
		}
	},
	get_odds: function(part){
		part = part || this.hc_status.game_part;
		if(!this.preferences.get('hc_hide_odds') && this.hc_status.notify){
			var query = 'iterations=15000&board=';
			if(part>=1){
				var cards = this.hand.flop.actions[0].cards;
				query += '["'+cards[0]+'","'+cards[1]+'","'+cards[2]+'"';
				if(part==1){
					query +=',"__","__"]';
				}
			}
			if(part>=2){
				var cards = this.hand.turn.actions[0].cards;
				query += ',"'+cards[0]+'"';
				if(part==2){
					query +=',"__"]';
				}
			}
			if(part>=3){
				var cards = this.hand.river.actions[0].cards;
				query += ',"'+cards[0]+'"]';
			}
			if(part==0){
				query += '["__","__","__","__"]';
			}
			query += '&pockets=[';
			var comma = '';
			var player = null;
			for(var i = 0;i<10;i++){
				player = this.hc_status.players[i];
				if(player && !player.fold){
					if(player.cards){
						query += comma+'["'+player.cards[0]+'","'+player.cards[1]+'"]';
					}else{
						query += comma+'["__","__"]';
					}
					comma = ',';
				}
			}
			query += ']';
			if(this.odds_request){
				this.odds_request.cancel();
			}
			this.odds_request = new Request({'url':'/poker-eval-webservice','method':'get'}).addEvent('complete',function(resp){
					this.set_odds(JSON.decode(resp).eval);
			}.bind(this)).send(query);
		}
	},
	set_chips: function(){
		this.chips = new Array();
		this.chips[0] = this.min_chip;
		this.chips[1] = this.min_chip*5;
		this.chips[2] = this.min_chip*10;
		this.chips[3] = this.min_chip*20;
		this.chips[4] = this.min_chip*50;
	},
	init_odds: function(){
		if(!this.preferences.get('hc_hide_odds')){
			for(var i = 0;i<10;i++){
				var value = {};
				player_id = i;
				var player = this.hand.info.players[i];
				if(player){
					var player_seat = player.seat-this.hc_status.player_offset;
					if(player_seat<0){
						player_seat =player_seat+10;
					}
					value.name = player.name;
					if(
							(this.preferences.get('hc_show_all_card') && (this.preferences.get('hc_hide_main_player_card') && !player.main_player)) || 
							(!this.preferences.get('hc_hide_main_player_card') && player.main_player) ||
							(this.preferences.get('hc_show_all_card') && (!this.preferences.get('hc_hide_main_player_card') && !player.main_player))
					  )
					{
						value.cards = player.cards;
					}
					this.hc_status.players.set(player_seat,value);
				}
			}
		}
	},
	init_table: function(){
		var player_seat;
		var hc_c_player;
		//Place all players
		if(this.hand.info.player_offset==0) this.hand.info.player_offset = 1;
		this.hc_status.player_offset = this.hand.info.player_offset;
		//Set counter for game part
		this.hc_status.i_game_part[0] = this.hand.preflop.actions.length;
		this.hc_status.i_game_part[1] = this.hc_status.i_game_part[0] + this.hand.flop.actions.length;
		this.hc_status.i_game_part[2] = this.hc_status.i_game_part[1] + this.hand.turn.actions.length;
		this.hc_status.i_game_part[3] = this.hc_status.i_game_part[2] + this.hand.river.actions.length;

		this.players = new Hash();
		//Loop throught player to find the small blind and init chips
		this.hand.info.players.each(function(player){
			if(player.ant){
				this.min_chip = player.ant;
				this.set_chips();
			}else if(player.small_blind){
				this.min_chip = player.small_blind;
				this.set_chips();
			}
		}.bind(this));

		//Init pot
		this.set_main_pot(0);

		//Create all players
		this.hand.info.players.each(function(player){
			player_seat = player.seat-this.hc_status.player_offset;
			if(player_seat<0){
				player_seat =player_seat+10;
			}
			this.players.set(player.name,player_seat);
			hc_c_player = this.hc_el.players[player_seat];
			hc_c_player.hc_player.removeClass('hc_player_empty');
			this.set_player_name(hc_c_player,player.name);
			this.set_player_pot(hc_c_player,player.money);
			this.set_player_stack(hc_c_player,0);
			if(player.main_player && !this.preferences.get('hc_hide_main_player_card')){
				this.set_player_cards(hc_c_player,player.cards);
			}
			if(player.cards && !player.main_player && this.preferences.get('hc_show_all_card')){
				this.set_player_cards(hc_c_player,player.cards);
			}
			//Place small blind and big blind
			if(player.small_blind){
				hc_c_player.hc_player.addClass('hc_smallblind');
				this.set_player_action(hc_c_player,'SB : '+player.small_blind);
				this.set_player_stack(hc_c_player,player.small_blind)
				//this.update_main_pot(player.small_blind);
			}
			if(player.dealer){
				this.set_dealer(hc_c_player);
			}
			if(player.big_blind){
				hc_c_player.hc_player.addClass('hc_bigblind');
				this.set_player_action(hc_c_player,'BB : '+player.big_blind);
				this.set_player_stack(hc_c_player,player.big_blind)
				//this.update_main_pot(player.big_blind);
			}
			if(player.ant){
				this.update_player_stack(hc_c_player,-(player.ant))
			}
		}.bind(this));
		this.init_odds();
	},
	set_dealer: function(player_el){
		new Element('div',{'class':'hc_dealer'}).inject(player_el.hc_player_container);
	},
	set_player_name: function(player,name){
		if(name.length>8){
			name = name.substr(0,7)+'...';
		}
		player.hc_player_name.set('text',name);
	},
	set_main_pot:function(money){
		money = Math.round(money*100)/100; 
		this.hc_el.hc_main_pot.store('money',money);
		this.hc_el.hc_main_pot.getElements('div').dispose();
		if(money>0){
			var margin = this.create_stack(this.hc_el.hc_main_pot,money);
			new Element('div',{styles:{'margin-left':margin+'px'},'class':'stack_txt','text':'$ '+money}).inject(this.hc_el.hc_main_pot);
		}
	},
	reset_players_stack: function(){
		this.players.each(function(player_id){
				var player_el = this.hc_el.players[player_id]
				this.move_chips(player_el.hc_stack,this.hc_el.hc_main_pot);
				this.set_player_stack(player_el,0);
		}.bind(this));
	},
	update_main_pot:function(money){
		if(money==null){
			var money = 0;
			this.hc_el.players.each(function(player){
				money += this.get_player_stack(player);
			}.bind(this));

		}
		//Get pot money
		var pot = this.hc_el.hc_main_pot.retrieve('money');
		//Update main pot
		this.set_main_pot(pot+parseFloat(Math.round(money*100)/100));
	},
	update_player_stack: function(player,money){
		var stack = this.get_player_stack(player);
		this.update_player_pot(player,-(money));
		this.set_player_stack(player,stack+parseFloat(money));
	},
	move_chips: function(from_el,dest_el){
		if(this.hc_status.notify && !this.hc_status.no_chips_anim){
			var pos = null;
			var fx = null;
			var chips = from_el.getElements('.chip');
			var dest_pos = dest_el.getCoordinates();
			for(var i = 0;i<chips.length;i++){
				pos = chips[i].getCoordinates();
				chips[i].setStyles({'left':pos.left,'top':pos.top}).inject(this.hc_el.hc_content);
				fx = new Fx.Morph(chips[i],{duration:400});
				fx.start({
						'left':dest_pos.left,
						'top':dest_pos.top,
						'opacity':0
				});
			}
		}	
	},
	create_stack: function(el,money){
		el.getElements('.chip').dispose()
		var z = 1;
		var x = 1;
		var margin = 0;
		var tmp = 0;
		var tot_money = money;
		if(money>0){
			for(var i = 0;i<3;i++){
				if(money>0){
					for(var y = 4;y>=0;y--){
						if(money>0){
							tmp = money/this.chips[y]; 
							tmp = tmp.toString();
							tmp = tmp.charAt(0).toInt();
							if(tmp>0){
								money = money-(this.chips[y]*tmp);
								for(var nb = 0;nb<tmp;nb++){
									if(z==1){
										margin = x*23;
									}
									var tmp_y = y+1;
									new Element('div',{'class':'chip chip_'+tmp_y+' z_'+z+' x_'+x}).inject(el);
									z++;
									if(z==5){
										z=1;
										x++;
									}
								}
							}
						}else{
							break;
						}
					}
				}else{
					break;
				}
			}
		}
		if(x>3){
			el.getElements('.chip').dispose();
			new Element('div',{'class':'chip chip_6 z_1 x_1'}).inject(el);
			margin = 49;
		}
		return margin;
	},
	set_player_stack: function(player,money){
		player.hc_stack.store('money',money);
		player.hc_stack.getElements('div').dispose();
		var margin = this.create_stack(player.hc_stack,money);
		money = parseFloat(Math.round(money*100)/100);
		if(money>0){
			new Element('div',{styles:{'margin-left':margin+'px'},'class':'stack_txt','text':'$ '+money}).inject(player.hc_stack);
		}
	},
	get_player_stack: function(player){
		var money = player.hc_stack.retrieve('money'); 
		if(money) return parseFloat(money);
		else return 0;
		
	},
	set_player_pot: function(player,money){
		var tmp = parseFloat(Math.round(money*100)/100);
		player.hc_pot.set('text',tmp);
	},
	update_player_pot: function(player,money){
		//Get player money
		var pot = parseFloat(player.hc_pot.get('text'));
		//Update player pot
		this.set_player_pot(player,pot+parseFloat(Math.round(money*100)/100));
	},
	set_player_odds:function(player,value){
		value = value.toString();
		player.hc_odds.set('text',value+'');
	},
	set_player_action: function(player,action){
		player.hc_last_action.set('text',action);
	},
	reset_player_cards:function(player){
		this.reset_card(player.hc_card_1);
		this.reset_card(player.hc_card_2);
	},
	set_table_card: function(){
		if(this.hc_status.game_part==1){
			this.set_flop_cards();
		}else if(this.hc_status.game_part==2){
			this.set_turn_card();
		}else if(this.hc_status.game_part==3){
			this.set_river_card();
		}
		this.hide_notify(true);
		this.update_main_pot();
		this.reset_players_stack();
		this.get_odds();
	},
	set_player_cards: function(player,cards){
		var card_size;
		player.hc_card_1.removeClass('card_back');
		player.hc_card_2.removeClass('card_back');
		if(cards[0]) player.hc_card_1.addClass('card_'+cards[0]);
		if(cards[1]) player.hc_card_2.addClass('card_'+cards[1]);
	},
	set_card:function(el,card){
		if(el.hasClass('card_big')){
			el.addClass('cardb_'+card);
		}else{
			el.addClass('card_'+card);
		}
	},
	set_flop_cards:function(){
		this.reset_card(new Array(this.hc_el.hc_table_cards[0],this.hc_el.hc_table_cards[1],this.hc_el.hc_table_cards[2]));
		this.set_card(this.hc_el.hc_table_cards[0],this.hand.flop.actions[0].cards[0]);
		this.set_card(this.hc_el.hc_table_cards[1],this.hand.flop.actions[0].cards[1]);
		this.set_card(this.hc_el.hc_table_cards[2],this.hand.flop.actions[0].cards[2]);
	},
	set_turn_card:function(){
		this.reset_card(this.hc_el.hc_table_cards[3]);
		this.set_card(this.hc_el.hc_table_cards[3],this.hand.turn.actions[0].cards[0]);
	},
	set_river_card:function(){
		this.reset_card(this.hc_el.hc_table_cards[4]);
		this.set_card(this.hc_el.hc_table_cards[4],this.hand.river.actions[0].cards[0]);
	},
	reset_card:function(el, card_value){
		var card_size;
		if($type(el)!='array'){
			el = new Array(el);
		}
		el.each(function(card){
			card_size = 'card_small';
			if(card.hasClass('card_big')){
				card_size = 'card_big';
			}
			card.removeProperty('class');
			card.removeProperty('class');
			if(card_value){
				if(card_size=='card_big'){
					card.addClass('cardb_'+card_value);
				}else{
					card.addClass('card_'+card_value);
				}
			}
			card.addClass(card_size);
		});
	},
  	reset_player:function(player_el,player){
		player_el.hc_player.removeClass('hc_player_folds');
		this.reset_player_cards(player_el);
		this.set_player_action(player_el,'');
		this.set_player_pot(player_el,player.money);
		if(player.main_player && !this.preferences.get('hc_hide_main_player_card')){
			this.set_player_cards(player_el,player.cards);
		}
		if(this.preferences.get('hc_hide_odds')){
			player_el.hc_odds.addClass('none');
		}else{
			player_el.hc_odds.removeClass('none');
		}
		this.set_player_odds(player_el,'');
		if(player.cards && !player.main_player && this.preferences.get('hc_show_all_card')){
			this.set_player_cards(player_el,player.cards);
		}else{//Others player
			this.set_player_cards(player_el,new Array('back','back'));
		}
		//Place small blind and big blind
		if(player.small_blind){
			this.set_player_action(player_el,'SB : '+player.small_blind);
			//this.update_main_pot(player.small_blind);
			this.set_player_stack(player_el,player.small_blind);
//			this.update_player_pot(player_el,-(player.small_blind));
		}
		if(player.big_blind){
			this.set_player_action(player_el,'BB : '+player.big_blind);
			//this.update_main_pot(player.big_blind);
			this.set_player_stack(player_el,player.big_blind)
//			this.update_player_pot(player_el,-(player.big_blind));
		}
		if(player.ant){
			//this.update_player_stack(player_el,player.ant)
      //TEST
  		this.update_player_pot(player_el,-player.ant);
  		//this.move_chips(player.ant,this.hc_el.hc_main_pot);
  		this.update_main_pot(player.ant);
  		//this.update_player_pot(-player.ant);
		}
		if(player.blind){
			this.update_player_stack(player_el,player.blind)
			this.update_main_pot(-player.blind)
  		this.update_player_pot(player_el,player.blind);
		}
	},
	reset_table:function(){
		this.hc_status.notify = false;
		//Reset pot
		this.set_main_pot(0);
		//Reset players
		this.reset_players_stack();
		this.hand.info.players.each(function(player){
			var player_el = this.hc_el.players[this.players.get(player.name)]; 
			this.reset_player(player_el,player);
		}.bind(this));
		//reset table cards
		this.reset_card(this.hc_el.hc_table_cards,'back');
		//Reset timer
		this.reset_timer();
		//Reset game var 
		this.hc_status.i = 0;
		this.hc_status.play = false;
		this.hc_status.game_part = 0;
		this.hc_status.game_finisehd = false;
		//Reset play button
		this.hc_el.controls.playpause.removeClass('hc_paused');
		this.init_odds();
		//Hide notify
		this.hc_status.notify = true;
		this.hide_notify(true);
	},
	reset_timer:function(){
		$clear(this.hc_status.timer);
	},
	toggle_option: function(){
		this.hc_el.hc_options.toggleClass('none');
		this.hc_el.hc_options_container.toggleClass('none');
		if(this.hc_el.hc_options.hasClass('none')){
			this.hc_el.hc_info.set('text','Options');
			this.hc_el.hc_info.removeClass('hc_info_option');
		}else{
			this.hc_el.hc_info.set('text','Fermer');
			this.hc_el.hc_info.addClass('hc_info_option');
		}
		this.hc_el.hc_options_container.getElements('input').each(function(el){
			var item_id = el.getProperty('id');
			if(this.preferences.get(item_id)){
				el.set('checked',true);
			}
		}.bind(this));
	},
	create_options:function(){
		new Element('div').addClass('hc_title').set('text','Options').inject(this.hc_el.hc_options_container,'inside');

		//Create options content
		new Element('input').setProperties({'value':'1','type':'checkbox','id':'hc_show_all_card'}).addEvent('click',function(){this.save_options()}.bind(this)).inject(this.hc_el.hc_options_container,'inside');
		new Element('label').set('text','Afficher toutes les cartes connues').inject(this.hc_el.hc_options_container,'inside');

		new Element('input').setProperties({'value':'1','type':'checkbox','id':'hc_hide_main_player_card'}).addEvent('click',function(){this.save_options()}.bind(this)).inject(this.hc_el.hc_options_container,'inside');
		new Element('label').set('text','Masquer les cartes du joueur principal').inject(this.hc_el.hc_options_container,'inside');

		new Element('input').setProperties({'value':'1','type':'checkbox','id':'hc_show_text_hand'}).addEvent('click',function(){this.save_options()}.bind(this)).inject(this.hc_el.hc_options_container,'inside');
		new Element('label').set('text','Afficher la main sous forme textuelle').inject(this.hc_el.hc_options_container,'inside');

		new Element('input').setProperties({'value':'1','type':'checkbox','id':'hc_hide_odds'}).addEvent('click',function(){this.save_options()}.bind(this)).inject(this.hc_el.hc_options_container,'inside');
		new Element('label').set('text','Masquer les probabilités').inject(this.hc_el.hc_options_container,'inside');
	},
	save_options:function(){
		var current_i = this.hc_status.i;
		var show_text_hand = false;
		this.hc_el.hc_options_container.getElements('input').each(function(el){
			var item_id = el.getProperty('id');
			var item_checked = el.get('checked');
			this.preferences.set(item_id,item_checked);
			if(item_id=='hc_show_text_hand'){
				if(item_checked){
					show_text_hand = true;
				}
			}
		}.bind(this));
		if(show_text_hand){
			this.toggle_text_version();
		}else{
			this.init_full_version();
			this.toggle_text_version('hide');
			this.reset_table();
			this.go_to(current_i);
			this.get_odds();
		}
	},
	init_options:function(){
		var item_value;
		this.hc_el.hc_options_container.getElements('input').each(function(el){
			item_value = this.preferences.get(el.getProperty('id')); 
			if(el.getProperty('type')=='checkbox'){
				if(item_value==true){
					el.setProperty('checked','checked');
				}
			}
		}.bind(this));
	},
	toggle_text_version: function(force){
		if(!this.hc_status.text_version_loaded){
			this.init_text_version();
		}
		if(force=='hide'){
			this.hc_el.hc_text.addClass('none');
			this.hc_el.hc_info.removeClass('hc_info_text');
		}else{
			this.hc_el.hc_info.addClass('hc_info_text');
			this.hc_el.hc_text.removeClass('none');
		}
		for(control_el in this.hc_el.controls){
			if(force=='hide'){
				this.hc_el.controls[control_el].removeClass('none');
			}else{
				this.hc_el.controls[control_el].addClass('none');
			}

		}
		this.play_pause(false,true);
	},
	create_table: function(){
		//Init some vars
		var hc_top = '';
		var hc_player_empty = 'hc_player_empty';
		var hc_player;
		this.hc_el = {};
		this.hc_el.players = new Array();
		this.hc_el.controls = {};
		var hc_player_container;
		var hc_stack;
		var hc_odds;
		var hc_player_name;
		var hc_cards;
		var hc_last_action;
		var hc_pot;
		var hc_foot;
		var card_size = 'card_small';

		//First we create the structure
		this.hc_el.hc = new Element('div').addClass('hc');

		//Option container
		this.hc_el.hc_options = new Element('div').addClass('hc_options').setStyle('opacity','0.7').inject(this.hc_el.hc,'inside');
		this.hc_el.hc_options_container = new Element('div').addClass('hc_options_container').inject(this.hc_el.hc,'inside');

		//Create text version
		this.hc_el.hc_text = new Element('div').addClass('hc_text none').inject(this.hc_el.hc,'inside');

		//Options button
		var hc_info = new Element('div').addClass('hc_info').inject(this.hc_el.hc,'inside');
		this.hc_el.hc_info = new Element('a').addEvent('click',function(){this.toggle_option()}.bind(this)).set('text','Options').setProperty('href','javascript:void(0)').inject(hc_info,'inside');

		//Head
		this.hc_el.hc_content = new Element('div').addClass('hc_content').inject(this.hc_el.hc,'inside');
		hc_foot = new Element('div').addClass('hc_foot').inject(this.hc_el.hc,'inside');


		//Create options
		this.create_options();
		this.init_options();
		this.toggle_option();
		
		//Create footer content
		this.hc_el.controls.preflop =	new Element('a').setProperty('href','javascript:void(0)').set('text','Pre-flop').addClass('hc_game_part hc_current_game_part').inject(hc_foot,'inside').addEvent('click',function(){this.part_preflop()}.bind(this));
		this.hc_el.controls.flop =	new Element('a').setProperty('href','javascript:void(0)').set('text','Flop').addClass('hc_game_part').inject(hc_foot,'inside').addEvent('click',function(){this.part_flop()}.bind(this));
		this.hc_el.controls.turn =	new Element('a').setProperty('href','javascript:void(0)').set('text','Turn').addClass('hc_game_part').inject(hc_foot,'inside').addEvent('click',function(){this.part_turn()}.bind(this));
		this.hc_el.controls.river =	new Element('a').setProperty('href','javascript:void(0)').set('text','River').addClass('hc_game_part').inject(hc_foot,'inside').addEvent('click',function(){this.part_river()}.bind(this));
		
		this.hc_el.controls.next =	new Element('a').setProperty('href','javascript:void(0)').addClass('hc_action hc_next').inject(hc_foot,'inside').addEvent('click',function(){this.play_pause(false,true);this.next()}.bind(this));
		this.hc_el.controls.playpause =	new Element('a').setProperty('href','javascript:void(0)').addClass('hc_action hc_playpause').inject(hc_foot,'inside').addEvent('click',function(){this.play_pause(true)}.bind(this));
		this.hc_el.controls.previous =	new Element('a').setProperty('href','javascript:void(0)').addClass('hc_action hc_previous').inject(hc_foot,'inside').addEvent('click',function(){this.previous()}.bind(this));

		//Create the table
		var hc_table = new Element('div').addClass('hc_table').inject(this.hc_el.hc_content,'inside');
		var hc_cards = new Element('div').addClass('hc_table_cards').inject(hc_table,'inside');
		this.hc_el.hc_table_cards = new Array(
			new Element('div').addClass('card_big cardb_back').inject(hc_cards,'inside'),
			new Element('div').addClass('card_big cardb_back').inject(hc_cards,'inside'),
			new Element('div').addClass('card_big cardb_back').inject(hc_cards,'inside'),
			new Element('div').addClass('card_big cardb_back').inject(hc_cards,'inside'),
			new Element('div').addClass('card_big cardb_back').inject(hc_cards,'inside')
		);
		this.hc_el.hc_main_pot = new Element('div').addClass('hc_main_pot hc_stack').store('money',0).inject(hc_table,'inside');
		//Create players
		for(var i = 0; i < 10; i++){
			card_size = 'card_small';
			hc_player_empty = 'hc_player_empty';
			if(i>=0 && i<4 || i>7){
				hc_top = 'hc_top';
			}else{
				hc_top = '';
			}
			hc_player = new Element('div').addClass('hc_player hc_player_'+i+' '+hc_top+' '+hc_player_empty);
			hc_player_container = new Element('div').addClass('hc_player_container').inject(hc_player,'inside');
			hc_player_name = new Element('div').addClass('hc_player_name').inject(hc_player_container,'inside');
			hc_cards = new Element('div').addClass('hc_cards').inject(hc_player_container,'inside');
			hc_card_1 = new Element('div').addClass(card_size+' card_back').inject(hc_cards);
			hc_card_2 = new Element('div').addClass(card_size+' card_back').inject(hc_cards);
			hc_last_action = new Element('div').addClass('hc_last_action').inject(hc_player_container,'inside');
			hc_pot = new Element('div').addClass('hc_pot').inject(hc_player_container,'inside');
			hc_stack = new Element('div',{'class':'hc_stack'}).inject(hc_player,'inside');
			hc_odds = new Element('div',{'class':'hc_odds'}).inject(hc_player_container,'inside');
			if(this.preferences.get('hc_hide_odds')){
				hc_odds.addClass('none');
			}
			this.hc_el.players.push({
						'hc_player':hc_player,
						'hc_player_container':hc_player_container,
						'hc_player_name':hc_player_name,
						'hc_cards':hc_cards,
						'hc_last_action':hc_last_action,
						'hc_pot':hc_pot,
						'hc_card_1':hc_card_1,
						'hc_card_2':hc_card_2,
						'hc_odds':hc_odds,
						'hc_stack':hc_stack
					});
			hc_player.inject(this.hc_el.hc_content,'inside');
		}

		//Create notify element
		this.hc_el.hc_notify = new Element('div').addClass('hc_notify none').inject(this.hc_el.hc_content,'inside');
		this.hc_el.hc_notify_container = new Element('div').addClass('hc_notify_container').inject(this.hc_el.hc_notify,'inside');
		
		//Draw it ;)
		this.hc_el.hc.inject(this.config.container,'inside');
	},
	go_to:function(i_game_part){
		this.hc_status.notify = false;
		for(var i=0;i<i_game_part;i++){
			this.next();
		}
		this.hc_status.notify = true;
	},
	previous: function(){
		var current_i = this.hc_status.i
		this.reset_table();
		this.go_to(current_i-2);
		if(current_i>1){
			this.get_odds();
			this.hc_status.no_chips_anim = true;
			this.next();
			this.hc_status.no_chips_anim = false;
		}else{
			this.get_odds();
		}
	},
	part_preflop:function(){
		this.reset_table();
		this.get_odds(0);
	},
	part_flop:function(){
		this.reset_table();
		this.go_to(this.hc_status.i_game_part[0]+1);
		this.get_odds(1);
	},
	part_turn:function(){
		this.reset_table();
		this.go_to(this.hc_status.i_game_part[1]+1);
		this.get_odds(2);
	},
	part_river:function(){
		this.reset_table();
		this.go_to(this.hc_status.i_game_part[2]+1);
		this.get_odds(3);
	},
	play_pause:function(direct_play,force_pause){
		if(force_pause){
			this.hc_el.controls.playpause.removeClass('hc_paused');
			this.hc_status.play = false;
			this.reset_timer();
		}else{
			this.hc_el.controls.playpause.toggleClass('hc_paused');
			if(this.hc_status.play){//User paused
				this.hc_status.play = false;
				this.reset_timer();
			}else{//user played
				this.hc_status.play = true;
				if(direct_play){
					this.next();
				}
				this.hc_status.timer = this.next.periodical(this.config.delay,this);
			}
		}
	},
	stop:function(){
		$clear(this.hc_status.timer);
	},
	fill_text_version: function(content){
		this.hc_el.hc_text.set('html',content);
	},
	next:function(){
		var game_part = this.hand[this.game_parts[this.hc_status.game_part]];
		var item;
		var player_el;
		var i = 0;
		if(this.hc_status.game_part>0){//After preflop
			i = this.hc_status.i_game_part[this.hc_status.game_part-1];
		}
		var l = this.hc_status.i_game_part[this.hc_status.game_part];
		var i_part = 0;
		var next_action = '';
		if(!this.hc_status.game_finisehd){//Game is not finisehd
			for (i; i < l; i++){
				if(this.hc_status.i==i){
					item = game_part.actions[i_part];
					if(game_part.actions[i_part]){
						if(game_part.actions[i_part].action=='show'){
							this.update_main_pot();
							this.reset_players_stack();
						}
					}

					if(this.current_player_el){
						this.current_player_el.hc_player.removeClass('hc_current_player');
					}

					player_el = this.hc_el.players[this.players.get(item.player)]; 

					if(player_el){
						this.current_player_el = player_el;
						player_el.hc_player.addClass('hc_current_player');
					}
					if(item.action=='cards') this.set_table_card(item);
					else if(item.action=='fold') this.action_fold(player_el,item);
					else if(item.action=='raise') this.action_raise(player_el,item);
					else if(item.action=='call') this.action_call(player_el,item);
					else if(item.action=='check') this.action_check(player_el,item);
					else if(item.action=='collect') this.action_collect(player_el,item);
					else if(item.action=='uncalledbet') this.action_uncalledbet(player_el,item);
					else if(item.action=='show') this.action_show(player_el,item);
					else if(item.action=='return') this.action_return(player_el,item);
					this.hc_status.i++;//Increment the part of the game
					prev_player = player_el;
					//Exit the while
					break;
				}
				//Update
				i_part++;
			}
		}
		//We have done all the actions for this part of the game
		if(this.hc_status.i==this.hc_status.i_game_part[this.hc_status.game_part]){
			//Increment game part
			this.hc_status.game_part++;
			//update current game part
			game_part = this.hand[this.game_parts[this.hc_status.game_part]];
			if(!game_part){
				this.hc_status.game_finisehd = true;
				this.set_main_pot(0);
				this.stop();
			}
		}
	},
	action_return: function(player_el,action){
		var message = 'Return '+action.money;
		//Set action
		this.set_player_action(player_el,message);
		//Update player pot
		this.update_player_pot(player_el,action.money);
		this.update_player_stack(player_el,-action.money);
		//Update main pot
		//this.update_main_pot(-action.money);
		//Notify
		this.player_notify(player_el,message);
	},
	action_show: function(player_el,action){
		this.player_notify(player_el,'Show '+action.cards.join(','));
		this.set_player_cards(player_el,action.cards);
		//update player info
		var player_id = this.players.get(action.player);
		var tmp = this.hc_status.players.get(player_id);
		tmp.cards = action.cards;
		this.hc_status.players.set(player_id,tmp);
		if(!this.preferences.get('hc_show_all_card') || (this.preferences.get('hc_hide_main_player_card') && player_id==0)){
			this.get_odds();
		}
	},
	action_uncalledbet: function(player_el,action){
		var message = action.money+' returned';
		//Set action
		this.set_player_action(player_el,message);
		//Update player pot
		this.update_player_pot(player_el,action.money);
		//Update main pot
		//BLOP
		if(this.get_player_stack(player_el)==0){
  		this.move_chips(this.hc_el.hc_main_pot,player_el.hc_stack);
  		this.update_main_pot(-(action.money));
  		this.set_player_stack(player_el,0);
    }else{
  		this.set_player_stack(player_el,this.get_player_stack(player_el)-(action.money));
    }

		//Notify
		this.player_notify(player_el,message);
	},
	action_collect: function(player_el,action){
		var message = 'Collect '+action.money;
		//Set action
		this.set_player_action(player_el,message);
		//Update player pot
		this.update_player_pot(player_el,action.money);
		//Update main pot
		this.move_chips(this.hc_el.hc_main_pot,player_el.hc_stack);
		this.update_main_pot(-(action.money));
		this.set_player_stack(player_el,action.money);

		//Notify
		this.player_notify(player_el,message);
	},
	action_check: function(player_el,action){
		//Set action
		this.set_player_action(player_el,'Check');
		//Notify
		this.player_notify(player_el,'Check');
	},
	action_call: function(player_el,action){
		var action_name = 'Call';
		if(action.allin==true){
			action_name = 'All-In';
		}
		var message = action_name+' '+action.money;
		//Set action
		this.set_player_action(player_el,message);
		this.update_player_stack(player_el,action.money);
		if(action.allin){
			//Some room bug so if it's a all in set player pot to 0
			this.set_player_pot(player_el,0);
		}
		//Notify
		this.player_notify(player_el,message);
	},
	action_raise: function(player_el,action){
		var action_name = 'Raise';
		var la_name = 'Raise';
		if(action.allin==true){
			action_name += ' (All-In)';
			la_name = 'All-In';
		}
		var message = action_name+' '+action.money;
		var message_la = la_name+' '+action.money;
		//Set action
		this.set_player_action(player_el,message_la);
		this.update_player_stack(player_el,action.money);
		if(action.allin){
			//Some room bug so if it's a all in set player pot to 0
			this.set_player_pot(player_el,0);
		}
		//Notify
		this.player_notify(player_el,message);
	},
	action_fold: function(player_el,action){
		//Move chips
		this.move_chips(player_el.hc_stack,this.hc_el.hc_main_pot);
		this.update_main_pot(this.get_player_stack(player_el));
		this.set_player_stack(player_el,0);
		//update player info
		if(!this.preferences.get('hc_hide_odds')){
			var player_id = this.players.get(action.player);
			var tmp = this.hc_status.players.get(player_id);
			tmp.fold = true;
			this.hc_status.players.set(player_id,tmp);
			this.get_odds();
		}
		//Set player folds
		player_el.hc_player.addClass('hc_player_folds');
		//Reset the cards
		this.reset_player_cards(player_el);
		//The the folds card skin
		this.set_player_cards(player_el,new Array('fold','fold'));
		//Set actions
		this.set_player_action(player_el,'Fold');
		//Notify
		this.player_notify(player_el,'Fold');
	},
	player_notify: function(player_el,text){
		if(this.hc_status.notify){
			var position = player_el.hc_player_name.getPosition('relative');

			var left = position.x-115; 
			var top = position.y-45;

			if(player_el.hc_player.hasClass('hc_player_7') || player_el.hc_player.hasClass('hc_player_8') || player_el.hc_player.hasClass('hc_player_9')){
				this.hc_el.hc_notify.addClass('bubble_right');
				left = left+189;
			}else{
				this.hc_el.hc_notify.removeClass('bubble_right');
			}

			this.hc_el.hc_notify.setStyles({
				'left':left+'px',
				'top':top+'px'
			});

			this.hc_el.hc_notify_container.set('text',text);
			this.hc_el.hc_notify.removeClass('none');
			this.hide_notify.delay(this.config.delay-500,this);
		}
	},
	hide_notify: function(force){
		if((this.hc_status.play || force) && this.hc_status.notify){
			this.hc_el.hc_notify.addClass('none');
		}
	}
})
window.addEvent('domready',function(){
		$$('.hand_converter').each(function(el){
			//Hide pre
			el.addClass('none');
			//Create table
			var tmp = new Element('div').addClass('hand_converter_player').setProperty('id','hc_'+el.getProperty('id')).inject(el,'after');
			new Element('a').inject(tmp,'inside').setProperties({'href':'javascript:void(0)','rel':el.getProperty('id')}).addEvent('click',function(ev){
				var el = $(ev.target);
				var el_parent = $(ev.target).getParent();
				//Init hand converter
				var hand = new hand_converter(el_parent);
				el_parent.removeClass('hand_converter_player');
				el.dispose();
				hand.load($(el.getProperty('rel')).get('html'));
			});;
		});
});
