var $active_span = $('<span class="cb-active-triangle"></span>');

function calculate() {
		var game = $('#bcf-game').attr('rel');
		var sum = $('#bcf-sum').attr('rel');
		
		var html = '';
		
		var bonuses = BonusCalculator.calc(sum, game);
		var bestCasinoIds = BonusCalculator.getBestCasinoIds(bonuses);
		
		for (var casino in bonuses) {
			var item = bonuses[casino];
			
			var codes = item.codes.slice(0, item.deposits.length);
			while (codes.length<item.deposits.length) {
				codes[codes.length] = 'auto';
			}
			
			var $td_bonus = $('.result-bonus td[rel='+casino+']');
			if (item.bonus!='0') {
				$td_bonus.text('$'+item.bonus);
			} else {
				$td_bonus.text('-');
			};
			
		}
		$('.cb-best').removeClass('cb-best');
		$('.cb-active').removeClass('cb-active')
		for (var i in bestCasinoIds) {
			$('td[rel='+bestCasinoIds[i]+']').addClass('cb-best');
		}
		
		var best_first = $('.cb-best:first').attr('rel');
		$('.cb-best:first').addClass('cb-active').append($active_span);
		var casinodesc_default = '<tr><td class="cb-result-calculating">Calculating...</td></tr>';
		$('.cb-result-casino').html(casinodesc_default);
		
		setTimeout(function(){
			casinoDesc(best_first);
			}, 500);
			
		$('.result-casino td').each(function(){
			$(this).removeClass('cb-clickable');
			$(this).unbind('click');
			if ($('.result-bonus td[rel='+$(this).attr("rel")+']').text()!='-') {
				$(this).addClass('cb-clickable');
			}
		});
		$('.cb-clickable').click(function(e){
			$('.result-casino td.cb-active').removeClass('cb-active');
			$(this).addClass('cb-active').append($active_span);
			var best_first = $(this).attr('rel');
			casinoDesc(best_first);
		});
		
		return false;
		
	};
	
function casinoDesc(best_first){
	var casino_template = 	'<tr>'+
							'<td class="cbrc-logo">'+
							'<a target="_blank" href="visit/$id" class="$id"></a>'+
							'</td>'+
							'<td class="cbrc-text">'+
							'$medal'+
							'<a target="_blank" href="visit/$id" class="cbrct-title cbrc-us">$name</a>'+
							'<p>$desc</p>'+
							'<div class="cbrct-links">'+
							'<span class="cbrctl-l"><a target="_blank" href="visit/download/$id">Download</a> ($sfsz)</span>'+
							'<span class="cbrctl-r"><a href="$review" id="cbrf-submit">How to claim</a></span>'+
							'</div>'+
							'</td>'+
							'</tr>';
	var bounuses_review = partners_php[best_first].review.substring(0, partners_php[best_first].review.length-4)+'-bonuses.htm';
	
	
	var best_casino_html = casino_template
			.replace(/\$name/g, BonusCalculator.casinos[best_first].name)
			.replace(/\$id/g, best_first)
			//.replace(/\$desc/, BonusCalculator.casinos[best_first].description)
			.replace(/\$desc/, $('#cb-description-'+best_first).html())
			.replace(/\$sfsz/, partners_php[best_first].sfsz)
			.replace(/\$review/, bounuses_review);
	var medal = '<div class="cbrct-medal"><span>$'+$('#bcf-sum').text()+'</span></div>';
	if (best_first==$('.result-bonus .cb-best').attr('rel')) {
		best_casino_html = best_casino_html.replace(/\$medal/g, medal);
	} else {
		best_casino_html = best_casino_html.replace(/\$medal/g, '');
	}
	$('.cb-result-casino').html(best_casino_html);
	
	
	var game = $('#bcf-game').attr('rel');
	var sum = $('#bcf-sum').attr('rel');
	
	$('#cbrf-submit').unbind('click').click(function(e){
		e.preventDefault();
		var action = $(this).attr('href');
		$('#cb-real-form').attr('action', action);
		$('#cbrf-game').val(game);
		$('#cbrf-sum').val(sum);
		$('#cb-real-form').submit();
	});
}
function fformUpdate($this){
	var $sel = $this.find('.selected');
	$this.find('.bcf-txt').text($sel.text()).attr('rel', $sel.attr('rel'));
	
};

$(document).ready(function() {
	$('body').bind('click', function(e){
		if (!$(e.target).is('.bc-form *') && !$(e.target).is('.bc-form')) {
			$('.bcf-icont').hide();
		};
	});
	$('.bc-form').each(function(){
		var $this = $(this);
		fformUpdate($this);
		$this.click(function(){
			$('.bcf-icont').not($('.bcf-icont',$this)).hide();
			$('.bcf-icont',$this).toggle();
			$('.bcf-item',$this).click(function(){
				$this.find('.selected').removeClass('selected');
				$(this).addClass('selected');
				fformUpdate($this);
				calculate();
				$('.bcf-item',$this).unbind('click');
			});
		});
		
	});
	
	$('.main-bonus-calc, .cb-bonus-calc').each(function(){
		calculate();
	})
	
	
	
});
