$(function() {
	
	var navHovering = false;
	var dropdownHovering = false;
	
	var navHoverProperties = {
		over: navHover,
		out: navLeave,
		interval: 100,
		timeout: 800,
		sensitivity: 7
	};
	$("#nav-startingpoints").hoverIntent(navHoverProperties);
	
	var dropdownHoverProperties = {
		over: dropdownHover,
		out: dropdownLeave,
		interval: 10,
		timeout: 1200,
		sensitivity: 25
	};
	$("#dropdown-startingpoints").hoverIntent(dropdownHoverProperties);
	
	function navHover() {
		if($.browser.msie && $.browser.version < 7.0) {
			$("#dropdown-startingpoints").show();
		} else {
			$("#dropdown-startingpoints").slideDown("fast");
		};
		navHovering = true;
	};
	
	function navLeave() {
		if(!dropdownHovering) {
			if($.browser.msie && $.browser.version < 7.0) {
				$("#dropdown-startingpoints").hide();
			} else {
				$("#dropdown-startingpoints").slideUp("fast");
			};
		};
		navHovering = false;
	};
	
	function dropdownHover() {
		$("#dropdown-startingpoints").show();
		dropdownHovering = true;
	}

	function dropdownLeave() {
		if(!navHovering) {
			if($.browser.msie && $.browser.version < 7.0) {
				$("#dropdown-startingpoints").hide();
			} else {
				$("#dropdown-startingpoints").slideUp("fast");
			};
		};
		dropdownHovering = false;
	}

	$('#dropdown-startingpoints').hide();
	
	
	<!--tillagt av Robert Rutherford 2008-09-18-->
	$(".navgroup").hover( 
		function() {
			$(this).css("background-color","#ddd");
		},
		function() {
			$(this).css("background-color","transparent");
		}
	);
	<!--tillagt av Robert Rutherford 2008-09-18-->
	
	// Check for presence of masthead
	if ( $(".masthead").length > 0 ) { mC.init(); }
	
});


/*
** FRIENDLY LOG FUNCTION
** Logs messages to console if present
*/
function fLog( message ) {
	// Look for precence of console
	if ( ( typeof( console ) == "object" ) && ( typeof( console.log ) == "function" ) ) {
		console.log( message );
	}
}


/*
** MASTHEAD CAROUSEL
** Applies a timed interactive carousel to .masthead
*/
var mC = {
	index: 0,
	loop: false,
	defaultTime: 4500,
	defaultAnimationSpeed: "slow",
	init: function() {
		fLog( "mC:init" );
		// Make carousel pause on hover
		$(".masthead ul.articles li.article").hover(
			function () {
				mC.stopTimer();
			},
			function () {
				mC.startTimer();
			}
		);
		// Load carousel item on nav click and reset the timer
		$(".masthead ul.nav li").click(
			function () {
				var index = $(".masthead ul.nav li").index( this );
				fLog( "mC | Clicked item " + index );
				mC.stopTimer();
				mC.loadIndex( index );
				return false;
			}
		);
		// Remove display limitations for non-js browsers
		var showNav = true;
		if ( $.browser.msie ) {
			if ( parseInt($.browser.version) < 8 ) {
				showNav = false;
			}
		}
		if ( showNav ){
			$(".masthead").css("height","440px");
			$(".masthead ul.nav").css("visibility","visible");
			$(".masthead ul.nav").css("display","block");
		}
		$(".masthead ul.articles li.article").css("visibility","visible");
		$(".masthead ul.articles li.article").css("display","block");
		$(".masthead ul.articles li.article:gt(0)").hide();
		// Start timer
		mC.startTimer();
	},
	loadIndex: function( index ) {
		fLog( "mC:loadIndex( index:" + index + " )" );
		// Crossfade images
		$(".masthead .articles li.article:eq(" + mC.index + ")").fadeOut( mC.defaultAnimationSpeed );
		$(".masthead .articles li.article:eq(" + index + ")").fadeIn( mC.defaultAnimationSpeed );
		// Select the correct nav item
		$(".masthead ul.nav li.selected").removeClass("selected");
		$(".masthead ul.nav li:eq(" + index + ")").addClass("selected");
		// Save new interval index
		mC.index = index;
		mC.startTimer();
	},
	loadNext: function() {
		fLog( "mC:loadNext" );
		var index;
		// See if end of items is reached
		if ( mC.index >= $(".masthead ul.articles li.article").length-1 ) {
			fLog( "mC:loadNext | Last item reached" );
			if ( mC.loop ) {
				// Loop if on last item
				index = 0;
				mC.loadIndex( index );
			}
		} else {
			index = mC.index + 1;
			mC.loadIndex( index );
		}
	},
	loadPrevious: function() {
		fLog( "mC:loadPrevious" );
		var index;
		// See if end of items is reached
		if ( mC.index <= 0 ) {
			if ( mC.loop ) {
				// Loop if on last item
				index = $(".masthead ul.articles li.article").length-1;
				mC.loadIndex( index );
			}
		} else {
			index = mC.index - 1;
			mC.loadIndex( index );
		}
	},
	startTimer: function() {
		fLog( "mC:startTimer" );
		// Store the default time
		var time = mC.defaultTime;
		// Check for custom item time in the rel attribute
		var rel = $(".masthead .articles li.article:eq(" + mC.index + ")").attr( "rel" );
		if ( rel ) {
			fLog( "mC:startTimer | Custom time is " + rel );
			time = rel;
		}
		// Create new timeout and call loadNext
		mC.timeout = setTimeout( mC.loadNext, time );
	},
	stopTimer: function() {
		fLog( "mC:stopTimer" );
		// Clear interval
		clearInterval( mC.timeout );
	}
}
