/*global Offline, O */
define([
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/string",
    "dojo/on",
    "dojo/dom",
    "esri/graphic",
    "dojo/i18n!application/nls/resources",
    "application/localStorageHelper"
], function (
    declare,
    lang,
    string,
    on,
    dom,
    Graphic,
    i18n,
    localStorageHelper
) {
    return declare(null, {
        // create class
        constructor: function (options) {
            // save defaults
            this.defaults = options;
            // localstorage test
            var localStorageSupport = new localStorageHelper();
            // supports storage
            if (localStorageSupport.supportsStorage()) {
                // get offline library
                require(["offline/offline-edit-min"], lang.hitch(this, function(){
                    // create offline manager
                    this.offlineFeaturesManager = O.esri.Edit.OfflineFeaturesManager();
                    // enable offline attachments
                    this.offlineFeaturesManager.initAttachments();
                    // once layer is loaded
                    if (this.defaults.layer.loaded) {
                        this.initEditor();
                    } else {
                        on.once(this.defaults.layer, 'load', lang.hitch(this, this.initEditor));
                    }
                    Offline.check();
                    Offline.on('up', lang.hitch(this, function () {
                        this.goOnline();
                    }));
                    Offline.on('down', lang.hitch(this, function () {
                        this.goOffline();
                    }));
                }));
            }        
        },

        // update online status
        updateConnectivityIndicator: function () {
            var html = '';
            var status = this.offlineFeaturesManager.getOnlineStatus();
            switch (status) {
            case this.offlineFeaturesManager.OFFLINE:
                html = '<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-exclamation-sign"></span> ' + i18n.onlineStatus.offline + '</div>';
                break;
            case this.offlineFeaturesManager.RECONNECTING:
                html = '<div class="alert alert-info"><span class="glyphicon glyphicon-exclamation-sign"></span> ' + i18n.onlineStatus.reconnecting + '</div>';
                break;
            case this.offlineFeaturesManager.ONLINE:
                html = '';
                break;
            }
            var node = dom.byId('connection_status');
            if (node) {
                node.innerHTML = html;
            }
        },

        updateStatus: function () {
            var node = dom.byId('pending_edits');
            // clear html
            node.innerHTML = '';
            // edit store
            var es = new O.esri.Edit.EditStore(Graphic);
            // if we have edits
            if (es.hasPendingEdits()) {
                var edits = es.retrieveEditsQueue();
                var total = edits.length;
                if (total) {
                    var html = '';
                    html += '<div class="alert alert-warning" role="alert">';
                    html += '<span class="glyphicon glyphicon-signal"></span> ';
                    html += string.substitute(i18n.onlineStatus.pending, {
                        total: total
                    });
                    html += '</div>';
                    node.innerHTML = html;
                }
            }
        },

        // now online
        goOnline: function () {
            // go online and then update status
            this.offlineFeaturesManager.goOnline(lang.hitch(this, function () {
                this.updateConnectivityIndicator();
            }));
            this.updateConnectivityIndicator();
        },

        // now offline
        goOffline: function () {
            // go offline
            this.offlineFeaturesManager.goOffline();
            this.updateConnectivityIndicator();
        },

        // setup editing
        initEditor: function () {

            // status for pending edits
            this.offlineFeaturesManager.on(this.offlineFeaturesManager.events.EDITS_ENQUEUED, lang.hitch(this, function () {
                this.updateStatus();
            }));
            this.offlineFeaturesManager.on(this.offlineFeaturesManager.events.EDITS_SENT, lang.hitch(this, function () {
                this.updateStatus();
            }));
            this.offlineFeaturesManager.on(this.offlineFeaturesManager.events.ALL_EDITS_SENT, lang.hitch(this, function () {
                this.updateStatus();
            }));


            /* handle errors that happen while storing offline edits */
            this.offlineFeaturesManager.on(this.offlineFeaturesManager.events.EDITS_ENQUEUED, function (results) {
                var errors = Array.prototype.concat(
                    results.addResults.filter(function (r) {
                        return !r.success;
                    }),
                    results.updateResults.filter(function (r) {
                        return !r.success;
                    }),
                    results.deleteResults.filter(function (r) {
                        return !r.success;
                    })
                );
                // log errors  
                if (errors.length) {
                    console.log(errors);
                }
            });

            /* extend layer with offline detection functionality */
            this.offlineFeaturesManager.extend(this.defaults.layer);

            // update indicator and check status
            this.updateConnectivityIndicator();
        }
    });
});
@@ -196,38 +196,32 @@
    %22
-application/localStorageHelper
+offline/offline-edit-min
 %22%0A%5D,
@@ -309,32 +309,8 @@
 i18n
-,%0A    localStorageHelper
 %0A) %7B
@@ -487,324 +487,8 @@
   //
- localstorage test%0A            var localStorageSupport = new localStorageHelper();%0A            // supports storage%0A            if (localStorageSupport.supportsStorage()) %7B%0A                // get offline library%0A                require(%5B%22offline/offline-edit-min%22%5D, lang.hitch(this, function()%7B%0A                    //
  cre
@@ -507,24 +507,16 @@
 manager%0A
-        
         
@@ -595,32 +595,24 @@
             
-        
 // enable of
@@ -637,32 +637,24 @@
             
-    
-    
 this.offline
@@ -700,24 +700,16 @@
         
-        
 // once 
@@ -732,32 +732,24 @@
             
-   
-     
 if (this.def
@@ -782,32 +782,24 @@
             
-        
 this.initEdi
@@ -817,24 +817,16 @@
         
-  
-      
 %7D else %7B
@@ -826,24 +826,16 @@
  else %7B%0A
-        
         
@@ -927,34 +927,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -966,32 +966,24 @@
             
- 
-       
 Offline.on('
@@ -1030,32 +1030,24 @@
             
-        
 this.goOnlin
@@ -1063,36 +1063,21 @@
         
-        %7D));%0A       
+%7D));%0A
         
@@ -1072,33 +1072,32 @@
 ));%0A            
- 
 Offline.on('down
@@ -1142,32 +1142,24 @@
             
-        
 this.goOffli
@@ -1180,64 +1180,13 @@
     
-       
- 
 %7D));%0A
-                %7D));%0A            %7D        %0A
     

Revert "fix IE8"