$(document).ready(function(){

	RunSlideshow();
	PrepareAJAXNavigation();
	PrepareFAQDisplay();
	RunContentPreperation();

});

var interval;
function RunSlideshow() {
	if($('div#presentation').length > 0) {
		interval = setInterval("slideSwitch()", 5000);
	}
}
function slideSwitch() {
	var $active = $('#presentation span.active');

	if ($active.length == 0) {
		$active = $('#presentation span:last');
	}

//	var $next =  $active.next().length ? $active.next() : $('#presentation img:first');
	if($active.next().length) {
		var $next = $active.next();
	} else {
		clearInterval(interval);
		return false;
	}

	$active.addClass('last_active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity:  1.0}, 1000, function() {
			$active.removeClass('active last_active');
		});
}

function PrepareAJAXNavigation() {
	setInterval('pollHash()', 100);
	changeHashLocation();
	
	$("div#main_page_quick_navigation ul#quick_navigation a").click(function () {
		if($(this).hasClass('noajax')){
			return;
		}
		// Set the focus
		$('div#main_page_quick_navigation ul#quick_navigation li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		
		var that = this;
		
		$('div#section_two').height($('div#section_two').height());
		$('div#section_two div.content_width').fadeOut('fast', function() {

			$.get($(that).attr("href"), {AJAX:'1'}, function(data) {
				$("div#section_two div.content_width").html(data);
				RunContentPreperation();
				$('div#section_two div.content_width').fadeIn('fast');

				var slideToHeight = $('div#section_two div.content_width').height();
				$("div#section_two").animate({"height": slideToHeight}, 'fast', function() {
					$("div#section_two").removeAttr('style');
					location.hash = $(that).attr("href");
					lastHash = location.hash;
				});
			});
		});

		return false;
	});
}

var lastHash = '';
if(location.hash) {
	lastHash = location.hash;
}
function pollHash() {
    if(lastHash !== location.hash) {
        lastHash = location.hash;
		changeHashLocation();
    }
}

function changeHashLocation() {
	if (location.hash) {
		$.get(location.hash.substring(1), {AJAX:'1'}, function(data) {$("div#section_two div.content_width").html(data); RunContentPreperation();})
		//Set the focus
		$('div#main_page_quick_navigation ul#quick_navigation a').each(function() {
			if(location.hash.substring(1) == $(this).attr('href')) {
				$('div#main_page_quick_navigation ul#quick_navigation li').removeClass('selected');
				$(this).parent('li').addClass('selected');
			}
		});
	} else {
		$('ul#quick_navigation a').each(function() {
			URLLength = location.href.length;
			HREFLength = $(this).attr('href').length;
			CurrentPage = location.href.substring(URLLength - HREFLength);
			if(CurrentPage == $(this).attr('href')) {
				$('div#main_page_quick_navigation ul#quick_navigation li').removeClass('selected');
				$(this).parent('li').addClass('selected');
			}
		});
	}
	if($('ul#quick_navigation li.selected').length == 0) {
		second = $('ul#quick_navigation').children().get(1);
		$(second).addClass('selected');
	}
}


function RunContentPreperation() {
	$('dl#faq dt').css('cursor', 'pointer');
	$('dl#faq dd').css('display', 'none');
}

function PrepareFAQDisplay() {
	$('dl#faq dt').live('click', function() {
		$(this).next('dd').siblings('dd').slideUp('fast');
		$(this).siblings('dt').children('span').html('+');

		that = this;
		$(this).next('dd').slideToggle('fast', function() {
			if($(this).is(':visible')) {
				$(that).children('span').html('-');
			} else {
				$(that).children('span').html('+');
			}
		});
	});
}
