﻿var currentFontSize = '';
$(function() {	
	$('#nav').hover(function(){$.dimScreen(100, 0.5); showMenu();},
	function() {$.dimScreenStop(); hideMenu(); });

	$('#nav ul.menu-main > li').mouseover(function(){$(this).addClass('highlighted'); });
	$('#nav ul.menu-main > li').mouseleave(function(){$(this).removeClass('highlighted');});

	$('.teaser.clickable, .shortcuts li:has(a), #articles .article:has(a)').click(function(){location.href = $(this).find('a:first').attr('href');});

	$('#slideshow .links ul li, .shortcuts li:has(a)').hover( function(){ $(this).addClass('hover'); }, function(){$(this).removeClass('hover'); });
					
	$('ul.subsection-menu li').hover( function(){
                $(this).addClass('focus'); 
                $(this).prev().addClass('preceding');
                },
                    function(){
                    $(this).removeClass('focus');
                    $(this).prev().removeClass('preceding');
                    });

$('.leftArrow, .rightArrow').children('img').hover( function() {
	var src = $(this).attr('src').replace('.gif', '-on.gif');
	$(this).attr('src', src);  },
	function () {
		var src = $(this).attr('src').replace('-on.gif', '.gif');
		$(this).attr('src', src);
	});
	
$('.searchfield').find('button').hover( function() {
	$('.searchfield button').css('background-color', '#675C53');
	$('.searchfield button span').css('background-color', '#675C53'); },
	function () {
		$('button').css('background-color', '#988F86');
		$('button span').css('background-color', '#988F86');
	});

	
	/* Samme høyde for alle snarveier i horizontal view */
	var maxShortcutHeight = 0;
	
	$('div#south > ul.shortcuts > li').each(function(){
		if(maxShortcutHeight < $(this).height()) {
			maxShortcutHeight = $(this).height(); 
		}
	});

	if(maxShortcutHeight != 0) {
		$('div#south > ul.shortcuts > li').each(function(){
			$(this).height(maxShortcutHeight); 
		});
	}
	
	var maxSlideshowHeight = 0;
	
	$('div#teaserContainer > div.box').each(function(){
		if(maxSlideshowHeight < $(this).height() ) {
			maxSlideshowHeight = $(this).height(); 
		}
	});
	
	if(maxSlideshowHeight != 0) {
		var padding = 28;
		$('div#teaserContainer').height(maxSlideshowHeight + padding);
	}
	
	//Pga sIFR må høyden på slideshow regnes fra normal fontstørrelse og justeres dersom siden blir refresha mens font_large eller font_largest er i bruk
	//currentFontSize = document.getElementById( 'fontsize').href;
    currentFontSize = $('body').attr('class');
	if(currentFontSize.indexOf('largest') != -1) {
		currentFontSize = 'normal';
		var factor = getHeightAdjustment('largest');
		adjustSlideshowHeight(factor);
	}
	else if (currentFontSize.indexOf('large') != -1) {
		currentFontSize = 'normal';
		var factor = getHeightAdjustment('large');
		adjustSlideshowHeight(factor);
	}
	else if (currentFontSize.indexOf('normal') != -1) {	currentFontSize = 'normal';}
	
	//Åpne eksterne lenker i nytt vindu
	$("a[rel='external']").click(function() {window.open( $(this).attr('href') ); return false; });
	
	// Google analytics
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
	})();
}); // document ready

//Kalles fra accessibility.js
function adjustBoxHeightsByFontsize(size) {
	var factor = getHeightAdjustment(size);
	adjustShortcutHeight(factor);
	adjustSlideshowHeight(factor);
}

function adjustShortcutHeight(factor) {
	$('div#south > ul.shortcuts > li').each(function(){
		$(this).height($(this).height() * factor); 
	});
}

function adjustSlideshowHeight(factor) {
	$('div#teaserContainer').height($('div#teaserContainer').height() * factor);
}

function getHeightAdjustment(size) {
	var heightAdjustment = 1;
	if (currentFontSize == 'largest' && size != 'largest'){
		if(size == 'normal') {
			heightAdjustment = 0.73 / 0.9;
			currentFontSize = 'normal';
		} else {
			heightAdjustment = 0.8 / 0.9;	
			currentFontSize = 'large';
		}		
	} else if (currentFontSize == 'large' && size != 'large') {
		if(size == 'normal') {
			heightAdjustment = 0.73 / 0.8;
			currentFontSize = 'normal';			
		} else {
			heightAdjustment = 0.9 / 0.8;	
			currentFontSize = 'largest';
		}
	}else if(currentFontSize == 'normal' && size != 'normal') {
		if(size == 'large') {
			heightAdjustment = 0.8 / 0.73;	
			currentFontSize = 'large';
		} else {
			heightAdjustment = 0.9 / 0.73;	
			currentFontSize = 'largest';
		}		
	}
	return heightAdjustment;
}

function showMenu(){
$('ul.menu-sub, #nav .home-button').show();

 var submenuHeight = 0; 
$("#nav ul.menu-main > li").each(function(){  
 if ($(this).height() > submenuHeight) { submenuHeight = $(this).height(); }  
 });
 
$('#menu-wrapper').addClass('expanded').css('height', submenuHeight);
$('ul.menu-main > li').css('height', submenuHeight + 28);// legg til litt for å dra bakgrunnen helt ned

} // showMenu end




function hideMenu(){
//var isOpen = false;
$('#nav ul.menu-sub, #nav .home-button').hide();
$('#menu-wrapper').removeClass('expanded').css('height', '');
$("#nav ul.menu-main > li").css('height', '');
} // hideMenuEnd

/*function menuDimScreen(){
$('#nav').mouseenter(function(){
$.dimScreen(100, 0.5);
});
$('#nav').mouseleave(function(){
$.dimScreenStop();
});
}*/

//dimScreen()
//by Brandon Goldman
jQuery.extend({
    //dims the screen
    dimScreen: function(speed, opacity, callback) {
        
		
		if(jQuery('#__dimScreen').size() > 0) return;
        
        if(typeof speed == 'function') {
            callback = speed;
            speed = null;
        }

        if(typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }

        if(speed < 1) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }
        
        if(opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }

        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        return jQuery('<div></div>').attr({
                id: '__dimScreen'
                ,fade_opacity: opacity
                ,speed: speed
            }).css({
            background: '#000'
            ,height:( ($.browser.msie && parseInt($.browser.version) == 8) ? jQuery(document).height() -4:jQuery(document).height())+'px'
            ,left: '0px'
            ,opacity: 0
            ,position: 'absolute'
            ,top: '0px'
            ,width: '100%'
            ,zIndex: 900
        }).appendTo('#content').fadeTo(speed, opacity, callback);
        // her stod det document.body - men det blir krøll i IE 6 og 7
        // muligens pga background
    },
    
    //stops current dimming of the screen
    dimScreenStop: function(callback) {
        var x = jQuery('#__dimScreen');
        x.fadeOut(0, function() {
            x.remove();
            if(typeof callback == 'function') callback();
        });
    }
});

