var jsb = {

	init : function(uri) {
		this.uri.set(uri);
		this.admin.check();
		this.search.global.init();
		this.schema.initSellerSchema();
		this.menus.init();
		this.fixes.prospectbox();
	},
	uri : {
		set : function(uri) {
			if(uri) {
				$("body").append("<input type=\"hidden\" id=\"__uri\" value=\"" + uri + "\" />");
			}
		},
		get : function() {
			return $("#__uri").val();	
		},
		is : function(val) {
			return this.get().indexOf(val)>=0;
		}
	}, 
	setIndex : function(i) {
		jsb.cooky.set("flashindex", i);
	},
	getIndex : function() {
		var index = jsb.cooky.get("flashindex");
		return (index ? index : 1);
	},
	initObject : function() {
		this.schema.initObjSchema();
		$("#_removehaze").unbind("click").click(function() {
			jsb.haze.hide(function() {
				jsb.schema.cleanProspectSchema();	
			});
			return false;
		});
		this.schema.initProspectSchema();
	},
	flash : {
		goto : function(loca) {
			window.location=loca;
		},
		get : function(src, flashvars, bgColour, width, height) {
			return AC_FL_RunContent_String(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
				'width', width,
				'height', height,
				'src', src,
				'flashvars',flashvars + "&random=" + Math.random(),
				'quality', 'best',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'showall',
				'wmode', 'transparent',
				'devicefont', 'false',
				'id', src,
				'bgcolor', bgColour,
				'name', src,
				'menu', 'true',
				'allowFullScreen', 'false',
				'allowScriptAccess','always',
				'movie', src,
				'salign', ''
				);
		},
		addBrokers : function() {
			$("#meglere_coverflow").html(this.get("gfx/flash/meglere", "", "#f9f9f8", 678, 316)); 
		},
		addSold : function() {
			$("#objectrightpad").html(this.get("swf/nokkelanim_flash8", "", "#f9f9f8", 130, 244)); 
		},
		addGallery : function() {
			$("#gallery").html(this.get("gfx/flash/objekt_bildef", "xmlfile=gallery.xml","#ffffff", 680, 495));
		},
		addKeys : function(id) {
			$(id).html(this.get("swf/nokkelanim_regularis", "", "#f9f9f8", 130, 244));
		},
		addBolig : function() {
			$("#bolig_coverflow").html(this.get("gfx/flash/atilaa", "", "#f9f9f8", 678, 316)); 
		},
		addTopBanner : function(options) {
			options=$.extend({ 
				banner:"gfx/flash/topbanner",
				width:672,
				height:82
			}, options);
			$("#topbanner").html(this.get(options.banner, "", "#f9f9f8", options.width, options.height)); 
		}
	},
	fixes : {
		prospectbox : function() {
			$("#prospectwrapper").height($("#aboutobj").height());
		},
		xmlpoker : function(xmlpath) {
			jsb.ax(xmlpath, "random=" + Math.random(), function(msg) {
				alert(msg);
			});
		},
		rightColumn : function(w) {
			if(!w)
				$("#main_right").height($("#main_left").height());
			else 
				$("#main_right").height(w);
		},
		replaceNorwegian : function(txt) {
			return encodeURIComponent(txt);
		}
	},
	haze : {
		init : function(callback) {
			$("#imperialhaze").unbind("click").click(function(event) {
				if($(event.target).attr("id")==$(this).attr("id")) {//parent will not be affected when clicking on children. hirr hirr.
					jsb.haze.hide(callback);
				}
			});
		},
		show : function(callback, id) {
			if(id) {
				$("#imperialhaze .hazeoverlay").hide();
				$(id).show();
			}
			$("#imperialhaze").fadeIn("fast", function() {
				if(callback) callback();
			});
		}, 
		hide : function(callback, id) {
			
			if(!jsb.schema.isSending()) {
				$("#imperialhaze").fadeOut("fast", function() {
					$("#imperialhaze .hazeoverlay").hide();
					if(callback) callback();
						
					});
			}
		} 
	},
	cooky : {
		is : function(id) {
			return this.get(id)!="";
		},
		add : function(id, val) {
			var pos = $.cookie(id);
			if(!pos) {
				pos=0;
			}
			$.cookie(id, parseInt(pos) + val);
		},
		set : function(id, val) {
			$.cookie(id, val);
		},
		get : function(id) {
			var v=$.cookie(id);
			return (v!=undefined ? v : "");
		}
	},
	schema : {
		send : function(dim, type, dummymsg) {
			var err=false;
				if($("#nName").val()=="") {
					err=true;
					$("#_err_nName").show();
				} else {
					$("#_err_nName").hide();
				}
				if(!jsb.schema.isEmail($("#nEpost").val())) {
					err=true;
					$("#_err_nEpost").show();
				} else {
					$("#_err_nEpost").hide();
				} 
				if($("#nTlf").val()=="") {
					err=true;
					$("#_err_nTlf").show();
				} else {
					$("#_err_nTlf").hide();
				}
				if(err) {
					
				}	else {
			//		if(confirm("Følgende informasjon vil bli sendt:\n\nNavn: "+$("#nName").val()+"\nAdresse: "+($("#nHomeAddr").val()!=""?$("#nHomeAddr").val():"Ikke angitt")+"\nE-post: "+($("#nEpost").val()!=""?$("#nEpost").val():"Ikke angitt")+"\nTelefon: "+($("#nTlf").val()!=""?$("#nTlf").val():"Ikke angitt")+"\n\n\Vil du sende informasjonen?")) {
						$(dim).show();
						jsb.ax("ax", "type=" + type +
						"&oppdragsnr=" + $("#oppdragsnr").val() + 
						"&name=" + encodeURIComponent($("#nName").val()) + 
						"&address=" + encodeURIComponent($("#nHomeAddr").val()) + 
						"&email=" + encodeURIComponent($("#nEpost").val()) + 
						"&phone=" + encodeURIComponent($("#nTlf").val()) + 
						"&message=" + encodeURIComponent($("#nMessage").val()), function(msg) {
								switch(msg) {
									case "1" :
										$("#nName").val("");
										$("#nHomeAddr").val("");
										$("#nEpost").val("");
										$("#nTlf").val("");
										$("#nMessage").val(dummymsg);
										
										$("#_response").html("Takk for henvendelsen!").fadeIn("fast", function() {
											$(this).animate({opacity:1.0},5000, function() {
												$(this).fadeOut("fast");
											});
										});
										
										$(dim).hide();
									break;
									case "3" : 
										$(dim).hide();
										$("#_response").html("Ugyldig e-post!").fadeIn("fast", function() {
											$(this).animate({opacity:1.0},5000, function() {
												$(this).fadeOut("fast");	
											});
										});
									break;
							}
						});
				//	}
				}
				return false;
		},
		initSellerSchema : function() {
			$("#cmdSendSellerSchema").unbind("click").click(function() {
				jsb.schema.send("#sellerdim", "sendsellermail", $("#_sellertext").val());
				return false;
			});
		},
		initObjSchema : function() {
			jsb.haze.init(function() {
				jsb.schema.cleanProspectSchema();
			});
			$("#_objectformsubmit").unbind("click").click(function() {
				jsb.schema.send("#objectdim", "sendbrokermail", $("#_contactmetext").val() + " " + $("#_address").val());
				 return false;
			});
		},		
		initTypfSchema : function(typf) {
			$("#cmdTypfSchema").unbind("click").click(function() {
				var err=false;
				if($("#nName").val()=="") {
					err=true;
					$("#_err_nName").show();
				} else {
					$("#_err_nName").hide();
				}
				if(!jsb.schema.isEmail($("#nEpost").val())) {
					err=true;
					$("#_err_nEpost").show();
				} else {
					$("#_err_nEpost").hide();
				} 
				if($("#nTlf").val()=="") {
					err=true;
					$("#_err_nTlf").show();
				} else {
					$("#_err_nTlf").hide();
				}
				
				if(!$(".inputblock .typf:checked").attr("id")) {
					err=true;
					$("#_err_nTypf").show();
				} else {
					$("#_err_nTypf").hide();
				}
				
				if(err) {
					
				}	else {
			//		if(confirm("Følgende informasjon vil bli sendt:\n\nNavn: "+$("#nName").val()+"\nAdresse: "+($("#nHomeAddr").val()!=""?$("#nHomeAddr").val():"Ikke angitt")+"\nE-post: "+($("#nEpost").val()!=""?$("#nEpost").val():"Ikke angitt")+"\nTelefon: "+($("#nTlf").val()!=""?$("#nTlf").val():"Ikke angitt")+"\n\n\Vil du sende informasjonen?")) {
						//$(dim).show();
						$(".inputblock label").hide();
						$("#sellerdim").show();
						jsb.ax("ax", "type=" + typf +
						"&typf=" + $(".inputblock .typf:checked").attr("id") + 
						"&name=" + encodeURIComponent($("#nName").val()) + 
						"&address=" + encodeURIComponent($("#nHomeAddr").val()) + 
						"&email=" + encodeURIComponent($("#nEpost").val()) + 
						"&phone=" + encodeURIComponent($("#nTlf").val()) + 
						"&message=" + encodeURIComponent($("#nMessage").val()), function(msg) {
								switch(msg) {
									case "1" :
										$("#nName").val("");
										$("#nHomeAddr").val("");
										$("#nEpost").val("");
										$("#nTlf").val("");
										$("#nMessage").val("");
										$(".inputblock .typf").attr("checked", false);
										$("#_response").html("Takk for henvendelsen!").fadeIn("fast", function() {
											$(this).animate({opacity:1.0},5000, function() {
												$(this).fadeOut("fast");
											});
										});
										
										$("#sellerdim").hide();
									break;
									case "3" : 
										$("#sellerdim").hide();
										$(".inputblock label").show();
										$("#_response").html("Ugyldig e-post!").fadeIn("fast", function() {
											$(this).animate({opacity:1.0},5000, function() {
												$(this).fadeOut("fast");	
											});
										});
									break;
							}
							$(".inputblock label").show();
						});
				//	}
				}
				return false;
			});
		},
		setSending : function(sending) {
			if(!sending) {
				$("#_sending").val("0");
			} else {
				$("#_sending").val("1");
			}
		}, 
		isSending : function() {
			return ($("#_sending").val()=="1");
		},
		initProspectSchema : function() {
			$("#_hazeSubmit").unbind("click").click(function() {
			var err=false;
				if($("#_hazeFirstname").val()=="") {
					$("#_err_hazeFirstname").show();
					err=true;
					//sx+="-Vennligst fyll ut fornavn.\n";
				} else {
					$("#_err_hazeFirstname").hide();
				}
				if($("#_hazeSurname").val()=="") {
					err=true;
					$("#_err_hazeSurname").show();
					//sx+="-Vennligst fyll ut etternavn.\n";
				} else {
					$("#_err_hazeSurname").hide();
				}
				if(!jsb.schema.isEmail($("#_hazeEmail").val())) {
					err=true;
					$("#_err_hazeEmail").show();
					//sx+="-Vennligst fyll ut korrekt e-post.\n";
				} else {
					$("#_err_hazeEmail").hide();
				}
				if(err) {
					//alert("Få prospektet til " + $("#_address").val().toUpperCase() + " tilsendt på e-post.\n\n" + sx);
				}	else {
			//		if(confirm("Få prospektet til " + $("#_address").val().toUpperCase() + " tilsendt på e-post.\n\nFølgende informasjon vil bli sendt:\n\nNavn: "+$("#_hazeFirstname").val()+" " + $("#_hazeSurname").val() + "\nE-post: "+($("#_hazeEmail").val()!=""?$("#_hazeEmail").val():"Ikke angitt")+"\nTelefon: "+($("#_hazePhone").val()!=""?$("#_hazePhone").val():"Ikke angitt")+"\n\n\Vil du sende informasjonen?")) {
						$("#prospectdim").show();
						jsb.schema.setSending(true);
						jsb.ax("ax", "type=prospectmail" +
						"&oppdragsnr=" + $("#oppdragsnr").val() + 
						"&firstname=" + encodeURIComponent($("#_hazeFirstname").val()) + 
						"&surname=" + encodeURIComponent($("#_hazeSurname").val()) + 
						"&email=" + encodeURIComponent($("#_hazeEmail").val()) + 
						"&phone=" + encodeURIComponent($("#_hazePhone").val()), function(msg) {
									$("#_hazeFirstname").val("");
										$("#_hazeSurname").val("");
										$("#_hazeEmail").val("");
										$("#_hazePhone").val("");
										$("#keysoverlay").fadeIn("fast", function() {
										 $("#prospectdim").fadeOut("fast", function() {	
												jsb.schema.setSending(false);
												setTimeout(function() {
												$("#imperialhaze").fadeOut("fast", function() {
													jsb.schema.cleanProspectSchema();
													$("#keysoverlay").hide();
												});
											}, 5000);
											});	
										});
						});
			//		}
				}
			return false;
		});
		$("#_orderprospect").unbind("click").click(function() {
			jsb.haze.show();	
		});
		},
		isProspectSchemaFilled : function() {
			return ($("#_hazeFirstname").val()!="" || $("#_hazeSurname").val()!="" || $("#_hazeEmail").val()!="" || $("#_hazePhone").val()!="");
		},
		cleanProspectSchema : function() {
			$("#_err_hazeFirstname").hide();
			$("#_err_hazeSurname").hide();
			$("#_err_hazeEmail").hide();
			
			$("#_hazeFirstname").val("");
			$("#_hazeSurname").val("");
			$("#_hazeEmail").val("");
			$("#_hazePhone").val("");
		},
		hideProspectSchemaHaze : function() {
			
		},
		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;
		},
		wishPage : {
			init : function() {
				//activate this for live areas... mhmhihi..
				//jsb.search.areas("#_areawish", false, function() {
					//alert("hirr hirr");
				//});
				$("#_submitwish").unbind("click").click(function() {
					var err=false;
					if($("#_wishName").val()=="") {
						err=true;
						$("#_err_wishName").show();
						//sx+="-Vennligst fyll ut navn.\n";
					} else {
						$("#_err_wishName").hide();
					}
					if($("#_wishAddress").val()=="") {
						err=true;
						$("#_err_wishAddress").show();
						//sx+="-Vennligst fyll ut adresse.\n";
					} else {
						$("#_err_wishAddress").hide();
					}
					if($("#_wishPhone").val()=="") {
						err=true;
						$("#_err_wishPhone").show();
						//sx+="-Vennligst fyll ut telefonnummer.\n";
					} else {
						$("#_err_wishPhone").hide();
					}
					if(!jsb.schema.isEmail($("#_wishEmail").val())) {
						err=true;
						$("#_err_wishEmail").show();
						//sx+="-Vennligst fyll ut korrekt e-post.\n";
					} else {
						$("#_err_wishEmail").hide();
					}
					if(err) {
						
					}	else {
					/*	if(confirm("Følgende informasjon vil bli sendt:\n" +
						"\nOmråde: "+$("#_areawish").val() + 
						"\nBoligtype: "+$("#_typewish").val() + 
						"\nPris: "+$("#_pricegroupwish").val() + 
						"\nAreal: "+$("#_spacewish").val() + 
						"\n\nNavn: "+$("#_wishName").val() + 
						"\nAdresse: "+($("#_wishAddress").val()!=""?$("#_wishAddress").val():"Ikke angitt")+
						"\nE-post: "+($("#_wishEmail").val()!=""?$("#_wishEmail").val():"Ikke angitt")+
						"\nTelefon: "+($("#_wishPhone").val()!=""?$("#_wishPhone").val():"Ikke angitt")+
						"\n\n\Vil du sende informasjonen?")) {*/
							$("#prospectdim").show();
							jsb.schema.setSending(true);
							jsb.ax("ax", "type=wishmail" +
							"&area=" + $("#_areawish").val() + 
							"&typf=" + encodeURIComponent($("#_typewish").val()) + 
							"&price=" + encodeURIComponent($("#_pricegroupwish").val()) + 
							"&space=" + encodeURIComponent($("#_spacewish").val()) + 
							"&name=" + encodeURIComponent($("#_wishName").val()) + 
							"&address=" + encodeURIComponent($("#_wishAddress").val()) + 
							"&phone=" + encodeURIComponent($("#_wishPhone").val()) + 
							"&comment=" + encodeURIComponent($("#_wishComment").val()) + 
							"&email=" + encodeURIComponent($("#_wishEmail").val()), function(msg) {
								$("#_areawish option[index='0']").attr("selected",true);
								$("#_typewish option[index='0']").attr("selected",true);
								$("#_pricegroupwish option[index='0']").attr("selected",true);
								$("#_wishName").val("");
								$("#_wishAddress").val("");
								$("#_wishEmail").val("");
								$("#_wishPhone").val("");
								$("#_wishComment").val("");
								$("#_respontz").fadeIn("fast", function() {
										$(this).animate({opacity:1.0},5000, function() {
											$(this).fadeOut("fast");
										});
									});
							});
					//	}
					}
				});
			}
		}
	},
	search : {
		global : {
			init : function() {

//edit:
			var defaultValue = "a-kode, adresse eller megler";
				$("#globalfreetext").css({color: "#999", "font-size": "9px"});
				$("#globalfreetext").val(defaultValue);
				
				$("#globalfreetext").focus(function(){
					$("#globalfreetext").css({color: "#000", "font-size": "11px"});
					$(this).val("");
				});

				$("#globalfreetext").blur(function(){
					if($(this).val() == ""){
						$(this).val(defaultValue);
					}
				}); //end edit


				$("#globalfreetext").keyup(function(event) {
					if(event.keyCode==13) {
						jsb.search.global.action();
					}
				});
				$("#globalSearch").unbind("click").click(function() {
					jsb.search.global.action();
					return false;
				});
			},
			action : function() {
	//edit
				if($("#globalfreetext").val() == "a-kode, adresse eller megler"){
					$("#globalfreetext").val("");
				}//end edit

				if(!jsb.admin.login()) {
					jsb.cooky.set("global", true);
						jsb.search.pager.reset();
						jsb.cooky.set("area", "");
						jsb.cooky.set("communalarea", "");
						jsb.cooky.set("pricegroup", "");
						jsb.cooky.set("types","");
						jsb.cooky.set("freetext", $("#globalfreetext").val());
						window.location="eiendom";
				}
			}
		},
		saveTypes : function() {
			var str = "";
			$.each($("#checkbox input[type='checkbox']"), function(i, val) {
				if($(val).attr("checked")) {
					str+=$(val).attr("id") + ",";
				}
			});
			str=str.substring(0,str.length-1);
			
			jsb.cooky.set("types", str);
			
			return str;
		},
		fillTypes : function() {
			var t = jsb.cooky.get("types");
			$.each($("#checkbox input[type='checkbox']"), function(i, val) {
				if(t) {
					$(val).attr("checked", t.indexOf($(val).attr("id")) >= 0);
				} else {
					$(val).attr("checked", true);
				}
			});
		},	
		init : function(maxonpage, found, total) {
			
			
			
			jsb.cooky.set("maxonpage", maxonpage);
			
			$("#_freetext").val(jsb.cooky.get("freetext"));


			// edits:
			$("#freetextwrapper").hide();
			


			var pos = parseInt(jsb.search.pager.getPos());
			
			if(found < maxonpage && total < maxonpage) {
				jsb.setIndex(1);
			}
			
			if(pos<=0) {
				jsb.search.pager.previous.deactivate();
			} else {
				jsb.search.pager.previous.activate();
			}
			
			if(found < maxonpage  || (pos+1)*maxonpage >= total) { // || pos*found == total...
				jsb.search.pager.next.deactivate();	
			} else {
				jsb.search.pager.next.activate();	
			}
			
			/* Event -vs- cookie handlers */
			$("#checkbox input[type='checkbox']").unbind("click").click(function() {
				var types=jsb.search.saveTypes();
			});
			
			jsb.search.fillTypes();
			
			this.communalAreas("#_communalareas", true, function() {
				$("#_communalareas").change(function() {
					
					jsb.cooky.set("communalarea", $(this).val());
					jsb.search.areas("#_areas", true, function() {
						jsb.cooky.set("area","");
					}, encodeURIComponent($(this).val()));
				});
				$("#_communalareas option[value='" + jsb.cooky.get("communalarea") + "']").attr("selected",true);
				jsb.search.loader.hide();
				jsb.search.flashloader.hide();
				if(jsb.cooky.get("global")=="true") {
					$("#_search").click();
					jsb.cooky.set("global", false);
				}
			});
			
			this.areas("#_areas", true, function() {
				$("#_areas").change(function() {
					jsb.cooky.set("area", $(this).val());
				});
				$("#_areas option[value='" + jsb.cooky.get("area") + "']").attr("selected",true);
				jsb.search.loader.hide();
				jsb.search.flashloader.hide();
				if(jsb.cooky.get("global")=="true") {
					$("#_search").click();
					jsb.cooky.set("global", false);
				}
			});
			
			/* Set the pricegroup to correct index */
			$("#pricegroup").change(function() {
					jsb.cooky.set("pricegroup", $(this).val());
				});
			$("#pricegroup option[value='" + jsb.cooky.get("pricegroup") + "']").attr("selected",true);
			
			var searchf = function() {
				jsb.cooky.set("freetext", $("#_freetext").val());
			//	jsb.cooky.set("freetext", $("#globalfreetext").val());
				//$("#objectflow").hide();
				/* reset the pager cookie */
				jsb.search.pager.reset();
				jsb.setIndex(1);
				jsb.search.flashloader.show();
				jsb.search.loader.show();
				//$(this).attr("disabled", true);
				
				jsb.search.doSearch(function(pos, found, total) {
					//$("#_search").attr("disabled", false);
					if(found < maxonpage || (pos+1)*maxonpage >= total) { // || pos*found == total...
						jsb.search.pager.next.deactivate();	
					} else {
						jsb.search.pager.next.activate();	
					}
				});
			};
			
			$("#_search").unbind("click").click(function() {
				searchf();
				return false;
			});
			$("#_freetext").keydown(function(event) {
				if(event.keyCode==13) {
					searchf();
					return false;
				}
			});
			
			//"backstilling" the schema
			$("#_backstill").unbind("click").click(function() {
				$("#pricegroup").find("option[index='0']").attr("selected",true);
				$("#_areas").find("option[index='0']").attr("selected",true);
				$("#_communalareas").find("option[index='0']").attr("selected",true);
				$("#_freetext").val("");
				$("#checkbox input[type='checkbox']").attr("checked", true);
				
				jsb.cooky.set("freetext","");
				jsb.cooky.set("types","");
				jsb.cooky.set("area","");
				jsb.cooky.set("communalarea","");
				jsb.cooky.set("pricegroup","");
				
				return false;
			});
			jsb.search.pager.setCount(found, total);


						////$("#_freetext").val("");

			jsb.search.setFlow();
			
		},
		doSearch : function(callback) {
		 		jsb.ax("ax","type=search" +
				"&text=" + $("#_freetext").val() + 
				"&pricegroup=" + encodeURIComponent($("#pricegroup").val()) +
				"&communalarea=" + encodeURIComponent($("#_communalareas").val()) + 
				"&area=" + encodeURIComponent(($("#_areas").val()+"").replace("null","")) + 
				"&apptypes=" + encodeURIComponent(jsb.search.saveTypes()) +
				"&ordertypes=" + $("#_ordertypf").val() +
				"&pos=" + jsb.search.pager.getPos(), function(msg) {
					eval(msg);
					$("#_freetext").val("");
					if(response.found > 0) {
						jsb.search.pager.init(function() {
						//	jsb.search.pager.show();	
						});
					} else {
						//jsb.search.pager.hide();
					}
					jsb.search.updateFlow(response.found, response.total, function() {
						jsb.search.loader.hide();
						jsb.search.flashloader.hide();
						//$("#objectflow").show();
					});	
					if(callback) callback(parseInt(jsb.search.pager.getPos()), response.found, response.total);
				});
		},
		loader : {
			show : function() {
				$("#searchdim").show();
			},
			hide : function() {
				$("#searchdim").hide();
			}
		},
		flashloader : {
			show : function() {
				$("#searchdim2").show();
			},
			hide : function() {
				$("#searchdim2").hide();
			}
		},
		setFlow : function() {
			
			if(jsb.cooky.get("global")!="true") {
				$("#objectflow").html(jsb.flash.get("Eiendom", "index=" + jsb.getIndex(), "#ffffff", 678, 316));
			}
		},
		updateFlow : function(found, total, callback) {
			jsb.search.pager.setCount(found, total);
			this.setFlow();
										
			if(callback) callback();
		},
		communalAreas : function(id, useValue, callback) {
			jsb.ax("ax","type=communalareas&ordertypes=" + $("#_ordertypf").val(), function(msg) {
				eval(msg);
				var st="<option " + (useValue ? "value=''" : "") + ">Alle kommuner</option>";
	  			for(var i=0;i<response.communalareas.length;i++) {
	  				st+="<option " + (useValue ? "value='" + encodeURIComponent(response.communalareas[i].area) + "'" : "") + ">" + response.communalareas[i].area + (useValue ? " (" + response.communalareas[i].count + ")" : "") + "</option>";
	  			}
	  		$(id).empty().html(st);	
	  		if(callback) callback();
	  	});			
		},
		areas : function(id, useValue, callback, communalArea) {
			jsb.ax("ax","type=areas&ordertypes=" + $("#_ordertypf").val() + "&communalarea=" + (!communalArea ? "" : communalArea), function(msg) {
				eval(msg);
				var st="<option " + (useValue ? "value=''" : "") + ">Alle omr&aring;der</option>";
	  			for(var i=0;i<response.areas.length;i++) {
	  				st+="<option " + (useValue ? "value='" + encodeURIComponent(response.areas[i].area) + "'" : "") + ">" + response.areas[i].area + (useValue ? " (" + response.areas[i].count + ")" : "") + "</option>";
	  			}
	  		$(id).html(st);	
	  		if(callback) callback();
	  	});			
		},
		pager : {
			getPos : function() {
				if(!jsb.cooky.get("pos")) {
					jsb.search.pager.reset();
				}
				return jsb.cooky.get("pos");
			},
			cmd : {
				activate : function(id, pos) {
					$(id).unbind("click").click(function() {
							jsb.search.pager.clicked(this, pos);
						}).css("color","#88877d").hover(function() {
							$(this).css("color","#3d3a3e");
						}, function() {
							$(this).css("color","#88877d");
						});
				}, 
				deactivate  : function(id) {
					$(id).unbind("click").css("color","#cac9ba")
					.hover(function() {
						$(this).css("color","#cac9ba");
						}, function() {
							$(this).css("color","#cac9ba");
					});
				}
			},
			next : {
				activate : function() {
					jsb.search.pager.cmd.activate("#next", 1);
				}, 
				deactivate : function() {
					jsb.search.pager.cmd.deactivate("#next");
				}
			},
			previous : {
					activate : function() {
						jsb.search.pager.cmd.activate("#previous", -1);
					},
					deactivate : function() {
						jsb.search.pager.cmd.deactivate("#previous");
					}
			},
			clicked : function(parent, pos) {
				
				jsb.setIndex(1);
				jsb.search.loader.show();
				jsb.search.flashloader.show();
				//$("#objectflow").hide();
				$(parent).blur();
				jsb.cooky.add("pos", pos);
				//$(parent).attr("id") + jsb.cooky.get("pos"));
				
				if(jsb.cooky.get("pos")=="0") {
					jsb.search.pager.previous.deactivate();
				} else { //activate previous
					jsb.search.pager.previous.activate();
				}
				jsb.search.doSearch(function(pos, found, total) {
					var maxonpage=parseInt(jsb.cooky.get("maxonpage"));
					if(found < maxonpage  || (pos+1)*maxonpage >= total) {
						jsb.search.pager.next.deactivate();	
					} else {
						jsb.search.pager.next.activate();	
					}
					jsb.search.flashloader.hide();
				});
			},
			init : function(callback) {
				if(jsb.cooky.get("pos")=="0") {
					jsb.search.pager.previous.deactivate();
				} else {			
					jsb.search.pager.previous.activate();
				}
				$("#next").unbind("click").click(function() {
					jsb.search.pager.clicked(this, 1);
				});
				if(callback) callback();
			},
			reset : function() {
				jsb.cooky.set("pos","0");
			},
			show : function() {
				$("#pager").slideDown("fast");
			},
			hide : function(callback) {
				$("#pager").slideUp("fast", function() {
					if(callback) callback();
				});
			},
			hideInstantly : function(callback) {
				$("#pager").hide(function() {
					if(callback) callback();
				});
			},
			setCount : function(found, total) {
				if(total > 0) {
					
					var maxonpage=parseInt(jsb.cooky.get("maxonpage"));
					var pos=parseInt(jsb.search.pager.getPos());
					
					$("#from").html(maxonpage*(pos+1)-maxonpage+1);
					var fn=(maxonpage*(pos+1));
					$("#found").html(fn>total?total:fn);
					$("#total").html(total);
					jsb.search.pager.init(function() {
					//	jsb.search.pager.show();	
					});
				} else {
					$("#from").html(0);
					$("#found").html(0);
					$("#total").html(0);
					//jsb.search.pager.hide();
				}
			}
		}
	},
	frontpage : {
		/*fetch : function(interval, callback) {
			jsb.ax("object/random","exclude=" + jsb.cooky.get("currentObject"), function(msg) {
					eval(msg);
					var i=jsb.cooky.get("currindex")=="0" ? 1 : 0;
					var p = $("#frontobject .eiendomimg").eq(i);
						$("#mainimg", p).css("background-image", "url(" + response.image1 + ")");
						$("#top", p).css("background-image", "url(" + response.image2 + ")");
						$("#bottom", p).css("background-image", "url(" + response.image3 + ")");
					setTimeout(function() {
						jsb.frontpage.transform(response, parseInt(jsb.cooky.get("currindex")), i);
						jsb.cooky.set("currindex", i);
						if(callback) callback();
					}, interval / 2);
				});
		},*/
		init : function(objs, interval) {
			var i=0;
			$(".eiendomimg, #eiendominfo").unbind("click").click(function() {
				jsb.obj(objs[i].oppdragsnr);
			});
			
			setInterval(function() {
				i=(i<objs.length?++i:0);
				jsb.frontpage.populateNext(i, objs[i], interval);
			}, interval);
		},
		populateNext : function(i, obj, interval) {
			var n=(i%2);
			var p = $("#frontobject .eiendomimg").eq(n);
			if(obj) {
				$("#mainimg", p).css("background-image", "url(" + obj.image1 + ")");
				$("#top", p).css("background-image", "url(" + obj.image2 + ")");
				$("#bottom", p).css("background-image", "url(" + obj.image3 + ")");
				jsb.frontpage.xfade(obj, (n>0?0:1), n);
		  }
		},
		xfade : function(obj, previousIndex, nextIndex) {
			$("#frontobject .eiendomimg").eq(nextIndex).fadeIn("slow", function() {
				
				$("#heading").html((obj.overskrift1+"")!="-1" ? obj.overskrift1 : "");
				$("#info").html((obj.overskrift2+"")!="-1" ? obj.overskrift2 : "");
				$("#pris #prisval").html(obj.pris);			
				jsb.cooky.set("currentObject", obj.Oppdragsnummer);
				$("#frontobject .eiendomimg").eq(previousIndex).fadeOut("slow");
			});	
		}
	},
	ax : function(urlf, d, callback) {
		$.ajax({
			type: "POST",
		  url: urlf,
		  data : d,
			cache : false,
		  success: function(msg){
		  	callback(msg);
		  }
		 });
	},
	axJson : function(urlf, d, callback) {
		$.post(urlf, d, function(msg){
		  	callback(msg);
		  }, "script");
	},
	obj : function(obj) {
		window.location=obj;
	},
	toggle : function(settings) {
		$(settings.cmd.id).unbind("click").click(function() {
			if($(settings.container).css("display")=="none") {
				$(settings.cmd.id + " img").attr("src",settings.cmd.hide);
				$(settings.container).slideDown("fast");
			} else {
				$(settings.cmd.id + " img").attr("src", settings.cmd.show);
				$(settings.container).slideUp("fast");
			}
			$(this).blur();
		});
	},
	menus : {
		init : function() {
			this.mainmenu.init();
			this.submenu.init();
		},
		mainmenu : {
			init : function() {
				jsb.preload([
					"gfx/forside_h.jpg", 
					"gfx/eiendom_h.jpg",
					"gfx/kontakt_h.jpg"]);
				
				jsb.redirect("forside"); jsb.redirect("forside_active", "forside");
				jsb.redirect("eiendom"); jsb.redirect("eiendom_active", "eiendom");
				
				jsb.redirect("prosjekt"); jsb.redirect("prosjekt_active", "prosjekt");
			}
		},
		submenu : {
			init : function() {
				jsb.preload([
					"gfx/sub_budskjema_h.jpg",
					"gfx/sub_onske_h.jpg", 
					"gfx/sub_ameglerne_h.jpg", 
					"gfx/sub_bolig_h.jpg", 
					"gfx/sub_blogg_h.jpg"]);
				
				/*$("#onske").unbind("click").click(function() {
					window.location.href="mailto:post@ameglerne.no";
				});*/
				
				jsb.redirect("onske");
				/*$("#blogg").click(function() {
					alert("Denne siden lanseres senere");
				});*/
				//jsb.redirect("blogg");
				jsb.redirect("bolig","bolig");
				jsb.redirect("ameglerne","kontakt");
			}
		}
	},
	redirect : function(id, link) {
			$("#"+id).unbind("click").click(function() {
				if(link) {
					if(link.indexOf("http")>=0) {
						window.open(link);
					} else {
						window.location=link;
					}
				} else {
					window.location=id;
				}
			});
		},
		preload : function(imgs) {		    
      $.each(imgs, function(i, img) {
          var pimg = new Image();
          pimg.src = img;
      });
		},
		blog : {
			schema : {
				clean : function() {
					$("#_hazeName").val("");
					$("#_hazeEmail").val("");
					$("#_hazeHeading").val("");
					$("#_hazeContent").val("");
					$(".err").hide();
				}
			},
			comments : {
				updateIndexes : function() {
					$.each(".kommentar_wrap #index", function(i) {
						$(".kommentar_wrap #index").eq(i).html(i+".");
					});
				}
			},
			init : function() {
				jsb.blog.scroller.init();
				jsb.haze.init(function() {
					jsb.blog.schema.clean();
				});
				
					if(jsb.blog.hash.postInitial()) {
						jsb.blog.upperMenu.show();
						jsb.blog.open(jsb.blog.hash.post());
					} else {
						$("#blogpostwrapper2").fadeIn("fast", function() {
							jsb.blog.hash.overview();
						});
					}
				
				$("._blog_readmore, .kommentar_overskrift").unbind("click").click(function() {
					jsb.blog.open($(this).attr("id"));
				});
				$("#_blog_showlist").unbind("click").click(function() {
					jsb.blog.close();
				});
				$("#_removehaze").unbind("click").click(function() {
					jsb.haze.hide(function() {
						jsb.blog.schema.clean();
					});
					return false;
				});
				$("#cmd_blogpostcomment").unbind("click").click(function() {
					//$("#_hazeingress").html($("#_axBlogPostIngress").html().substring(0,300));
					$("#_hazeBlogCommentOverlay #_hazeingress").html("Skriv en kommentar til bloggposten " + unescape("%AB") + $("#_axBlogPostHeading").html() + unescape("%BB"));
					jsb.haze.show(function() {
						$("#_hazeName").focus();
					}, "#_hazeBlogCommentOverlay");	
				});
				$("#_cmdSendBlogComment").unbind("click").click(function() {
					if(!jsb.schema.isSending()) {
					var err=false;
					if($("#_hazeName").val()=="") {
						$("#_err_hazeName").show();
						err=true;
					} else {
						$("#_err_hazeName").hide();
					}
					if($("#_hazeHeading").val()=="") {
						err=true;
						$("#_err_hazeHeading").show();
					} else {
						$("#_err_hazeHeading").hide();
					}
					if($("#_hazeContent").val()=="") {
						err=true;
						$("#_err_hazeContent").show();
					} else {
						$("#_err_hazeContent").hide();
					}
					if(!jsb.schema.isEmail($("#_hazeEmail").val())) {
						err=true;
						$("#_err_hazeEmail").show();
					} else {
						$("#_err_hazeEmail").hide();
					}
					if(!err) {
							$("#prospectdim").show();
							jsb.schema.setSending(true);
							jsb.ax("ax", "type=blog" +
								"&s=addcomment" +
								"&postid=" + $("#_activeBlogPostId").val() +
								"&author=" + encodeURIComponent($("#_hazeName").val()) +
								"&email=" + encodeURIComponent($("#_hazeEmail").val()) +
								"&heading=" + encodeURIComponent($("#_hazeHeading").val()) +
								"&content=" + encodeURIComponent($("#_hazeContent").val()) , function(msg) {
									eval(msg);
									$("#__nocomments").remove();
									jsb.blog.comment.prepend(response.comment.id, response.comment.heading, response.comment.content, response.comment.created, function() {
										try { jsbadmin.blog.comment.addEvent(); } catch(ex) { }
										jsb.blog.comments.updateIndexes();
										jsb.haze.hide(function() {
											jsb.schema.setSending(false);
											$("#_hazeName").val("");
											$("#_hazeEmail").val("");
											$("#_hazeHeading").val("");
											$("#_hazeContent").val("");
											$("#prospectdim").hide();	
										});
									});
								});
					}
					} else {
			 	alert("is sending...");
			 }
				});
			 
			 	
			},
			hash : {
				overview : function() {
					$.hash("#oversikt");
				},
				post : function(id) {
					if(id) {
						$("#_activeBlogPostId").val(id);
						$.hash("post:" + id);
					} else {
						return $.hash().replace("#post:","");
					}
				},
				postInitial : function() {
					return $.hash().indexOf("post:") >= 0;
				}
			},
			upperMenu : {
				show : function(callback) {
					$("#blog_showlist_wrapper").fadeIn("fast", function() {
						if(callback) callback();
					});
				},
				hide : function(callback) {
					$("#blog_showlist_wrapper").fadeOut("fast", function() {
						if(callback) callback();
					});
				}
			},
			comment : {
				prepend : function(id, heading, content, created, callback) {
					$("#blogpost_comments_container").prepend(
						$("#hidden_kommentar_wrap").html()
							.replace("#index#","X.")
							.replace("#wrapper_id#", id)
							.replace("#comment_id#", id)
							.replace("#comment_heading#", heading)
							.replace("#comment_content#", content)
							.replace("#comment_created#", created)
					);
					if(callback) callback();
				},
				append : function(i, id, heading, content, created) {
					$("#blogpost_comments_container").append(
						$("#hidden_kommentar_wrap").html()
							.replace("#index#",i)
							.replace("#wrapper_id#", id)
							.replace("#comment_id#", id)
							.replace("#comment_heading#", heading)
							.replace("#comment_created#", created)
							.replace("#comment_content#", content)
					);
				},
				noComments : function() {
					$("#blogpost_comments_container").append("<span style=\"font-size: 12px; \" id=\"__nocomments\">Ingen kommentarer funnet.</span>");
				}
			},
			open : function(id) {
				$("#annoyingscroller").scrollTop(0);
				$("#blogpostwrapper2").fadeOut("fast", function() {
				 $("#blogloader").fadeIn("fast", function() {
				 jsb.ax("ax", "type=blog&s=getpost&id=" + id, function(msg) {
						eval(msg);
						if(response.post!="invalid") {
							jsb.blog.upperMenu.show();
							$("#_axBlogPostHeading").html(response.post.heading);
							$("#_axBlogPostCreated").html(response.post.created);
							$("#_axBlogPostIngress").html(response.post.ingress);
							$("#_axBlogPostContent").html(response.post.content);
							$("#_axBlogPostImage").attr("src", response.post.image);
							if(response.post.comments) {
								$("#blogpost_comments_container").empty();
								if(response.post.comments.length > 0)	{
									$.each(response.post.comments, function(i) {
										jsb.blog.comment.append((i+1), this.id, this.heading, this.content, this.created);
									});
									jsb.blog.comments.updateIndexes();
								} else {
									jsb.blog.comment.noComments();
								}
							}
							$("#blogloader").fadeOut("fast", function() {
								
								$("#blogpostwrapper").fadeIn("fast", function() {
									if(jsb.uri.is("blogg")) {
										
										try {
											jsbadmin.blog.call();
										} catch(ex) {}
									}
									jsb.blog.hash.post(id);	
								});	
							});
						} else {
							jsb.blog.close(function() {
								$("#blogloader").fadeOut("fast");
							});
						}
					});
				 });
				});
			}, 
			close : function(callback) {
				jsb.blog.upperMenu.hide();
 				$("#blogpostwrapper").fadeOut("fast", function() {
					if(callback) callback();
					$("#blogpostwrapper2").fadeIn("fast", function() {
						
					});	
				});
				jsb.blog.hash.overview();
			},
			scroller : {
				init : function() {
					if(!jsb.cooky.is("scroller")) {
						jsb.cooky.set("scroller", "n");
					}
					switch(jsb.cooky.get("scroller")) {
						case "a" : 
							jsb.blog.scroller.add(function() {
								jsb.fixes.rightColumn();
								$("#annoyingscroller").scrollTop(0);
							});
						break;
					}
					$("#scrollcontrol").css("background-image","url(gfx/scroll_" + jsb.cooky.get("scroller") + ".gif)")
						.hover(function() {
							$(this).css("background-image","url(gfx/scroll_o.gif)");
						}, function() {
							$(this).css("background-image","url(gfx/scroll_" + jsb.cooky.get("scroller") + ".gif)");
						}).unbind("click").click(function() {
							switch(jsb.cooky.get("scroller")) {
								case "n" : 
									jsb.cooky.set("scroller", "a");
									jsb.blog.scroller.add(function() {
										jsb.fixes.rightColumn();
									});
								break;
								case "a" : 
									jsb.cooky.set("scroller", "n");
									jsb.blog.scroller.remove();
								break;
							}
						});
				},
				add : function(callback) {
					$("#annoyingscroller").addClass("annoyingscroller");
					if(callback) callback();
				},
				remove : function() {
					$("#annoyingscroller").removeClass("annoyingscroller");
				}
			}
		},
		admin : {
			check : function() {
				jsb.ax("ax", "type=admin&s=check", function(msg) {
					eval(msg);
					if(response.admin.script) {
						jsb.admin.loadScript(response.admin.script, function() {
							jsbadmin.init();
						});
					}
				});
			},
			login : function() {
				if($("#globalfreetext").val().indexOf("admin:")>=0) {
					jsb.ax("ax", "type=admin&s=login&key=" + encodeURIComponent($("#globalfreetext").val()), function(msg) {
						eval(msg);
						if(response.admin.script) {
							jsb.admin.loadScript(response.admin.script, function() {
								jsbadmin.init();
							});
							$("#globalfreetext").val("").blur();
						} else {
							$("#globalfreetext").val("").focus();
						}
					});
					
					return true;
				} else {
					return false;
				}
			},
			loadScript : function(src, callback) {
					$.getScript(src, function() {
							if(callback) callback();	
					});
			}
		}
};

