jQuery.fn.newsExpand = function() {
	if($(this).is('.active'))
		$(this).contentExpand();
	else
	$(this).siblings('.news')
				.children(':not(h3)')
					.slideUp(300)
					.end()
				.addClass('collapsed')
				.removeClass('active')
				.end()
			.removeClass('collapsed')
			.addClass('active')
			.find('p,img')
				.slideDown(300,function(){
					$(this).parents('.news.active')
						.find('.trigger-news')
						.text('Les mer')
						.contentExpand();
					});
	};



jQuery.fn.contentExpand = function() {
	
	// variables
	var speed_down = 500;	
	var speed_up = 500;	
	var text_more = 'Les mer';
	var text_less = 'Lukk'
	
	if($(this).is('.trigger,.trigger-news'))ele=$(this);
	else ele=$(this).find('.trigger:first,.trigger-news:first');
	
	// expand/collaps: articles (as seen on Priser)
	if(ele.text()==text_more){
		ele.text(text_less);
		// show the div and set the appropriate height before expanding to avoid jumpy animatino
		ele.parents('.article').find('.expandable,.expandable-news').show();
		height = ele.parents('.article').find('.expandable,.expandable-news').attr({'style':''}).height();
		ele.parents('.article').find('.expandable,.expandable-news').hide();
		ele.parents('.article').find('.expandable,.expandable-news').attr({'style':''}).css({height: height}).stop().slideDown(speed_down);

		if($(this).hasClass('no_id')){
			// do nothing - when deep linking is not relevant, as with info blocks on subjects view
		}
		else{
			// removing id before setting hash so it doesn't scroll down
			setHash(ele.parents('.article').attr('id'));
		}
	}
	else if(ele.text()==text_less){
		ele.text(text_more);
		ele.parents('.article').find('.expandable,.expandable-news').stop().slideUp(speed_up);
		//window.location.hash='';
	}

};

function setHash(hash){
	
	// removing id before setting hash so it doesn't scroll down
	hash = hash.replace('#','');
	var node = $( '#' + hash );
	if ( node.length ) {
	  node.attr( 'id', '' );
	}
	document.location.hash = hash;
	if ( node.length ) {
	  node.attr( 'id', hash );
	}
	
}

