﻿DScript.DScovery.implement
({
    Bookmarks : new Class
    ({  
        initialize : function()
        {
            var keys = new Array();
            
            this.sourcerecord = new Class
            ({
                'Name': '',
                'Key' : '',
                'Ids' : []
            })
        
            this.keys = keys;
            this.context = new Array();
            
            // Load from local copy
            var restorestate = DScript.restoreState('Bookmarks');
            
            if (restorestate != null)
            {
                 this.context = restorestate;
                 
                 this.context.each( function(src, index)
                 {
                    this.keys.extend([src.Name]);
                 },this);
            }
            else // Load from server session
            {
                new XHR({method: 'get',async: true,onSuccess: this.onRestoreSuccess.bind(this), onFailure: this.onFail.bindAsEventListener(this)}).send('./bookmark.aspx', 'get=');
            }
        },
        
        Add : function(datasource)
        {
            
            if (!datasource || !datasource.context.record.id) return;
            
            var db = this.context.associate(this.keys);
            var name = datasource.context.fullname;
            var ids = [];
            
            if (this.keys && this.keys.contains(name))
            {
                ids = db[name].Ids;
            }
            else
            {
                this.keys.extend([name]);
                var src = new this.sourcerecord();
                src.Name = datasource.context.fullname;
                src.Key = datasource.context.keyfield;
                ids = src.Ids;
                this.context.extend([src]);
            }
            
            if (!ids.contains(datasource.context.record.id))
                ids.extend([datasource.context.record.id]);
                
            new XHR({method: 'get',async: true,onSuccess: this.onSuccess.bind(this), onFailure: this.onFail.bindAsEventListener(this)}).send('./bookmark.aspx', 'add=' + name + ':' + datasource.context.record.id);
        },
        
        Delete : function(datasource)
        {
            if (!datasource || !datasource.context.record.id) return;
            
            var name = datasource.context.fullname;
            
            if (this.keys.contains(name))
            {
                var db = this.context.associate(this.keys);
                if (db[name].Ids.contains(datasource.context.record.id))
                {
                    db[name].Ids.remove(datasource.context.record.id);
                }
            }
            
            new XHR({method: 'get',async: true,onSuccess: this.onSuccess.bind(this), onFailure: this.onFail.bindAsEventListener(this)}).send('./bookmark.aspx', 'remove=' + name + ':' + datasource.context.record.id);
        },
        
        
        DeleteId : function(id)
        {
            new XHR({method: 'get',async: true,onSuccess: this.onRemoveIdSuccess.bind(this), onFailure: this.onFail.bindAsEventListener(this)}).send('./bookmark.aspx', 'removeid=' + id);
        },
        
        Contains : function(datasource)
        {
            if (!datasource || !datasource.context.record.id) return;
            
            var db = this.context.associate(this.keys);
            var name = datasource.context.fullname;
            var ids = [];
            
            if (this.keys && this.keys.contains(name))
            {
                ids = db[name].Ids;
                var flag = false;
                ids.each( function(id, index)
                {
                    if (id == datasource.context.record.id) 
                    {
                        flag = true;
                        return;
                    }
                    
                },this);
            }
            
            return flag;
        },
        
        Save : function()
        {
            DScript.saveState(Json.toString(this.context),'Bookmarks')
        },
        
        onRemoveIdSuccess : function(response)
        {
            if (response != 0)
            {
                this.context = new Array();
                this.context = Json.evaluate(response);
                this.keys = new Array();
                
                 this.context.each( function(src, index)
                 {
                    this.keys.extend([src.Name]);
                 },this);
                 
                 this.Save();
            }
        },
        
        onRestoreSuccess : function(response)
        {
            if (response != 0)
            {
                this.context = Json.evaluate(response);
                
                 this.context.each( function(src, index)
                 {
                    this.keys.extend([src.Name]);
                 },this);
            }
            
            this.onSuccess(1);
        },
        
        onSuccess : function(response)
        {
            if (response > 0)
            {
                this.Save();
            }
        },
        
        onFail : function(response)
        {
        
        },
        
        Command : function(command)
        {
            
            this.myXHR.send('./bookmark.aspx', command);
        }
    })
});

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}