$(document).ready(function() {

	// homepage primary carousel
  $('.tabs').tabs('div.panes > div', {
    effect: 'fade', 
    fadeOutSpeed: 'slow'
  });
  
  // Cufon
  Cufon.replace('body.home .panes h2, #content h1, .sub-content h2, #sidebar .module h2');
  
  // Misc.
  $("#googleapps-carousel #overblik li:nth-child(even)").addClass("last");
  $(".pdfs li:nth-child(3n)").addClass("last");

  /* Accessible input values */
  $("input.propagate").each(function(){
      var $this = $(this);
      var val = $('label[for=' + $this.attr('id') + ']').html();
      if($this.val() == '') $this.val(val);
      $this.focus(function(){
          if($this.val() == val) $this.val('');
      }).blur(function(){
          if($this.val() == '') $this.val(val);
      });
  });
  
  $('#product-menu .product-nav li a').click(function(){
  	var _target = $(this).attr('href');
  	$('#product-menu .current').removeClass('current');
  	$('#product-menu .menu').hide();
	$(_target).fadeIn(2000);
	$(this).parent().addClass('current');
  	
  	return false;
  })
  
  homeCarousel('body.home #produkter');
  homeCarousel('body.home #support');
  homeCarousel('body.home #kurser');
  homeCarousel('body.home #produktudvidelser');
  homeCarousel('body.home #radgivning');
  
  // Googleapps page
  $('.funktion > div').hide();
	$('.funktion > h2').click(function(){
		$(this).parent().find('a.open').trigger('click');	
	})
	$('#funktioner .funktion a.open').click(function(){
		if($(this).hasClass('close')){
			$(this).removeClass('close');			
			$(this).prev().slideUp();
		}else{
			$('#funktioner .funktion a.close').removeClass('close');
			$('#funktioner .funktion .opened').slideUp();
			$(this).addClass('close');
			$(this).prev().slideDown();
		}
		return false;
	})
  
  // homepage secondary carousel 
});

function homeCarousel(group){
	var _child = $(group).find('ul > li').length;
	var _totalWidth = _child * 192;
	var _nslide = Math.ceil(_child/5);

	$(group).find('ul').width(_totalWidth);
	if(_nslide > 1){
		$(group).find('p.next a').show().attr('rel','2');
		$(group).find('p.prev a').hide();
	}

	if(_nslide == 1){
		$(group).find('p.prev a').hide();
		$(group).find('p.next a').hide();
	}
	
	$(group).find('p.next a').click(function(){
		$(group).find('ul').animate({
			marginLeft: '-' + ((5*192)*parseFloat(($(this).attr('rel')-1))-26*(parseFloat(($(this).attr('rel')-1)))) 
		}, 1000, function(){
			$(group).find('p.prev a').show();
			$(group).find('p.prev a').attr('rel',parseFloat($(group).find('p.next a').attr('rel')));
			$(group).find('p.next a').attr('rel',parseFloat($(group).find('p.next a').attr('rel'))+1);
			if(($(group).find('p.next a').attr('rel')-1) == _nslide){
				$(group).find('p.next a').hide();
			}
		})
		$(group).find('ul').attr('rel','-' + ((5*192)*parseFloat(($(this).attr('rel')-1))-26*(parseFloat(($(this).attr('rel')-1)))));
		return false;
	})	

	$(group).find('p.prev a').click(function(){
		$(group).find('ul').animate({
			marginLeft: parseFloat($(group).find('ul').attr('rel')) + (960-26) 
		}, 1000, function(){
			$(group).find('p.next a').attr('rel',parseFloat($(group).find('p.next a').attr('rel'))-1);
			$(group).find('p.next a').show();
			if(($(this).attr('rel')) == 0){
				$(group).find('p.prev a').hide();
			}
		})
		$(group).find('ul').attr('rel',parseFloat($(group).find('ul').attr('rel')) + (960-26));		
		return false;
	})	


}
