// Intranett
if (global.modus == 'intranett') {
	$(document).ready(function(){
		$("#browser").treeview({
			control:"#sidetreecontrol",
			collapsed: true,
			unique: false,
			persist: "cookie"
		});
	});
	
	animatedcollapse.addDiv('forklaring', 'fade=0,speed=400');
	animatedcollapse.addDiv('forklaring2', 'fade=0,speed=400');
	animatedcollapse.init();
}

// Bilde rollover
$(document).ready(function(){
	$('.simpleRollover').hover(function() {  
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));  
		$(this).attr('hover', currentImg);  
	}, function() {  
		var currentImg = $(this).attr('src');  
		$(this).attr('src', $(this).attr('hover'));  
		$(this).attr('hover', currentImg);  
	});  
});

// Galleri - Space galleriet
if (global.meny_sys == 6 && ( (config.galleri_vis_space == 'gruppe' && get.mappe_id) || (config.galleri_vis_space == 'enkel' && get.vis) ) ) {
	$(function() {
		$('#myGallery').spacegallery({
			loadingClass: 'loading',
			perspective: '100',
			duration: '800'
		});
	});
}

// Galleri - JQuery1 - Galleria
if (global.meny_sys == 6 && (config.galleri_type == 'jquery1' || config.galleri_type == 'jquery1_singel')) {
	$(document).ready(function(){
		
		$('ul.galleria_gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#galleria_main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				//image.attr('title','Neste >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				);
			}
		});
	});
}

// Artikkel Galleri - JQuery1 - Galleria2
if (config.artikkel_galleri_type == 'jquery1') {
	$(document).ready(function(){
		
		$('ul.galleria2_gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#galleria2_main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				//image.attr('title','Neste >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				);
			}
		});
	});  
}

// Mosaic plugin for slides
jQuery(function($){
    $('.js_mosaic_slides').mosaic({
        animation: 'slide',
        speed: config.mosaic_speed                    
    });
});

// Slides
$(document).ready(function() {
    
    // custom easing called "custom"
    $.easing.custom = function (x, t, b, c, d) {
    	var s = 1.70158; 
    	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
    }

		
	// initialize scrollable with mousewheel support
	$("#browsable_slides") .scrollable({ 	vertical: (config.slides == "true"),
											easing: config.slides_scroller_easing,
                                            speed: parseInt(config.slides_hastighet),
                                            loop:true, 
                                            keyboard:false,
											clickable:false, 
											size: parseInt(config.slides_scroller_antall_bilder),
											circular: true
							})
							// NB: .circular må muligens være i .scrollable som: "circular: true," i nyere versjon av Jquery Tools
                    		.autoscroll({ 	interval: parseInt(config.slides_interval), 
											steps: parseInt(config.slides_scroller_antall_slides), 
											autoplay: (config.slides_scroller_autoplay == "true") 
							});
                            if (config.slides_scroller_navigator == 'ja') {
                                $("#browsable_slides").navigator();
                            }	
});

// Slides - Fading
if (config.slides_type == 'fading') {	
	$(document).ready(
		function(){
			$("#fading_slides:hidden").show();
			$('#fading_slides').innerfade({
				speed: parseInt(config.slides_hastighet),
				timeout: parseInt(config.slides_interval),
				animationtype: 'fade',
				type: 'sequence'
			});
		}
	);
}

// Banner - Fading
if (config.banner_fade_aktiv == 'ja') {	
	$(document).ready(
		function(){
			$("#portfolio:hidden").show();
			$('#portfolio').innerfade({
				speed: config.banner_fade_tid_fade,
				timeout: config.banner_fade_tid_vis,
				animationtype: 'fade',
				type: config.banner_fade_type,
				containerheight: config.banner_fade_hoyde
			});
		}
	);
}

