var hoverElms = new Array();
function hover(elm) {
	if(!hoverElms[elm]) {
		hoverElms[elm] = $(elm).css('background-color');
	}
	$(elm).hover(
	function () {
		$(this).css('background-color', hoverElms[elm]);
	},
	function () {
		$(this).css('background-color', '#000000');
	}
	);
}
function updateCity() {
	$.getJSON("/new/ajax/city.php", {action: 'getCityFromPostalCode', postalCode: $('#postalCode').val()},
	function(data){
		$('#cityId').val(data.id);
		$('#city').val(data.name);
	});
}
function popupPage(url) {
	popupWidth = 800; // set the popup width
	widthHeightRatio = 1.618;
	popupHeight = popupWidth / widthHeightRatio;

	leftVal = (screen.width / 2) - (popupWidth / 2);
	topVal = 100;
	var page = window.open(url, '', 'width=' + popupWidth + ', height = ' + popupHeight + ', scrollbars=yes, resizable=no, left=' + leftVal + ', top=' + topVal);
	page.focus();
}
function popupImage(filename, imageWidth) {
	popupWidth = imageWidth + 40; // set the popup width
	widthHeightRatio = 1.618;
	popupHeight = popupWidth / widthHeightRatio;

	leftVal = (screen.width / 2) - (popupWidth / 2);
	topVal = 100;
	var page = window.open('/popupImage.php?filename=' + filename + '&width=' + imageWidth, '', 'width=' + popupWidth + ', height = ' + popupHeight + ', scrollbars=no, status=0, menubar=0, location=0, toolbar=0, directories=0, resizable=yes, left=' + leftVal + ', top=' + topVal);
	page.focus();
}
function nl2br(str) {
	return str.replace(/([^>])\n/g, '$1<br />\n');
}
function subtractVat(elmId) {
	exVat = roundNumber($('#' + elmId).val() * 0.8, 2);
	$('#' + elmId).val(exVat);
}
function roundNumber(num, dec) {
	var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10,dec);
	return result;
}
function validateMail(mail) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(mail)) {
		return true;
	} else {
		return false;
	}
}

var attachedFiles = new Array();

function drawAttachedFiles() {
	if(attachedFiles.length > 0) {
		var html = '<ul style="list-style-type:none;margin:0;padding:0;"';
		for(var i in attachedFiles) {
			html += '<li>';
			html += '<input type="hidden" name="attachedFiles[]" value="' + attachedFiles[i]['id'] + '" />';
			html += attachedFiles[i]['filename'];
			html += '<a href="javascript:void(0)" onclick="deattachFile(' + attachedFiles[i]['id'] + ')"><img src="images/delete.png" alt="Fjern vedhæftet fil" /></a>';
			html += '</li>';
		}
		html += '</ul>';
	} else {
		html = 'Ingen';
	}

	$('#files').html(html);
}
function attachFile(fileId, filename) {
	if(!isAttached(fileId)) attachedFiles.push({id : fileId, filename : filename});

	drawAttachedFiles();
}
function deattachFile(fileId) {
	var idx = isAttached(fileId);
	if(idx) {
		attachedFiles.splice(idx, 1);
	}
	drawAttachedFiles();
}
function isAttached(fileId) {
	for(var i in attachedFiles) {
		if(attachedFiles[i]['id'] == fileId) return i;
	}
	return false;
}
function submenu(elm) {
	if($(elm).hasClass('openItem') && $($(elm)).next().hasClass('openLevel')) {
		$(elm).removeClass('openItem');
		$($(elm)).next().stop(true, true).hide('blind', {}, 500).removeClass('openLevel');
		$.cookie('openLevels', null, { path: '/', domain: '.markiseeksperten.dk' });
	} else {
		$(elm).addClass('openItem');
		
		// creating cookie to remember tree view
		var cookieStr = '';
		$(elm).parents('.openLevel').each(function() {
			cookieStr += '_' + $(this).attr('id');
		});
		cookieStr += '_' + $($(elm)).next().attr('id');
		$.cookie('openLevels', cookieStr, { path: '/', domain: '.markiseeksperten.dk' });
		
		// animating treeview
		$($(elm)).next().stop(true, true).show('blind', {}, 500).addClass('openLevel');
	}
}

$(document).ready(function() {
	$('.box').mouseover(function() {
		$(this).addClass('boxOver');
	}).mouseout(function() {
		$(this).removeClass('boxOver');
	});
	
	// dynamically changes the height of textareas
	$("textarea").keypress(function (e) {
		lines = $(this).val().split("\n");
		if(lines.length > $(this).attr('rows')) {
			$(this).attr('rows', lines.length + 1);
		}
	});
	
	// move c2a box up and down
	if($("#callToActionBox").length) {
		var posOrig = $("#callToActionBox").position().top;
		$("#callToActionBox").css('top', $(window).scrollTop() > posOrig ? ($(window).scrollTop() - posOrig) + 20 : 0);
		
		$(window).scroll(function () {
			var top = $(window).scrollTop() > posOrig ? ($(window).scrollTop() - posOrig) + 20 : 0;
			$("#callToActionBox").stop().animate({
				top: top + "px"
			}, 200 );
		});
	}
	
	// add hover class to buttons
	$('.btn').mouseover(function(e) {
		var className = $(this).attr('class').match(/btn\-[a-z\-]+/i);
		$(this).addClass(className + '-hover');
	}).mouseout(function(e) {
		var matches = $(this).attr('class').match(/btn\-[a-z\-]+\-hover/gi);
		if(matches && matches.length > 0) {
			if(matches.length == 1) {
				$(this).removeClass(matches.toString());
			} else {
				for(var i in matches) {
					$(this).removeClass(matches[i]);
				}
			}
		}
	});
});
