

$(document).ready(function() {
	
	init();		   
	windowResize();
	
	if(jQuery().kwicks) {
		kwicks();
	}
	
	if( $("#cycle").size() ) {
		cycleManual();
	}
	
	//if( $("#cycle-home").size() ) {
		//cycleAuto();
	//}

	if( $("#description").size() ) {
		descriptionBox();
	}
	
	if( $("#news").size() ) {
		news();
		vAlignNews();
	}
	
	if( $("#team").size() ) {
		team();
	}

	thoughtProcess();
	thoughtProcessCycle();

});

function init() {

	$(".jsEnabled #content")
		.delay(1000)
			.animate({ "opacity" : "1" }, 1200, 'easeOutExpo', function() {
			
				//alert("content faded in");
			
			});
	 
	 
	if ( isIOS() && $("#cycle-home").size() ) {
		cycleAuto();
		noFlash();
	} else if ( !isIOS() && $("#cycle-home").size() ) { 
	
		cycleAuto();
		$("#skip-flash").click(function(){
			doSomething();
			return false;							
		});
	
	} else {
		setGlobalItems();
	}

	setContentSize();
	vAlignPage();
	vAlignTeam();
	navHighlight();
	
	
	var $columns = $("#columns");
	if( $columns.size() ) {
		//alert("hello");
		setInterval ( "throbbingColumns()", 5000 );
	}

}

function navHighlight() {

	var path = location.pathname;
	var parsed = path.split("/");
	
	for( var i = 0; i < parsed.length; i++ ) {
		
		var object = parsed[i];
		
		switch (object) {
			
			case "portfolio-item":
				$("#menu-item-18").addClass("current-menu-item");
				break;
				
			case "other-project-item":
				$("#menu-item-42").addClass("current-menu-item");
				break;
				
			case "2011":
				$("#menu-item-44").addClass("current-menu-item");
				break;
				
			case "2012":
				$("#menu-item-44").addClass("current-menu-item");
				break;
				
			case "2013":
				$("#menu-item-44").addClass("current-menu-item");
				break;
				
			default:
				// Default action.
				
		}
		
		
	}

	
}

function vAlignPage() {

	var $page = $("#page");
	
	if( jQuery().vAlign && $page.size() ) {
		$page.vAlign();
	}
	
}

function vAlignTeam() {

	var $team = $("#team");
	
	if( jQuery().vAlign && $team.size() ) {
		$team.vAlign();
	}
	
}

function randomFromTo(from, to){
	return Math.floor(Math.random() * (to - from + 1) + from);
}

function throbbingColumns() {

	$("#columns a div.column")
		.animate({ "background-color" : "#89734F" }, 1500, "linear")
			.animate({ "background-color" : "#000000" }, 2000, "linear");

	
}

function throbbingGrid() {
	$("div.thought-process-button")
		.animate({ "background-color" : "#89734F" }, 1500, "linear")
			.animate({ "background-color" : "#000000" }, 2000, "linear");
}
		
