$(function(){
	$('#footer #reference, #nyhedsliste li, #moduler .fast-nyhed')
		.each(function() {
			var $this = $(this);
			if ($this.find('a[href]').length > 0) {
				$(this)
					.addClass('clickable')
					.click(function(e) {
						e.preventDefault();
						var link = $(this).find('a').attr('href');
						location.href = link;
					});
			}
		});
	$('a.betingelserlink, a.visitkortlink')
		.ceebox({htmlGallery:false,imageGallery:false,videoGallery:false,titles:false, htmlWidth: 600, htmlRatio: 1.2});
	$('.udpensling a, a.pakkebeskrivelseslink')
		.ceebox({htmlGallery:false,imageGallery:false,videoGallery:false,titles:false, htmlWidth: 700, htmlHeight: $(window).height()-100});
	$('.nyhedsbrevoversigt a')
		.ceebox({htmlGallery:false,imageGallery:false,videoGallery:false,titles:false, htmlWidth: 780, htmlHeight: $(window).height()-100});
	$('a.videopopuplink')
			.ceebox({htmlGallery:false,imageGallery:false,videoGallery:false});
		
	//$('a[href="#"]').bind('mouseup', function() { this.blur(); return false;});	

	if ($('body#forside').length > 0) {
		var ih = $('#info').height(),
			kh = $('#info #koeb').height(),
			nh = $('#info #nyheder').height(),
			eh = $('#info #ekstrainfo').height(),
		newh = Math.max(kh,nh,eh)
		if (ih < newh) {$('#info').css('height',newh);}
	}

	$('.betaling-formular')
		.submit(function(e) {
			e.preventDefault();
			var $form = $(this);
			$('#acceptfejl')
				.remove();
			if ($form.find('#input_accepterbetingelser').is(':checked')) {
				submitToPopup($form[0]);
			} else {
				var $label = $form.find('label[for=input_accepterbetingelser]'),
					$fejl = $('<p/>');
				$fejl
					.addClass('meddelelse fejl')
					.attr('id','acceptfejl')
					.html('Du skal angive, at du accepterer handelsbetingelserne!')
					.insertAfter($form);
			}
		})

	$('form.loginform').not('.noajax')
		.submit(function(e) {
			e.preventDefault();
			var $form = $(this),
				$panel = $form.parent('.sidepanel'),
				headline = $panel.find('h2').html();
			if ($panel.length == 0) $panel = $form.parent('div');
			if ($form.find('input[name=brugernavn]').val() != '' && $form.find('input[name=brugernavn]').val() != '' ) {
				var params = $form.serialize(),
					action = $form.attr('action');
				$panel
					.overlayScreen()
					.load(action,params,function(response,status,xhr) {
						if (response.indexOf('meddelelse fejl') == -1) {
							$panel
								.addClass('aktiv');
						}
						$panel
							.overlayScreen({show:false})
							.prepend(headline != null ? '<h2>' + headline + '</h2>' : '');
					})
			}
		})
	$('form.glemtloginform')
		.submit(function(e) {
			e.preventDefault();
			var $form = $(this),
				$panel = $form.parent('div');
				params = $form.serialize(),
				action = $form.attr('action');
				$panel
					.overlayScreen()
					.load(action,params,function(response,status,xhr) {
						$panel
							.overlayScreen({show:false})
					})
		});
		
	$('#sidebar .kontakt-formular')
		.submit(function(e) {
			e.preventDefault();
			var $form = $(this),
				$panel = $form.parent('.sidepanel'),
				headline = $panel.find('h2').html(),
				formOk = true;
			$form
				.find('.obligatorisk')
					.each(function(){
						var $felt = $(this);
						if ($felt.val() == '') {
							formOk = false;
							$felt
								.parent('li')
									.addClass('fejl');
						} else {
							$felt
								.parent('li')
									.removeClass('fejl');
						}
					})
			if (formOk) {
				var params = $form.serialize(),
					action = $form.attr('action');
				$panel
					.overlayScreen()
					.load(action,params,function(response,status,xhr) {
						$panel
							.overlayScreen({show:false})
							.prepend('<h2>' + headline + '</h2>');
					})
			}
		})
	$('.logoutlink')
		.click(function(e) {
			e.preventDefault();
			var $link = $(this),
				url = $link.attr('href'),
				$panel = $link.parents('.sidepanel'),
				headline = $panel.find('h2').html();
			$panel
				.overlayScreen()
				.load(url,function() {
					$panel
						.removeClass('aktiv')
						.overlayScreen({show: false})
						.prepend('<h2>' + headline + '</h2>');
				})
		})

	$('.linkboks, .footerindhold.reference')
		.addClass('clickable')
		.click(function(e) {
			e.preventDefault();
			var url = $(this).find('a:first').attr('href');
			if (url != '') {
				location.href = url;
			}
		})

	var $styresystem = $('#input_styresystem option:selected'),
		windows,
		bit,
		version;

	if ( $styresystem.val() == '-' ) {
		windows = getPlatform();
		bit = getBit();
		if (windows == 'xp') {
			if (bit == 'x64') {
				version = 'Windows XP (64bit)';
			} else {
				version = 'Windows XP (32bit)';
			}
		} else if (windows == 'vista') {
			if (bit == 'x64') {
				version = 'Windows Vista (64bit)';
			} else {
				version = 'Windows Vista (32bit)';
			}
		} else if (windows == 'win7') {
			if (bit == 'x64') {
				version = 'Windows 7 (64bit)';
			} else {
				version = 'Windows 7 (32bit)';
			}
		} else {
			version = 'Andet';
		}
		$('#input_styresystem option')
			.attr('selected',false);
		$('#input_styresystem option[value="' + version + '"]')
			.attr('selected',true);
	}


	var $hjaelpLink = $('<span/>');
	$hjaelpLink
		.addClass('hjaelplink clickable')
		.html('forklaring');
	$('.hjaelp')
		.hide()
		.before($hjaelpLink)
		.find('a')
			.ceebox({htmlGallery:false,imageGallery:false,videoGallery:false,titles:false, htmlWidth: 600, htmlRatio: 1.2});
	$('.hjaelplink')
		.click(function(){
			$(this).siblings('.hjaelp').slideToggle();
		})
		
	$('select.sideskifter')
		.change(function() {
			$(this)
				.parents('form')
					.submit();
		})
		.siblings('button')
			.hide();

	var $socialbar = $('#socialbar'),
		$tipboks = $('#tipboks'),
		$fb = $('<ul><li></li></ul>');
	$tipboks
		.appendTo($fb.find('li'))
	$socialbar
		.css('left','-' + $socialbar.width() + 'px')
		.show()
		.animate({'left':0},750)
		.find('#fb')
			.hover(function(){
				var o = $(this).offset();
				$fb
					.show();
			}, function() {
				$fb.hide();
			})
			.find('a')
				.css('cursor','default')
				.click(function(e){ e.preventDefault() })
				.end()
			.append($fb)


// End ready			
});

