



$(document).ready(function() {
	imageItemLayout();
	textItemLayout();
	bulletinItemLayout();
	newsletterSubscribe();
	init_rotator("div.image-rotator", "div.image");
	init_rotator("div.mwr-element-rotator", ".mwr-element");

	inputDefaultValue(".form-element input[type='text']");
	initMwrSelector("select.mwr-selector");
});

function imageItemLayout() {
	var items = $("ul.image-gallery li");
	var elms = new Array();
	var y = 0;
	var h = 0;
	for (var i = 0; i < items.length; i++) {
		y = parseInt($(items[i]).attr("offsetTop"));
		h = Math.max(h, parseInt($(items[i]).attr("offsetHeight")));
		elms[elms.length] = items[i];

		// if last item in row, set heights
		if (i == items.length - 1 || $(items[i + 1]).attr("offsetTop") > y) {
			$(elms).height(h);
			elms.length = 0;
			h = 0;
		}
	}
}

function textItemLayout() {
	var items = $("#content-area.section-front-page div.text-section");
	var elms = new Array();
	var y = 0;
	var h = 0;
	for (var i = 0; i < items.length; i++) {
		y = parseInt($(items[i]).attr("offsetTop"));
		h = Math.max(h, parseInt($(items[i]).attr("offsetHeight")));
		elms[elms.length] = items[i];

		// if last item in row, set heights
		if (i == items.length - 1 || $(items[i + 1]).attr("offsetTop") > y) {
			$(elms).height(h);
			elms.length = 0;
			h = 0;
		}
	}
}

function bulletinItemLayout() {
	var items = $(".BNoticeHolder");
	var elms = new Array();
	var y = 0;
	var h = 0;
	for (var i = 0; i < items.length; i++) {
		y = parseInt($(items[i]).attr("offsetTop"));
		h = Math.max(h, parseInt($(items[i]).attr("offsetHeight")));
		elms[elms.length] = items[i];

		// if last item in row, set heights
		if (i == items.length - 1 || $(items[i + 1]).attr("offsetTop") > y) {
			$(elms).height(h);
			elms.length = 0;
			h = 0;
		}
	}
}

/* input field defaultValue/empty toggle */
function inputDefaultValue(str) {
	$(str).focus(
			function() {
				if ($(this).attr("value") == $(this).attr("defaultValue")) $(this).attr("value", "");
			}
		).blur(
			function() {
				if ($(this).attr("value") == "") $(this).attr("value", $(this).attr("defaultValue"));
			}
		);
}

function newsletterSubscribe() {

	var $subscribeButton = $('#newsletterSubscribe');

	if ($subscribeButton.length) {

		$subscribeButton.click(function() {

			var subscribeURL = 'http://www.anpdm.com/public/process-subscription-form.aspx\
?formId=474758437747465043\
&pf_FormType=OptInForm\
&pf_OptInMethod=SingleOptInMethod\
&pf_CounterDemogrFields=1\
&pf_CounterMailinglists=4\
&pf_AccountId=4152\
&pf_ListById=1\
&pf_Version=2\
&Submit=Abonner';
			var userData = '';

			userData += '&pf_SubscriberName=' + $('#pf_SubscriberName').val();
			userData += '&pf_Email=' + $('#pf_Email').val();
			userData += '&pf_Demographicfield1=' + $('#pf_Demographicfield1').val();

			if ($('#pf_MailinglistName1:checked').length)
				userData += '&pf_MailinglistName1=9268';
			if ($('#pf_MailinglistName2:checked').length)
				userData += '&pf_MailinglistName2=9265';
			if ($('#pf_MailinglistName3:checked').length)
				userData += '&pf_MailinglistName3=9264';
			if ($('#pf_MailinglistName4:checked').length)
				userData += '&pf_MailinglistName4=9266';

			userData += '&pf_DeliveryFormat=' + $('#pf_DeliveryFormat option:selected').val();

			window.open(subscribeURL + userData,'subscribe','width=300, height=300');

			return false;

		})
	}
}


$.extend($.LightboxClass.prototype,
    {
    	files: {
    		js: {
    			lightbox: '/js/jquery.lightbox.packed.js'
    		},
    		css: {
    			lightbox: '/css/jquery.lightbox.css'
    		},
    		images: {
    			prev: '/images/lightbox/prev.gif',
    			next: '/images/lightbox/next.gif',
    			blank: '/images/lightbox/blank.gif',
    			loading: '/images/lightbox/loading.gif'
    		}
    	},
    	show_linkback: false
    });

$.extend($.LightboxClass.prototype,
    {
    	text: {
    		image: 'Billede',
    		of: 'af',
    		close: 'Luk X',
    		closeInfo: 'Du kan ogs&aring; lukke ved at klikke uden for billedet.',
    		help: {
    			close: 'Klik for at lukke',
    			interact: 'Hold musen over for interaktion'
    		}
    	}
    });