// Artikkel galleri fade
if(config.artikkel_galleri_type == 'fade-loop') {		
	$(document).ready(
		function(){
			$("#artikkel_galleri_fade_loop:hidden").show();
			$('#artikkel_galleri_fade_loop').innerfade({
				speed: parseInt(config.artikkel_galleri_fade_tid_fade),
				timeout: parseInt(config.artikkel_galleri_fade_tid_vis),
				animationtype: 'fade',
				type: config.artikkel_galleri_fade_type,
				containerheight: config.artikkel_galleri_fade_hoyde
			});
		}
	);
}

// Banner - Bakgrunn Fading
if (config.banner_bakgrunn_fade_aktiver == 'ja') {
	$(document).ready(function(){
		$.fn.supersized.options = {  
			startwidth: config.banner_bakgrunn_fade_min_bredde,  
			startheight: config.banner_bakgrunn_fade_min_hoyde,
			vertical_center: 1,
			slideshow: 1,
			navigation: 0,
			thumbnail_navigation: 0,
			transition: config.banner_bakgrunn_fade_type, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover: 0,
			slide_counter: 0,
			slide_captions: 0,
			slide_interval: config.banner_bakgrunn_fade_intervall  
		};
		
		$('#supersize').supersized(); 
	});
}

// Artikkel - Fading
if (config.artikkel_fade_aktiv == 'ja') {	
	$(document).ready(
		function(){
			$("#browsable_artikkel:hidden").show();
			$('#browsable_artikkel').innerfade({
				speed: parseInt(config.artikkel_fade_tid_fade),
				timeout: parseInt(config.artikkel_fade_tid_vis),
				animationtype: 'fade',
				type: config.artikkel_fade_type,
				containerheight: config.artikkel_fade_hoyde
			});
		}
	);
}


// LMS Kallender
if (get.lms_handling == 'leggtil_oppgave' || get.lms_handling == 'endre_oppgave') {
	$(document).ready(function(){
		$('#datepicker').datepicker({
			dateFormat: 'dd.mm.yy', 
			monthNames: ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember'],
			dayNamesMin: ['Sø', 'Ma', 'Ti', 'On', 'To', 'Fr', 'Lø'],
			nextText: 'Neste', 
			prevText: 'Tilbake',
			buttonText: 'Velg dato'
		});
	});
}

// Kurs
if (get.kurs_handling == 'kjop' || get.bestill || get.betaling_handling == 'kasse') {
	$(document).ready(function() {
		inputField = $('#postnummerInput');
		outputElement = $('#postnummerResult');

		inputField.keyup (function() {
			if ($(this).val().length == 4) {
				$.getJSON('http://fraktguide.bring.no/fraktguide/postalCode.json?pnr='+ $(this).val() +'&callback=?',
					function(data){
					   outputElement.text(data.result);
					}
				);
			}
		});
	});
}

// Nettbutikk
if (get.butikk_handling == 'kasse') {
   $(document).ready(function() {
      inputField = $('#postnummerInput');
      outputElement = $('#postnummerResult');
            
      inputField.keyup(function() {
         if ($(this).val().length == 4) {
            $.getJSON('http://fraktguide.bring.no/fraktguide/postalCode.json?pnr='+ $(this).val() +'&callback=?',
				function(data){
				   outputElement.text(data.result);
				}
			);
         }
         else {
            outputElement.text(SP_butikk_finner_poststed_selv);
         }
      });

	  inputField2 = $('#postnummerInput2');
      outputElement2 = $('#postnummerResult2');
            
      inputField2.keyup(function() {
         if ($(this).val().length == 4) {
            $.getJSON('http://fraktguide.bring.no/fraktguide/postalCode.json?pnr='+ $(this).val() +'&callback=?',
				function(data){
				   outputElement2.text(data.result);
				}
			);
         }
         else {
            outputElement2.text(SP_butikk_finner_poststed_selv);
         }
      });
   });
}

// Artikkelbilde skygge
if(config.konto_artikkelbilde_skygge == 'ja') {
	$(document).ready(function(){
		$("img.artikkelbilde").wrap(
		"<div class='skygge_wrap0'><div class='skygge_wrap1'><div class='skygge_wrap2'>" + 
		"<div class='skygge_wrap3'></div></div></div></div>");
	});
}

