
/********************[WIDGETS]*******************/
/* Un widget est créé par l'éditeur ou par un script serveur. Il est
 * stoqué et manipulé dans sa forme finale. Il est accompagné de
 * fonctions permettant de le controler des deux cotés.
 */

/* L'objet Widget */
var widgetCounter = 0;

function Widget(id) {
  alert(id);
}

Widget.prototype.register = function(node) {
  widgetId = widgetCounter++;
  widgets[widgetCounter] = node;
  return widgetId;
}

WidgetNoteBook.prototype.create = function() {
}

function WidgetNoteBook () {
}

/* Widget List */
WidgetList.prototype.parse = function(list) {
  childNodes = elementChildNodes(list);
  for(var i=0; i<childNodes.length; i++) {
    node = childNodes[i];
    if(node.nodeType == 1) {
      var name = nodeGetName(node);
      this.list[name]=node;
    }
  }
}

function WidgetList() {
  this.list = {};
}

/****[Notebook widget]****
 * It take a HTMLElement, append a div.tabs and a div.pages, move the
 * elements in div.pages and create links to select pages and set the
 * first page class attribute to "selected" */
function noteBookSelect(noteBookId, id) {
  var noteBook = document.getElementById(noteBookId);
  var tabs = getChildNodeByAttribute(noteBook, "class", "tabs");
  var pages = getChildNodeByAttribute(noteBook, "class", "pages");
  nodesRemoveAttribute(elementChildNodes(tabs), "class");
  nodesSetStyle(elementChildNodes(pages), "display", "none");
  document.getElementById(noteBookId + "Tab" + id).setAttribute("class", "selected");
  document.getElementById(noteBookId + "Page" + id).style.display = "block";//INSPECTME!class=selected doesn't work
}

function createLink(href, text) {
  var link = document.createElement("a");
  link.appendChild(document.createTextNode(text));
  link.href = href;
  return link;
}

function appendLink(parent, name, callBack) {
  var link = createLink(name, callBack);
  parent.appendChild(link);
  return link;
}

function nodesSetStyle(nodes, style, value) {
  for(var i=0; i<nodes.length; i++) {
    if(nodes[i].nodeType==1) { nodes[i].style[style] = value; }
  }
}

function elementSetStyle(element, style, value) {
  element.style[style] = value;
}

function nodesRemoveAttribute(nodes, attribute) {
  for(var i=0; i<nodes.length; i++) {
    if(nodes[i].nodeType==1) { nodes[i].removeAttribute(attribute); }
  }
}

function alertElement(element) {
  protoString = ""
  for(i in element) { protoString += i; }
  alert("type: " + typeof(element));
  alert("nodeType: " + element.nodeType +
	"\ntagName: " + element.tagName);
}

function elementChildNodes(element) {
  if(IE4) return element.children;
  return element.childNodes;
}

var nameIdCounter = 0;
function nodeGetName(node) {
  if(node.hasAttribute("name"))
    return node.getAttribute("name");
  var name = (nameIdCounter++);
  node.setAttribute("name", name);
  return name;
}

function appendElement(parent, tagName) {
  var element = document.createElement(tagName);
  parent.appendChild(element);
  return element;
}

function createText(text) {
  return document.createTextNode(text);
}

function appendText(parent, text) {
  var textNode = createText(text);
  parent.appendChild(textNode);
  return textNode;
}

function createButton(name, onclick) {
  var button = document.createElement("button");
  button.appendChild(document.createTextNode(name));//FIXME: setAttribut("value", name) ?
  button.onclick = onclick;
  return button;
}

function appendButton(parent, name, callBack) {
  var button = createButton(name, callBack);
  parent.appendChild(button);
  return button;
}

function createSelect(name, options, selected, onchange) {
  var select = document.createElement("select");
  select.setAttribute("name", name);
  for(var i = 0; i < options.length; i++) {
    option = document.createElement("option");
    if(options[i] == selected) option.setAttribute("selected", "true");
    option.appendChild(document.createTextNode(options[i]));
    select.appendChild(option);
  }
  select.onchange=onchange;
  return select;
}

function appendSelect(parent, name, options, selected, callBack) {
  var select = createSelect(name, options, selected, callBack);
  parent.appendChild(select);
  return select;
}

function clearNode(node) {
  while(node.hasChildNodes()) {
    node.removeChild(node.lastChild);
  }
}

function objectProperties(obj) {
  var names="";
  for(var name in obj) names += name + ", ";
  return names;
}

