/*!
#######################################################################
# 
# The Leas Cliff Hall
# JavaScript setup routines
#
# Hand-crafted by Phenotype (phenotype.net)
#
#######################################################################
*/
	
	////////////////////////////////////////////////////////////////////
	//	Initialise setup routines
	////////////////////////////////////////////////////////////////////
	
		// Called when DOM is ready
		$(document).ready( domSetup );
		
		// Called when entire page is loaded
		$(window).load( pageSetup );
		
		// Called when DOM is unloaded
		$(window).unload( domUnload );
	
	////////////////////////////////////////////////////////////////////
	//	Global variables
	////////////////////////////////////////////////////////////////////
	
		// Set site root
		var siteRoot = '/leascliffhall/';
		//var siteRoot = '/';
		
		// Set global scroll offset
		var globalScrollOffset = 0;
			
	////////////////////////////////////////////////////////////////////
	//	Define setup routines
	////////////////////////////////////////////////////////////////////
	
	/* 
	 * domSetup()
	 * 
	 * All JavaScript requiring initialisation on DOM LOAD should be called
	 * from this routine.
	 */
	
	function domSetup() {

		////////////////////////////////////////////////////////////////////
		//	Enhancements
		////////////////////////////////////////////////////////////////////
		
			// Set JS enabled flag for CSS to determine page layout type
			$('html').addClass('js-enabled');
			
			// Create page layout
			createLayout();
			
			// Bind window resize event to recreate layout
			//$(window).bind('resize.default', createLayout);
			
			// Add pipes to navigation
			$('.page-navigation li:not(.page-navigation li:last-child), #footer ul li').append('<span>|</span>');

			/**
			 * Multi-column content
			 * Uses <hr /> as separator
			*/
			$('.page-content hr').each(function(){
				
				// Add class to parent
				$(this).parents('.page').addClass('multi-column');
				
				// Find siblings before separator
				var before = $(this).prevAll().length;
				// Wrap with column
				$(this).siblings().slice( 0, before ).wrapAll('<div class="column" />');
				
				// Find siblings after separator
				var after = $(this).prevAll().length;
				// Wrap with column
				$(this).siblings().slice( after ).wrapAll('<div class="column last" />');
				
				// Remove separator
				$(this).remove();
				
			})
			
			/**
			 * Input buttons
			*/
			// Replace	
			$('input[type=submit]').each(function(){
				// Get value
				var value = $(this).val();
				// Replace element
				$(this).replaceWith('<a href="#" class="button">'+value+'</a>');
			});
			// Bind actions
			$('a.button').bind('click.default', function(e){
				// Override default
				e.preventDefault();
				// Submit form
				$(this).parents('form').submit();
			})
			
			
		////////////////////////////////////////////////////////////////////
		//	/Enhancements
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		//	IE6 PNG fix
		////////////////////////////////////////////////////////////////////

			if( $.browser.msie && $.browser.version == '6.0' ) {
				DD_belatedPNG.fix('#header h3 a, .landing #header, .landing #header h2, .landing #content-container, .page-navigation ul, .page-content a, .page-media p a, #content ul li, a.button, #footer-container, .errors');
			}
		
		////////////////////////////////////////////////////////////////////
		//	IE6 PNG fix
		////////////////////////////////////////////////////////////////////

		////////////////////////////////////////////////////////////////////
		//	Navigation
		////////////////////////////////////////////////////////////////////
			
			/**
			 * Local navigation links
			 */
			
			// Custom click events
			$('#navigation a, .page-navigation a').bind('click.default', function(e) {
				
				if( $(this).attr('rel') ) {
					
					// Do not follow link to new page
					e.preventDefault();

					// Get local link target
					var hash = $(this).attr('rel');
					var target = '#' + hash;

					// Scroll to local link target
					$.scrollTo(target, {
						duration: 600,
						easing: 'easeInCirc',
						offset: globalScrollOffset,
						onAfter: function() {
							// Set URL hash
							window.location.hash = hash;
						}
					});
					
					// Set current page for site navigation
					if( $(this).parents('#navigation')[0] ){
						$('#navigation li').removeClass('current');
						$(this).parents('li').addClass('current');
					}
					
				}

			});
			
			/* // Initial current page highlighting for site navigation
			if( window.location.hash ) {
				$('#navigation a').each(function(){
		
				if( $(this).attr('rel') == window.location.hash ){
					$(this).parents('li').addClass('current');
				}
			}); */
			
			// Current page highlighting for page navigation
			$('.page-navigation a').each(function(){
		
				if( $(this).attr('rel') == $(this).closest('.page').attr('id') ){
					$(this).parents('li').addClass('current');
				}
			});
			
			/**
			 * Local scroll
			 */
			$.localScroll({
				duration: 600,
				hash : true,
				lazy : true,
				stop : true,
				axis : 'x',
				easing: 'easeInCirc',
				offset: globalScrollOffset
			});
			
			/*
			Setup external links (target attribute not allowed by XHTML 1.1)
			(see http://www.sitepoint.com/article/standards-compliant-world/)
			*/
			$('a[rel="external"]').attr('target','blank');
		
		////////////////////////////////////////////////////////////////////
		//	/Navigation
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		//	Images
		////////////////////////////////////////////////////////////////////

			$('.gallery-list a, .page-media a.featured-image').colorbox({
				maxWidth: '960px',
				maxHeight: '600px',
				opacity: '0.8'
			});
	
		////////////////////////////////////////////////////////////////////
		//	/Images
		////////////////////////////////////////////////////////////////////

	} // End domSetup()
	
	/* 
	 * pageSetup()
	 *
	 * All JavaScript requiring initialisation on PAGE LOAD should be called
	 * from this routine (all images and elements should be loaded and ready to
	 * manipulate by this point)
	 */
	
	function pageSetup() {
	
		////////////////////////////////////////////////////////////////////
		//	Navigation
		////////////////////////////////////////////////////////////////////s
			
			/*
			Local scroll to existing hash
			*/	
			$.localScroll.hash({
				duration: 0,
				axis : 'x',
				easing: 'linear',
				offset: globalScrollOffset
			});
		
		////////////////////////////////////////////////////////////////////
		//	/Navigation
		////////////////////////////////////////////////////////////////////

	} // End pageSetup()
	
	/* 
	 * domUnload()
	 *
	 * Called when page/DOM is unloaded
	 */
	
	function domUnload() {

	} // End domUnload()
	
	
	/* 
	 * createLayout()
	 *
	 * Creates side-scrolling page layout on load and window resize
	 */
	function createLayout() {
	
		// Pad out each page slide to be half a screen width apart
		$('.page').each(function() {
			$(this).css({'padding-right' : $('#header-container').width()/2 });
		})
		
		// Calculate width of #content
		var contentWidth = 0;
		
		$('.page').each(function() {
			contentWidth += $(this).outerWidth();
			$(this).find('.page-navigation').css({ 'width' : $(this).outerWidth() });
		});
		
		$('#content:not(.landing #content)').css({ 'width' : contentWidth });
	
	}