function thoughtProcess(isResize) {

	if (!isResize) {
		setInterval ( "throbbingGrid()", 5000 );
	}
	
	var $thoughtProcess = $("#thought-process"); 
	
	if( jQuery().vAlign && $thoughtProcess.size() ) {

		var $thoughtProcessItem = $(".thought-process-item");
		
		$thoughtProcess.vAlign();
		$thoughtProcess.after('<div id="thought-process-buttons"><div class="thought-process-button"></div><div class="thought-process-button"></div><div class="thought-process-button"></div><div class="thought-process-button"></div><div class="thought-process-button"></div><div class="thought-process-button"></div></div>');

		var $thoughtProcessButtons = $("#thought-process-buttons");

		$thoughtProcessItem.not(".disabled").hover(
		
			function() {
			
				$(this)
					.find("h2")
						.stop(true,false)
							.animate({ "padding-bottom" : "20px" }, 200);
			},
			function() {
				
				$(this)
					.find("h2")
						.stop(true,false)
							.animate({ "padding-bottom" : "10px" }, 200);
			}
		
		);

		$thoughtProcessButtons.click(function(){

			$(".current-slide")
				.stop(true,false)
					.animate({ "top" : $(window).height() - 160 + "px" }, 1000, "easeOutExpo", function() {
					
						$(this).css({ "opacity" : "0" });
						
					})
						.removeClass("current-slide");
						
			$thoughtProcessButtons
				.animate({ "opacity" : "0" }, 1000, "linear", function() { 
					$(this)
						.css({ "bottom" : "-100px" });
				});
	
		});
		
		$thoughtProcessItem.not(".disabled").click(function(){
															
			var itemID = $(this).attr("id").replace("thought-process-item-","");
			
			$("#thought-process-slide-" + itemID)
				.stop(true,false)
					.css({ "opacity" : "1" })
						.animate({ "top" : "0" }, 1000, "easeOutExpo")
							.addClass("current-slide");
						
			$thoughtProcessButtons
				.css({ "bottom" : "17px" })
					.stop(true,false)
						.animate({ "opacity" : "1" }, 1000, "linear");
		
		});
		
		
		// Description Boxes
		
		var $info = $(".description");
		var $infoContent = $(".description-content");
		var $label = $(".description-label");
		
		if(jQuery().jScrollPane) {
			$infoContent.jScrollPane();
		}
		
		$info
			.css({ "opacity" : "1" })
				.draggable({ containment: 'parent' });
				
		$label
			.find("h2")
				.css({ "opacity" : "0" });
		
		$label
			.add( $(".description-close") )
				.click(function() {
			
					var descriptionString = $(this).attr("id");
					var descriptionID = descriptionString.charAt(descriptionString.length-1);
					var $descriptionBox = $("#description-" + descriptionID);
					
					if( $descriptionBox.css("opacity") == "0" && $label.find("h2").css("opacity") == "1" ) {
					
						$descriptionBox
							.stop(true,true)
								.animate({ "opacity" : "1" }, 1200, 'easeOutExpo');
								
						$label
							.find("h2")
								.stop(true,true)
									.animate({ "opacity" : "0" }, 1200, 'easeOutExpo');
					
					} else {
						
						$descriptionBox
							.stop(true,true)
								.animate({ "opacity" : "0" }, 1200, 'easeOutExpo');
								
						$label
							.find("h2")
								.stop(true,true)
									.animate({ "opacity" : "1" }, 1200, 'easeOutExpo');
						
					}
							 
				});
		
		
	}
	
}

function thoughtProcessCycle() {

	if(jQuery().cycle) {
	
		$(".thought-process-cycle").each(function(j) {
												  
			var $cycle = $(this);
			
			if( $cycle.find("li").size() > 1 ) { 
			
				$cycle.before('<div id="cycle-prev-' + j + '" class="cycle-prev"><span>Previous</span></div><div id="cycle-next-' + j + '" class="cycle-next"><span>Next</span></div>');
				var $cyclePrev = $("#cycle-prev-" + j);
				var $cycleNext = $("#cycle-next-" + j);
				
				$cycle.cycle({ 
					//after: alertIT,
					//before: '',
					fx:     	'fade',
					//easing: 	'swing',
					speed:  	2000,
					fit:		1,
					width:		'100%',
					height:		'100%',
					timeout: 	0,
					next:    	$cycleNext, 
					prev:    	$cyclePrev,
				})
				.find("> li img")
					.fullBg();
					
			} else {
				
				$cycle
					.find("> li img")
						.fullBg();
				
			}

		});
			
	}
	
}

