$(document).ready(function() {
						   
  $('#maps').click(function() { //may need to be changed
	var page = "../modules/Users/geocode.php";
	var slaapplekje = new google.maps.LatLng(52.108,5.489);
	var user = $("#googlemaps").attr('value');
	
	var myOptions = {
	  zoom: 7,
	  center: slaapplekje,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
	  draggableCursor: 'url("/images/place_marker.png") 10 34,crosshair'
	}
	var map = new google.maps.Map(document.getElementById("googlemaps"), myOptions);
	google.maps.event.addListener(map, 'click', function(event) {
	  $.Marker(event.latLng);
	});
	
	var marker;
	$.Marker = (function(p){
	  if(marker){
		 marker.setPosition(p);
	  }
	  else
	  {
		map.setZoom(15);
		marker = new google.maps.Marker({
		  position: p,
		  map: map,
		  draggable: true
		});
	  }
	  map.setCenter(p);
	  $.get(page, {user: user, lat: p.lat(), lng: p.lng()} );
	});
	
	///*var provincies = new Array(
//		new google.maps.Marker({
//		position: new google.maps.LatLng(53.08228930610739,5.862535156250033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.84406458404338,6.609605468750033),
//		map: map,
//		draggable: false
//	  }),
//	    new google.maps.Marker({
//		position: new google.maps.LatLng(53.279808234882026,6.686509765625033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.437404325715804,6.400865234375033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.01343451403617,5.961412109375033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.477571430493576,5.576890625000033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.5845046454958,4.818833984375033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.09450290129763,5.115464843750033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(52.00667217422719,4.489244140625033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(51.53076543861813,3.8300644531250327),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(51.57858202984255,5.137437500000033),
//		map: map,
//		draggable: false
//	  }),
//		new google.maps.Marker({
//		position: new google.maps.LatLng(51.18775509045164,5.961412109375033),
//		map: map,
//		draggable: false
//	  })
//	);*/
//	
//	/*google.maps.event.addListener(provincies[0], 'click', function(event) {
//	  console.log(event);
//	});*/
//	//alert(provincies);
//	//console.log(provincies);
	
	$.getJSON( page, {user: user}, function( data ) {
	  if(data != null)
	  {
		console.log(data,user);
		$.each(data, function(i, location) {
		  if((typeof location.lat != 'undefined') && (typeof location.lng != 'undefined'))
		  {
			$.Marker(new google.maps.LatLng(location.lat,location.lng));
		  }
		  else if(typeof location.address != 'undefined')
		  {
			geocoder = new google.maps.Geocoder();
			geocoder.geocode( { 'address': location.address}, function(results, status) {
			  if (status == google.maps.GeocoderStatus.OK) {
				$.Marker(results[0].geometry.location);
			  }
			});
		  }
		  google.maps.event.addListener(marker, 'dragend', function() {
			$.Marker(marker.getPosition());
		  });
		})
	  }
	});
  });
  
  $('table .compareall:checkbox').change(function(){
	state = $(this).attr('checked');
	$('table input:checkbox').attr('checked',state).parents("tr").toggleClass('selected',state);
  });
  
  $('td.compare input:checkbox').change(function(){
	state = $(this).attr('checked');
	$(this).parents('tr').toggleClass('selected',state);
  });
  
  $('select#region, select#city, select#limit').change(function(){
	$(this).parents('form').submit();
  });
  
  //$('select#city').change(function(){
  //  $(this).parents('form').submit();
  //});
  
  $('#imageonly:checkbox').change(function(){
	$(this).parents('form').submit();
	//$.post($(this).parents('form').attr('action'), $(this).parents('form').serialize(), function(data) {
	  //$('html').html(data);
	  //alert('done');
	//});
  });
  
  $(function() {
	  $( "#thumbnails" ).sortable().bind( "sortupdate", function(event, ui) {
		  $.ajax({
			  url: "../index.php?mact=Uploads,m1_,sort,0",
			  data: $( "#thumbnails" ).sortable("serialize")
		  });
	  });
	  $( "#thumbnails" ).disableSelection();
  });
  
  $(function() {
	  var cache = {},
		  lastXhr;
	  $( "#mb191aterm" ).autocomplete({
		  minLength: 2,
		  select: function(event, ui) {
			  $("#attribute").attr("value", ui.item.attribute);
			  $("#value").attr("value", ui.item.value);
			  $(this).parents('form').submit();
		  },
		  source: function( request, response ) {
			  var term = request.term;
			  if ( term in cache ) {
				  response( cache[ term ] );
				  return;
			  }
				  lastXhr = $.getJSON( "modules/Users/search.php", request, function( data, status, xhr ) {
				  cache[ term ] = data;
				  if ( xhr === lastXhr ) {
					  response( data );
				  }
			  });
		  }
	  });
  });

});

