//-------------------------------------------------------------------
// Define WxSiteWidgets.Setup module
//-------------------------------------------------------------------

if (typeof(JSAN) !== 'undefined') {
  JSAN.use('MochiKit.Base');
  JSAN.use('MochiKit.DOM');
}

if (typeof(WxSiteWidgets) === 'undefined') {
  WxSiteWidgets = {};
}

WxSiteWidgets.Setup = {};
WxSiteWidgets.Setup.NAME = 'WxSiteWidgets.Setup';
WxSiteWidgets.Setup.VERSION = '1.0';

WxSiteWidgets.Setup.EXPORT = ['Setup'];

WxSiteWidgets.Setup.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};
WxSiteWidgets.Setup.toString = function () {
    return this.__repr__();
};

//-------------------------------------------------------------------
// WxSiteWidgets.Setup.LiveSetup
//-------------------------------------------------------------------
WxSiteWidgets.Setup.LiveSetup = function(options) {
  // private
  update(
    options = {
      'widgets': ['googlemap','hilite','livesearch','wx-livesearch']
    }, options || {}
  );  
  return {
    // public
    loaded: {
      'hilite': false,
      'livesearch': false,
      'wx-livesearch': false,
      'googlemap': false
    },
    
    dispatch: {
      'googlemap':  {
        //'script':(typeof(google.maps)=="undefined") ? '/libjs/intraxxion/WxSiteWidgets/Google/Maps.js':'/libjs/intraxxion/WxSiteWidgets/Google/Maps2.js', 
        'script':'/libjs/intraxxion/WxSiteWidgets/Google/Maps.js', 
        'klass': 'WxSiteWidgets.Maps.Map'
      },
      'livesearch': {
        'script':'/libjs/intraxxion/WxSiteWidgets/Search/Search.js', 
        'klass': 'WxSiteWidgets.Search.LiveSearch'
      },
      'wx-livesearch': {
        'script':'/libjs/intraxxion/WxSiteWidgets/Search/WXSearch.js', 
        'klass': 'WxSiteWidgets.WXSearch.LiveSearch'
      },
      'hilite': {
        'script':'/libjs/intraxxion/WxSiteWidgets/Hilites/Hilite.js', 
        'klass':'WxSiteWidgets.Hilites.Hilite'
      }
    },
    
    init: function() { 
      bindMethods(this);
      map(this.createWidgets, options.widgets);
    },
    
    createWidgets: function(ob) {
      elements = getElementsByTagAndClassName('wx:widget', ob);
      if (elements.length === 0) {
        elements = getElementsByTagAndClassName('div', ob);
      }      
      if (elements.length>0) {
        // recurse through iterator to initilize 1 or more widgets
        forEach(iter(elements), this.setupWidget, this);
      }
      return;
    },
    
    setupWidget:function(ob) {      
      try {
        // import the widget JS
        if (this.loaded[ob.className]===false) {
          this.loaded[ob.className] = true;
          // Only load script once
          d = this.loadWidgetScript(this.dispatch[ob.className].script);
          d.addCallback(this.loadWidgetScriptReady);
          d.addCallback(this.initWidget, ob);
          d.addErrback(function(err){
            if (e !== StopIteration) {
              log(ob.className+': '+err); 
            } else {
              throw e;
            }
          }); 
        } else {
          //this.dispatch[ob.className].init(ob);
          this.initWidget(ob);
        }
      } catch(e) {
        if (e !== StopIteration) {
          throw e;
        }
        // pass
      }        
    },
    
    initWidget: function(ob) {
      // Initialize the widget
      var opts = null;
      try {
        opts = evalJSON(ob.getAttribute('options'));
      } catch(e1) {
        alert('Geen options attribuut! '+e1.message);
      }
      try {
        ob.removeAttribute('options');
        ob.innerHTML='';
      } catch(e2) {
        // only for cleanup googlemap
      }
      var wdg = eval(this.dispatch[ob.className].klass); 
      var gm = new wdg(ob, opts);
    },
        
    loadWidgetScript:function(url) {
        return MochiKit.Async.doSimpleXMLHttpRequest(url);
    },
    
    loadWidgetScriptReady: function(data) {
       eval(data.responseText); 
    }
        
  }.init();
};

WxSiteWidgets.Setup.__new__ = function () {
    MochiKit.Base.nameFunctions(this);
    this.EXPORT_TAGS = {
      ":common": this.EXPORT,
      ":all": this.EXPORT
    };
};

WxSiteWidgets.Setup.__new__();
MochiKit.Base._exportSymbols(this, WxSiteWidgets.Setup);
