jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

/* 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>');

            $('body').click(function() {
                $('div.sharethisbox').fadeOut();
            });

            $('li.' + options.printClass + ' a').click(function() {
                window.print();
                return false;
            });

            $('li.' + options.shareClass + ' a.toggle_share').click(function(event) {
                event.stopPropagation();
                $('div.sharethisbox').fadeToggle();
                return false;
            });

            $('div.sharethisbox').click(function(event) {
                event.stopPropagation();
            });

            $('fieldset.sharethisboxright a').click(function() {
                $('div.sharethisbox').hide();
            });


        });
    };

    $.fn.addTools.defaultOptions = {
        printClass: 'print',
        printText: 'Print',
        shareClass: 'share',
        shareText: 'Share content'
    };
})(jQuery);

/**
* 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);

/* Input text replacer by Hein Haraldson Berg -- http://haraldson.ch/blog/jquery-input-text-replacer/ */
var InputTextReplacer = function(inputs)
{
	if($(inputs).size() > 0)
	{
		this.init(inputs);
	}
}

InputTextReplacer.prototype = {
	init: function(inputs)
	{
		var self = this;
		this.inputs = $(inputs).filter(':text, textarea');
		
		$(this.inputs).each(function(i, elem)
		{
			var label = $('label[for="' + $(this).attr('id') + '"]');
			$(label).hide();
			var labelText = $(label).text();
		
			$(this).val(labelText)
			.focus(function()
			{
				if($(this).val() == labelText)
				{
					$(this).val('');
				}
			})
			.blur(function()
			{
				if($(this).val() != labelText && $(this).val() == '')
				{
					$(this).val(labelText);
				}
			});
		});
	}
};
/* equalHeights – http://www.filamentgroup.com/lab/setting_equal_heights_with_jquery/ */ 
$.fn.equalHeights=function(px){$(this).each(function(){var currentTallest=0;$(this).children().each(function(i){if($(this).height()>currentTallest){currentTallest=$(this).height();}});if($.browser.msie&&$.browser.version==6.0){$(this).children().css({'height':currentTallest});}$(this).children().css({'min-height':currentTallest});});return this;};

jQuery(document).ready(function(){
	// fjern border i main-nav før active tab
	$('ul#main-nav').find('li.active').prev().eq(0).find('a').css('border', '0 none');
	
	// fjern border på annenhver dl i tjenester-boksen på forsiden
	$('#tjenester dl:nth-child(even)').each(function(){
		$(this).css({'border-right': '0 none'});
	});
	
	// sett lik høyde på rows i footer 
	$('#footer div.row').equalHeights();
	
	// lik høyde i rows i søk i foreninger/lag
	$('fieldset.sok_lag_forening ul.selectables').equalHeights();

	// Legg paa klasser paa breadcrumbs for siste og foerste
	$('#breadcrumbs li:first-child').addClass('first');
	$('#breadcrumbs li:last-child').addClass('last');
	
	/* Mega dropdown */ 
	var activePage;
//	$('div.megaDropdown').equalHeights();
//	$('div.megaDropdown').hide();
	arrow = '<span class="sf-sub-indicator"> &#187;</span>';

	$('ul#main-nav 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#main-nav li.mega').hoverIntent(megaConfig);
	
	
	/* Mega dropdown end */

		// gi sidebar lik hoeyde som hovedcontent
	var masterHeight = $('#main-article').height();
	
	if(masterHeight>$('#left-content div.nav-box').height()){
		$('#left-content div.nav-box').css('min-height', masterHeight-19);
	}
	/* Legg på klasse på annenhver rad i tabell */
	$('table.alt_row tr:nth-child(odd)').addClass('alt');

	/* Article tools */
	$('ul.article-tools').addTools({
	    printText: 'Skriv ut',
	    shareText: 'Del innhold'
	});
});
