/*
Title: boot_man
Author: Kevin Huisman
&copy; Kevin Huisman 2006-2007 All Rights Reserved

Description:
  Config stuff,
  Server communcation stuff
*/
var Config = {
  ctrl : "admin.php",
  js_path : "js/",
  includes : ['prototype.js', 'scriptaculous.js', 'builder.js','effects.js','dragdrop.js','controls.js','slider.js', 'ajax.js', 'lsp_ctrl.js', 'lightbox.js'],
  init : function() {
    var tmp = location.href.split("/") ;
    if (tmp[tmp.length - 2] == "admin") {
      this.ctrl = "admin/index.php" ;
    }
    if (this.ctrl == "") this.ctrl = "index.php" ;
    for (i in this.includes) this.incFile(this.includes[i]) ;
    this.includes = [] ;
  },
  getCfg : function (_which) { return this[_which] ; },
  setCfg : function (obj) { $H(obj).each(function (v) { Config[v[0]] = Config[v[1]] ; }) ; },
  incFile : function (_path) { document.write('<script type="text/javascript" src="' + this.js_path + _path + '"></script>'); }
}
// shortcut / alias to get at config settings
cfg = function(_which) { return Config[_which] } ;

// setup request helper

var Comm = {
  request : function(obj) {
    var _type = 'request' ;
    var _url = (obj['url']) ? obj['url'] : cfg('ctrl');
    if (obj['success']) { _type = 'update' ; var _succ = { success : obj['success'] } ; }
    var _meth = (obj['method']) ? obj['method'] : 'post' ;
    var _call = (obj['callback']) ? obj['callback'] : null ;
    var _param = $H() ;
    if (obj['formid']) _param.merge($(obj['formid']).serialize().toQueryParams()) ;
    if (typeof(obj['parameters']) == "string") obj['parameters'] = obj['parameters'].toQueryParams() ;
    _param = _param.merge(obj['parameters']).toQueryString() ;
    switch (_type) {
      case "update" : new Ajax.Updater( _succ, _url, { method : _meth, evalScripts : true, parameters : _param, onComplete : function(request){ Comm.callback(request, _call); }}) ; break ;
      case 'request' : new Ajax.Request( _url, { method : _meth, evalScripts : true, parameters : _param, onComplete : function(request){ Comm.callback(request, _call); }}); break ;
    }
  },
  callback : function (request, _call) { if (_call) _call(request) ; }
}

Config.init() ;