$(document).ready(function(){
	
	windowResize();

	var $body = $("body");
	var $cycle = $("#cycle");
	var $cycleHome = $("#cycle-home");
	var $content = $("#content");
	var $navigation = $("#menu-primary");
	var $overlay = $("#overlay");

	$overlay
		.append('<div id="close-overlay">Close</div>');
	
	if( jQuery().cycle && $cycle.find("li").size() ) {
		
		$body
			.append('<div id="open-overlay"><p class="cycle-controls" /> Show menu</div>');
	
	} else {
		
		$body
			.append('<div id="open-overlay">Show menu</div>');
		
	}
	
	$("#close-overlay")
		.click(function() { 
			closeOverlay();
		});

	$("#open-overlay")
		.click(function() { 
			closeInfoButton();
		});
	
	
	
	$navigation
		.find('li a')
			.each(function() {
			
				var thisID = $(this).attr("title");
				$(this)
					.parent()
						.attr("id", "nav-" + thisID);
			
			})
	.end()
		.find('li:not(.current-menu-item, .current-menu-ancestor)')
		//.not('.current-menu-item, .current-menu-ancestor')
		.find('a')
			.hover(
				
				function() { 
					$(this)
						.stop(true,true)
							.animate({ "opacity" : 1 }, 500, "easeOutExpo") 
				},
				function() { 
					$(this)
						.stop(true,true)
							.animate({ "opacity" : 0.6 }, 1000, "easeOutExpo")
				}
			
			)
	
	

	// MENU COLLAPSE / EXPAND
	
	if( getParameterByName("red") == "true" ) {
	
		$navigation
				.find('li.current-menu-item > ul, li.current-menu-parent > ul')
					.delay(1500)
						.animate({ "height" : "70px", "padding-top" : "4px", "padding-bottom" : "10px" }, 1000, "easeInOutExpo");
	} else {
		
		$navigation
			.find('li.current-menu-item > ul, li.current-menu-parent > ul')
				.css({ "height" : "70px", "padding-top" : "4px", "padding-bottom" : "10px" });
		
	}
	
	
	
	
	if( jQuery().cycle && $cycle.find("li").size() ) {
		
		$content
			.append('<p class="cycle-controls" />');
		
		$(".cycle-controls")
			.html('<span class="cycle-prev">&nbsp;</span> <span class="cycle-count"></span> <span class="cycle-next">&nbsp;</span>');
		
		$cycle
			.cycle({
				fx:     		'fade',
				//easing: 		'swing',
				speed:  		1500,
				fit:			1,
				width:			'100%',
				height:			'100%',
				timeout:		0,
				next:   		'.cycle-next, #cycle', 
				prev:   		'.cycle-prev',
				after:			slideCount,
				cleartypeNoBg: 	true
			})
			.css({ "width" : $(window).width() + "px", "height" : $(window).height() + "px" })
				.delay(500)
					.animate({ "opacity" : 1 }, 1000, "swing");
		
		$cycle
			.find("li")
				.css({ "width" : $(window).width() + "px", "height" : $(window).height() + "px" });
			
		$cycle
			.find("li > img")
				.fullBg();

	}
	
	
	
	if( jQuery().cycle && $cycleHome.find("li").size() ) {
		
		$cycleHome
			.cycle({
				fx:     		'fade',
				//easing: 		'swing',
				speed:  		1500,
				fit:			1,
				width:			'100%',
				height:			'100%',
				timeout:		4000,
				cleartypeNoBg: 	true
			})
			.css({ "width" : $(window).width() + "px", "height" : $(window).height() + "px" })
				.delay(500)
					.animate({ "opacity" : 1 }, 1000, "swing");
		
		$cycleHome
			.find("li")
				.css({ "width" : $(window).width() + "px", "height" : $(window).height() + "px" });
			
		$cycleHome
			.find("li > img")
				.fullBg();

	}

	
	if( $.cookie('didMenuExpand') === null ) {
		
		openInitialOverlay();
		$.cookie('didMenuExpand', 'true');
		
	} else {
		
		$("#overlay")
			.delay(500)
				.animate({ "opacity" : "1" }, 1000, "swing");
		
	}
	
	
	
	$body
		.find("a")
			.removeAttr("title");
	
	postList();
	
	$("a.open-fb").click(
								
		function () {
			
			var theURL = $(this).attr("href");
			window.open(theURL,'Share','toolbar=0,status=0,width=1000,height=590');
	
			return false;
			
		}
	
	);
	
	$("a.open-tw").click(
								
		function () {
			
			var theURL = $(this).attr("href");
			window.open(theURL,'Tweet','toolbar=0,status=0,width=700,height=340');
	
			return false;
			
		}
	
	);
	
});


// =================================== //
// 			GLOBAL FUNCTIONS
// =================================== //

function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)
	return "";
		else
	return decodeURIComponent(results[1].replace(/\+/g, " "));
}


function postList() {

	var $posts = $("#post-list");

	if($posts.size() ) {
	
		var winHeight = ($(window).height() - 40) + "px";
		$posts
			.css({ "height" : winHeight});
		
		if(jQuery().jScrollPane) {
			$posts
				.jScrollPane();
		}
		
	}
	
}

function slideCount(curr,next,opts) {
	
	var slideText = (opts.currSlide + 1) + ' of ' + opts.slideCount;
	$(".cycle-count")
		.html(slideText);
	
} 


function openInitialOverlay() {
	
	var $overlay = $("#overlay");
	
	$overlay
		.css({ "bottom" : "-" + $(this).height() + "px" })
			.delay(500)
				.animate({ "bottom" : "0px", "opacity" : "1" }, 1500, "easeOutExpo");
	
}


function openOverlay() {
	
	var $overlay = $("#overlay");
	
	$overlay
		.css({ "bottom" : "-" + $(this).height() + "px" })
			.animate({ "bottom" : "0px", "opacity" : "1" }, 1200, "easeOutExpo");
	
}


function closeOverlay() {
	
	var $overlay = $("#overlay");
	
	$overlay
		.animate({ "bottom" : "-" + $(this).height() + "px", "opacity" : "0" }, 600, "easeInExpo", function() { 
			
			openInfoButton();
			
		});
	
}


function openInfoButton() {
	
	var $openOverlay = $("#open-overlay");
	
	$openOverlay
		.animate({ "bottom" : "0px", "opacity" : "1" }, 600, "easeOutExpo");
	
}


function closeInfoButton() {
	
	var $openOverlay = $("#open-overlay");
	
	$openOverlay
		.css({ "bottom" : "0px" })
			.animate({ "bottom" : "-40px", "opacity" : "0" }, 600, "easeInExpo", function() { 
				
				openOverlay();
				
			});
	
}

function resizeContainers() {
	
	var $container = $("#container");
	var $body = $("body");
	var $cycle = $("#cycle");
	var $cycleLi = $cycle.find("li");
	var $cycleHome = $("#cycle-home");
	var $cycleHomeLi = $cycle.find("li");
	
	$container
		.add($body)
		.add($cycle)
		.add($cycleLi)
		.add($cycleHome)
		.add($cycleHomeLi)
			.css({ "width" : $(window).width() + "px" , "height" : $(window).height() + "px" });
	
}

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

			if (timers[uniqueId]) {
				clearTimeout (timers[uniqueId]);
			}
			
			timers[uniqueId] = setTimeout(callback, ms);
		
		};
	
	})();

	$(window).resize(function() {
							  
		waitForFinalEvent(function(){
			
			resizeContainers();
			postList();
			
		}, 10, "xxxxx");

	});	
	
}
