$(document).ready(function() {
    var editor = initEditor();

    $('#tabs a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    })

    $("#tabs > li > a ").click(function() {
        if (editor.map.toolbars && editor.map.toolbars.length > 0) {
            var tab = $(this).attr('href');

            $.each(editor.map.toolbars, function(id, toolbar) {
                toolbar.deactivateAllControls();
                if (toolbar.select) {
                    toolbar.select.olControl.unselectAll();
                }
                if (toolbar.select && tab == '#edit') {
                    toolbar.select.activate();
                }
            });
        }
   });

   $('#remove_all_features').click(function() {
        var activeLayer = editor.layerManager.active();
        if(activeLayer instanceof gbi.Layers.SaveableVector) {
            editor.map.toolbars[1].delete_.olControl.deleteFeatures(activeLayer.features)
            activeLayer.changesMade();
        }
        $('#deleteAllGeometries').modal('hide');
        return false;
   });

   var toolbarButton = $('#edit-toolbar .olButton');
   var toolbarWork = ['DrawFeaturePoint', 'DrawFeatureLine', 'DrawFeaturePolygon'];
   $('#edit-toolbar').append('<div id="toolbar-draw" class="span11">'+
        '</div><div id="toolbar-work"></div>');
   // order toolbar as long as gbi editor dont support groups
   $.each(toolbarButton, function(id, button) {
        var class_ = $(button).attr('class');
        var toolbar = false;
        $.each(toolbarWork, function(index, buttonClass) {
            if (class_.indexOf(buttonClass) >= 0) {
              $(button).appendTo('#toolbar-draw')
              toolbar = true;
            }
        });

        if (!toolbar) {
             $(button).appendTo('#toolbar-work')
        }

   });

});

function initEditor() {
     var editor = new gbi.Editor({
       map: {
            element: 'map',
            numZoomLevels : numZoomLevels,
            theme: OpenlayersThemeURL
        },
        imgPath: OpenlayersImageURL
    });
    editor.addLayer(backgroundLayer)

    $.each(raster_sources, function(index, layer) {
        editor.addLayer(layer);
    });

    $.each(couchLayers, function(index, layer) {
        editor.addLayer(layer);
    });

    var layermanager = new gbi.widgets.LayerManager(editor, {
        element: 'layermanager'
    });

    var measure = new gbi.widgets.Measure(editor, {
    	element: 'measure-toolbar',
        srs: ['EPSG:3857', 'EPSG:4326', 'EPSG:25832', 'EPSG:25833', 'EPSG:31466', 'EPSG:31467', 'EPSG:31468']
    });

    var toolbar = new gbi.Toolbar(editor, {
        element: 'edit-toolbar',
        tools: {
            'drawPoint': true,
            'drawLine': true,
            'drawPolygon': true,
            'select': true,
            'edit': true,
            'split': true,
            'merge': true,
            'copy': true,
            'delete': true
        }
    });
    toolbar.select.deactivate();

    var attributeEditor = new gbi.widgets.AttributeEditor(editor);
    var styleEditor = new gbi.widgets.StyleEditor(editor);
    var pointStyleEditor = new gbi.widgets.PointStyleEditor(editor);

    var layerfilter = new gbi.widgets.Filter(editor, {
        element: 'filtermanager'
    });

    $('#save_changes').click(function() {
        var layer = editor.layerManager.active();
        layer.save();
        $(this).removeClass('btn-success').attr('disabled', 'disabled');
    });

	return editor;
}


@@ -645,24 +645,60 @@
 activate();%0A
+                    orderToolbar();%0A
             
@@ -1122,16 +1122,69 @@
    %7D);%0A%0A
+   orderToolbar();%0A%0A   function orderToolbar() %7B%0A    
    var t
@@ -1228,16 +1228,20 @@
 tton');%0A
+    
    var t
@@ -1316,16 +1316,20 @@
 ygon'%5D;%0A
+    
    $('#e
@@ -1385,17 +1385,17 @@
 s=%22span1
-1
+2
 %22%3E'+%0A   
@@ -1399,16 +1399,20 @@
         
+  
+  
 '%3C/div%3E%3C
@@ -1443,16 +1443,20 @@
 div%3E');%0A
+    
    // or
@@ -1512,16 +1512,20 @@
 oups%0A   
+  
+  
 $.each(t
@@ -1565,24 +1565,28 @@
 ) %7B%0A        
+    
 var class_ =
@@ -1615,24 +1615,28 @@
 ');%0A        
+  
+  
 var toolbar 
@@ -1644,16 +1644,20 @@
  false;%0A
+    
         
@@ -1715,24 +1715,28 @@
             
+  
+  
 if (class_.i
@@ -1769,32 +1769,36 @@
 %7B%0A              
+    
 $(button).append
@@ -1827,24 +1827,28 @@
             
+  
+  
 toolbar = tr
@@ -1863,26 +1863,32 @@
         
-%7D%0A
+    %7D%0A  
         
 %7D);%0A%0A   
@@ -1875,29 +1875,35 @@
 %7D%0A          
+  
 %7D);%0A%0A
+    
         if (
@@ -1919,32 +1919,36 @@
  %7B%0A             
+  
+  
 $(button).append
@@ -1979,17 +1979,31 @@
     
+    
 %7D%0A%0A   
-%7D);
+    %7D);%0A    %7D
 %0A%0A%7D)

add function to order Toolbar