/**
 * Bookmaker.js
 * @version $Id$
 */

//Bookmaker = {};

/**
 * Betslip constructor
 */
function Betslip() {
}

Betslip.prototype.totalStake = 0;
Betslip.prototype.estReturn = 0;


/**
 * Initialises Betslip by way of registering event handlers to all necessary 
 * elements and calculating stakes and returns {@see Betslip.prototype.calcStakeAndReturn()}
 * @return Betslip
 */
Betslip.prototype.init = function() {

	$("#betslip input:text").each(function(){
		$(this).keyup(function() {
			Bookmaker.Betslip.calcStakeAndReturn();
		});
		cookieName = $(this).attr("name");
		cookieName = cookieName.replace(/(\])|(\[)/g,"");
		cookieName = "betslip["+cookieName+"]";
		if ($.cookie(cookieName)) {
			$(this).val($.cookie(cookieName));
		}
	});
	
	$("#betslip a.betslip-clear").each(function() {
		$(this).click(function(){Bookmaker.Betslip.clearBet(); return false})
	});
	
	$("#betslip a.betslip-refresh").each(function(){
		$(this).click(function(){
			$.ajax({
				url: '/ajax/Bet/bookmaker/betslip',
				contentType: "html",
				type: "GET",
				cache: false,
				success: function(html) {
					$("#sidebar #betslip").replaceWith(html);
					Bookmaker.Betslip.init();
				}
			});
		})
	});

	$("#betslip #betslip-submit").mouseup(function(){Bookmaker.Betslip.placeBet()});
	$("#betslip form#betslip-form").submit(function(){return false});
	
	this.calcStakeAndReturn();
	return this;
	
}

/**
 * Adds bet to Betslip via Ajax call
 * @param string href
 * @return Betslip
 */
Betslip.prototype.addBet = function(href) {
	this.clearCookies();
	$("#sidebar #betslip").fadeTo('normal','0.5');
	$.ajax({
		url: '/ajax' + href,
		contentType: "html",
		dataType: 'xml',
		type: "GET",
		cache: false,
		success: function(xml) {
			$("#sidebar #betslip").replaceWith($(xml).find("betslip").text());
			Bookmaker.Betslip.init();
		}
	});
	return this;
}



/**
 * Clears stake cookies 
 * @return Betslip
 */
Betslip.prototype.clearCookies = function() {
	$("#sidebar #betslip input:text").each(function(){
		cookieName = $(this).attr("name");
		cookieName = cookieName.replace(/(\])|(\[)/g,"");
		cookieName = "betslip["+cookieName+"]";
        var date = new Date();
        date.setTime(date.getTime() - (3 * 24 * 60 * 60 * 1000));
		$.cookie(cookieName,"",{path: '/', expire: date});
	});
}

/**
 * Clears current bet from Betslip
 * @return Betslip
 */
Betslip.prototype.clearBet = function() {
	
	this.clearCookies();
	
	$.ajax({
		url: '/ajax/Bet/bookmaker/clearbet/',
		contentType: "html",
		type: "GET",
		cache: false,
		success: function(html) {
			$("#sidebar #betslip").replaceWith(html);
		}
	});
	return this;
}




/**
 * Calculates total stakes and returns for each bet in betslip and outputs to target elements
 * @return Betslip
 */
Betslip.prototype.calcStakeAndReturn = function(){
	
	Bookmaker.Betslip.totalStake = 0;
	Bookmaker.Betslip.estReturn = 0;
	
	$("#sidebar #betslip-form input:text").each(function(){
		Bookmaker.Betslip.addStake($(this).val());
		Bookmaker.Betslip.addReturn($(this));
		
		cookieName = $(this).attr("name");
		cookieName = cookieName.replace(/(\])|(\[)/g,"");
		cookieName = "betslip["+cookieName+"]";
        $.cookie(cookieName, $(this).val(),{ path: '/', expires: 0 });
	});
	
	$("#sidebar #betslip #total-stake span").contents().replaceWith(Bookmaker.Betslip.totalStake.toFixed(2));
	$("#sidebar #betslip #est-return span").contents().replaceWith(Bookmaker.Betslip.estReturn.toFixed(2));
	
	return this;
}


/**
 * Adds supplied "stake" to total stake
 * @param mixed stake
 * @return Betslip 
 */
Betslip.prototype.addStake = function(stake) {
	if (!isNaN(parseFloat(stake))) {
		this.totalStake = parseFloat(this.totalStake) + parseFloat(stake);
	}
	return this;
}


/**
 * Adds return value object 'obj' to total estimated return 
 * @param JQuery obj
 * @return Betslip
 */
Betslip.prototype.addReturn = function(obj) {
	 if (!isNaN(parseFloat(obj.val()))) {
		 this.estReturn = parseFloat(this.estReturn) + (parseFloat(obj.val()) * parseFloat(obj.attr("title")));
	 }
	 return this;
}


/**
 * Places bet and refreshes ticket 
 * @see Ticket.prototype.refresh()
 * @return Betslip
 */
Betslip.prototype.placeBet = function() {
	$("#betslip-submit").val("processing").attr("disabled","disabled");
	$.ajax({
		url: '/ajax/Bet/bookmaker/placeBet',
		type: 'POST',
		dataType: 'xml',
		cache: false,
		data: $('#sidebar #betslip form').serialize(),
		success: function(xml) {
			$("#sidebar #betslip").replaceWith($(xml).find("betslip").text());
			$("#mini-account-summary").empty().append($(xml).find("accountSummary").text());
			Bookmaker.Ticket.refresh();
			Bookmaker.Betslip.init();
		}
	});
		
	return this;
}



/**
 * Ticket constructor
 */
function Ticket() {
}


Ticket.prototype.refresh = function() {
	$.ajax({
		url: '/ajax/Bet/bookmaker/ticket',
		contentType: 'html',
		type: 'GET',
		cache: false,
		success: function(html){
			/**
			 * TODO {@link http://backup.moneyam.com/trac/php/ticket/389}
			 */
			$('#sidebar #closed-bets').remove();
			$('#sidebar #current-bets').replaceWith(html);
		}
	});
	return this;
}

function closePlayer(){
	//$('#streamingmedia-player object').remove();
	$('.streamingmedia-player').fadeTo('slow', 0, function(){
		$(this).slideUp('slow', function(){
			$(this).remove();
		});
	});
}

$(document).ready(function() {
	
	Bookmaker = {
		Betslip : new Betslip,
		Ticket : new Ticket,
		init : function() {
			this.Betslip.init();
			$("a.price").each(function(){
				
				$(this).append('<input type="hidden" value="'+$(this).attr("href")+'" />');
				$(this).removeAttr("href");
				
				$(this).click(function(){
					Bookmaker.Betslip.addBet($(this).find('input').val()); 
					return false;
				});
				

			});
			
		}
	};
	
	Bookmaker.init();
	
	$('a.audio-link').click(function(){
		if ($('.streamingmedia-player').length > 0) {
			closePlayer();
		}
		else {
			$.ajax({
				url: '/ajax/streamingmedia/audioplayer',
				contentType: "html",
				type: "GET",
				cache: false,
				success: function(html) {
					$("#sidebar").prepend(html);
					$("#sidebar .streamingmedia-player").slideDown();
				}
			});
		}
		return false;
	});
});



		