function team() {
	
	var initialClick = false;
	var $team = $("#team");
	var $teamPersons = $team.find("> .team-person");
	var numTeam = $teamPersons.size();
	var $teamPerson = $(".team-person");
	
	$teamPersons
		.each(function(i) {
								   
			var offset = ((160 * numTeam) - 160 * i);
			var difference = offset - 160 * (numTeam / 2) - 15;
			
			if ( difference < 0 ) {
				var marginOffset = Math.abs(difference) + "px";
			} else {
				var marginOffset = "-" + difference + "px";
			}
			
			$(this)
				.css({ "margin-left" : marginOffset });
			
		});

	$teamPerson.live("click", function(){
		
		if(!initialClick) {
			
			// Now set initial click to true to disable clicking main item again.
			initialClick = true;
			
			
			// Set vars.
			var $this = $(this);
			var $thisID = $this.attr("id");
			var $thisOffset = $this.offset();
			var curX = $thisOffset.left - $this.css("margin-left").replace("px","");
			var curY = $thisOffset.top - 80;
			
			
			// Clone the clicked item.
			var $cloned = $this.clone();
				$cloned.removeClass("team-person");
				$cloned.addClass("team-person-small");
				$cloned.find(".team-hover").css({ "opacity" : 0 });
				$team.append($cloned);
			
			// Move the clicked item center and modify some of it's properties.
			$this
				.addClass("puma")
				.find(".team-hover")
					.stop(true,false)
						.animate({ "opacity" : 0 }, 600, "linear")
				.end()
					.animate({ "left" : "50%", "margin-left" : "-65px" }, 1000, "easeOutCirc");
				
			
			// Fade out initial content of the page.
			$("#team-content")
				.animate({ "opacity" : 0 }, 1000, "easeOutCirc", function() {
				
					var theID = $this.attr("id").replace("team-person-","");
					$("#team-person-content-" + theID).animate({ "opacity" : 1 }, 1000, "easeOutCirc", function() {
					
					$this
						.attr("id", "puma");
																												
					});
	
				});
	
	
			// Move all the others (and the clone) down into position at bottom.
			$this
				.siblings(".team-person")
					.add($cloned)
						.each(function(j) {
		
							var $thisItem = $(this);
							var animationTime = 1000;
							//var endY = curY + $(window).height() - 285;
							//var endY = $(window).height() - 250;
							var endY = 335;
		
							// Hard-coded number of items. Change this!
							var offsetZ = ((50 * 4 ) - 50 * j);
							var differenceZ = offsetZ - 50 * (4 / 2) - 5;
							
							if ( differenceZ < 0 ) {
								var marginOffsetZ = Math.abs(differenceZ) + "px";
							} else {
								var marginOffsetZ = "-" + differenceZ + "px";
							}
							
							
							$thisItem
								.animate({
								 
									 "width" : 40,
									 "height" : 40,
									 "top" : endY + "px",
									 "margin-left" : marginOffsetZ
								 
								 }, animationTime, "easeOutExpo", function() {
									
										var $thisSmall = $(this);
										$thisSmall.removeClass();
										$thisSmall.addClass("team-person-small");
								
									});
								
							$thisItem
								.find("img")
									.animate({
					
										 "width" : 40,
										 "height" : 40
									 
									 }, animationTime, "easeOutExpo");
					
						});
			
			
			// This needs reworking.
			/*$this
				.attr("id", "puma");*/
			
			
		}
		// End 'initialClick' switch.		
				
					   
	});
	
	$teamPerson.hover( 
	
		function() { 
		
			if(!initialClick) {
				$(this)
					.find(".team-hover")
						.stop(true,false)
							.animate({ "opacity" : 1 }, 200, "linear");
			}
		
		}, 
		function() { 
		
			if(!initialClick) {
				$(this)
					.find(".team-hover")
						.stop(true,false)
							.animate({ "opacity" : 0 }, 600, "linear");
			}
		
		}
		
	);
	
	var $teamPersonSmall = $(".team-person-small");
	
	$teamPersonSmall
		.live("click", function(){
		
			var theClickID = $(this).attr("id").replace("team-person-","");
			var $theItem = $("#team-person-content-" + theClickID);
			var theImageSrc = $("#team-person-" + theClickID).find("img").attr("src");
			
			//alert(theImageSrc);
	
			$("div.team-person-content")
				.not($theItem)
				.animate({ "opacity" : 0 }, 300, "linear", function() {
				
					$theItem.animate({ "opacity" : 1 }, 300, "linear");
					
				});
	
			$(".puma")
				.find("img")
					.animate({ "opacity" : 0 }, 300, "linear", function() {
					
						$(this).attr("src", theImageSrc)
							.animate({ "opacity" : 1 }, 300, "linear");
							
						//alert(theImageSrc);
					
					});
										   
		});
	
}

function news() {
	
	var $news = $("#news-posts");
	var $newsContainer = $("#news-container");
	var newsWidth = $(window).width() - 30;

	$newsContainer.css({ "width" : newsWidth + "px" });
	
	var $newsWrap = $("#news-posts-wrap");
	var $postImages = $("#post-images");
	
	if( $postImages.size() ) {
		
		// Single Post Page
		var numPosts = $postImages.find("> img").size();
		var newsWrapWidth = ((435 * numPosts) + 301) + "px";
		//var newsWrapWidth = ((285 * numPosts) + 301) + "px";
		
	} else {
		
		// Index or category post pages
		var numPosts = $newsWrap.find("> .post").size();
		var newsWrapWidth = ((300 * numPosts) + numPosts) + "px"; // Factor in right 1px border
		
	}

	$newsWrap.css({ "width" : newsWrapWidth });

	if(jQuery().jScrollPane) {
		
		$news.jScrollPane({
			showArrows: true,
			autoReinitialise: true,
			//paneWidth: newsWidth
			//paneWidth: newsWidth + "px"
			//contentWidth: newsWidth + "px"
		});
		
		//var api = $news.data('jsp').getContentPane();
		//api.reinitialise();
	}

	//alert("Window width = " + $(window).width() + ", New width = " + newsWidth + ", Actual width = " + $news.width() );
	
}

