// JavaScript Document

// Variables
var tweets = {};



/**
*
*/
function init_tweets() {
	
	tweets.tweet_count = 0;
	tweets.num_tweets = $(".home .latest-tweets ul li").size();
	
	if (tweets.num_tweets > 1) {
		
		// Clone the first tweet
		$(".home .latest-tweets ul").find("li:first").clone(true).appendTo($(".home .latest-tweets").find("ul"));
		tweets.num_tweets ++;
		
		// Position for the first tweet
		$(".home .latest-tweets ul").find("li:first").css({ "margin-top": $(".home .latest-tweets ul").height()+"px" });
		
		scroll_tweets();
		
	}
	
}



/**
* Scrolls the twitter feed
*/
function scroll_tweets() {
	
	if (tweets.tweet_count >= tweets.num_tweets) {
		
		$(".home .latest-tweets ul").find("li:first").css({ "margin-top": "0px" });
		
		tweets.tweet_count = 1;
		
		scroll_tweets();
		
	}
	
	else {
		
		// Animate with a bounce!
		$(".home .latest-tweets ul").find("li:first").animate({
			
			"marginTop" : "-="+$(".home .latest-tweets ul").height()+"px"
			
		}, 200, "swing", function() {
			
			$(".home .latest-tweets ul").find("li:first").animate({
				
				"marginTop" : "+=10px"
				
			}, 50, "linear", function() {
				
				$(".home .latest-tweets ul").find("li:first").animate({
					
					"marginTop" : "-=10px"
					
				}, 50, "linear", function() {
					
					$(".home .latest-tweets ul").find("li:first").animate({
						
						"marginTop" : "+=5px"
						
					}, 50, "linear", function() {
						
						$(".home .latest-tweets ul").find("li:first").animate({
							
							"marginTop" : "-=5px"
							
						}, 50, "linear", function() {
							
							tweets.tweet_count ++;
							
							tweets.tweetTimer = window.setTimeout(scroll_tweets, 8000);
							
						});
						
					});
					
				});
				
			});
			
		});
		
	}
	
}



/**
* Animates a cloud
*/
function animate_cloud(cloud) {
	
	$("#"+cloud['item']).animate({
		
		"left" 	: (($(window).width() + $(this).width()) / 2)+"px"
		
	}, cloud['speed'], "linear", function() {
		
		// Reset
		$("#"+cloud['item']).css({ "left": "-"+($("#"+cloud['item']).width())+"px" });
		
		animate_cloud(cloud);
		
	});
	
}



/**
* Initiate some basic animations
*/
function bring_to_life() {
	
	// Up and away, Clouds
	var clouds = Array();
	
	clouds[1] = {
		"item" 		: "cloud-1",
		"start_x" 	: 750,
		"start_y" 	: 500,
		"speed" 	: 45000
	};
	
	clouds[2] = {
		"item" 		: "cloud-2",
		"start_x" 	: 400,
		"start_y" 	: 680,
		"speed" 	: 35000
	};
	
	clouds[3] = {
		"item" 		: "cloud-3",
		"start_x" 	: 1100,
		"start_y" 	: 910,
		"speed" 	: 25000
	};
	
	for (cloud in clouds) {
		
		$("#up-and-away").prepend("<div class=\"cloud-"+cloud+"\" id=\"cloud-"+cloud+"\"></div>");
		
		$("#cloud-"+cloud).css({ "left": clouds[cloud]['start_x']+"px", "top": clouds[cloud]['start_y']+"px" });
		
		animate_cloud(clouds[cloud]);
		
	}
	
	// Office parrot
	$("#office .room").prepend("<div class=\"parrot\" id=\"parrot\"></div>");
	
	// Kitchen, dog blink
	$("#kitchen .room").prepend("<div class=\"dog-blink-kitchen\" id=\"dog-blink-kitchen\"></div>");
	
	// Kitchen, aga fan
	$("#kitchen .room").prepend("<div class=\"aga-fan\" id=\"aga-fan\"></div>");
	
	// Home, dog blink
	$("#home .room").prepend("<div class=\"dog-blink-home\" id=\"dog-blink-home\"></div>");
	
	// Home, bees
	$("#home .room").prepend("<div class=\"bees\" id=\"bees\"></div>");
	
	// Factory, winder
	$("#workshop .room").prepend("<div class=\"winder\" id=\"winder\"></div>");
	
	// Info button
	$(".info-button").fadeIn(1000);
	
}



/**
* Detects iphone users
*/
function detect_iphone() {
	
	var is_iphone;
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		
		if (document.cookie.indexOf("iphone_redirect=false") == -1) is_iphone = true;
		
	}
	
	return is_iphone;
	
}



/**
* Scrolls to a selected scene
*/
function scroll_to_scene(targetId, scrollSpeed, show_info) {
	
	var target = $(targetId);
	var targetOffset = target.offset().top;
	var windowHeight = $(window).height();
	var scrollToPos = targetOffset - (windowHeight - target.height());
	
	$("html:not(:animated), body:not(:animated)").animate({ "scrollTop": scrollToPos }, scrollSpeed, function() {
		
		if (show_info) $(targetId+" .info-window").fadeIn(50);
		
	});
	
	$(".main-menu li").removeClass("selected");
	$(".main-menu a[href="+targetId+"]").parent().addClass("selected");
	
}



/**
* Replaces the noscript text notice
*/
function replace_noscript() {
	
	$(".too-far").append("<p>You've gone too far! scroll down to go back to my mill</p>");
	
}



// Make sure the document is ready
$(document).ready(function() {
	
	/**
	* Adds the IE6 warning to the document
	*/
	if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) {
		
		$("body").prepend("<div id=\"ie6-warning\"></div>");
		$("#ie6-warning").load("/templates/filberts-house/templates/custom/ie6.php");
		
	}
	
	else {
		
		// Scroll the twitter feed
		init_tweets();
		
		// Scroll to a scene from a main menu click
		$("a.jump").click(function(event) {
			
			event.preventDefault();
			
			scroll_to_scene($(this).attr("href"), 2000);
			
		});
		
		// Info popups
		$(".info-button a").click(function(event) {
		
			event.preventDefault();
			
			// Close any open info windows
			$(".info-window").fadeOut(50);
			
			var scene = "#"+$(this).closest(".scene").attr("id");
			scroll_to_scene(scene, 1000, true);
		
		});
		
		$(".info-window a.close").click(function(event) {
			
			event.preventDefault();
			
			$(this).closest(".info-window").fadeOut(50);
			
		});
		
		// Animate! (not on iphones)
		if (!detect_iphone()) bring_to_life();
		
		// Replace the noscript message
		replace_noscript();
		
		// Room scenes
		scroll_to_scene((window.location.hash ? window.location.hash : "#home"), 1000);
		
	}
	
});

