/**
 * Title:   Javascript > Global
 * Website: http://www.bradbergeron.com
 * Author:  Brad Bergeron
 * Contact: brad@bradbergeron.com
 * Updated: 1 Oct 2011
 */

$(document).ready(function () {
	
	/**
	 * Background Image
	 */
	jQuery(function($) {
			$.supersized({
				start_slide: 0,
				slides : [
					{ image : '/images/backgrounds/image_1.jpg' },
					{ image : '/images/backgrounds/image_2.jpg' },
					{ image : '/images/backgrounds/image_3.jpg' },
					{ image : '/images/backgrounds/image_4.jpg' },
					{ image : '/images/backgrounds/image_5.jpg' },
					{ image : '/images/backgrounds/image_6.jpg' },
					{ image : '/images/backgrounds/image_7.jpg' },
					{ image : '/images/backgrounds/image_8.jpg' },
					{ image : '/images/backgrounds/image_9.jpg' }
				]
			});
		});
	
	/**
	 * Hashbang-based Navigation Listener
	 */
	$(window).bind('hashchange', function () {
		var page, pageRequest, section;
		pageRequest = window.location.hash.split('?')[0].split('/');
		for (page in pageRequest) {
			if (pageRequest[page] === "") {
				delete pageRequest[page];
			}
		}

		// Quit processing if the hashbang is missing
		if (pageRequest[0] !== "#!") {
			return;
		}

		if (pageRequest[1]
		&& $("#" + pageRequest[1]).length != 0
		&& $("#" + pageRequest[1]).find(":visible").length == 0) {
			toggleSection(pageRequest[1]);
		}
	});
	
	// Handle hashbang present at page load
	if (window.location.hash !== '') {
		$(window).trigger('hashchange');
	} else {
		window.location.hash = "!/welcome";
	}
	
	// Handle Resizing
	$(window).bind('resize', function () {
		$("#content").height($("#content > div:visible").height() + 20);
	});
	
	/**
	 * Initialize Sections
	 */
	$("#header").delay(300).fadeIn();
});

/**
 * Handle Section Changes
 */
function toggleSection (section) {
	if ($("#content:visible").length == 0) {
		$("#content").css("display", "block");
	}
	$("#content > div:visible").fadeOut(300);
	$("#content").animate({
		height: $("#" + section).height() + 20
	}, 300).find("#" + section).fadeIn(300);
    
    	
	// Make sure images don't cause issues with the resizing
	$("#" + section + " img").load(function () {
		$("#content").animate({
			height: $("#" + section).height() + 20
		}, 300);
	});
}

/**
 * Font Replacement
 */
Cufon.DOM.ready(function () {
	Cufon.replace('h1, h2, h3, h4, h5, h6', {
		fontFamily: 'Myriad Pro Semibold',
		onAfterReplace: function () {
			alert($("#content").height());
		}
	});
	Cufon.replace('#navigation', {
		fontFamily: 'Myriad Pro',
		textShadow: '1px 1px rgba(0, 0, 0, 0.7)',
		hover: {
			fontFamily: 'Myriad Pro Semibold'
		}
	});
});

