
var getTitle;
$(document).ready(function(){
  var scrollPaneApi
  var scrollPane

	scrollPane = $('.scroll-pane').jScrollPane();
	scrollPaneApi = scrollPane.data('jsp');

	
	var imagesBase = "images/";
	
	$(settings.backgrounds).each(function(num, bg){
		var im = new Image();
		im.src = imagesBase + bg;
		var obj = $(im);
		obj.css("display", "none");
		$("body").append(obj);
	});
	
	
	var chooseBackground = (function(){
		var lastChosen;
		return function(){
			var newBG = settings.backgrounds[Math.floor(Math.random() * settings.backgrounds.length)];
			if(newBG != lastChosen){
				lastChosen = newBG;
				$("body").css(
					"background-image", 
					"url("+ imagesBase + newBG + ")"
				);	
			}else{
				chooseBackground();	
			}
		}
	})();
	
	chooseBackground();
	
	getTitle = function(elem){
		var title = arguments[1] || "";
		title = $(elem).find(".title").html() + " / " + title;
		var parents = $(elem).parents("li");
		if(parents.length > 0){
			return getTitle(
				parents, 
				title
				);
		}else{
			return title.slice(0, title.length - 3);
		};
	}
	
	$(".nav li li").click(function(){
	  if(soundManager._mp_currentSO){
	      soundManager.fadeOut(soundManager._mp_currentSO);
	  }
		chooseBackground();
		//soundManager.stopAll();
		$(".nav").addClass("navClicked");
		$("li").removeClass("selected");
		$(this).addClass("selected");
		var newDiv = $('<div class="contentDestination scroll-pane"></div>');
		$(".contentDestination")
		  .replaceWith(newDiv);
		newDiv
		  .show()
		  .html(
    			"<h1>" + 
    			$(this).find(".title").html() + 
    			"</h1>" + 
    			$(".content", this).html()
    		);
    		
    if($(this).find(".title").html().trim() == "Thicket"){
      newDiv.css("margin-right", 80);
    }
  	if(newDiv.height() > 620){
  	  newDiv.height(620).jScrollPane();
  	}
		return false;
	});
	
	
	var mailingListFormDefaulText;

	var subForm = function(){
	    $('#listForm').hide();
		var baseUrl = '/emailSignup.php'; 
		var email = $('#listForm').attr("value");
		var doneProcessing = false;	
		$.ajax({
		    type: 'get',
			url: baseUrl,
			data: {email: email},
			success: function(){
				doneProcessing = true;
				$('#listSignUpMessage').text("Thank you! My robot servants have been notified and will be contacting you soon.");
				setTimeout(function() {
				    $("#listSignupStuff").fadeOut(1000, function(){$("#listSignupStuff").hide('slow')});
				}, 6000);
				
			},
			error: function(){
				$('#listSignUpMessage').text("Oops! Something went wrong. Sorry! This is embarrassing.");
			}
		});
		$('#listSignUpMessage').show();
		animateProcessingText = (function(){
			var step = 0;
			var steps = ["Processing", "Processing.", "Processing..", "Processing..."];
			return function(){
				$('#listSignUpMessage').show().text(steps[step++ % steps.length]);
				setTimeout(function() {
					if(!doneProcessing){
						animateProcessingText();	
					}
				}, 200);
			}
		})();
		animateProcessingText();
        $("#submitLink").hide();
	}

	$("#submitLink").live('click', subForm);

	mailingListFormDefaulText = $('#listForm').attr("value");
	$('#listForm').live(
		'click',
		function(){
			if($('#listForm').attr("value") == mailingListFormDefaulText){
				$('#listForm').attr("value", "");
				$("#submitLink").show();	
			}
		}
	)
	$('#listForm').live('keypress', function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) { //Enter keycode
		  subForm();
		}
	 
	});
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
});
