/*
	Created By: Richard Delph
	For: Mother.je Website
	Date: 2009-06-04 16:45
*/

var activeNav;
var clickedNav;
var activeContentUrl;
var navItems = new Array();

$(
	function ($) {
		$(document).ready(onDocumentReady);
	}
);

function onDocumentReady(e) {
	
	var stars = AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		'width', '969',
		'height', '254',
		'src', '/images/stars',
		'quality', 'high',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'stars',
		'bgcolor', '#140e14',
		'name', 'stars',
		'menu', 'true',
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', '/images/stars',
		'salign', ''
	);
	
	$('#stars-wrapper').html($(stars));
	
	// load page relative to hash or default text
	if ( location.hash.replace(/#/,'') != '' ) {
		loadPageContent(location.hash.replace(/#/,''));
	} else {
		loadPageContent('mother/index');
	}
	
	
	// set height variables for secondary nav objects
	$('#navigation li.primary')
		.each(function() {
			
			var id = $(this).attr('id');
		
			navItems[id] = $(this);
			navItems[id].subNav = $(this).find('ul');
			navItems[id].subNav.fullHeight = navItems[id].subNav.height();
			navItems[id].subNav.height(0);
		
		})
		.find('a:first').click(function(e) {
		
			// prevent default action
			//e.preventDefault();
			
			clickedNav = $(this).parent();
		
			// when page loads activeNav is empty so set to clicked nav here so we can animate the first sub nav out
			if ( activeNav == undefined ) {
				activeNav = clickedNav;
			}
			
			var url = $(this).attr('href');

			
			// check if clicked nav id equals active nav id
			if ( activeNav.attr('id') == clickedNav.attr('id') ) {
				// check if there is a sub nav
				if ( navItems[activeNav.attr('id')].subNav.length == 1 ) {
					// if the height equals 0 open it, if not close it
					if (navItems[activeNav.attr('id')].subNav.height() == 0) {
						setActiveNavItem();
					} else {
						closeSubNav();
					}
				}
				
				if ( activeContentUrl != url )
					loadPageContent(url);
				
			} else {
				// check if active nav has a sub nav and close it if so then set clicked nav
				if (navItems[activeNav.attr('id')].subNav.length == 1) {
					navItems[activeNav.attr('id')].subNav.animate( { height: 0 }, 300, 'easeInOutQuart', function() {
						setActiveNavItem();
					});
				} else {
					setActiveNavItem();
				}
				loadPageContent(url);
			}
			
		})
		
	$('#navigation li ul li a').click(function(e) {
		// prevent default action
		// e.preventDefault();
		
		loadPageContent($(this).attr('href'));
	})
	
	$('#print-page').click(function(e) {
		e.preventDefault();
		
		window.print();
	})
	
	$('#logo').click(function(e) {
		e.preventDefault();
		
		closeSubNav();
		loadPageContent('mother/index');
	})
	
}

function loadPageContent(url) {
	
	url = url.replace(/#/,'') + '.xml';
	
	if ( url == '' ) url = 'mother/index';
	
	if ( activeContentUrl != url && url != '' ) {		

		$.ajax({
			url: url,
			global: true,
			type: "GET",	      
			dataType: "xml",
			success: function(xml){

				var fadeSpeed = 400;
				
				// update site
				$('#page-content').fadeOut(fadeSpeed,function() {
					
					if ( $('#page-subtitle').text() != '' ) {
						
						$('#page-subtitle').fadeOut(fadeSpeed,function() {
							
							$('#page-title').fadeOut(fadeSpeed,function() {
								
								$('#banner').remove();
								fadeContentIn(xml,fadeSpeed)

							})
						});
						
					} else {
						$('#page-title').fadeOut(fadeSpeed,function() {
							
							$('#banner').remove();
							fadeContentIn(xml,fadeSpeed)

						})
					}
					
					activeContentUrl = url;
					
				});
								

			},
			failure: function() {
				loadPageContent('mother/index');
			}
		});
	}

}


function fadeContentIn(xml,fadeSpeed) {
	
	var banner = AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		'width', '969',
		'height', '254',
		'src', $(xml).find('banner').text(),
		'quality', 'high',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'banner',
		'bgcolor', '#140e14',
		'name', 'banner',
		'menu', 'true',
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', $(xml).find('banner').text(),
		'salign', ''
	);
	
	var title = $(xml).find('title').text();
	var subTitle = $(xml).find('subTitle').text();
	var content = $(xml).find('content').text();
	
	// set content of page
	$('#page-title').html(title);
	$('#page-subtitle').html(subTitle);
	$('#page-content').html(content);
	
	
	// load new banner
	$('#banner-wrapper').html($(banner));
	
	// figure out new height of page wrapper and animate it.
	$('#content-wrapper').children().css({'display':'block', 'visibility':'hidden'})

	//remove sifr classes
	$('#page-title').removeClass('sIFR-replaced');	
	$('#page-subtitle').removeClass('sIFR-replaced');

	// replace the fonts with sIFR
	sIFR.replace(eurostilebold, {
		selector: '#page-title',
		css: '.sIFR-root { color: #ffffff; font-size: 22px; }',
		wmode: 'transparent'
	});

	sIFR.replace(eurostilemedium, {
		selector: '#page-subtitle',
		css: '.sIFR-root { color: #981053; font-size: 18px }',
		wmode: 'transparent'
	});
	
	var newHeight = 319 + $('#content-wrapper').height();
	$('#content-wrapper').children().css( { 'display':'none', 'visibility':'visible'} );
	
	if ( ($('#navigation-wrapper').height()+294) > newHeight ) {
		newHeight = $('#navigation-wrapper').height()+294;
	}
	
	$('#page-wrapper').animate({ height: newHeight}, fadeSpeed, 'easeInOutQuart')

	// fade elements back in
	$('#page-title').fadeIn(fadeSpeed,function() {
		if(subTitle != '') {
			$('#page-subtitle').fadeIn(fadeSpeed,function() {
				$('#page-content').fadeIn(fadeSpeed);
			})
		} else {
			$('#page-subtitle').text('$nbsp;');
			$('#page-content').fadeIn(fadeSpeed);
		}
		
	})
	
	
	//$('#content-wrapper').children().css( { 'display':'block' });

}

function setActiveNavItem() {
	
		// set activeNav to clicked nav
		activeNav = clickedNav;

		// see if new active nav has any sub navs and animate out if so
		if ( navItems[activeNav.attr('id')].subNav.length == 1 ) {
			navItems[activeNav.attr('id')].subNav.animate( { height: navItems[activeNav.attr('id')].subNav.fullHeight }, 300, 'easeInOutQuart');
		}


}

function closeSubNav() {
	navItems[activeNav.attr('id')].subNav.animate( {height: 0}, 300, 'easeInOutQuart');
}
