
jQuery.fn.equalHeights = function(settings) {
	
	var maxHeight = 0;
	
	$(this).each(function(){
		var height = $(this).offset().top + $(this).outerHeight();
		maxHeight = Math.max(height, maxHeight);
	});
	
	return $(this).each(function() {
		$(this).height(maxHeight - $(this).offset().top + ($(this).height() - $(this).outerHeight()));
	});
}


jQuery(document).ready(function() {
	
	// undermeny dropdown
	$('div.menuDropdown div.activePage').click(function() {
		$(this).parent().find('ul#subMenu').slideToggle(200);
	});
	
	
	// nyheter
    jQuery(".newsReel").jcarousel({
        scroll: 1,
        auto: 10,
        vertical: false,
        wrap: 'both',
        initCallback: newsReel_initCallback,
        itemVisibleInCallback: {
            onBeforeAnimation: newsReel_itemVisibleInCallbackBeforeAnimation,
            onAfterAnimation:  newsReel_itemVisibleInCallbackAfterAnimation
        },
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    
    
	// galleri
    jQuery("div.gallery ul.images").jcarousel({
        scroll: 1,
        auto: 5,
        wrap: 'both',
        initCallback: gallery_initCallback,
        itemVisibleInCallback: {
            onBeforeAnimation: gallery_itemVisibleInCallbackBeforeAnimation,
            onAfterAnimation:  gallery_itemVisibleInCallbackAfterAnimation
        },
        buttonNextHTML: null,
        buttonPrevHTML: null
    });    
    
    
    // justerer kolonnehøyder
    $('.contentWrapperLast').equalHeights();
    $('.cFrontpage .offers li a').equalHeights();
    
	adjustRightCol();
	adjustLeftCol();
	var timer = setInterval(checkColHeights, 250);
	
	// Justerer bakgrunnsbilde
	resizeBackground();
    
	$(window).resize(function() {
		resizeBackground();
	});
	
//	// Sticky online booking
//	var onlineBookingContainer = $('#r360Cart').parent()
//	var scroller = new StickyScroller("#r360container",
//	        {
//	            start: 300,
//	            end: 1800,
//	            interval: 300,
//	            range: 100,
//	            margin: 100
//	        }); 
	
	
	$('div.webcamthumbs a').click(function(e){
		
		e.preventDefault();
		
		var imageContainer = $('div.largeImage.webcam');
		var imageSrc = $(this).attr('href');
		var player = $('#live');
		
		showWebcamImage(imageContainer, player, imageSrc);	
		
		$(this).parent().addClass('active').siblings().removeClass('active');
		return false;
		
	});
	
	$('a.liveStream').click(function(e) {
		
		e.preventDefault();

		var imageContainer = $('div.largeImage.webcam');
		var player = $('#live');
		
		showWebcamStream(imageContainer, player);
		return false;
		
	});
	     
});


function showWebcamStream(imageContainer, player) {
	imageContainer.find('img').hide();
	imageContainer.find('a').hide();
	player.show();
}

function showWebcamImage(imageContainer, player, url) {
	imageContainer.find('img').show().attr('src', url);
	imageContainer.find('a').show();
	player.hide();
}


function resizeBackground() {

	var img = $('#background img');

	var pageWidth = $('#background').outerWidth();
	var pageHeight = $('#background').outerHeight();
	var pageXyRatio = pageWidth/pageHeight;

	var imgHeight = 800;
	var imgWidth = 1200;
	var imgXyRatio = imgWidth / imgHeight;

	if (imgXyRatio < pageXyRatio) {
		img.removeAttr('height');
		img.attr('width', pageWidth);
	}
	else {
		img.removeAttr('width');
		img.attr('height', pageHeight);
	}
	
}


//window.onload = function() {adjustCols()};

// Nyhetshjul
function newsReel_initCallback(carousel) {
	
	var nav = $(carousel.container).find('div.nav');
	nav.find('.prev a').click(function() {carousel.prev(); return false;});
	nav.find('.next a').click(function() {carousel.next(); return false;});
	
	nav.find('.items li a').click(function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).text())); 
		return false;}
	);
	
};

function newsReel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
	$(carousel.container).find('.nav .items li a').removeClass('active');
};

function newsReel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
	$(carousel.container).find('.nav .items li a').removeClass('active').eq(idx - 1).addClass('active');
};



//Galleri
function gallery_initCallback(galleryUl) {
	
	var nav = $(galleryUl.container).parents('div.gallery').find('div.galleryNav ul');
	nav.find('li.prev a').click(function() {galleryUl.prev(); return false;});
	nav.find('li.next a').click(function() {galleryUl.next(); return false;});
	
};

function gallery_itemVisibleInCallbackBeforeAnimation(galleryUl, item, idx, state) {
	
	var textDiv = $(galleryUl.container).parents('div.gallery').find('div.text');
	textDiv.stop().fadeOut('fast');
};

function gallery_itemVisibleInCallbackAfterAnimation(galleryUl, item, idx, state) {

	var textDiv = $(galleryUl.container).parents('div.gallery').find('div.galleryText');
	var navDiv = $(galleryUl.container).parents('div.gallery').find('div.galleryNav');

	navDiv.find('span.imgNo').html(idx);
	
	var text = $(item).find('img').attr('alt'); 
	if (text != '') {
		textDiv.html(text).stop().fadeTo('fast', 1);
	}

};

function checkColHeights() {
	var l = $("#leftCol .contentWrapperLast");
	var r = $("#rightCol .contentWrapperLast");
	
	if (l.length == 0 || r.length == 0) {
		return;
	}

	if (l[0].clientHeight > l.height()) {
		l.css('height', 'auto');
		adjustLeftCol();
		adjustRightCol();
	}

	if (r[0].clientHeight > r.height()) {
		r.css('height', 'auto');
		adjustRightCol();
		adjustLeftCol();
	}

}

function adjustLeftCol() {
	var col = $("#leftCol .contentWrapperLast");
	adjustCol(col);	
}

function adjustRightCol() {
	var col = $("#rightCol .contentWrapperLast");
	adjustCol(col);
}

function adjustCol(col) {

	if (col == null || col.length == 0) {
		return;
	}
	
	var footer = $("#footerWrapper");
	
	var oldHeight = col.height();
	var newHeight = col.height()+(footer.offset().top - col.offset().bottom);

	if (Math.abs(oldHeight - newHeight) < 12) {
		return;
	}
	
	if (footer.offset().top > col.offset().bottom) {
		col.height(col.height()+(footer.offset().top - col.offset().bottom));
	}
}

function log(text) {
	if (window.console && window.console.log) {
		window.console.log(text);
	}
} 




