//tom.space = {};
/**
 * Tom Ordinarily Widget(TOW)
 */
tom.space.TOW = tom.Class.create();
tom.extend(tom.space.TOW.prototype, {
  initialize: function() {
    var fields = {
      id: null,
      body: null,
      autoRefresh: 0,
      //TODO
      lang: null,
      //TODO
      locale: null
    }
  },
  callback: function(sCallback) {
    eval("this." + sCallback + "(arguments)");
  },
  /**
   * events
   */
  //triggered when the widget is launched 
  onLoad: tom.fnEmpty,
  //triggered when the widget is refreshed (manually or automatically) 
  onRefresh: tom.fnEmpty,
  //TODO: triggered when the widget is resized (todo)
  onResize: tom.fnEmpty,
  //TODO: triggered when a search is performed within Netvibes. 
  onSearch: tom.fnEmpty,
  //TODO: triggered when the search is reset in Netvibes 
  onResetSearch: tom.fnEmpty,
  //TODO: triggered when a key is pressed. You can associate specific keys to specific methods
  onKeyboardAction: tom.fnEmpty,
  
  /**
   * methods
   */
  //adds content to the end of the existing body. If the content is of type string, addBody encapsulates it in a div before adding it all to the body 
  addBody: function(content) {
    if (typeof(content) == "string") {
      var el = document.createElement("DIV");
      el.innerHTML = content;
    } else {
      el = content;
    }
    this.body.appendChild(el);
  },
  //create a new DOM element 
  createElement: function(tagName) {
    return document.createElement(tagName);
  },
  //gets the value set in name 
  getValue: function(name) {
    var elem = document.getElementById(name + "_" + this.id) || document.getElementById(name);
	switch (elem.type.toLowerCase()) {
      case "checkbox":
		return elem.checked;
	  case "textarea":
		return elem.innerHTML;
      default:
		return elem.value;
    }
  },
  //TODO: logs string in the JavaScript console (provided the browser supports it, i.e. with Firebug, and that debugMode is set to true in the file's meta tags) 
  log: tom.fnEmpty,
  //opens a new browser window with the given URL 
  openURL: function(url) {
    
  },
  //sets autoRefresh meta value. delay is expressed in minutes 
  setAutoRefresh: function(delay) {
    this.autoRefresh = delay;
  },
  //sets the body of the widget. content must be valid XHTML code. The previous content of the body is erased and replaced by content 
  setBody: function(content) {
    this.body.innerHTML = content;
  },
  //TODO: updates the widget's title with the result count, if greater or equal to zero 
  setSearchResultCount: tom.fnEmpty,
  //sets the title of the widget 
  setTitle: function(title) {
    document.getElementById("mod_title_" + this.id).innerHTML = title;
  },
  //TODO: updates the title with the unread count, if greater or equal to zero 
  setUnreadCount: tom.fnEmpty,
  //sets name with the value value
  setValue: function(name, value) {
    var elem = document.getElementById(name + "_" + this.id) || document.getElementById(name);
    switch (elem.type.toLowerCase()) {
      case "checkbox":
		elem.checked = value == "ture" ? true : false;
        break;
	  case "textarea":
		elem.innerHTML = value;
      default:
		elem.value = value;
        break;
    }
  }
})

tom.space.TOW.Element = tom.Class.create();
tom.extend(tom.space.TOW.Element.prototype, {
  initialize: function() {
  },
  //adds content in a div at the end of the element's existing content 
  addContent: function(content) {  },
  //adds a new text node at the end of the element's existing content 	
  appendText: function(text)  {  },
  //empties the element. (  innerHTML = ¡±¡± equivalent) 
  empty: function()  {  },
  //returns a JSON object with 2 properties: width and height, each relating to the element's widht and height 
  getDimensions: function() {      return{
        width:this.offsetWidth,height:this.offsetHeight
      }},	
  //returns an array of elements corresponding to the given tag name 
  getElementsByTagName: function(tagName) {  },
  //return a reference to the element's parent node 
  getParent: function() { alert(this.parentNode.nodeName); },	
  //return a collection of the element's child nodes 
  getChildren: function() {  },
  //sets a text node in the element 
  setText: function(text) {  },	
  //sets the HTML content of the element (  innerHTML equivalent) 
  setHTML: function(html) {  },	
  //empties the element, then fills it with content 
  setContent: function(content) {  },	
  //sets the property CSS property, with the value value Display related extensions 
  setStyle: function(property, value) {  },	
  //sets the element's display CSS rule to none 
  hide: function() {  },	
  //empties the element's display CSS rule 
  show: function() {  },	
  //toggles the element's visible CSS rule between hide and show 
  toggle: function() {  },	
  //removes the element from the DOM tree Class related extensions 
  remove: function() {  },	
  //returns all the elements that are associated with the given CSS class name 
  getElementsByClassName: function(className) {  },	
  //returns true if the element has the given class name as one of its class names 
  hasClassName: function(className) {  },	
  //adds the given class name to the element's existing class names 
  addClassName: function(className) {  },	
  //removes the given class name from the element's existing class names})
  removeClassName: function(className) {  }
})

tom.space.TOW.$element = function(id) {
  return tom.extend(document.getElementById(id), new tom.space.TOW.Element());
}
tom.space.TOW.Data = {
  //gets the content of a feed, in our JSON format 
  getFeed: function(url, callback, proxy) {
    //TODO
	if (typeof UWA.feedCallbackType == "undefined") UWA.feedCallbackType = "json";
    return this.request(url, { method : 'post', proxy: 'feed', type: UWA.feedCallbackType, onComplete: callback } );
  },
  //gets the content of an external XML data source. It can be used to retrieve the content of a feed in XML format 
  getXml: function(url, callback, proxy) {
    //TODO
  },
  //gets the content of an external data source, in JSON format. The JSON format is the one from the API you call, not the same as the one obtained through UWA.Data.getFeed() 
  getJson: function(url, callback, proxy) {
    //TODO
	return this.request(url, { method : 'post', proxy: 'ajax', type: 'json', onComplete: callback }, proxy );
  },
  //gets the content of an external XML data source, in plain text format 
  getText: function(url, callback, proxy) {
    //TODO
    	return this.request(url, { method : 'post', proxy: 'ajax', type: 'text', onComplete: callback }, proxy );
  },
  //this is the master data request method, of which the getXml(), getJson() and getText() are shortcuts to
  //request:
//       { 
//     method: 'get', 
//     proxy: 'ajax', 
//     type: 'xml', 
//     cache: 3600,
//     onComplete: YourWidgetName.dataProcessor
//     }
  request: function(url, request, proxy) {
    var req = tom.extend(
      {
        method: "post",
        proxy: "ajax",
        type: "text",
        cache: 3600,
        onComplete: tom.fnEmpty
      },
      request);
    //urlÓÃproxy×ªÒ»ÏÂ
	if(proxy !== false)	{
	  url = "/proxy.php?url=" + url;
	}

    new tom.XHConn().connectAsync(
        url, 
        req.method, 
        "", 
        function(xhr) {
          switch (req.type) {
            case "xml":
              req.onComplete(xhr.responseXML);
              break;
            case "json":
              req.onComplete(eval("(" + xhr.responseText + ")"));
              break;
            default:
              req.onComplete(xhr.responseText);
              break;
          }
          
        }
      );
  }
}

var UWA = tom.space.TOW;
var tow3rd = {};
