﻿/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function toggleFontSize() {
    if(document.body.style.fontSize != '0.85em'){document.body.style.fontSize = '0.85em';}
    else{document.body.style.fontSize = '0.75em';}
}

/* Article tools 
(function($) {
    $.fn.addTools = function(customOptions) {
        var options = $.extend({}, $.fn.addTools.defaultOptions, customOptions);
        
        return this.each(function() {
//            $(this).append('<li class="' + options.printClass + '"><a href="">' + options.printText + '</a></li><li class="' + options.shareClass + '"><a href="">' + options.shareText + '</a></li>');
          
            $('li.' + options.printClass + ' a').click(function() {
                window.print();
                return false;
            });
            
            $('li.' + options.shareClass + ' a.toggle_share').toggle(function(event) {
                $('div.sharethisbox').slideDown();
                $('li.' + options.shareClass + ' a').html(options.closeText);
                return false;
            }, function(){
                $('div.sharethisbox').slideUp();
                $('li.' + options.shareClass + ' a').html(options.shareText);
                return false;
            });
            
            $('fieldset.sharethisboxright a').click(function() {
                $('div.sharethisbox').slideUp();
                $('li.' + options.shareClass + ' a').html(options.shareText);
                window.open(this.href);
                return false;
            });
        });
    };
 
    $.fn.addTools.defaultOptions = {
        printClass: 'print',
        printText: 'Print',
        shareClass: 'share',
        shareText: 'Share content',
        closeText: 'Lukk'
    };
})(jQuery);*/

jQuery(document).ready(function(){
    /* Article tools 
    $('ul.article_tools').addTools({
        printText: 'Skriv ut',
        shareText: 'Del innhold' 
    });*/
    
    /* Open links in new window */
    $('a.external').click(function(){
        window.open(this.href);
        return false;
    });
    
    /* IE6 message */
    if(!$.cookie('hideie6msg')){
        $('#ie6msg').show();
    };
    $('#ie6msg a.close').click(function(){
        $('#ie6msg').slideUp();
        $.cookie('hideie6msg', true, { expires: 2 });
        return false;
    });
    
    
    /* Mega dropdown */ 
	var activePage;
//	$('div.megaDropdown').equalHeights();
//	$('div.megaDropdown').hide();
	arrow = '<span class="sf-sub-indicator"> &#187;</span>';

	$('ul#MainNav li').each(function() {
		if($(this).hasClass('active')) {
			activePage = this;
		}
		if($(this).hasClass('mega')){
			$(this).find('a').eq(0).append(arrow);
		}
	});

	var megaConfig = {
		interval: 500,
		sensitivity: 4,
		over: addMega,
		timeout: 400,
		out: removeMega
	}
	
	function addMega() {
		$($(this).find("div.megaDropdown")).slideDown("fast");
		if(this !== activePage){
			$(this).addClass('active');
			$(activePage).removeClass('active');
		}
		return false;
	}
	
	function removeMega() {
		$($(this).find("div.megaDropdown")).slideUp("fast");
		if(this !== activePage){
			$(this).removeClass('active');
			$(activePage).addClass('active');
		}
		return false;
	}
	
	$('ul#MainNav li.mega').hoverIntent(megaConfig);
	
	/* Mega dropdown end */
	
	// Hide Google Custom Search branding at onclick in search field
	$.fn.toggleGBrand = function () {
		if(!$(this).attr('value').length){
			$(this).addClass('gbrand');
			$(this).focusin(function(){
				$(this).removeClass('gbrand');
			});
			$(this).focusout(function(){
				if(!$(this).attr('value').length)
					$(this).addClass('gbrand');
			});
		}
	};
	$('div.QuickSearchArea input.quickSearchField').toggleGBrand();
});