function submitToPopup(formref) {
	var newwin;
	window.name = 'WinKASPro';
	newwin = window.open('','paywin','scrollbars,status,width=550,height=600');
	formref.submit();
	newwin.focus();  
}

function getPlatform(){
	var uAgent = navigator.userAgent.toLowerCase(),
		strOs;

//Windows
	if (uAgent.indexOf('win')!=-1) {
// Win Vista
		if ((uAgent.indexOf('windows nt 6.0')!=-1) || (uAgent.indexOf('vista')!=-1)){
			strOs = 'vista';
		}
// Win 7
		else if ((uAgent.indexOf('windows nt 6.1')!=-1) || (uAgent.indexOf('windows nt 7.0')!=-1)){
			strOs = 'win7';
		}
// Win XP
		else if ((uAgent.indexOf('windows nt 5.1')!=-1) || (uAgent.indexOf('winxp')!=-1)){
			strOs = 'xp';
		}
// Unknown
		else {
			strOs = '';
		}
	}
//Unknown platform
	else {
		strOs = '';
	}

return strOs;

}

function getBit(){
	var uAgent = navigator.userAgent.toLowerCase(),
		strBit;

	if ((uAgent.indexOf('wow64')!=-1) || (uAgent.indexOf('x64')!=-1) || (uAgent.indexOf('win64')!=-1) || (uAgent.indexOf('ia64')!=-1)) {
		strBit = 'x64'
	}
	else {
		strBit = 'x68'
	}
	
	return strBit
}

