(function($) {
	var scrollbarWidth = 0;
	$.getScrollbarWidth = function() {
		if ( !scrollbarWidth ) {
			if ( $.browser.msie ) {
				var $textarea1 = $('<textarea cols="10" rows="2"></textarea>')
						.css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body'),
					$textarea2 = $('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>')
						.css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body');
				scrollbarWidth = $textarea1.width() - $textarea2.width();
				$textarea1.add($textarea2).remove();
			} else {
				var $div = $('<div />')
					.css({ width: 100, height: 100, overflow: 'auto', position: 'absolute', top: -1000, left: -1000 })
					.prependTo('body').append('<div />').find('div')
						.css({ width: '100%', height: 200 });
				scrollbarWidth = 100 - $div.width();
				$div.parent().remove();
			}
		}
		return scrollbarWidth;
	};
})(jQuery);

$(document).ready(function() {
	
	$('#openIframe').click(function() {
		openIframe();
	});
	
	$('#close').live('click', function() {
		var a = confirm('Er du sikker på at du vil avslutte?');
		if(a) {
			$('#skjema').fadeOut(function() {
				$(this).remove();
			});
			$('#overlay').fadeOut(function() {
				$(this).remove();
			});
		}
	});
	
	function openIframe() {
		var viewportwidth;
		var viewportheight;
		 
		if (typeof window.innerWidth != 'undefined')
		{
			viewportwidth = window.innerWidth,
			viewportheight = window.innerHeight
		}
		 	
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
		{
			viewportwidth = document.documentElement.clientWidth,
			viewportheight = document.documentElement.clientHeight
		}
		 
		else
		{
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
			viewportheight = document.getElementsByTagName('body')[0].clientHeight
		}
		if(viewportheight < $('body').height()) {
			viewportheight = $('body').height();
		}
		
		viewportwidth = viewportwidth - $.getScrollbarWidth();
		
		$('body').append('<div id="overlay"></div>');
		$('#overlay').css({
			'width':viewportwidth,
			'height':viewportheight
		});
		
		$('#overlay').append('<div id="skjema" class="round"><div id="top"><div id="close">X</div></div><div style="clear: both;"></div><iframe src="http://kff.antoma.no/skjema/index.php" width="680" height="100%" frameBorder="0" scrolling="no"></iframe><div style="clear: both;"></div></div>');
		
		$('#skjema').css({
			'left':(viewportwidth / 2) - ($('#skjema').width() / 2)
		});
		
		$('#overlay').fadeIn();
		$('#skjema').fadeIn();
	}
});