// rotator 
var rotator_id = 0;
function init_rotator(str, item) {
	var sid = rotator_id;
	$(str).each(function() {
		var o = $(this).get(0);
		o.sid = "rotator" + sid;
		o.current = -1;
		o.currentDepth = null;
		o.timer = 0;

		o.seconds = 8;
		var sClass = $(this).attr("class").split(" ");
		for (var i = 0; i < sClass.length; i++) {
			if (sClass[i].indexOf("seconds-") != -1) {
				o.seconds = parseFloat(sClass[i].substr(8));
				break;
			}
		}

		o.setAttribute("id", o.sid);

		o.elms = $(this).find(item).get();

		if ($(this).find("div.rotator-controls:first").length > 0) {
			var oControls = $(this).find("div.rotator-controls:first").get(0);
			o.links = new Array();
			if (o.elms.length > 1) {
				for (var i = 0; i < o.elms.length; i++) {
					var link = document.createElement("a");
					link.setAttribute("href", "javascript:void(0)");
					link.sid = o.sid;
					link.num = i;
					link.onclick = function() { rotator_show(this.sid, this.num) };
					link.innerHTML = i + 1;

					oControls.appendChild(link);

					o.links[i] = link;
				}
			}
		} else {
			o.links = false;
		}
		rotator_show(o.sid, 0);
		sid++;
	});
	rotator_id = sid;
}

function rotator_show(id, n) {
	var o = $("#" + id).get(0);

	if (o.current != n) {
		if (o.timer) clearTimeout(o.timer);

		// reset all images to their default zIndex
		for (var i = 0; i < o.elms.length; i++) {
			//o.elms[i].style.zIndex = i;
			if (o.links) o.links[i].className = "";
		}
		/*if (o.current>=0) fadeOut(o.images[o.current]);*/

		// set active image higher, and fade in
		if (o.links) o.links[n].className = "active";
		o.currentDepth = (o.currentDepth == null) ? o.elms.length + 10 : o.currentDepth + 1;
		setAlpha(o.elms[n], 0);
		o.elms[n].style.zIndex = o.currentDepth;
		fadeIn(o.elms[n]);

		o.current = n;
		eval("o.timer = setTimeout(\"rotator_showNext('" + id + "')\"," + (o.seconds * 1000) + ")");
	}
}
function rotator_showPrev(id) {
	var o = document.getElementById(id);
	rotator_show(id, (o.current == 0) ? o.elms.length - 1 : o.current - 1);
}
function rotator_showNext(id) {
	var o = document.getElementById(id);
	rotator_show(id, (o.current + 1) % o.elms.length);
}
function fadeIn(o, n) {
	if (n == undefined) var n = 0;
	setAlpha(o, Math.min(n, 100));
	if (n <= 100) {
		var t = setTimeout(function() {
			fadeIn(o, n + 10);
		}, 20);
	}
}

function fadeOut(o, n) {
	if (n == undefined) var n = 100;
	setAlpha(o, Math.max(n, 0));
	if (n >= 0) {
		var t = setTimeout(function() {
			fadeOut(o, n - 10);
		}, 20);
	}
}

function setAlpha(o, n) {
	o.style.opacity = n / 100;
	o.style.MozOpacity = n / 100;
	o.style.KhtmlOpacity = n / 100;
	o.style.filter = "alpha(opacity=" + n + ")";
	window.status = n;
}

//function initMwrSelector(str) {
//	$(str).change(function() {
//		$(this).parents(".mwr-element:first").find("div.section").hide().filter("#"+$(this).find("option:selected").val()).show();
//	});
//}

function initMwrSelector(str) {
	$(str).change(function() {
		$(this).parents(".mwr-element:first").find("div.section").hide();
		if ($(this).find("option:selected").val() != "")
			$(this).parents(".mwr-element:first").find("div.section").filter("#" + $(this).find("option:selected").val()).show();
	});
}


function showImgInFrame(str) {
	alert(str);
}

function checkEmail(ctrl) {
	var o = document.getElementById(id);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(o.value)) {
		return (true)
	}
	alert("Invalid E-mail Address! Please re-enter.")
	return (false)
}

function checkAll(myForm) {
	if (myForm.mailtxt_name.value == '' || myForm.mailtxt_name.value == 'Dit navn') {
		alert("Indtast navn.")
		return false;
	}
	if (myForm.mailtxt_phone.value == '' || myForm.mailtxt_phone.value == 'Dit telefonummer') {
		alert("Indtast telefonnummer.")
		return false;
	}
	return checkEmail(myForm);
}

function checkEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.mailtxt_email.value)) {
		return (true)
	}
	alert("Forkert email.")
	return (false)
}