function vAlignNews() {

	var $news = $("#news-container");
	
	if ( $(window).height() > 600 ) {
		
		if(jQuery().vAlign) {
			$news
				.css({ "position" : "absolute" })
					.vAlign();
		}
		
	} else {
		
		$news
			.css({ "position" : "static", "margin-top" : "0" });
				
	}
	
}

function noFlash() {
	
	$("#flash").remove();
	
	// Cycle was paused in cycleAuto(). 
	//Let's resume it once the Flash is done for.
	$("#cycle-home")
		.cycle("resume");
		
	setGlobalItems();

}

function isIOS() {
    return ( (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) || (navigator.platform.indexOf("iPad") != -1) );
}

function doSomething() {
	
	//alert("flash call");
	
	$("#flash")
		.animate({ "opacity" : "0"}, 1500, "linear", function() {
			$(this).remove();
			//alert("flash finished fade");
		});
		
	//$("#cycle-home").cycle(0);
	
	// Cycle was paused in cycleAuto(). 
	//Let's resume it once the Flash is done for.
	$("#cycle-home")
		.cycle("resume");
			
	setGlobalItems();
	
}

function descriptionBox() {
	
	var $info = $("#description");
	var $infoContent = $("#description-content");
	var $label = $("#description-label");
	
	if(jQuery().jScrollPane) {
		$infoContent.jScrollPane();
	}
	
	$info
		.append("<div id='description-close'>Close</div>");
			
	$info
		.draggable({ containment: 'parent' });
	
	$label
		.add( $("#description-close") )
			.click(function() {
		
				if( $info.css("opacity") == "0" && $label.find("h2").css("opacity") == "1" ) {
				
					$info
						.stop(true,true)
							.animate({ "opacity" : "1" }, 1200, 'easeOutExpo');
							
					$label
						.find("h2")
							.stop(true,true)
								.animate({ "opacity" : "0" }, 1200, 'easeOutExpo');
				
				} else {
					
					$info
						.stop(true,true)
							.animate({ "opacity" : "0" }, 1200, 'easeOutExpo');
							
					$label
						.stop(true,true)
							.find("h2")
								.animate({ "opacity" : "1" }, 1200, 'easeOutExpo');
					
				}
						 
			});
	
}

function setContentSize(isThoughtProcessResize) {
	
	var windowHeight = $(window).height();
	newContentHeight = (windowHeight - 160);
	$("#content, #news, #flash, #swfObjectContent").css({ "height" : newContentHeight + "px" });
	
	if (isThoughtProcessResize) {
		$("div.thought-process-slide").css({ "height" : newContentHeight + "px" });
	} else {
		$("div.thought-process-slide").css({ "height" : newContentHeight + "px", "top" : newContentHeight + "px" });
	}
	
}

function setGlobalItems() {
	
	var $logo = $("#logo");
	var $menu = $("#menu-primary");
	
	$logo
		.delay(200)
		.animate({ "opacity" : 1 }, 800);

	$menu
		.find("li:first-child a")
			.css({ "background-image" : "none" })
		.end()
			.find("> li")
				.delay(200)
					.each(function(i) {			
						$(this)
							.delay( i * 200 )
								.animate({ opacity: 1 }, 500, function() {
									$(this).css('filter', 'none');
								});
				})
			.end()
				.find("> li")
					.hover(
						function () {
							$(this)
								.find("> ul")
									.stop(true,true)
										.css({ "left" : "-5px" })
											.fadeTo(1000,1, "easeOutExpo");
						}, 
						function () {
							$(this)
								.find("> ul")
									.stop(true,true)
										.fadeTo(1000,0, "easeOutExpo", function() {
											$(this)
												.css({ "left" : "-9999px" })
										});
						}
					)
				.end()
					.find('li a')
						.hover(
							function() {
								$(this)
								.stop(true, true)
								.addClass('hover',500);
							},
							function() {
								$(this)
								.stop(true, true)
								.removeClass('hover',500);
							}
						);

}

