function show() {
  $('html').css({'visibility':'visible'});
}

function init(){
  if($.browser.msie) {
    $('.shadowed').removeClass('shadowed');
  }

  $('html').css('visibility', 'hidden'); 
  
  
  // aktuelt rss links return false & redirect
  
  if ($('body').hasClass('category-aktuelt')) {
    $('#article li').each(function(){
      $(this).find('a').click(function(){
        return false;
      });
    });
  }  
  
  $('.news_intro').each(function(){
    $(this).find('a').click(function(){
      window.location.href = '/category/aktuelt/';
      return false;
    });
  });
  
  // *** topmenu
  $('#topmenu .sub-menu, #topmenu .sub-menu li').width($('#topmenu').width()+80);
  $('#topmenu .sub-menu li').each(function(){
    $(this).find('p.sub').load($(this).find('a').attr('href')+' #article .sub_head');      
  });
  var position = $('#topmenu').offset();
  $('#topmenu li').each(function(){
    
    $(this).find('.sub-menu').hide();
    $(this).mouseenter(function(){
      $(this).find('a:first, span:first').css('background', '#FFE03A');
      
      $(this).find('.sub-menu').css({'left': position.left-110, 'top': position.top+30});
      $(this).find('.sub-menu').show();
      
      $(this).find('.sub-menu li').mouseenter(function(){
        $(this).css({'background': '#ffe89e'});
        $(this).find('a').css({'background': 'none'});
      });
      $(this).find('.sub-menu li').mouseleave(function(){
        $(this).css({'background': '#FFE03A'});
        $(this).find('a').css({'background': 'none'});
      });
    });
    $(this).mouseleave(function(){
      $(this).find('a:first, span:first').css('background', 'none');
      $(this).find('.sub-menu').hide();
    });
  });
  
  $('#topmenu').find('a').each(function(){
    if ($(this).attr('title') == 'noreturn') {
      $(this).contents().wrap('<span/>');
      $(this).find('span').unwrap();
    }
    if ($(this).attr('title') == 'special') {
      $(this).attr('onClick', "_gaq.push(['_trackEvent', 'Nyhetsfeed', 'Go to', 'Gå til seksjonsvisning for nyheter']);");
    }
  });
  
  $('#topmenu li').not('#topmenu .sub-menu li').each(function(){
    $(this).find('a:first').addClass('topmenu_top');
  });
  
  $('.topmenu_top:last').parent().css('background', 'none');
  
  $('#topmenu').find('.sub-menu').each(function(){
    $(this).parent().find('span:first').addClass('topmenu_top');
    $(this).find('a:first').removeClass('topmenu_top');
  });
  
  if ( ! $('#img_holder img').length ) { 
    var the_link = $('#img_holder').next('#rightmenu_view a:first').attr('href');
    $('#img_holder').load(the_link+' #img_holder img.wp-post-image:first');
  }
  
  $('#article').find('p').each(function(i){
    if ($(this).text().length > 1) {
      $(this).addClass('filled');
    }
  });
  
   
  
  if ($('#rightmenu_holder').find('#rightmenu')) {
    $('#rightmenu').find('li').each(function(){
      $(this).find('p.sub').load($(this).find('a').attr('href')+' #article .sub_head');
    });
    doClick($('#rightmenu li:first a'));
    tumbleranimation = setInterval('tumble($("#rightmenu"), $("#placeholder"), $("#rightmenu li"))', 10000 );      
  }
  
  $('#rightmenu li a').bind('click', doStop);  
  
  // Send knapp open content in fancybox
  
  $('.wpcf7-submit').live('click', function (){
    $('.wpcf7-form').ajaxForm({
        success: function(responseText){
            $.fancybox({
                'content' : responseText
            });
        }
    }); 
  });

   /* fancy shmancy */
  
  $('a.lightbox').fancybox({
    'autoDimensions': true,
    'width': 460,
    'scroll': 'no'
  });
  
  show();
  }
  
