$(document).ready(function(){

// Intitialize //

///// SET FUNCTION TO PRELOAD IMAGES ////////////////////
jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++)  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
///// PRELOAD IMAGES ////////////////////
$.preloadImages("/images/addbox_bg_blue.gif");

///// CHANGE BUTTONS ////////////////////
// return false on all links
$(".addbox .nav a").click(function(){
	$(".addbox").removeClass("blueswitch");
   return false;
});

// clicking one link adds its own class
$("#changegreen").click(function(){
	$('#save_abstain_switch').html("<input type='hidden' name='savings' id='savings' value='1' />");  
});
$("#changeblue").click(function(){
	$(".addbox").addClass("blueswitch");
	$('#save_abstain_switch').html("<input type='hidden' name='savings' id='savings' value='0' />");  
});

$(".refreshform").click(function(){
	$("#successwrapper").addClass("hideme");
	document.forms[0].reset();
	return false;
});


///// RESET BUTTON //////////////////////
$(".resetbutton").click(function() {
 	var answer = confirm('Are you sure you want to reset your balance?');
	if(answer){
		  $.ajax({
		    url: "/scripts/php/reset_balance.php",
		    data: null,
		    success: function() {
				// *TODO* Consider a pretty Ajax alert?
				$('#user_balance').text("$0.00");
		    }
		  });
	}
});


///// HANDLE SUBMIT REQUEST ////////////////////
  $('.error').hide();

  $(".addboxbutton").click(function() {
    // validate and process form here

    $('.error').hide();
	var amount = $("input#amount").val();
		if (amount == "") {
      		$("label.amount_error").show();
      		$("input#amount").focus();
      		return false;
    	} 
		// Also write something that strips the $ up front

	var category = $("select#category").val();
		if (category == "") {
      		$("label.category_error").show();
      		$("select#category").focus();
      		return false;
    	}

	var charity = $("input#charity").val();
	var description = $("#description").val();
	var savings = $("input#savings").val();
		/*
	var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }	*/

	var dataString = 'amount='+ amount + '&category=' + category + '&charity=' + charity + '&description=' + description + '&savings=' + savings;
    //alert (dataString);return false;

  $.ajax({
    type: "POST",
    url: "/scripts/php/take_entry.php",
    data: dataString,
    success: function() {
		$('#successwrapper').removeClass("hideme");
			// *TODO* consider adding a unique id to "recentnewadd"...
		var shortdesc = description.substring(0,30);
		amount = (parseFloat(amount)).toFixed(2);
		$('#recententries').prepend("<div id='recentnewadd1'>$"+amount+" <small><span class='minicat'>"+category+"</span> <i><a title='"+description+"'>"+shortdesc+"</a></i></small></div>");
		$('#recentnewadd1').hide().slideDown(800).
			animate({backgroundColor: "black"}, 800);// doesn't work
			// You have to use slideDown.. but it has to be applied to a previously hidden id, so we have to figure out how to track the id here
			
		var newbalance = $('#user_balance').text().substring(1);
		newbalance = (parseFloat(newbalance)+parseFloat(amount)).toFixed(2);
		$('#user_balance').text("$"+newbalance);
	
//      $('#successwrapper').html("<div id='message-icon' style='float:left; width:50px'><img id='checkmark' src='/images/check.png' style='margin-top:25px; padding:10px;'/></div><div id='message' style='float:left; width: 300px;'></div>");  
//      $('#message').html("<h2 style='margin-bottom:0px;padding-bottom:0px;'>Entry added</h2>") 
//      .append("<p style='margin-top:5px;'>It wasn't really validated very well, though. I'm working up a clever way to add a button here saying 'add another'...and possibly making this entry slide into place in the statistics column on the right.</p>")  
//      .hide()
//      .fadeIn(1500, function() {
//         $('#message-icon').append(""); 
//      });
    }
  });
  return false;
  
  });
});
