var geolocation;

window.addEvent('domready',function(){

	// blur the input
	$$('.blur').each(function(item){
		if(item.value=='') item.value = item.get('title');
	});
	$$('.blur').addEvent('focus',function(){
		if(this.hasClass('blur')) { this.removeClass('blur'); this.value = ''; }
	})

	// maxlength on textarea's
	$$('textarea[max]').addEvent('keypress', function(e){
		if(this.value.length >= this.get('max')) {
			this.value = this.value.substr(0,this.get('max'));
		}
	});

	// hide the message
    //$$('div#message').slide('out');
	//.setStyle('height',0).tween('height',40);
    //(function(){ $$('div#message').tween('height',0)}).delay(5000);

	var uris = $$('a');
	uris.addEvent('click', function(e){
		e.stop();
		
		var uri = new URI(this);
		if(geolocation && geolocation.lat && geolocation.lng){
			uri.setData(geolocation,true);
		}
		
		uri.go();
		
	});

	if(geo_position_js.init()){
		locationinfo('Bezig locatie te bepalen..');
		geo_position_js.getCurrentPosition(has_browser_geo_api,error);
	} else {
		no_browser_geo_api();
	}

	function has_browser_geo_api(position){
		changedGeolocation(position);
		if(navigator.geolocation){
			navigator.geolocation.watchPosition(function(e){ changedGeolocation(e) },error);
		}
	}

	function no_browser_geo_api(){
		locationinfo('Browser kan locatie niet bepalen..');
		locationstrength(false);
	}

	function locationinfo(message){
		if($('locationinfo')){
			$('locationinfo').set('text',message);
		}
	}

	function locationstrength(accuracy){
		if($('locationstrength')){
			switch(true){
				case (accuracy < 100):
					$('locationstrength').set('text',5).set('class','level-5');
					break
				case (accuracy < 200):
					$('locationstrength').set('text',4).set('class','level-4');
					break
				case (accuracy < 300):
					$('locationstrength').set('text',3).set('class','level-3');
					break
				case (accuracy < 400):
					$('locationstrength').set('text',2).set('class','level-2');
					break
				case (accuracy < 1000):
					$('locationstrength').set('text',1).set('class','level-1');
					break
				default:
					$('locationstrength').set('text',0).set('class','level-0');
					break		
			}
			
		}
	}

	function changedGeolocation(position){

		if(position.coords){

			locationinfo('Locatie gevonden.. Ververs.');
			locationstrength(position.coords.accuracy);

			geolocation = {lat: position.coords.latitude,lng: position.coords.longitude}

			$$('input[name=lat]').each(function(el){ el.value=geolocation.lat; })
			$$('input[name=lng]').each(function(el){ el.value=geolocation.lng; })

		}
	}

	function error(e){
		if (typeof(console) != "undefined") {
			console.log(e);
		}
	}

});

function refresh(){
	
	var uri = new URI();
	if(geolocation && geolocation.lat && geolocation.lng){
		uri.setData(geolocation);
	}
	uri.go();
}

MooTools.More={version:"1.2.4.4",build:"6f6057dc645fdb7547689183b2311063bd653ddf"};String.implement({parseQueryString:function(){var b=this.split(/[&;]/),a={};
if(b.length){b.each(function(g){var c=g.indexOf("="),d=c<0?[""]:g.substr(0,c).match(/[^\]\[]+/g),e=decodeURIComponent(g.substr(c+1)),f=a;d.each(function(j,h){var k=f[j];
if(h<d.length-1){f=f[j]=k||{};}else{if($type(k)=="array"){k.push(e);}else{f[j]=$defined(k)?[k,e]:e;}}});});}return a;},cleanQueryString:function(a){return this.split("&").filter(function(e){var b=e.indexOf("="),c=b<0?"":e.substr(0,b),d=e.substr(b+1);
return a?a.run([c,d]):$chk(d);}).join("&");}});var URI=new Class({Implements:Options,options:{},regex:/^(?:(\w+):)?(?:\/\/(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?)?(\.\.?$|(?:[^?#\/]*\/)*)([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,parts:["scheme","user","password","host","port","directory","file","query","fragment"],schemes:{http:80,https:443,ftp:21,rtsp:554,mms:1755,file:0},initialize:function(b,a){this.setOptions(a);
var c=this.options.base||URI.base;if(!b){b=c;}if(b&&b.parsed){this.parsed=$unlink(b.parsed);}else{this.set("value",b.href||b.toString(),c?new URI(c):false);
}},parse:function(c,b){var a=c.match(this.regex);if(!a){return false;}a.shift();return this.merge(a.associate(this.parts),b);},merge:function(b,a){if((!b||!b.scheme)&&(!a||!a.scheme)){return false;
}if(a){this.parts.every(function(c){if(b[c]){return false;}b[c]=a[c]||"";return true;});}b.port=b.port||this.schemes[b.scheme.toLowerCase()];b.directory=b.directory?this.parseDirectory(b.directory,a?a.directory:""):"/";
return b;},parseDirectory:function(b,c){b=(b.substr(0,1)=="/"?"":(c||"/"))+b;if(!b.test(URI.regs.directoryDot)){return b;}var a=[];b.replace(URI.regs.endSlash,"").split("/").each(function(d){if(d==".."&&a.length>0){a.pop();
}else{if(d!="."){a.push(d);}}});return a.join("/")+"/";},combine:function(a){return a.value||a.scheme+"://"+(a.user?a.user+(a.password?":"+a.password:"")+"@":"")+(a.host||"")+(a.port&&a.port!=this.schemes[a.scheme]?":"+a.port:"")+(a.directory||"/")+(a.file||"")+(a.query?"?"+a.query:"")+(a.fragment?"#"+a.fragment:"");
},set:function(b,d,c){if(b=="value"){var a=d.match(URI.regs.scheme);if(a){a=a[1];}if(a&&!$defined(this.schemes[a.toLowerCase()])){this.parsed={scheme:a,value:d};
}else{this.parsed=this.parse(d,(c||this).parsed)||(a?{scheme:a,value:d}:{value:d});}}else{if(b=="data"){this.setData(d);}else{this.parsed[b]=d;}}return this;
},get:function(a,b){switch(a){case"value":return this.combine(this.parsed,b?b.parsed:false);case"data":return this.getData();}return this.parsed[a]||"";
},go:function(){document.location.href=this.toString();},toURI:function(){return this;},getData:function(c,b){var a=this.get(b||"query");if(!$chk(a)){return c?null:{};
}var d=a.parseQueryString();return c?d[c]:d;},setData:function(a,c,b){if(typeof a=="string"){data=this.getData();data[arguments[0]]=arguments[1];a=data;
}else{if(c){a=$merge(this.getData(),a);}}return this.set(b||"query",Hash.toQueryString(a));},clearData:function(a){return this.set(a||"query","");}});URI.prototype.toString=URI.prototype.valueOf=function(){return this.get("value");
};URI.regs={endSlash:/\/$/,scheme:/^(\w+):/,directoryDot:/\.\/|\.$/};URI.base=new URI(document.getElements("base[href]",true).getLast(),{base:document.location});
String.implement({toURI:function(a){return new URI(this,a);}});

