// (c) 2010 Hein Tore Tønnesen, Anagram.no

$(document).ready(function() {
	// preload and cycle pictures
	if ($('#pictures img').length > 1) {
		$.preload( '#pictures img' );
		
		var cycleID = setInterval("cycleMainFeatured()", 3000);
	}
	
	// put › in front of menu links (IE6/IE7)
	if ($.browser.msie && $.browser.version.substr(0,1)<8) 
		$("#nav > li > a").prepend("<span class='pil'>›</span> ");
	
	$(".news div:nth-child(3)").addClass("last");
	
	// add icons to PDF- and Word documents
	$("a[href$='.pdf']:not(:empty)").addClass("pdf");
	$("a[href$='.doc']:not(:empty)").addClass("doc");
	
	// animated scroll
	$("a.anchorLinks").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
});

function cycleMainFeatured() {
	var mainImgVisible = $("#pictures img:visible");
	var lastImage = "mainImage" + $("#pictures img").length;
	
	if (mainImgVisible.hasClass(lastImage)) {
		loadNext = $(".mainImage1");
	} else {
		loadNext = mainImgVisible.next();
	}
	
	var photographer = loadNext.attr("alt");
	
	// if (photographer != '') photographer = "<em>" + photographer + "</em>";
	
	$(mainImgVisible).fadeOut("fast", function() {
		loadNext.fadeIn("slow");
		
		if (photographer && photographer != '.')
			$("#photographer").text(photographer);
	});	
}