$(document).ready(function(){	
	// make sure footer content is generated
	loadElements("firstLoad");
	
	if ($.browser.msie && $.browser.version < 7) {
		$('#mainNav,#mainContentShadow').supersleight();
	}
	
	// load element - move to bottom!
	function loadElements(firstLoad, toShow, toHide) {
		$("#nav").innerHTML = $("#nav").innerHTML;
		
		// on first page load - generate elements
		if (firstLoad) {
			var newHeading = $("#mainContent h2:first").clone().html();
			var newText = $("#mainContent .text:first").clone().html();
			var bottomSection = $("#footer .group");
			
			if (newText) {
				if (!$.browser.msie) {
					$(bottomSection)
						.append("<header><h2>" + newHeading + "</h2></header>")
						.append("<article class='footerContent'>" + newText + "</article>");
				} else {
					$(bottomSection)
						.append("<div class='header'><h2>" + newHeading + "</h2></div>")
						.append("<div class='footerContent'>" + newText + "</div>");
				}
			}
				
			$(bottomSection).fadeIn("slow");
			
			if ($("#newsList").length > 0) {
				$("#newsList ul").width(($("#nav ul").width()-18));
				$("#newsList ul:first").fadeIn("fast");
			} else if ($("#contactInfo").length > 0) {
				$("#contactInfoInside").width(($("#nav ul").width()-18)).fadeIn("fast");
			}
			
			alignElements();
		// if navigation buttons are clicked
		} else {
			
			toHide.fadeOut("slow", function() {
			$("#mainContent").addClass("loader");
				toShow.fadeIn("slow", function() {
					$("#mainContent").removeClass("loader");
				});
			});			
				
			// if archive
			if ($("#newsList").length) {				
				changeBodyColour();
				alignElements();
			} else {
				// assign new text to bottom section
				var newHeading = $("h2", toShow).html();
				var newPara = $("p", toShow).text();
				
				
				$("h2", $("#footer")).fadeOut("fast", function() {
					$(this).html(newHeading).fadeIn("fast");
				});
				
				$(".footerContent", $("#footer")).fadeOut("fast", function() {
					$(this).html("<p>" + newPara + "</p>").hide("fast", function() {				
						$(this).fadeIn();
						
						alignElements();
					
						changeBodyColour();
					});
				});
			}
		}
	}
	
	// next/previous
	$("#mainNav").bind("click", function(e) {
		if ($(this).hasClass("noDynamic")) return;
		
		var clicked = $(e.target);
		var clickedID = clicked.parent().attr("id");
		var elToHide = $("#mainContent .content:visible");	
		
		if (clickedID == 'navNext') {
			var elToShow = elToHide.next(".content");
		
			if (elToShow.length == 0) {
				var elToShow = $("#mainContent .content:first");
			}
		} else if (clickedID == 'navPrevious') {
			var elToShow = elToHide.prev(".content");
			
			if (elToShow.length == 0) {
				var elToShow = $("#mainContent .content:last");
			}
		}
		
		loadElements(false, elToShow, elToHide);
		
		return false;
	});

	resize_viewport();
});


function changeBodyColour() {
	var bodyColourNow = $("body").attr("id");
	
	if (bodyColourNow == 'pink') bodyColourNew = 'blue';
	else if (bodyColourNow == 'blue') bodyColourNew = 'orange';
	else bodyColourNew = 'pink';
	
	$("body").attr("id", bodyColourNew)
}

// align function
function alignElements() {
	var totalWidth = 768;
	var navWidth = $("#nav ul").width();
	
	$(".footerContent", $("#footer")).width((navWidth-34));
	$("h2", $("#footer")).width((totalWidth-navWidth-10))
	
	var footerContentHeight = $(".footerContent", $("#footer")).height();
	var footerHeadingHeight = $("h2", $("#footer")).height();
	var footerHeadingMargin = Math.round((footerContentHeight-footerHeadingHeight)/2);
	
	$("h2", $("#footer")).css({"margin-top" : footerHeadingMargin});
	
	$("#mainNav").vAlign(null, false).show();
	$("#footer .group").vAlign(null, true);
	
	if ($("#newsArchive").length > 0) {
		var footerHeight = $("#footer").height();
		
		var footerNewsArchiveHeight = $("#newsArchive").height();
		var footerNewsArchiveMargin = (footerHeight/2)-footerNewsArchiveHeight+7;

		$("#newsArchive").css({"top" : footerNewsArchiveMargin});
	}
}

// on resize
function resize_viewport() {
	$.event.remove( this, "resize", alignElements);
	$.event.add( this, "resize", alignElements);
}

// center elements vertically
$.fn.vAlign = function(el, t) {	
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
			
		if (ph > ah) {
			if (t) $(this).css({'padding-top' : mh});
			else $(this).css({'margin-top' : mh});
		}
		
	});
};


// custom jquery functions

// jQuery.fn.fadeIn = function(speed, callback) { 
//     return this.animate({opacity: 'show'}, speed, function() { 
//         if (jQuery.browser.msie)  
//             this.style.removeAttribute('filter');  
//         if (jQuery.isFunction(callback)) 
//             callback();  
//     }); 
// }; 
//  
// jQuery.fn.fadeOut = function(speed, callback) { 
//     return this.animate({opacity: 'hide'}, speed, function() { 
//         if (jQuery.browser.msie)  
//             this.style.removeAttribute('filter');  
//         if (jQuery.isFunction(callback)) 
//             callback();  
//     }); 
// };
// 
// jQuery.fn.fadeToggle = function(speed, easing, callback) { 
//    return this.animate({opacity: 'toggle'}, speed, easing, callback); 
// };