var reload;
var city;
var addthis_localize = {
	share_caption: "Delen & Bookmark",
    email_caption: "Email een vriend",
    email: "Email",
    favorites: "Favorieten", 
    more: "Meer"
};
var addthis_config = {
    ui_cobrand: "112meldingen.nl",
    ui_language: "nl",
	pubid: "ra-4dd922fb3db2bfad",
	"data_track_clickback":true
};
var RecaptchaOptions = {
	lang : 'nl',
	theme : 'clean'
};

window._uds_vbw_donotrepair = true;

$(document).ready(function(){
	
	if (typeof addthis != 'undefined') {
    	addthis.init();
	}
	
	if ($("#divContentAlerts").length) {
		loadAlerts();
	}
	
	if ($("#map_canvas").length) {
		var geocoder;
		var map;
		var panorama;
		var directionDisplay;
		var directionsService = new google.maps.DirectionsService();
		setTimeout(function(){
			var panoramaOptions = {
				position: latlng,
				pov: {
					heading: 0,
					pitch: 0,
					zoom: 1
				}
			};
			panorama = new  google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
			directionsDisplay = new google.maps.DirectionsRenderer();
			geocoder = new google.maps.Geocoder();
			var latlng = new google.maps.LatLng(52.089496,5.110347);
			var myType = google.maps.MapTypeId.ROADMAP;
			if ($("#mapType").html() == "B") { myType = google.maps.MapTypeId.SATELLITE; }
			else if ($("#mapType").html() == "C") { myType = google.maps.MapTypeId.HYBRID; }
			else if ($("#mapType").html() == "D") { myType = google.maps.MapTypeId.TERRAIN; }
			var myOptions = {
		    	zoom: 7,
				center: latlng,
				mapTypeId: myType
			};
			map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			if ($("#lat1").html() != "") { var myMarker1 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat1").html(),$("#lng1").html()), map: map, title: $("#name_loc1").html() }); }  
			if ($("#lat2").html() != "") { var myMarker2 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat2").html(),$("#lng2").html()), map: map, title: $("#name_loc2").html() }); }  
			if ($("#lat3").html() != "") { var myMarker3 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat3").html(),$("#lng3").html()), map: map, title: $("#name_loc3").html() }); }  
			if ($("#lat4").html() != "") { var myMarker4 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat4").html(),$("#lng4").html()), map: map, title: $("#name_loc4").html() }); }  
			if ($("#lat5").html() != "") { var myMarker5 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat5").html(),$("#lng5").html()), map: map, title: $("#name_loc5").html() }); }  
			geocoder.geocode({'address': $("#addressEnd").html(), 'region': 'nl', 'language': 'nl'}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					map.setCenter(results[0].geometry.location);
					if ($("#mapZoom").html() == "A") { map.setZoom(17); }
					else if ($("#mapZoom").html() == "B") { map.setZoom(15); }
					else if ($("#mapZoom").html() == "C") { map.setZoom(13); }
					else if ($("#mapZoom").html() == "D") { map.setZoom(9); }
					else if ($("#mapZoom").html() == "E") { map.setZoom(7); }
					else { map.setZoom(17); }
					panorama.setPosition(results[0].geometry.location);
					var marker = new google.maps.Marker({
						map: map,
						position: results[0].geometry.location,
						shadow: '../images/icon_schaduw.png',
						icon: '../images/icon_' + $("#service").html() + '.png',
						title: $("H1").html()
					});
					city = results[0].formatted_address.replace(/, Nederland/i,"").replace(/.*,/i,"").replace(/[0-9]{4}( [A-Z]{2})?/,"").replace(/^\s+/,"").replace(/\s+$/,"").replace(/St\./,"Sint");
					$("#city").append(city);
					getTweets('ambulance OR brandweer OR brand OR ongeval OR ongeluk OR hulpdienst OR hulpdiensten OR storm OR aanrijding OR zwaailicht OR zwaailichten OR sirene "' + city + '" -"p 1" -"p 2" -"pr " -rt -"prio " -a1 -a2 -"br "');
				} else {
					getTweets('112meldingen.nl');
				}
			});	
			directionsDisplay.setMap(map);
			directionsDisplay.setPanel(document.getElementById("directionsPanel"));
			map.setStreetView(panorama);
		}, 1000);
		$("#startCalcRoute").change(function(){
			if ($("#startCalcRoute").val() != "-") {
				$("#startCalcRouteInput").val("");
			}
		});
		$("#startCalcRouteInput").focus(function(){
			$("#startCalcRoute").val("-");
		});
		$("#btnCalcRoute").click(function(){
			if ($("#startCalcRouteInput").val() != "" || $("#startCalcRoute").val() != "-") {
				var start;
				if ($("#startCalcRoute").val() == "1" || $("#startCalcRoute").val() == "2" || $("#startCalcRoute").val() == "3" || $("#startCalcRoute").val() == "4" || $("#startCalcRoute").val() == "5") {
					start = new google.maps.LatLng($("#lat"+$("#startCalcRoute").val()).html(),$("#lng"+$("#startCalcRoute").val()).html());
				} else {
					start = document.getElementById("startCalcRouteInput").value;
				}
				var request = {
					origin: start, 
					destination: $("#addressEnd").html(),
					travelMode: google.maps.DirectionsTravelMode.DRIVING,
					unitSystem: google.maps.DirectionsUnitSystem.METRIC,
					region: google.maps.nl
				};
				directionsService.route(request, function(result, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(result);
						$("#btnToggleItinerary").fadeIn("slow");
					}
					else {
						alert("Route kan niet gepland worden : " + status + " : " + start);
					}
				});
			} else {
				alert("Route kan niet gepland worden : u heeft geen start adres geselecteerd / ingevuld.");
			}
		});
		$("#btnToggleItinerary").click(function(){
			$("#directionsPanel").slideToggle();
		});
	}
	
	if ($("#map_location").length) {
		var geocoder;
  		var map;
    	geocoder = new google.maps.Geocoder();
    	var latlng = new google.maps.LatLng(52.089496,5.110347);
    	var myOptions = {
      		zoom: 7,
      		center: latlng,
      		mapTypeId: google.maps.MapTypeId.ROADMAP
    	}
    	map = new google.maps.Map(document.getElementById("map_location"), myOptions);
		if ($("#lat1").html() != "") { var myMarker1 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat1").html(),$("#lng1").html()), map: map, title: $("#name_loc1").html() }); }  
		if ($("#lat2").html() != "") { var myMarker2 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat2").html(),$("#lng2").html()), map: map, title: $("#name_loc2").html() }); }  
		if ($("#lat3").html() != "") { var myMarker3 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat3").html(),$("#lng3").html()), map: map, title: $("#name_loc3").html() }); }  
		if ($("#lat4").html() != "") { var myMarker4 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat4").html(),$("#lng4").html()), map: map, title: $("#name_loc4").html() }); }  
		if ($("#lat5").html() != "") { var myMarker5 = new google.maps.Marker({ position: new google.maps.LatLng($("#lat5").html(),$("#lng5").html()), map: map, title: $("#name_loc5").html() }); }  
		var address;
		$("#locationSet").change(function(){
			$("#rowLoc1").hide();
			$("#rowLoc2").hide();
			$("#rowLoc3").hide();
			$("#rowLoc4").hide();
			$("#rowLoc5").hide();
			$("#rowLocBtn").hide();
			$("#submitLocation").hide();
			if ($("#locationSet").val() == "1") {
				$("#rowLoc1").fadeIn("slow");
				if ($("#inputLocationAddress1").val() == "") {
					$("#deleteLocation").hide();
				} else {
					$("#deleteLocation").show();
				}
			} else if ($("#locationSet").val() == "2") {
				$("#rowLoc2").fadeIn("slow");
				if ($("#inputLocationAddress2").val() == "") {
					$("#deleteLocation").hide();
				} else {
					$("#deleteLocation").show();
				}
			} else if ($("#locationSet").val() == "3") {
				$("#rowLoc3").fadeIn("slow");
				if ($("#inputLocationAddress3").val() == "") {
					$("#deleteLocation").hide();
				} else {
					$("#deleteLocation").show();
				}
			} else if ($("#locationSet").val() == "4") {
				$("#rowLoc4").fadeIn("slow");
				if ($("#inputLocationAddress4").val() == "") {
					$("#deleteLocation").hide();
				} else {
					$("#deleteLocation").show();
				}
			} else if ($("#locationSet").val() == "5") {
				$("#rowLoc5").fadeIn("slow");
				if ($("#inputLocationAddress5").val() == "") {
					$("#deleteLocation").hide();
				} else {
					$("#deleteLocation").show();
				}
			}
			if ($("#locationSet").val() != "0") {
				$("#rowLocBtn").fadeIn("slow");
			}
		});
		$("#inputLocationAddress1,#inputLocationAddress2,#inputLocationAddress3,#inputLocationAddress4,#inputLocationAddress5").focus(function(){
			$("#submitLocation").hide();
		});
		$("#lookUpLocation").click(function(){
			if ($("#locationSet").val() == "1" && $("#inputLocationAddress1").val() != "") {
				address = $("#inputLocationAddress1").val();
				geocode(1);
			} else if ($("#locationSet").val() == "2" && $("#inputLocationAddress2").val() != "") {
				address = $("#inputLocationAddress2").val();
				geocode(2);
			} else if ($("#locationSet").val() == "3" && $("#inputLocationAddress3").val() != "") {
				address = $("#inputLocationAddress3").val();
				geocode(3);
			} else if ($("#locationSet").val() == "4" && $("#inputLocationAddress4").val() != "") {
				address = $("#inputLocationAddress4").val();
				geocode(4);
			} else if ($("#locationSet").val() == "5" && $("#inputLocationAddress5").val() != "") {
				address = $("#inputLocationAddress5").val();
				geocode(5);
			}
		});
		function geocode(field) {
			geocoder.geocode( { 'address': address, 'region': 'nl', 'language': 'nl'}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					$("#inputLocationCoords"+field).val(results[0].geometry.location);
					var marker = new google.maps.Marker({
						map: map,
						position: results[0].geometry.location
					});
					map.setCenter(results[0].geometry.location);
					map.setZoom(17);
					$("#submitLocation").fadeIn("slow");
				} else {
					alert("Het opgegeven adres is niet gevonden.");
				}
			});
		}
	}
	
	/* MAIN */
	$(".messageGood").fadeOut(10000);
	
	$("#btnSets").click(function(){$("#divCurrentSetting").html($("#divSettingSets").html());});
	$("#btnRegions").click(function(){$("#divCurrentSetting").html($("#divSettingRegions").html());});
	$("#btnServices").click(function(){$("#divCurrentSetting").html($("#divSettingServices").html());});
	$("#btnOptions").click(function(){$("#divCurrentSetting").html($("#divSettingOptions").html());});
	$("#btnSpec").click(function(){$("#divCurrentSetting").html($("#divSettingSpec").html());});
	$("#btnIgnore").click(function(){$("#divCurrentSetting").html($("#divSettingIgnore").html());});
	$("#btnDate").click(function(){$("#divCurrentSetting").html($("#divSettingDate").html());});
	$("#btnMinimal").click(function(){$("#divCurrentSetting").html($("#divSettingMinimal").html());});
	$("#btnSound").click(function(){$("#divCurrentSetting").html($("#divSettingSound").html());});
	$("#btnMaps").click(function(){$("#divCurrentSetting").html($("#divSettingMaps").html());});
	$("#btnRefresh").click(function(){$("#divCurrentSetting").html($("#divSettingRefresh").html());});
	$("#btnResetCookie").click(function(){$("#divCurrentSetting").html($("#divSettingResetCookie").html());});
	$("#btnResetSet").click(function(){$("#divCurrentSetting").html($("#divSettingResetSet").html());});
	$("#btnSetName").click(function(){$("#divCurrentSetting").html($("#divSetName").html());});	
});

// Catch enter key: IE Fix
$(document).keypress(function(e) {
  if (e.which == 13 && !$(e.target).is("textarea")) {
    return false;
  }
}); 

function loadAlerts() {
	var myRandom = parseInt(Math.random() * 9999999999);
	var url = "includes/alerts.php?rand=" + myRandom;
	$("#divContentAlerts").load(url);
}

var runCity = true;
function getTweets(aQuery) {
	$.getJSON(encodeURI('http://search.twitter.com/search.json?lang=nl&rpp=100&callback=?&q=' + aQuery),function(json){
		$("#twitter").html("");
		$.each(json.results,function(i,tweet){
			if (tweet.text.search(/\|/i) == -1 && tweet.source.search(/alarmeringen/i) == -1 && tweet.source.search(/p2000/i) == -1 && tweet.source.search(/GroteIncidenten/i) == -1) {
				d = new Date(tweet.created_at);			
				utc = d.getTime() + (d.getTimezoneOffset() * 60000);
				nd = new Date(utc + (3600000*2));
				var parsed_date = nd.getTime();
				var timeago = parseInt((new Date().getTime() - parsed_date) / 1000);
				var tweetTime = "";
				if (timeago < 60) tweetTime = 'minder dan een minuut geleden';
				else if(timeago < 120) tweetTime = 'ca. een minuut geleden';
				else if(timeago < (45*60)) tweetTime = (parseInt(timeago / 60)).toString() + ' minuten geleden';
				else if(timeago < (90*60)) tweetTime = 'ca. een uur geleden';
				else if(timeago < (24*60*60)) tweetTime = 'ca. ' + (parseInt(timeago / 3600)).toString() + ' uur geleden';
				else if(timeago < (48*60*60)) tweetTime = '1 dag geleden';
				else tweetTime = (parseInt(timeago / 86400)).toString() + ' dagen geleden';
				$("#twitter").append('<table>');
				$("#twitter").append('<tr><td><img id="twitterProfileImg" src="'+tweet.profile_image_url+'" width="65" height="65" /></td><td class="vtop"><a target="_blank" class="bold" href="http://twitter.com/' + tweet.from_user + '">' + tweet.from_user + '</a><br />' +tweet.text.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a target="_blank" href="$1">$1</a>').replace(/@+([_A-Za-z0-9-]+)/ig,'<a target="_blank" href="http://twitter.com/$1">@$1</a>').replace(/#+([_A-Za-z0-9-]+)/ig,'<a target="_blank" href="http://search.twitter.com/search?q=#$1">#$1</a>')+'<br /><img src="../images/twitter_bird.png" class="floatleft" />&nbsp;<span class="subtext">'+tweetTime+' via '+tweet.source.replace(/&lt;/ig,'<').replace(/&gt;/ig,' id="twitterSourceLink">').replace(/&quot;/ig,'"')+'</span></td></tr>');
				$("#twitter").append('</table>');
			}
		});
		
		if ($("#twitter").html() != "") {
			$("#twitter").slideDown("slow");
		} else if (runCity) {
			getTweets(city + ' -"p 1" -"p 2" -"pr " -rt -prio -a1 -a2');
			runCity = false;
		}
	});
	LoadVideoBar();	
}

function LoadVideoBar() {
	var videoBar;
	var options = {
		string_allDone : "Youtube video sluiten",
		largeResultSet : !false,
		horizontal : false,
		autoExecuteList : {
			cycleTime : GSvideoBar.CYCLE_TIME_SHORT,
			cycleMode : GSvideoBar.CYCLE_MODE_LINEAR,
			executeList : ["brand " + $("#city").html(),"ongeval " + $("#city").html()]
		}
	}
	videoBar = new GSvideoBar(document.getElementById("videoBar-bar"), GSvideoBar.PLAYER_ROOT_FLOATING, options);
}

