$(document).ready(function(){

	// ** Navigation ** //
	// On Hover Over
	function megaHoverOver(){
		$(this).find(".drop").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		(function($) {
		//Function to calculate total width of all ul's
		jQuery.fn.calcSubWidth = function() {
			rowWidth = 0;
			//Calculate row
			$(this).find("ul").each(function() { //for each ul...
				rowWidth += $(this).width(); //Add each ul's width together
			});
		};
	})(jQuery); 

	if ( $(this).find(".drop").length > 0 ) { //If row exists...
		var biggestRow = 0;	
		$(this).find(".drop").each(function() {	//for each row...
			$(this).calcSubWidth(); //Call function to calculate width of all ul's
			//Find biggest row
			if(rowWidth > biggestRow) {
				biggestRow = rowWidth;
			}
		});

		$(this).find(".drop").css({'width' :biggestRow}); //Set width
		$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

	} else { //If row does not exist...

		$(this).calcSubWidth();  //Call function to calculate width of all ul's
		$(this).find(".drop").css({'width' : rowWidth}); //Set Width
	}
	}
	// On Hover Out
	function megaHoverOut(){
		//$(this).find(".drop").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		//	$(this).hide();  //after fading, hide it
		//});
		$(this).find(".drop").stop().slideUp('fast');
	}

	// Set custom configurations
	var config = {
		sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		interval: 150, // number = milliseconds for onMouseOver polling interval
		over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		timeout: 100, // number = milliseconds delay before onMouseOut
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	$("#nav li .drop").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#nav li").hoverIntent(config); //Trigger Hover intent with custom configurations


	// Lagsnavigering
	$("#nav-lag").hoverIntent({
		interval: 50, // milliseconds delay before onMouseOver
		over: lagnav_show, 
		timeout: 400, // milliseconds delay before onMouseOut
		out: lagnav_hide
	});
	function lagnav_show(){
		$('#nav-lag h3').addClass('active');
		$('#nav-lag ul').slideDown('fast');
	}
	function lagnav_hide(){ 
		$('#nav-lag h3').removeClass('active');
		$('#nav-lag ul').slideUp('fast');
	}


	// ** Image gallery ** //
	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});




	// $('ul.paging').quickPager();
	// $("#imagepanel ul").wrap("<div class='imagestrip'>" + "</div>");
	
	// CP Dropdown
//	$('#cp>span').pulse({
//    	backgroundColor: ['#999', '#ccc', '#ddd', '#eee', '#fff'],
//		opacity: [1, 1],
//	}, {
//    	duration: 1000, // duration of EACH individual animation
//    	times: 1, // Will go three times through the pulse array [0,1]
//    	easing: 'linear', // easing function for each individual animation
//	});
//
	$("#cp>span").hoverIntent({
		interval: 50, // milliseconds delay before onMouseOver
		over: cp_show, 
		timeout: 400, // milliseconds delay before onMouseOut
		out: cp_hide
	});
	function cp_show(){
		$('#cp h5').addClass('active');
		$('#cp ul').slideDown('fast');
		$('#cp .logout').show();
	}
	function cp_hide(){ 
		$('#cp h5').removeClass('active');
		$('#cp ul').slideUp('fast');
		$('#cp .logout').hide();
	}




	// ** Blogger ** //
	$('#aside .blogg ul').hide(); //Hide/close all containers
	$('#aside .blogg h4:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

	$('#aside .blogg h4').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('#aside .blogg h4').removeClass('active').next().slideUp('fast'); //Remove all "active" state and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
		}
		return false; //Prevent the browser jump to the link anchor
	});




	// ** RIL TV ** //
	$("#riltv").hoverIntent({
		interval: 50, // milliseconds delay before onMouseOver
		over: riltv_show, 
		timeout: 400, // milliseconds delay before onMouseOut
		out: riltv_hide
	});
	function riltv_show(){
		$('#riltv>span').addClass('active');
		$('#riltv ul').slideDown('fast');
	}
	function riltv_hide(){ 
		$('#riltv>span').removeClass('active');
		$('#riltv ul').slideUp('fast');
	}




	// ** Meta deling ** //
	$("#content .meta dl").hoverIntent({
		interval: 50, // milliseconds delay before onMouseOver
		over: dl_show, 
		timeout: 400, // milliseconds delay before onMouseOut
		out: dl_hide
	});
	function dl_show(){
		$('#content .meta dl dt').addClass('active');
		$('#content .meta dl dd').slideDown('fast');
	}
	function dl_hide(){ 
		$('#content .meta dl dt').removeClass('active');
		$('#content .meta dl dd').slideUp('fast');
	}




	// ** Fancybox - Generell ** //
	$("a.fancyimg").fancybox({ 
		'zoomSpeedIn': 300, 
		'zoomSpeedOut': 300,
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.9
	});

	// ** Fancybox - Tekst ** //
	$("a.fancytext").fancybox({
		'frameWidth': 1000,
		'frameHeight': 695,
		'overlayShow': true,
		'hideOnContentClick': true
	});

	// ** Fancybox - Film ** //
	$("a.fancyfilm").fancybox({
		'frameWidth': 550,
		'frameHeight': 450,
		'overlayShow': true,
		'hideOnContentClick': false
	});




	// ** Droplist ** //
	$('.droplist h3').next().hide();
	$('.droplist h3:first').addClass('active').next().show();

	$('.droplist h3').click(function(){
		if( $(this).next().is(':hidden') ) {
			$('.droplist h3').removeClass('active').next().slideUp('fast');
			$(this).toggleClass('active').next().slideDown();
		}
		return false;
	});




	// ** Nivo Slider ** //
	$('.nivo').nivoSlider({
		effect: 'random', //Specify sets like: 'fold,fade,sliceDown'
		animSpeed: 500,
		pauseTime: 3000,
		startSlide: 0, //Set starting Slide (0 index)
		directionNav: false, //Next &amp; Prev
		directionNavHide: false, //Only show on hover
		controlNav: false, //1,2,3...
		controlNavThumbs: false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav: false, //Use left &amp; right arrows
		pauseOnHover: true, //Stop animation while hovering
		manualAdvance: false, //Force manual transitions
		captionOpacity: 0.8 //Universal caption opacity
	});

});