function FRISVOLDinit(){
	
	// variables
	var hover_in = 150;	
	var hover_out = 200;
	var expand_delay = 700;	

	// expandable content
	$('.trigger').click(function(){
		$(this).contentExpand();
		return false;
	});
		
	// enter/exit: form fields	
	$('input[type=text],input[type=password],textarea').focus(function(){
		if($(this).val()==$(this).attr('title'))$(this).val('');
	}).blur(function(){
		if($(this).val()=='')$(this).val($(this).attr('title'));
	});
	
	// hover: logo
	$('h1#logo a').hover(function(){
		$(this).animate({'top':29,'height':47},hover_in);
	},function(){
		$(this).stop().animate({'top':32,'height':44},hover_out);
	});
	
	// hover: social icons
	$('.icon').hover(function(){
		$(this).animate({'top':-2,'height':18},hover_in);
	},function(){
		$(this).stop().animate({'top':0,'height':16},hover_out);
	});




	// deep linking: expand content if hash exist
	if(window.location.hash != ''){
		var hash=window.location.hash;
		var url=window.location.pathname;
		if(hash.length!=1){
			//check if last part is online or nettstudie, if yes go for switching subjects
			var array=hash.split("-");
			var url_array=url.split("/");
			var url_test=url_array[url_array.length-1];
			//if a trailing '/' comes
			if(url_test==""){
				url_test=url_array[url_array.length-2];
				}
			url_test=url_test.split(".")[0];
			var test=array[array.length-1];
			if(url_test=='vare_fag'||url_test=='fag'||url_test=='varefag'){	
				if(test=='nettstudie'){	
					var id='#fag-'+hash.substring(1,hash.length-11);//retrieving id from hash link
					if($(id).is(':visible')){	
						if($('.subject.active .offline').is(':visible')){
							$('.subject.active .offline').fadeOut(300,function(){
								$('.subject.active .online').fadeIn();
								});
							}
						}
					else{
						$('.subject.active').hide().removeClass('active');
						$(id).fadeIn(300,function(){
							$(this).addClass('active');
							//switch back to online data if online data is set previously as offline link is clicked, this has to be done after switching subjects
							if($(id).is(':visible')){	
								if($('.subject.active .offline').is(':visible')){
									$('.subject.active .offline').hide(0,function(){
										$('.subject.active .online').show();
										});
									}
								}
							});
						$("#subnav ."+id.substring(5,id.length)).addClass('active');
						}
						$('#nav .badge').fadeOut(300);
					}
				else{	
						var id='#fag-'+hash.substring(1,hash.length);//retrieving id from hash link
						if($(id).is(':visible')){
							if($('.subject.active .online').is(':visible')){
								$('.subject.active .online').fadeOut(300,function(){
									$('.subject.active .offline').fadeIn(200);
									});
								}
							}
						else{
							$('.subject.active').hide().removeClass('active');
							$(id).fadeIn(300,function(){
								$(this).addClass('active');
								if($(id).is(':visible')){
									if($('.subject.active .online').is(':visible')){
										$('.subject.active .online').hide(0,function(){
											$('.subject.active .offline').show(0);
											});
										}
									}
								});
							$("#subnav ."+id.substring(5,id.length)).addClass('active');
							
							//switch back to offline data if online data is set previously as offline link is clicked, this has to be done after switching subjects
							
							}
						$('#nav .badge').fadeOut(300);
					}
				
				}
			else if(url_test=='nyheter'){
				setTimeout(function(){
						 $(hash).newsExpand();
				},expand_delay);	
			}
			else{
				setTimeout(function(){
					 $(hash).show().contentExpand();
				},expand_delay);	
				}
			}
	} 





	// expose search field
	$("#q").focus(function() {
		$("#search").expose({ color: '#000', opacity: 0.08, loadSpeed: 'slow' });

	});
	
	// expose login box
	$(".box input").focus(function() {
		$(this).parents('.box').expose({ color: '#000', opacity: 0.2, loadSpeed: 'slow' });
	});
	
	/*
	// close mask on mouse out
	$("#content").hover(function() {
		$.mask.close();
	});
	*/
	



	// toggle for online/offline subject view
	$('.online_offline_switch').click(function() {
		if($('.subject.active .online').is(':visible')){
			$('.subject.active .online').fadeOut(300,function(){
				$('.subject.active .offline').fadeIn(200);
				});
			// removing id before setting hash so it doesn't scroll down
			setHash($('.subject.active').attr('id'));
			// alert("#"+$('.subject.active').attr('id'));
			}
		else if($('.subject.active .offline').is(':visible')){
			$('.subject.active .offline').fadeOut(300,function(){
				$('.subject.active .online').fadeIn(200);
				});
			// removing id before setting hash so it doesn't scroll down
			setHash($('.subject.active').attr('id')+"-nettstudie");
			}
		return false;
		});
	





	// temp toggle for sub nav subject select
	// if clicked subject is already active do not toggle subject instead go for online offline switch
	$('#subnav a:not(.nettstudie)').click(function() {
		var hash=$(this).attr('href');
		var id='#fag-'+hash.substring(1,hash.length);
		if($(id).is(':visible')){
			if($('.subject.active .online').is(':visible')){
				$('.subject.active .online').fadeOut(300,function(){
					$('.subject.active .offline').fadeIn(200);
					});
				}
			}
		else{
			$('.subject.active').hide().removeClass('active');
			$(id).addClass('active');
			$(id).hide();
			$(id).fadeIn(300,function(){
				if($(id).is(':visible')){
					if($('.subject.active .online').is(':visible')){
						$('.subject.active .online').hide(0,function(){
							$('.subject.active .offline').fadeIn(300);
							});
						}
					}
				});
			$(this).parents('#subnav').find('.active').removeClass('active').end().end().parent().addClass('active');
			//switch back to offline data if online data is set previously as offline link is clicked, this has to be done after switching subjects
			
			}
			$('#nav .badge').fadeOut(300);
		// removing id before setting hash so it doesn't scroll down
		setHash(hash);
		return false;
	});
	// temp toggle for sub nav subject select
	$('#subnav a.nettstudie').click(function() {
		var hash=$(this).attr('href');//retrieving id from hash link
		var id='#fag-'+hash.substring(1,hash.length-11);
		if($(id).is(':visible')){	
			if($('.subject.active .offline').is(':visible')){
				$('.subject.active .offline').fadeOut(300,function(){
					$('.subject.active .online').fadeIn();
					});
				}
			}
		else{
			$('.subject.active').hide().removeClass('active');
			$(id).addClass('active');
			//$('.subject.active .offline').hide();
			//$(id).hide();
			$(id).fadeIn(300,function(){
				//switch back to online data if online data is set previously as offline link is clicked, this has to be done after switching subjects
				if($(id).is(':visible')){	
					if($('.subject.active .offline').is(':visible')){
						$('.subject.active .offline').hide(0,function(){
							$('.subject.active .online').fadeIn(300);
							});
						}
					}
				});
			$(this).parents('#subnav').find('.active').removeClass('active').end().end().parent().addClass('active');
			}
			$('#nav .badge').fadeOut(300);
		// removing id before setting hash so it doesn't scroll down
		setHash(hash);
		return false;
	});
	
	
	$('.trigger-news,.news h3')
		.click(function(){
			$(this).parents('.news').newsExpand();
			return false;
			})
		.css('cursor','pointer');

	// run frontpage slideshow if this is the frontpage
	if($('body').attr('id') == 'frontpage') SHOWCASEinit();	
	
	// run galleria if #galleria is present
	if($('#galleria').length > 0) {
		// loading theme
		Galleria.loadTheme('/sitefiles/22/js/galleria/galleria.classic.min.js');
		// getting autoplay and time from html
		var autoplay = $("#galleria .autoplay").html()*1000;
		var transition = $("#galleria .transition").html();
		var transitionSpeed = $("#galleria .transitionSpeed").html()*1000;
		// initiating slideshow
		$("#galleria").galleria({ autoplay: autoplay, transitionSpeed: transitionSpeed, transition: ''+transition+'' });
		// showing images initially hidden
		$("#galleria img").show();
	}
	
}