function cycleManual() { 
	
	if(jQuery().cycle) {
		
		var $cycle = $("#cycle");
		
		if( $cycle.find("li").size() > 1 ) { 
		
			$("#content").append('<div id="cycle-prev"><span>Previous</span></div><div id="cycle-next"><span>Next</span></div>');
			var $cyclePrev = $("#cycle-prev");
			var $cycleNext = $("#cycle-next");
			
			$cycle
			.cycle({
				//after: alertIT,
				//before: '',
				fx:     	'fade',
				//easing: 	'swing',
				speed:  	2000,
				fit:		1,
				width:		'100%',
				height:		'100%',
				timeout: 	0, 
				next:   	$cycleNext, 
				prev:   	$cyclePrev
			})
				.find("> li img")
					.fullBg();
					
		} else {
		
			$cycle
			.cycle({
				//after: alertIT,
				//before: '',
				fx:     	'fade',
				//easing: 	'swing',
				speed:  	2000,
				fit:		1,
				width:		'100%',
				height:		'100%',
				timeout: 	0
			})
				.find("> li img")
					.fullBg();
			
		}
		
	}
	
}

function cycleAuto() { 

	if(jQuery().cycle) {
		
		var $cycle = $("#cycle-home");
		
		$cycle
			.cycle({
				//after: alertIT,
				//before: '',
				fx:     	'fade',
				//easing: 	'swing',
				speed:  	2000,
				fit:		1,
				width:		'100%',
				height:		'100%',
				timeout: 	5000
			})
				.find("> li img")
				.fullBg();
				
		$cycle
			.cycle("pause");
				
	}

}

function kwicks() { 

	var $panels = $("#panels");
	$panels.kwicks("destroy");
	
	var panelCount = $panels.find("li").size();
	var panelWidth = ( Math.ceil( $(window).width() / panelCount )   ) + "px";
	var panelHeight = ( $("#content").height() ) + "px";
	
	var $header = $("#header");
	var $footer = $("#footer");
	
	$panels
		.css({ "width" : "100%", "height" : panelHeight })
			.find("li")
				.css({ "width" : panelWidth, "height" : panelHeight })	
					.hover(
										  
						function () {
							
							var $this = $(this);
							var $overlay = $this.find("div.panel-overlay");
							var $siblings = $this.siblings();
							
							if ( $overlay.css("opacity") < 1 ) {
								$overlay
									.stop(true, true)
										.animate({ "opacity" : 1 }, 1000, 'easeOutExpo');
							}
							
							$siblings
								.find("div.panel-overlay")
									.stop(true, true)
										.animate({ "opacity" : 0 }, 1000, 'easeOutExpo');
					
						},
						
						function () {
							
							var $this = $(this);
							var $overlay = $this.find("div.panel-overlay");
							
							if ( $overlay.css("opacity") == 1 ) {
								$overlay
									.stop(true, true)
								.		animate({ "opacity" : 0 }, 1000, 'easeOutExpo');
							}
							
						}
					
					)
					.end()
						.find("li a")
							.add("div.panel-overlay")
								.css({ "width" : "500px", "height" : panelHeight });
					
					
	$panels
		.find("li")
			.each(function(i) {
				$(this)
					.css({ "z-index" : "999" + i });
				$(this)
					.find("div.panel-overlay")
						.css({ "z-index" : "999" + i + 1 });
			});
	
		
	$header
		.add($footer)
			.hover(
								  
				function () {
				
					$panels
						.find("> li a div.panel-overlay")
							.stop(true, true)
								.animate({ "opacity" : 0 }, 1000, 'easeOutExpo');
					
				},
				
				function () {}
			
			);
		
		
	$panels
		.kwicks({  
			max: 500,
			sticky: false,
			spacing: 0,
			duration: 1000,
			easing: 'easeOutExpo'  
		});
	
	
	if(jQuery().vAlign) {
		$panels
			.find("> li a div.panel-overlay-content")
				.vAlign();
	}
	
}

function windowResize() {
	
	var waitForFinalEvent = (function () {
		
		var timers = {};
		
		return function (callback, ms, uniqueId) {
		
			if (!uniqueId) {
				uniqueId = "xxxxxxxxx";
			}

			if (timers[uniqueId]) {
				clearTimeout (timers[uniqueId]);
			}
			
			timers[uniqueId] = setTimeout(callback, ms);
		
		};
	
	})();
	
	
	$(window).resize(function() {
							  
		waitForFinalEvent(function(){
						
			setContentSize(true);
			
			if(jQuery().kwicks) {
				kwicks();
			}
			
			if( $("#news").size() ) {
				news();
				vAlignNews();
			}
			
			vAlignPage();
			vAlignTeam();
			thoughtProcess(true);
			
		}, 50, "xxxxx");

	});	
	
}
