(function() {
  window.internets = {};

  internets.Map = function(element, options) {
    var gmap;
    options = options || {}
    var coords = options.coordinates || { lat: 44.44, lon: 26.08 };

    function initializeMap() {
      var latlng = new google.maps.LatLng(coords.lat, coords.lon);
      var myOptions = {
        zoom: 13,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      return new google.maps.Map(element, myOptions);
    }

    gmap = initializeMap();
    navigator.geolocation.getCurrentPosition(function(pos) {
      var current_location = new google.maps.LatLng(pos.coords.latitude,
                                                    pos.coords.longitude);
      gmap.setCenter(current_location);
    });

    return {
      gmap: gmap,
    }

  }

  internets.newNetwork = function(map, point) {
    var poly_editor = new internets.PolyEditor(map, point);

    var frm = $('<form>').submit(function(evt) {
      evt.preventDefault();
      var points = [];
      poly_editor.vertice_points.forEach(function(p) {
        points.push({lat: p.lat(), lon: p.lng()});
      });
      var form_data = frm.serialize() + '&geo=' +
                      encodeURIComponent(JSON.stringify(points));
      $(':input', frm).attr('disabled', 'disabled');
      frm.append('saving... ');
      $.ajax({url: "/api/lan", type: 'POST', data: form_data,
              success: cleanup, error: function() {
                frm.append('ERROR');
                $(':input', frm).attr('disabled', null);
              } });
    });
    frm.append('<input type="text" name="name" placeholder="Lan Name">',
               '<textarea name="info" placeholder="Lan Information"></textarea>',
               '<input type="submit" name="do" value="save">');
    $('<input type="submit" name="do" value="cancel">').click(function(evt) {
      evt.preventDefault(); cleanup(); }).appendTo(frm);

    function cleanup() {
      frm.remove();
      poly_editor.destroy();
    }
    frm.appendTo($('div#new-forms'));
  }

  internets.newWifi = function(map, point) {
    var marker = internets.WifiPoint(map, point);
    var form = $('<form>').submit(function() {
      var form_data = form.serialize() + '&geo=' + encodeURIComponent(JSON.stringify(marker.position()));
      $.ajax({url: "/api/wifi", type: 'POST', data: form_data,

          beforeSend: function() {
            form.find(":input").attr('disabled', 'disabled');
            form.addClass('saving');
          },

          success: function() {
            form.remove();
            marker.destroy();
          },

          error: function() {
            form.addClass('error');
          },

          complete: function() {
            form.removeClass('loading');
            $(':input', form).attr('disabled', null);
          }
        });
      return false;
    });

    form.bind('reset', function() { form.remove(); marker.destroy(); });

    form.append('<input type="text" name="name" placeholder="Wireless Network Name">',
                '<textarea name="info" placeholder="Wireless Network Information"></textarea>',
                '<input type="submit" name="do" value="save">',
                '<input type="reset" name="do" value="cancel">');
    form.appendTo($('div#new-forms'));
  }
})();

$(function() {
  var map_element = document.getElementById('map');
  if ( map_element ) {
    var map = new internets.Map(map_element);

    $('button#new-lan').click(function(evt) {
      var button = $(this);
      var orig_text = button.text();
      button.attr('disabled', 'disabled').text("Click on map");
      google.maps.event.addListenerOnce(map.gmap, 'click', function(evt) {
        button.text(orig_text).attr('disabled', null);
        internets.newNetwork(map.gmap, evt.latLng);
      });
    });

    new internets.InternetsBrowser(map.gmap);
    $('button#new-wifi').click(function() {
      var button = $(this);
      var orig_text = button.text();
      button.attr('disabled', 'disabled').text("Click on map");
      google.maps.event.addListenerOnce(map.gmap, 'click', function(evt) {
        button.text(orig_text).attr('disabled', null);
        internets.newWifi(map.gmap, evt.latLng);
      });
    });
  }
});

@@ -921,16 +921,17 @@
    var f
+o
 rm = $('
@@ -1152,16 +1152,17 @@
 data = f
+o
 rm.seria
@@ -1246,37 +1246,144 @@
 ints));%0A
+%0A
       $
-(':input',
+.ajax(%7Burl: %22/api/lan%22, type: 'POST', data: form_data,%0A          beforeSend: function() %7B%0A           
  f
+o
 rm
+.find(%22:input%22
 ).attr('
@@ -1416,26 +1416,35 @@
     
-frm.append
+      form.addClass
 ('saving
 ... 
@@ -1443,12 +1443,8 @@
 ving
-... 
 ');%0A
@@ -1453,157 +1453,273 @@
     
-$.ajax(%7Burl: %22/api/lan%22, type: 'POST', data: form_data,%0A              success: cleanup, error: function() %7B%0A                frm.append('ERROR');%0A    
+    %7D,%0A%0A          success: function() %7B%0A            form.remove();%0A            marker.destroy();%0A          %7D,%0A%0A          error: function() %7B%0A            form.addClass('error');%0A          %7D,%0A%0A          complete: function() %7B%0A            form.removeClass('loading');%0A
     
@@ -1739,16 +1739,17 @@
 nput', f
+o
 rm).attr
@@ -1780,18 +1780,102 @@
     
+  %7D%0A
       %7D
- %7D)
+);%0A    %7D);%0A%0A    form.bind('reset', function() %7B%0A      cleanup();%0A      return false
 ;%0A  
@@ -1880,21 +1880,23 @@
     %7D);%0A
+%0A
     f
+o
 rm.appen
@@ -1947,32 +1947,33 @@
 r=%22Lan Name%22%3E',%0A
+ 
                '
@@ -2045,32 +2045,33 @@
 %0A               
+ 
 '%3Cinput type=%22su
@@ -2100,25 +2100,34 @@
 %22save%22%3E'
-);
+,
 %0A    
-$(
+            
 '%3Cinput 
@@ -2124,37 +2124,36 @@
   '%3Cinput type=%22
-submi
+rese
 t%22 name=%22do%22 val
@@ -2170,86 +2170,8 @@
 %22%3E')
-.click(function(evt) %7B%0A      evt.preventDefault(); cleanup(); %7D).appendTo(frm)
 ;%0A%0A 
@@ -2201,16 +2201,17 @@
 %0A      f
+o
 rm.remov
@@ -2255,16 +2255,17 @@
  %7D%0A    f
+o
 rm.appen

Standardize form requests