(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);

$(function() {
	
	$.preload([
	           "/images/receive-ball-on.jpg",
	           "/images/invest-ball-on.jpg",
	           "/images/give-ball-on.jpg",
	           "/images/tell-ball-on.jpg"
	], {
		init: function(loaded, total) {
			$("#indicator").html("Loaded: "+loaded+"/"+total);
		},
		loaded: function(img, loaded, total) {
			$("#indicator").html("Loaded: "+loaded+"/"+total);
		},
		loaded_all: function(loaded, total) {
			$("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
		}
	});

});


$(document).ready(function(){
  var doAnimate = true;
  var animateFeature = "invest";
  var rotateDelay = 7000;

  function contentRotate() {
	    
	  	if (doAnimate) {
	    	if (animateFeature == "receive") {
	    	  	rotateAllOff();
	    	    $("#invest-button").addClass("on");
	    		$("#invest").fadeIn();
	    		animateFeature = "invest";
	    	} else if (animateFeature == "invest") {
	    	  	rotateAllOff();
	    	    $("#give-button").addClass("on");
	    		$("#give").fadeIn();
	    		animateFeature = "give";
	    	} else if (animateFeature == "give") {
	    	  	rotateAllOff();
	    	    $("#tell-button").addClass("on");
	    		$("#tell").fadeIn();
	    		animateFeature = "tell";
	    	} else if (animateFeature == "tell") {
	    	  	rotateAllOff();
	    	    $("#receive-button").addClass("on");
	    		$("#receive").fadeIn();
	    		animateFeature = "receive";
	    		
	    	} else { animateFeature="invest"; }	    	
	  	}
	  	setTimeout(function () { contentRotate();}, rotateDelay);
  }

  function rotateAllOff() {
	    $("#invest").hide();$("#receive").hide(); $("#tell").hide(); $("#give").hide();
	    $("#invest-button").removeClass("on"); $("#receive-button").removeClass("on"); $("#give-button").removeClass("on"); $("#tell-button").removeClass("on");
  }
  
  $("#receive-button").mouseover(function(){
	  	rotateAllOff();
	  	$("#receive-button").addClass("on");
	  	$("#receive").fadeIn();
	  	doAnimate = false;
  });
  $("#receive-button").mouseout(function(){ doAnimate = true; animateFeature="tell"; });

  $("#invest-button").mouseover(function(){
	  	rotateAllOff();
	    $("#invest-button").addClass("on");
	    $("#invest").fadeIn();
	    doAnimate = false;
	  });
  $("#invest-button").mouseout(function(){ doAnimate = true;  animateFeature="receive"; });
  
  $("#give-button").mouseover(function(){
	  	rotateAllOff();
	    $("#give-button").addClass("on");
	  	$("#give").fadeIn();
	    doAnimate = false;
	  });
  $("#give-button").mouseout(function(){ doAnimate = true; animateFeature="invest"; });
  
  $("#tell-button").mouseover(function(){
	  	rotateAllOff();
	  	$("#tell-button").addClass("on");
	    $("#tell").fadeIn();
	    doAnimate = false;
  	});
  $("#tell-button").mouseout(function(){ doAnimate = true; animateFeature="give"; });
  
  contentRotate();  
  
});
