function getCredits(raceid) {
	
	jQuery(function($) {
		var currentTime = new Date();
		$.ajax({
				type:"POST",
				cache:false,
				url: FantasyRacingAjax.blogUrl + "/wp-admin/admin-ajax.php",
				data:"action=fantasy_racing_get_credits&raceid="+raceid,
				success: function(msg){
					$("#fantasy_racing_credits_"+raceid).html(msg);
				},
				error: function(xml, text, error) {
					alert("Error" + xml + text + error);
				}
			});
	});
}



function draganddrop(raceid) {

	
	jQuery(function($) {
		
		if ($("#save_"+raceid).hasClass("enabled")) {
			return;
		}
		$("#save_"+raceid).addClass("enabled");  // Toggle to prevent multiple click events
		
		
		// Once dropped into the garage replace the existing car
		$(".fantasy_racing_car_droppable").droppable({
			accept:'.connectConstructor',
			drop:function(event, ui) {
				$(this).empty(".connectConstructor");
				$(this).append(ui.draggable.clone());
			}
		}).disableSelection();
		
		// After drop replace driver on podium
		$(".fantasy_racing_driver_droppable").droppable({
			accept:'.connectDriver',
			drop:function(event, ui) {
				$(this).empty(".connectDriver");
				$(this).append(ui.draggable.clone());
			}
		}).disableSelection();
		
		$(".connectDriver").draggable({
					helper:'clone',
					revert:'invalid',
					zIndex: 1000
				}).disableSelection();


		$(".connectConstructor").draggable({
					helper:'clone',
					revert:'invalid',
					zIndex: 1000
				}).disableSelection();

		//
		// Buy credits
		//
		$("#fantasy_racing_buy_"+raceid).click(function(){
			  window.location=$(this).attr("name"); return false;
			});

		//
		// Save current selections
		//
		$("#save_"+raceid).click(function () {
			var drivers = "";
			var constructors = "";
			
			$("#fantasy_racing_garage_pits_"+raceid).find("div .connectConstructor").each(function(i){
				var currentId = $(this).attr('id').replace(/[^\d\.]+/,'');
				constructors = constructors + "&constructors[]=" + currentId;
			});
			
			$("#fantasy_racing_podium_"+raceid).find("div .connectDriver").each(function(i){
				var currentId = $(this).attr('id').replace(/[^\d\.]+/,'');
				drivers = drivers + "&drivers[]=" + currentId;
			});
			
			$.ajax({
				type:"POST",
				url: FantasyRacingAjax.blogUrl + "/wp-admin/admin-ajax.php",
				data:"action=fantasy_racing_save_entry&raceid="+raceid + drivers + constructors,
				success: function(msg){
					if (msg.search(/^(alert|info|\d+):/) != -1) {
						var s = msg.split(":");
						if (s[0] == 'alert') {
							alert(msg);
						}
						if (s[0] == 'info') {
							$("#info_"+raceid).html(s[1]).fadeIn();
						} else {
							$("#fantasy_racing_credits_"+raceid).html(s[0]);
							$("#info_"+raceid).html(s[1]).fadeIn();
						}
					} else {
						alert("Error : " + msg);
					}
				},
				error: function(xml, text, error) {
					alert("Error" + xml + text + error);
				}
			});
		});
		
	});

}

/*
 * Setup the tabs for each race.
 * When shown init drag and drop and update the available credits.
 */
jQuery(document).ready(function($) {
	
	$('.fantasy_racing_tabs').tabs({
		cache:true,
		show: function(event, ui) {
		
			var raceid = $("div.fantasy_racing_tabs ul li.ui-state-active").attr("raceid");
			getCredits(raceid);
			draganddrop(raceid);
		}
	});

});

/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/

/*
	Modified by: Ian Haycox
	Countdown timer to next prediction deadline
*/
function fantasy_racing_countdown(obj)
{
	this.obj			= obj;
	this.Div			= "clock1";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.ServerDate		= "12/31/2020 5:00 AM";
	this.DisplayFormat	= "%%D%%d, %%H%%h, %%M%%m, %%S%%s.";
	this.FinishStr      = "Too Late";
	this.CountActive	= true;
	
	this.DisplayStr;

	this.Calcage		= fantasy_racing_cd_Calcage;
	this.CountBack		= fantasy_racing_cd_CountBack;
	this.Setup			= fantasy_racing_cd_Setup;
}

function fantasy_racing_cd_Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function fantasy_racing_cd_CountBack(secs)
{
    if (secs < 0) {
	  document.getElementById(this.Div).innerHTML = this.FinishStr;
	  return;
  }

  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));

  document.getElementById(this.Div).innerHTML = this.DisplayStr;
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}
function fantasy_racing_cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
  	var dnow	= new Date(this.ServerDate);
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack(gsecs);
}
