
//mdp_cfg = {};
mdpc_v = {};
cfg = {};
session = {};
profile = {};

/*

a safe and fast fromJSON function ?

function safeObject(s) {
			var o = new Object();
			var a0 = s.split(’,’);
			var al = a0.length-1;
			$.each(a0, function(i) {
				// First item
				if (i == 0) {
					var sl = this.length-1;
					var nv = this.substr(1, sl);
				}
				// Last item
				else if (i = al) {
					var sl = this.length-1;
					var nv = this.substr(0, sl);
				}
				// Mid items
				else { var nv = this; }
				nv = nv.replace(’ ‘, ‘’);
				var nvs = nv.split(’:’);
				o[nvs[0]] = nvs[1];
			});
			return o;
		}

*/

mdpc=
 {
  init:function()
   {
    eval('mdpc_v = '+$("#vars_form > #vars_field").val());
    cfg = this.cfg = mdpc_v.cfg;
    session = this.session = mdpc_v.session;
    profile = this.profile = mdpc_v.profile;
    
    this.ui_notready = 0;
    this.ready=null;
    
    this.hidden_div = $('<div style="display:none" id="mdpc_dialogs">t</div>').prependTo($('body'));
    this.queue = [];
   },
  ui_notready_inc:function()
   {
    this.ui_notready++;
   },
  ui_notready_dec:function()
   {
    this.ui_notready--;
    if ((this.ui_notready ==0) && (this.ready))
     {
      this.ready(this);
      this.ready = null;
     }
   },/*
  enqueue:function(callback, client, level)
   {
    var elt = {'level':level, 'cb':callback, 'client':client};
    for (var i = this.queue.length-1 ; i >= 0 ; i--)
     {
      // the '!' and the '<' are not equivalent to '>' because of 'undefined' value handling !!
      if (!(level < this.queue[i].level))
       {
        this.queue.splice(i, 0, elt);
        break;
       }
      if (i < 0)
       {
        this.queue.splice(0, 0, elt);
       }
     }
   },
  remove:function(id)
   {
   },
  dequeue:function()
   {
   }, */
  var_set:function(group, varname, value, cb)
   {
    var expr = {};
    if (value == null)
     {
      expr = varname;
     }
    else
      expr[varname] = value;
    $.post(this.cfg.site_root+'tools/vdata_set',{'group':group, 'expr':$.toJSON(expr)}, cb);
    mdpc_v[group][varname] = value;
   },
  // Relational Data Anonymous Get
  rdata_aget:function(query, callback)
   {
    $.getJSON(this.cfg.site_root+'tools/rdata_aget',{'query':query}, callback);
   },
  dialog:function(content, show, autodestroy)
   {
    var self = this;
    var dlg =
     {
      show:function()
       {
        // simulate a click (for colorbox)
        this.activator.trigger('click');
       },
      hide:function()
       {
        $('#modalClose').trigger('click');
        //this.cb.close();
        //this.cb.colorbox('close');
       }
     };
    // if content is a jquery object, use first element as dialog content, else build a new one
    if (typeof content == 'string')
     {
      // build jquery/DOM object
     }
    selector = content;
      
    // create hidden activator
    dlg.activator=$('<a/>').appendTo(this.hidden_div);
    // create colorbox widget
    dlg.cb = dlg.activator.colorbox({inline:true, href:selector, "modalClose":''});

    if (show)
     {
      dlg.show();
     }
    return dlg;
   }
 };