// Banner kollone bilde skygge
if(config.banner_skygge == 'ja') {
	$(document).ready(function(){
		$(".banner_kolonne img")
			.wrap(
				"<div class='skygge_wrap0'><div class='skygge_wrap1'><div class='skygge_wrap2'>" + 
				"<div class='skygge_wrap3'></div></div></div></div>");
	});
}

// Artikkel Designet Scroller
if(config.artikkel_scroller_design == 'ja') {
	$(document).ready(function(){
		$('#scrolldesign').jScrollPane({
			showArrows:true,
			scrollbarWidth: config.artikkel_scroller_design_bredde,
			arrowSize: config.artikkel_scroller_design_pil_hoyde
		});
	});			
}

// Slidende boks
if (get.butikk_handling == 'kasse') {
	animatedcollapse.addDiv('forklaring', 'fade=0,speed=400');
	animatedcollapse.addDiv('forklaring2', 'fade=0,speed=400');
	//animatedcollapse.addDiv('jason', 'fade=1,height=80px')
	//animatedcollapse.addDiv('kelly', 'fade=1,height=100px')
	//animatedcollapse.addDiv('michael', 'fade=1,height=120px')
	//animatedcollapse.addDiv('cat', 'fade=0,speed=400,group=pets')
	//animatedcollapse.addDiv('dog', 'fade=0,speed=400,group=pets,persist=1,hide=1')
	//animatedcollapse.addDiv('rabbit', 'fade=0,speed=400,group=pets,hide=1')
	animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
		//$: Access to jQuery
		//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
		//state: "block" or "none", depending on state
	}
	animatedcollapse.init();
}

// Horizontal slide menu
if (config.meny_horizontal_slide.triggerEvent
	&& config.meny_horizontal_slide.eventFx
	&& config.meny_horizontal_slide.followMainLink
) {
	$(document).ready(function(){
		/**
		 * @author David Steinsland
		 * @copyright 2010
		 * Menu plugin for a two level navigation.
		 */
		
		// Stages: fade in, show, slide down, slide left
		var $this;
		var _event = config.meny_horizontal_slide.triggerEvent; // click|mouseenter
		var _changer = config.meny_horizontal_slide.eventFx; // show|fade|slide|slideleft
		var _followMainLink = config.meny_horizontal_slide.followMainLink;
		var _current;
		
		if ($('div.slidemenu-horizontal-sub p.current').length) {
			_current = $('div.slidemenu-horizontal-sub p.current').attr('id').replace('sub','');
		} else {
			_current = 0;
		}
		
		$('p.slidemenu-horizontal a').bind (_event, function () {
			var $id = $(this).attr('id').replace('main', '');
			$this = 'div.slidemenu-horizontal-sub p#sub' + $id;
			
			// If the hovered element has no sub menu, or is the one already being shown => abort
			if (!$($this).length || _current == $id) {
				return true;
			}
			
			$('div.slidemenu-horizontal-sub p').hide();
			
			var obj = $($this);
			
			$('div.slidemenu-horizontal-sub p.slidemenu-horizontal-sub-selected').removeClass('current slidemenu-horizontal-sub-selected');
			
			obj.addClass('current').stop(true, true);
			_current = $id;
			
			switch (_changer) {
				case 'show':
					obj.show();
				break;
				case 'fade':
					 obj.fadeIn();
				break;
				
				case 'slide':
					obj.slideDown(1000);
					$('div.slidemenu-horizontal-sub p').not(obj.get(0)).stop(true,true).hide();
				break;
				
				case 'slideleft':
					obj.show('slide', {direction: 'right', distance:'100px'}, 500);
					$('div.slidemenu-horizontal-sub p').not(obj.get(0)).stop(true,true).hide();
				break;
			}
			
			return eval(_followMainLink);
		});
	});
}
