$(document).ready(function() {

 	// Add +/- displays
 	$('#accordion-section h4.accordion-title').prepend('<span> + </span>');
 	$('#accordion-section h4.accordion-title').append('<span> +</span>');

	//ACCORDION BUTTON ACTION	
	$('#accordion-section h4.accordion-title').click(function() {
		//$('#accordion-section div.accordion-content').slideUp(); // Close all
		//$('#accordion-section h4.accordion-title span').html(' + '); // Close all
		$(this).next().slideToggle('normal');
		if ($('span:first', $(this)).html() == ' - ') {
			$('span', $(this)).html(' + ');
		} else {
			$('span', $(this)).html(' - ');
		}
		
	});
 
	// Hide accordions on page load by default
	$("#accordion-section div.accordion-content").hide();
	$("#accordion-section div.start-open").show();
	$('#accordion-section h4.start-open span').html(' - ');


	
	// Open all external links in a new window
	$("a[href^=http]").each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr('target', '_blank');
			}
		}
	)

	
	// Add tabbed styles for IE8 and below
	// IE won't support mixed selectors with nth-child so we'll have to use
	// javascript to style all browsers
	//if ($.browser.msie && $.browser.version < 9 ) {
		$('ul.tabbed-links li:nth-child(5n+1)').addClass('child-1');
		$('ul.tabbed-links li:nth-child(5n+2)').addClass('child-2');
		$('ul.tabbed-links li:nth-child(5n+3)').addClass('child-3');
		$('ul.tabbed-links li:nth-child(5n+4)').addClass('child-4');
		$('ul.tabbed-links li:nth-child(5n+5)').addClass('child-5');
	//}
	
	
	// Fix parent hover
	$('#pri-nav li ul').hover(
		function () {
			$(this).parent('li').addClass('current_page_ancestor');
		}, 
		function () {
			$(this).parent('li').removeClass('current_page_ancestor');
		}
	);

	// Keep menu open? Maybe...
	$('#pri-nav > li > a').hover(
		function () {
			$('#pri-nav > li.current_page_parent').removeClass('current_page_ancestor');
		}, 
		function () {
			//$('#pri-nav li.current_page_parent').addClass('current_page_ancestor');
		}
	);

	// Fix bottom border
	$('#pri-nav li ul li:last-child').addClass('last');
	
	
	// Create overview link for each sub menu
	$('#pri-nav > li').each(function(){
		if ($('li', this).length) {
			link = $('a:first', this);
			$('ul', this).prepend('<li><a href="'+link.attr('href')+'">Overview</a></li>');
		}
	});
	
	
	// --------------------------------------------------------------------------
	
	// Create home page animations
    $('.image-slideshow').cycle({
		fx: 'fade', 
        timeoutFn: calculateTimeout
	});
	
	function calculateTimeout(currElement, nextElement, opts, isForward) { 
	    // here we set even number slides to have a 2 second timeout; 
	    // by returning false for odd number slides we let those slides 
	    // inherit the default timeout value (4 sec) 
	    //var index = opts.currSlide; 
	    //return index % 2 ? 2000 : false; 
	    var rand = (Math.ceil(100*Math.random()) * 100);
	    //console.log('Random: '+rand);
	    return rand
	} 
		
		
		
		
		
});
