(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

//jQuery.preLoadImages();

$(document).ready(function() {
	var li_height = $("#services_list li").height();	
	var servicesList = $("#services_list li");
	var servicesContent = $("#services_content div");
	
	$("#services_list").on("click", "li", function(e){
		e.preventDefault();
		
		$$ = $(this);
		
		
		var new_arrow_position = 22 + $$.index()*li_height;
		$("#s_arrow").animate({top: new_arrow_position});
		
		servicesList.removeClass('active');
		$$.addClass('active');
		
		var new_id = $$.children("a").data("content");
		servicesContent.hide();
		$("#"+new_id).fadeIn();
		
	});
	
	// Bind to change on cboxes. If value of no = checked, then animate down, else, up.
	
	$(".cbox").on("click", "input", function(e) {
		if ($("#horse_location_no").prop("checked")) {
			$("#hidden_unless_horse_location_not_same").animate({height:"400px"});
		} else {
			$("#hidden_unless_horse_location_not_same").animate({height:"0px"});			
		}
	});
	
	var horse = $(".horse").clone();
	var counter = 1;
	
	$(".horse").children("input, textarea").each(function(i){
		$(this).attr("name", $(this).attr("name")+"$"+counter);
		$(this).attr("id", $(this).attr("id")+"$"+counter);			
	});
	
	$(".horse").children("label").each(function(i){
		$(this).attr("for", $(this).attr("for")+"$"+counter);					
	});
	
	counter = counter + 1;
	
	$("#add_a_horse").on("click", function(e) {
		var new_horse = horse.clone();
		
		new_horse.children("h3").html("Horse "+counter);
		
		new_horse.children("input, textarea").each(function(i){
			$(this).attr("name", $(this).attr("name")+"$"+counter);
			$(this).attr("id", $(this).attr("id")+"$"+counter);			
		});
		
		new_horse.children("label").each(function(i){
			$(this).attr("for", $(this).attr("for")+"$"+counter);					
		});
		
		$(this).before(new_horse);
		
		counter = counter + 1;
		
		e.preventDefault();
	});
	
	$('#form').ajaxForm({
		target: '#target',
		beforeSubmit:  addSpinner,
		success: function() {
			window.setTimeout(function() {  
				$("#submit").removeAttr("disabled");
			}, 2000);
		}
	});

	function addSpinner() {
		$("#submit").attr("disabled","disabled");
		$("#target").html("<p class='error'><img src='/media/img/spinner.gif' /></p>");
	};
	
	$("#blog_sidebar").css("min-height",$("#blogposts").height());	
});
