// This script was created by Jon Winstanley
// http://www.jonwinstanley.com
// If you use any of my code, please leave a note in your code crediting my work
// Thanks!

	$(document).ready(function() {
	
		$("#calcform").validate();
	
		var status = 0;
		
		$("#togglebutton").hover(function() {
			$(this).find("em").animate({opacity: "show", top: "280"}, "fast");
		}, function(){
			$('#status').fadeIn("slow");
			$(this).find("em").animate({opacity: "hide", top: "260"}, "slow");
		});
		
		$("#togglebutton").click(function() {
			if (status == 0) {
				status = 1;
				$('#status').html('I\'ve <strong>layed</strong> a market, now I want to <strong>back</strong> it');
				$('#togglebutton em').html('Click here to switch to the <strong>back</strong> bet calculator');
			} else {
				status = 0;
				$('#status').html('I\'ve <strong>backed</strong> a market, now I want to <strong>lay</strong> it');
				$('#togglebutton em').html('Click here to switch to the <strong>lay</strong> bet calculator');
			}
		});
	
		$("#calc").click(function() {

			//figure out the commision
			var commission_rate = ($("#commission").val() / 100);
			commission_rate = commission_rate * 1;
			var stake = $("#stake").val();
			stake = stake * 1;
			var odds = $("#odds").val();
			odds = odds * 1;
			var newodds = $("#newodds").val();
			newodds = newodds * 1;
			
			//swap the odds
			if(status == 1)
			{
				/*temp_odds = odds;
				odds = newodds;
				newodds = temp_odds;*/
			}
			
			var payout = stake * odds;
			var newpayout = stake / newodds;
			
			var orig_comm = (payout * commission_rate);
			orig_comm = Math.round(orig_comm * 100) / 100;
			var orig_profit = payout - orig_comm;

			var greenout = payout / newodds;
			greenout = Math.round(greenout * 100) / 100;
			
			var commission_cost = (greenout - stake) * commission_rate;
			commission_cost = Math.round(commission_cost * 100) / 100;

			var totalprofit = (greenout - commission_cost) - stake;
			totalprofit = Math.round(totalprofit * 100) / 100;

			if (commission_cost < 0) { commission_cost = commission_cost * -1; };
			if (totalprofit < 0) { totalprofit = totalprofit * -1; };
			
			var message = "<h2>Payout if the original bet wins, is <span class='green'>" + payout + "</span></h2>";

			if (status == 0){
				if (odds < newodds){
					message += "<p>It is not possible to guarantee a pay out because the odds are now longer than your original bet.</p>";
					message += "<p>Commission on the original bet will be <span class='red'>" + orig_comm + "</span></p>";
					message += "<p>Your profit will be <span class='green'>" + orig_profit + "</span> provided you win.</p>";
				} else {
					message += "<p>To win, whatever the result, lay <span class='green'>" + greenout + "</span> at odds of <span class='blue'>" + newodds + "</span></p>";
					message += "<p>Commission to pay would be <span class='red'>" + commission_cost + "</span></p>";
					message += "<p>Profit after commission will be <span class='green'>" + totalprofit + "</span></p>";
				}				
			} else {
				if (newodds < odds){
					message += "<p>It is not possible to guarantee a pay out because the odds are now longer than your original bet.</p>";
					message += "<p>Commission on the original bet will be <span class='red'>" + orig_comm + "</span></p>";
					message += "<p>You total profit will be <span class='green'>" + orig_profit + "</span> if you win.</p>";
				} else {
					message += "<p>To win, whatever the result, back <span class='green'>" + greenout + "</span> at odds of <span class='blue'>" + newodds + "</span></p>";
					message += "<p>Commission to pay would be <span class='red'>" + commission_cost + "</span></p>";
					message += "<p>Profit after commission will be <span class='green'>" + totalprofit + "</span></p>";
				}			
			}
			
			/*
			if (odds < newodds){
				message += "<p>It is not possible to guarantee a pay out because the odds are now longer than your original bet.</p>";
				message += "<p>Commission on the original bet will be <span class='red'>" + orig_comm + "</span></p>";
				message += "<p>You total profit will be <span class='green'>" + orig_profit + "</span> if you win.</p>";
			} else {
				message += "<p>To win, whatever the result, lay <span class='green'>" + greenout + "</span> at odds of <span class='blue'>" + newodds + "</span></p>";
				message += "<p>Commission to pay would be <span class='red'>" + commission_cost + "</span></p>";
				message += "<p>Profit after commission will be <span class='green'>" + totalprofit + "</span></p>";
			}
			*/
			
			$("p#result")
				.animate({opacity: "1"}, "fast")
				.slideDown("slow")
				.html(message)
			return false;				
			
			
		});
		$("#stake").change(function() {
			hideBox();
		});
		$("#odds").change(function() {
			hideBox();
		});
		$("#newodds").change(function() {
			hideBox();
		});
		$("#commission").change(function() {
			hideBox();
		});
	});
	function hideBox(){
		$("p#result").animate({opacity: 0}, 300);
	}
