loadMenu = function() {
	var id = "_kpmenu-main";
	$("body").append("<div id=\"" + id + "\"></div>");
	synchLoad("/menu/menu.php", $("#" + id));
}
loadCSS = function() {
	var id = "_kpmenu-css";
	$("head").append("<style id=\"" + id + "\"></style>");
	synchLoad("/menu/css/menu.css", $("#" + id));
}
synchLoad = function(url, target) {
	jQuery.ajax({
		url : url,
		success : function(result) {
			target.html(result);
		},
		async : false
	});

}
initDropdowns = function() {
	$('#_kpmenu-main .nav li').hover(function() {
		$('ul', this).slideDown(120);
	}, function() {
		$('ul', this).slideUp(120);
	});
}
initSlideOuts = function() {
	$('#_kpmenu-main .nav li.slideOut a').toggle(function() {
		var target = $('div', $(this).parent()).first();
		var height = target.outerHeight() + 2;
		target.animate({
			top : -height,
			opacity : 1
		}, {
			queue : false,
			duration : 300
		})
	}, function() {
		var target = $('div', $(this).parent()).first();
		var height = target.outerHeight() + 2;
		target.animate({
			top : height,
			opacity : 0
		}, {
			queue : false,
			duration : 300
		})
	});
}
initIcons = function() {
	$('._kpmenu-rollover').hover(function() {
		//		$('.hint', this).removeClass("hidden");
		$('.hint', this).fadeIn(200);
		$(".hint", this).css('left', $(this).outerWidth() / 2 - $(".hint", this).outerWidth() / 2);
		var offsetLeft = $(".hint", this).offset().left;
		if(offsetLeft < 0) {
			$(".hint", this).css('left', parseInt($(".hint", this).css('left')) - offsetLeft);
		}
		$(this).css('background-image', "url(" + $(this).attr('hover') + ")");
	}, function() {
		$('.hint', this).addClass("hidden");
		$('.hint', this).fadeOut(200);
		// $('.hint', this).addClass("hidden");
		$(this).css('background-image', "url(" + $(this).attr('normal') + ")");
	});

	$('._kpmenu-rollover').each(function() {
		$('.hint', this).addClass("hidden");
		$(this).css('background-image', "url(" + $(this).attr('normal') + ")");
	})
}
initCenter = function() {
	$('._kpmenu-center').each(function() {
		var top = ($(window).height() - $(this).outerHeight()) / 2;
		var left = ($(window).width() - $(this).outerWidth()) / 2;
		$(this).css({
			position : 'absolute',
			margin : 0,
			top : (top > 0 ? top : 0) + 'px',
			left : (left > 0 ? left : 0) + 'px',
			backgroundImage : 'url(/gfx/rounded.php?shadeHeight=60&height=' + $(this).outerHeight() + '&width=' + $(this).outerWidth() + '&color1=EEE&color2=EEE&opacity=1)'
		});
	});
}
initCompanyNumber = function() {
	$('#company_number').keyup(function() {
		var orgnr = $(this).val().replace("-", "");
		if(orgnr.length != 10)
			return;
		if(orgnr[2] < 2) {// Personnummer / enskild firma
			$('#company_type').val("Enskild firma");
			return;
		}
		var types = new Array();

		types[2] = "Stat, landsting, kommun, församling";
		types[5] = "Aktiebolag";
		types[6] = "Enkelt bolag";
		types[7] = "Ekonomisk förening";
		types[8] = "Ideell föreningar, stiftelse";
		types[9] = "Handelsbolag, kommanditbolag, enkelt bolag";

		if(types[orgnr[0]] == undefined) {
			$('#company_type').val("Ogiltligt organisationsnummer");
			return;
		}
		$('#company_type').val(types[orgnr[0]]);

	});
}
initPostalCode = function() {
	$('#location_postalcode').keyup(function() {
		if($('#location_country').val() != "SE")
			return;

		var postalcode = $(this).val().replace(" ", "");
		if(postalcode.length != 5) {
			$('#location_postaladdress').val("");
			return;
		}
		$.getJSON('/dynamic/postnr.php', 'nr=' + postalcode, function(json) {
			if(json.status_code != 0) {
				$('#location_postaladdress').val("Ogiltligt postnummer");
				return;
			}
			$('#location_postaladdress').val(json.result);
		});
	});
}
showRegistration = function() {
	$('#_kpmenu-main ._kpmenu-register').fadeIn();
	initCenter();
}
hideRegistration = function() {
	$('#_kpmenu-main ._kpmenu-register').fadeOut();
}
showPasswordReset = function() {
	$('#_kpmenu-main ._kpmenu-popup').fadeIn();
	initCenter();
}
hidePasswordReset = function() {
	$('#_kpmenu-main ._kpmenu-popup').fadeOut();
}
errorNotification = function(message) {
	var img = $("<img />");
	img.attr("src", "gfx/error.png");
	img.attr("message", message);
	img.mouseover(function() {
		var popup = $('<div>' + $(this).attr('message') + '</div>');
		popup.attr('class', 'errorPopup');
		$(this).before(popup);
	});
	img.mouseout(function() {
		$(this).prevAll(".errorPopup").remove();
	});
	return img;
}
$(document).ready(function() {
	loadMenu();
	loadCSS();
	//	initDropdowns();
	initSlideOuts();
	initIcons();
	initCenter();
	initCompanyNumber();
	initPostalCode();

	$('#_kpmenu-main ._kpmenu-register form').validate({
		rules : {
			user_firstname : {
				required : true
			},
			user_lastname : {
				required : true
			},
			user_gender : {
				required : true
			},
			user_email : {
				required : true,
				email : true
			},
			user_password : {
				required : true,
				minlength : 6
			},
			company_name : {
				required : true
			},
			company_number : {
				required : true
			},
			company_type : {
				required : true
			},
			location_streetaddress : {
				required : true
			},
			location_postalcode : {
				required : true
			},
			location_postaladdress : {
				required : true
			},
			location_country : {
				required : true
			}

		},
		messages : {
			user_firstname : {
				//				required : errorNotification("Du måste fylla i förnamn")
				required : errorNotification("Du måste fylla i förnamn")
			},
			user_lastname : {
				required : errorNotification("Du måste fylla i efternamn")
			},
			user_gender : {
				required : errorNotification("Du måste fylla i kön")
			},
			user_email : {
				required : errorNotification("Du måste fylla i e-post"),
				email : errorNotification("Ogiltlig e-post")
			},
			user_password : {
				required : errorNotification("Du måste fylla i lösenord"),
				minlength : errorNotification("Lösenordet måste vara minst 6 tecken långt")
			},
			company_name : {
				required : errorNotification("Du måste fylla i företagsnamn")
			},
			company_number : {
				required : errorNotification("Du måste fylla i organisationsnummer")
			},
			company_type : {
				required : errorNotification("Du måste fylla i företagstyp")
			},
			location_streetaddress : {
				required : errorNotification("Du måste fylla i gatuadress")
			},
			location_postalcode : {
				required : errorNotification("Du måste fylla i postnummer")
			},
			location_postaladdress : {
				required : errorNotification("Du måste fylla i postadress")
			},
			location_country : {
				required : errorNotification("Du måste fylla i land")
			}
		},
		errorPlacement : function(error, element) {
			element.before(error);
		}
	});
});

