$(document).ready(function() {
	$('#menu a, .side_menu a, a.inlink').live('click',function(){
		if($(this).hasClass('outbound_link')) {
			window.open(this.href);
			return false;
		}
		else if ($.browser.msie && $.browser.version.substr(0,1)<8) {
			return true;
		}
		else {
			window.location = "#" + $(this).attr('href');
			return false;
		}
	});
	$(window).hashchange();
});
$(function(){
	$(window).hashchange( function(){
		var hash = location.hash;
		var hrefToLoad = hash.split('#')[1];

		if(hash == "" || hash== "#") {
			var hrefSplit = $(location).attr('href').split("/");
			hrefToLoad = hrefSplit[hrefSplit.length - 1] == "" ? "index.html" : hrefSplit[hrefSplit.length - 1];
		}

		$('#menu a').each(function(){
			var that = $(this);
			var headerImage = '';
			var altText = ''
			if(that.hasClass('menu_industry') && that.attr('href') == hrefToLoad) {
				$(this).parent().parent().parent().children('a:first-child').addClass('selected');
			}
			else {
				that[ that.attr( 'href' ) === hrefToLoad ? 'addClass' : 'removeClass' ]( 'selected' );
			}
			headerImage = $(this).attr( 'href' ) === hrefToLoad ? $(this).attr('rel') : '';
			altText = $(this).attr( 'href' ) === hrefToLoad ? $(this).attr('title') : '';
			if(altText != '') {
				$(document).attr('title', altText + ' - FlexiCrew: Workforce. Flexibility.');
				$('#header > h1').html('<span>' + altText + ' - FlexiCrew: Workforce. Flexibility.</span>');
			}
			if(headerImage == 'none') {
				$('#header_overlays > div:first').animate({
					marginLeft: '-=3000',
				}, 900, function() {
					$(this).remove();
				});
			}
			else if(headerImage != '') {
				if($('#header_overlays > div:first').length > 0) {
					$('#header_overlays > div:first').animate({
						marginLeft: '-=3000',
					}, 900, function() {
						$(this).remove();
						$('#header_overlays').append("<div class='header_overlay_new'><div class='header_overlay_inner'><img src='' id='new_header_image' alt='" + altText + " - FlexiCrew: Workforce. Flexibility.' /></div>");
						$('#new_header_image').load( function(){
							$('#header_overlays > .header_overlay_new').animate({
								marginLeft: '-=3000',
							}, 900);
						}).attr('src', 'images/' + headerImage);
					});
				}
				else {
					$('#header_overlays').append("<div class='header_overlay_new'><div class='header_overlay_inner'><img src='' id='new_header_image' alt='" + altText + " - FlexiCrew: Workforce. Flexibility.' /></div>");
					$('#new_header_image').load(function(){
						$('#header_overlays > .header_overlay_new').animate({
							marginLeft: '-=3000',
						}, 900);
					}).attr('src', 'images/' + headerImage);
				}
			}
		});

		hrefToLoad += ' #content > *';

		$('#content_loadwrapper').addClass('content_loading');
		var loadContent = 0;
		$('html, body').animate({scrollTop:0}, 500, function() {
			if(loadContent == 0) {
				$('#content').animate({opacity: 'toggle'},500,function() {
					$('#content').load(hrefToLoad, function() {
						$('#content_loadwrapper').removeClass('content_loading');
						$('#content').animate({opacity:'toggle'},500);
					});
				});
			}
			loadContent++;
		});
	});
	if(location.hash) { $(window).hashchange(); }
});


