var _values={
	sted:"",
	eiendomstype:"",
	prisgruppe:"",
	navn:"",
	telefon:"",
	epost:""
};
var np = {
	init : function(options) {
		options=$.extend({ page:"" }, options);
		var $this=this;
		switch(options.page) {
			case "vip" : 
				$this.pages.vip();
			break;
		}
	},
	pages : {
		vip : function() {
			
			Dropdown.init({
			 	handle : "#sted .dropdown", 
			 	content : "#sted_content", 
			 	initial : "STED", 
			 	onInit : function(parent, content, initialValue) {
					$(content).css("top", $(parent).position().top+33)
					.find(".outerarea").unbind("click").click(function() {
								$(parent).html($(this).html());
								_values.sted=$(this).attr("id");
								isSliding=true;
								$(content).slideUp("fast", function() {
									isSliding=false;
									$(parent).removeClass("open");
								});
								return false;
							}).hover(function() {
								$(this).addClass("hover");
								if(!isSliding) {
									$(parent).html($(this).html());
								}
							}, function() {
								$(this).removeClass("hover");
								if(!isSliding && $(content).css("display")=="block") {	
									$(parent).html(initialValue);
								}
							});

			 	},
			 	onOpen : function(parent, content, initialValue) {
					$(parent).addClass("open");
			 	}, 
			 	onClose : function(parent) {
					$(parent).removeClass("open");
				 }
			});
			
			
			Dropdown.init({
			 	handle : "#boligtype .dropdown", 
			 	content : "#boligtype_content", 
			 	initial : "BOLIGTYPE", 
			 	onInit : function(parent, content, initialValue) {
					$(content).css("top", $(parent).position().top+33)
					.find(".outerarea").unbind("click").click(function() {
								$(parent).html($(this).html());
								_values.eiendomstype=$(this).attr("id");
								isSliding=true;
								$(content).slideUp("fast", function() {
									isSliding=false;
									$(parent).removeClass("open");
								});
								return false;
							}).hover(function() {
								$(this).addClass("hover");
								if(!isSliding) {
									$(parent).html($(this).html());
								}
							}, function() {
								$(this).removeClass("hover");
								if(!isSliding && $(content).css("display")=="block") {	
									$(parent).html(initialValue);
								}
							});

			 	},
			 	onOpen : function(parent, content, initialValue) {
					$(parent).addClass("open");
			 	}, 
			 	onClose : function(parent) {
					$(parent).removeClass("open");
				 }
			});
			
			Dropdown.init({
			 	handle : "#prisgruppe .dropdown", 
			 	content : "#prisgruppe_content", 
			 	initial : "PRISGRUPPE", 
			 	onInit : function(parent, content, initialValue) {
					$(content).css("top", $(parent).position().top+33)
					.find(".outerarea").unbind("click").click(function() {
								$(parent).html($(this).html());
								_values.prisgruppe=$(this).attr("id");
								isSliding=true;
								$(content).slideUp("fast", function() {
									isSliding=false;
									$(parent).removeClass("open");
								});
								return false;
							}).hover(function() {
								$(this).addClass("hover");
								if(!isSliding) {
									$(parent).html($(this).html());
								}
							}, function() {
								$(this).removeClass("hover");
								if(!isSliding && $(content).css("display")=="block") {	
									$(parent).html(initialValue);
								}
							});
			 	},
			 	onOpen : function(parent, content, initialValue) {
					$(parent).addClass("open");
			 	}, 
			 	onClose : function(parent) {
					$(parent).removeClass("open");
				 }
			});
			this.schema.init({schemawrapper : "#vip_wrapper .col", submit : "#vip_wrapper .col .send" });
		},
		schema : {
			init : function(options) {
				var $this=this;
				options=$.extend({ schemawrapper : "", submit:"input[type='submit']"}, options);
				$.each($("input"),function() {
					$(this).val($(this)[0].defaultValue);
				});
				if(options.schemawrapper) {
					$(options.schemawrapper).find("input").focus(function() {
			  		if($(this).val()==$(this)[0].defaultValue) {
			  			$(this).val("");
			  		}
			  	}).blur(function() {
			  		if($(this).val()=="") {
			  			$(this).val($(this)[0].defaultValue);
			  		}
			  	});
				}
				var isSending=false;
				$(options.submit).click(function() {
					_values.navn=escape($("#navn").val());
					_values.telefon=escape($("#telefon").val());
					_values.epost=escape($("#epost").val());
					
					if(_values.navn==$("#navn")[0].defaultValue) {
						_values.navn="";
					}
					if(_values.telefon==$("#telefon")[0].defaultValue) {
						_values.telefon="";
					}
					if(_values.epost==$("#epost")[0].defaultValue) {
						_values.epost="";
					}
					_values=$.extend({ type:"vip"}, _values);
					if(_values.sted && _values.eiendomstype && _values.prisgruppe && _values.navn && (_values.telefon || $this.isEmail(_values.epost))) {
						if(!isSending) {
							isSending=true;
							$.get("ax.asp", _values, function(s) {
								isSending=false;
								alert("Takk for din henvendelse!");
								window.location.reload();
							});
						}
					} else {
						alert("Vennligst fyll ut feltene som mangler!");
					}
					return false;
				}).hover(function() {
					$(this).addClass("hover");
				}, function() {
					$(this).removeClass("hover");
				});
			},
			isEmail : function(email) {
				return email.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)==null ? false : true;
			}
		}
	}
};
var isSliding=false;

var Dropdown = {
	init : function(options) { //handlebar, cont, shortname, iVal, oninit, onopen, onclose) {
			var isHandleClick = false;
			
			options = $.extend({ initial : "Tittel" }, options);
			
			if(options.onInit) {
				options.onInit(options.handle, options.content, $(options.handle).html());
			}
			$(options.content).css({display:"none"});
			$(options.handle).html(options.initial).unbind("click").click(function() {
				
				$(".dropdowncontent").not(options.content).slideUp("fast");
				if($(options.content).css("display")=="none") {
					isHandleClick = true;
					$(options.content).slideDown("fast", function() {
						if(options.onOpen) options.onOpen(options.handle, options.content, $(options.handle).html());
					});
				} else { 
					return false;
				}
			});
			
			$("div,input").mousedown(function(event){
				if($(options.content).css("display")=="block") {
					if(!isHandleClick && $(this).attr("id")!=options.content.replace("#","")) {
						$(options.content).slideUp("fast", function() {
							if(options.onClose) options.onClose(options.handle, options.content, $(options.handle).html());		
						});
					}
					isHandleClick=false;
					return false;
				}
			});
	}
};