$(function(){
  $('html').css('visibility', 'hidden');
  if ($('body').hasClass('home')) {
    $('body').addClass('front');
  } else {
    $('body').addClass('else');
  }
  
  if (!($('body').hasClass('chrome'))) {
    $('#content').css({'display': 'inline-block'});
  }
  
  if ($('body').hasClass('home, front')) {
    $('#placeholder').css({'float': 'left', 'margin-top': '5px'});
  }
  
  $('#rightmenu').find('li:last').css('border-bottom','none');

  if ($('#rightmenu_holder').find('#rightmenu_view')) {
    $('#img_holder').insertBefore('#rightmenu_view');
    if ($('#img_holder').height() > $('#rightmenu_view').height()) {
      $('#rightmenu_view').height($('#img_holder').height());
    } else {
      $('#img_holder').height($('#rightmenu_view').height());
    }
    $('#sidebar_content_holder').appendTo('#rightmenu_holder'); 
    
    $('#article').find('.archive_view:nth-child(3n)').css('margin-right', '0');
    
    
    if($('#article').find('.gallery-item')) {
      $('.gallery').find('br').remove();
      $('#article').find('.gallery-item:nth-child(3n)').css('margin-right', '0');
    }
    
    
    $('#article').find('.archive_view img').addClass('archive_img');
  } 
  
  
  if ($('body').hasClass('category-avdelinger')) {
    $('.archive_view, .archive_view img').css({'width': '300px', 'height': 'auto'});
    $('.archive_view:first').css('margin-right', '25px');
  }
  
    if($('#article').find('img').width() > 645) {
      if($.browser.msie) {
        $('#article').find('img').css({'width': '645px', 'height': 'auto'}).attr('height', '');
      } else {
        $('#article').find('img').css({'max-width': '645px', 'height': 'auto'}).attr('height', '');
      }
    }
    
    if ($('#the_content').find('p img').length > 2) {
      //alert('true')
      $('#the_content').find('p img').closest('p').addClass('slide');
      $('.slide').after('<p id="nav_slide"></p>') 
        .cycle({ 
          fx:     'fade', 
          speed:  'slow', 
          timeout: 0, 
          pager:  '#nav_slide', 
           
          // callback fn that creates a thumbnail to use as pager anchor 
          pagerAnchorBuilder: function(idx, slide) { 
              return '<a href="#"><img src="' + slide.src + '" width="100" height="75" /></a>'; 
          } 
      });
    }
   
  //***** menu buttons clickable 
  
  $('#topmenu .sub-menu li, #rightmenu_view li').click(function(){
    window.location.href = $(this).find('a').attr('href');
  });
  
  $('#rightmenu_view li, #rightmenu li').each(function(){
  
    $(this).mouseenter(function(){
      $(this).css({'background': '#ffe89e'});
      //$(this).find('a').css({'background': '#ffe89e'});
    });
    
    if ($(this).find('a').hasClass('activated')) {
      $(this).addClass('activated');
    } else {
    $(this).mouseleave(function(){
      $(this).css({'background': '#FFE03A'});
      //$(this).find('a').css({'background': '#FFE03A'});
    });
    }
  });
  
  
  $('.sidebar_content textarea').attr('rows', '');
  $('.sidebar_content input[type="text"]').attr('value', 'e-post');
  
  $('#news_section li, #news li').addClass('news_intro');
  $('.news_intro:last').css('border-right', 'none');
  $('#rightmenu').closest('#rightmenu_holder').height($('#placeholder').height());
  $('#rightmenu').closest('#rightmenu_holder').css({'background': '#FFE03A', 'border-bottom': 'solid 21px #FFE03A'});
  var para = $('#article .entry').find('p').not('img');
  if (para.find('strong')) {
    //$('strong').addClass('ingress');
  }
  
  //***** shadowed 
  $('#topmenu .sub-menu li, #rightmenu_view li, #rightmenu li, .head_bar h2').addClass('shadowed');
  
  
  
  $('#home').bind('click', function (ev) {
    window.location.href = "/";
  });
  
  
  $('#news_section').find('.post p').addClass('ingress');
  
//***** contact dropout 
  $('#contactmenu li.noview').attr('onClick', "_gaq.push(['_trackPageview', 'ring-oss']);");
  
  $('#contactmenu li.noview').live('click', function(){      
    $(this).addClass('activate');
    $(this).removeClass('noview');
    $(this).next('li#ring_content').show();
  });
  $('#contactmenu li.activate').live('click', function(){      
    $(this).addClass('noview');
    $(this).removeClass('activate');
    $(this).next('li#ring_content').hide();
  });
  
});



function doStop (){
  clearInterval(tumbleranimation);
  doClick($(this));
  $('.activated').removeClass('activated');
  $(this).closest('li').addClass('activated');
  return false;
}


function doClick(obj) {
  var the_link = obj.attr('href');
  obj.closest('#content').find('#placeholder a.linkage').attr('href', the_link);
  obj.closest('#content').find('#placeholder a.linkage').load(obj.attr('href')+' #article img:first' );
  obj.closest('#content').find('#placeholder span.excerpt').load(obj.attr('href')+' #article p.fp_text' );
  return false;
}

var menuChild = 0;
var menuChildObject;

function tumble (menu, area, item) {
 menuChild ++;
 
 if(menuChild == $('#rightmenu li').length ){
  menuChild = 0;
 }
 $('#rightmenu li').each(function(index) {
    if (index == menuChild){
    $('.activated').removeClass('activated');
    menuChildObject = $(this);
    $(this).addClass('activated');
    area.animate({
      opacity: 0,
      }, 100, function() {
      doClick(menuChildObject.find('a'));
      area.delay(1000).animate({
        opacity: 1,
        }, 100 );
        }  
      );
   }
 });
 }
 

var tumbleranimation;
  

window.onload = init;

