var nav_at_bottom = false;
var myURI = new URI();
var current_page = myURI.get('file').replace('.html','') || 'index';
var fragment = myURI.get('fragment');

if (fragment && fragment != current_page) {
	document.location.href = '/' + fragment + '.html';
}

window.addEvent('domready',function() {
	PrepNav();
});

// Prepare the navigation UL for shifting up and down the page depending on what button was clicked
function PrepNav(target_css) {
	if (Browser.Engine.trident) {
		return false;
	}
	target_css = target_css || 'ul#navigation li a, #logo';
	$$(target_css).each(function(link) {
	
		// Store the original HREF if it hasn't already been stored
		if (!link.retrieve('original_href')) { link.store('original_href',link.href); }
		
		// Get the original HREF
		var url = link.retrieve('original_href');
		var path = url.split('/');
		var page = path[path.length-1].split('.')[0];
		
		// Change the HREF to the new anchor-based system
		link.href = '#' + page;
		link.addEvent('click',function() {		
			if (page != current_page) {
				current_page = page;
				MoveNav( link.hasClass('bottom') );
				LoadContent( url );
				
				var page_handle = page.split('-');
				page_handle = page_handle || page;
			
				$$('ul#navigation li a.selected, #logo.selected').each(function(selected) { selected.removeClass('selected') });

				var new_selected_nav = $$('ul#navigation li a[href$='+page_handle+']')[0];
				if (new_selected_nav) { new_selected_nav.addClass('selected'); }
			}
		});
	});
}

// The actual act of moving the nav happens here
function MoveNav(goingDown) {
	// If the nav position is changing
	goingDown=false;
	if (goingDown != nav_at_bottom) {
		ChangeLogo(goingDown);
	}

	nav_at_bottom = false;
	var links = $$('#navigation a');
	$('navigation').set('morph', {
		duration: 1000,
		transition: Fx.Transitions.Sine.easeInOut,
		onComplete: function() { SetNavClass(goingDown); }
	});
	if (goingDown) {
		$('navigation').morph('ul#navigation.bottom');
		links.each(function(link) {
			link.set('morph', {
				duration: 1000,
			});
			link.addEvents({
				mouseover: function() {
					link.setStyles({'border-top-width': 5, 'top': -2, 'padding-top': 16 });
				},
				mouseout: function() {
					link.setStyles({'border-top-width': 1, 'top': 0, 'padding-top': 18 });
				}
			});
			link.morph('ul#navigation.bottom li a');
		});
	}
	else {
		$('navigation').morph('ul#navigation.top');
		links.each(function(link) {
			link.morph('ul#navigation.top li a');
			link.removeEvents('mouseover');
			link.removeEvents('mouseout');
		});
	}
}

function ChangeLogo(goingDown) {
	$('logo').set('slide', { duration: 500, transition: Fx.Transitions.Sine.easeIn });
	$('logo').slide('out');
	
	var ChangeLogoBG = function() {
		if (goingDown) {
			$('logo').removeClass('black').addClass('white');
		}
		else {
			$('logo').removeClass('white').addClass('black');
		}
		$('logo').slide('in');
	};
	ChangeLogoBG.delay(500);
}

function SetNavClass(goingDown) {
	if (goingDown) {
		$('navigation').removeClass('top').addClass('bottom');
	}
	else {
		$('navigation').removeClass('bottom').addClass('top');	
	}
}

// Loads the content from the next page via xmlhttprequest
function LoadContent(url) {
	var myRequest = new Request({
		method: 'get',
		url: url + '?ajax=1',
		evalScripts: true,
		onSuccess: function(text,xml) {
			console.log(xml);
			TransitionContent(xml,url);
		},
		onFailure: function( xhr ) {
			console.log('Error',xhr);
		}
	}).send();
	$('content').set('slide', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
	$('content').fade('out');
	window.fireEvent('beforeunload');
}

// Load in the new content
function TransitionContent(xhtml,url) {
	try {
           document.title = xhtml.getElement('title').innerHTML;
	} catch (e) {
           console.log(e);
        }
	var FillContent = function() {
		$('content').innerHTML = xhtml.getElement('#content').innerHTML;
		$('content').fade('in');
		window.fireEvent('domready');
	};
	
	FillContent.delay(500);
}

// Text Replacement Engine
function ReplaceText() {
	$$('.replace').each(function(element) {
		var text = element.innerHTML;
		if (element.getStyle('text-transform') == 'uppercase') {
			text = text.toUpperCase();
		}
		text = escape(text);
		var color = element.getStyle('color').replace('#','');
		var font = element.getStyle('font-family').replace(' ','').split(',')[0] + '.ttf';
		var size = (element.getStyle('font-size').toInt() * 1.0).toInt();
	
		var filename = [text,font,size,color].join('|') + '.png';
	
		element.empty();
	
		var img = new Element('img', {
			'src': '/cgi-local/text_render_cache/replace-' + filename
		});
		
		element.adopt(img);
	});
}
