jQuery.fn.topBottomMargin = function () {
    this.css("padding-top", ((($(window).height() - 260) / 2) + 30) + "px");  
    this.css("padding-bottom", (($(window).height() - 260) / 2) + "px");   
    return this;  
}

jQuery.fn.equalCols = function(){
    var sortNumber = function(a,b){return b - a;};
    var heights = [];
    $(this).each(function(){
        heights.push($(this).height());
    });
    heights.sort(sortNumber);
    var maxHeight = heights[0];
    return this.each(function(){
        $(this).css({'height': maxHeight});
    });
};

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

$(function() { //On load, without knowing of images have loaded & stuff

	// set sections & nav to 0 opacity
	$("#headerIn h1, #headerIn nav").css("opacity","0");
	
	// Add equal top & bottom padding to sections on load & resize
	var topBottomMarginSelectors = $("#intro");
	topBottomMarginSelectors.topBottomMargin();
	$(window).resize(function(){
		topBottomMarginSelectors.topBottomMargin();
	});
			
});

$(window).bind("load",function(){ // When everytihng has loaded, stuff that doesent need to happen when the page opens

	// Fade in body after half a second
	setTimeout(function(){
		$("#headerIn h1, #headerIn nav").animate({opacity: 1}, 300);
	}, 200);
	
	// Append arrows
	$("#intro span").append('<div id="arrows"><div class="arrow arrow1"></div><div class="arrow arrow2"></div><div class="arrow arrow3"></div></div>');
	$("#intro span #arrows .arrow").delay(50).css({"opacity":"0","display":"block"}).animate({opacity: 0.1});
	setInterval(function(){
		$(".arrow1").animate({opacity: 0.3}, 100).delay(200).animate({opacity: 0.1}, 300);
		$(".arrow2").delay(100).animate({opacity: 0.3}, 100).delay(200).animate({opacity: 0.1}, 300);
		$(".arrow3").delay(200).animate({opacity: 0.3}, 100).delay(200).animate({opacity: 0.1}, 300);
	}, 2000);	
	$("#intro span #arrows").click(function(){
		$("nav ul li:first a").click();
	});
	
	// Make 'Problem' arrows 10% opacity, and pulse them
	$("#problemTopArrow, #problemBottomArrow").css({opacity: 0.1});
	setInterval(function(){
		$("#problemTopArrow, #problemBottomArrow").animate({opacity: 0.2}, 1000).delay(200).animate({opacity: 0.1}, 1000);
	}, 2400);
	
	// Change header 'CbM' link to internal link
	$("header #headerIn h1 a").attr("href","#intro");

	// Smooth scrolling of imternal links
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = ($target.offset().top) - 120;
				$('html,body').stop(true, true).animate({scrollTop: targetOffset}, 700);
				return false;
			}
		}
	});
	
	// Contact validation, kinda. Nothing elaborate needed here.
	$("#contact form span").click(function(){
		var theContentBefore = $("#contact textarea").val();
		var theContent = $.trim(theContentBefore);
		var theFeedback = $("#contact p#feedback");
		if (!theContent) {
			theFeedback.html("You must have something to say, right?");
		} else {
			$("#contact form span").html("Sending...");
			$.ajax({
				type: "POST",
				url: "sendContact.php",
				data: "theValue=" + theContent,
				success: function(msg){
					$("form").fadeOut(function(){
						$("#success").fadeIn();
						$("#contactInfo").animate({"width":"640px","margin-left":"20px"});
						$("#contact div").animate({"height":"75px"});
					});
				}
			});
		}
		return false;
	});
	
	// Scroll header the oposite way, at 1/10 the speed of the body
	$(window).unbind("scroll").scroll(function(){
		var a = Math.max(document.documentElement.scrollTop, document.body.scrollTop) /10;
		$("header").css({"background-position": "0px " + +a + "px"});
	});
	
	// Equal height of 'Problem' divs
	$("#problemTopLeft, #problemTopArrow, #problemTopRight").equalCols();
	$("#problemBottomLeft, #problemBottomArrow, #problemBottomRight").equalCols();

	
});