//  JavaScript Document

//  iconsTransform (2010-03-26)

//  typical using of this component
//  window.iconsTransform = new iconsTransform();
//  window.iconsTransform.applyTo('question-2-0');

//

function iconsTransform() {
  var self = this;
  //
  this.containerID = '';
  this.containerElem = null;
  //
  this.textsArr = null;
  this.inputsArr = null;
  //
  this.directory = 'images_icons_projects/';
  this.mainQuery = 'div.bigicons-projects';
  //
  this.init = function() {
    return true;
  };
  //
  this.applyTo = function(containerID) {
    self.containerID = containerID;
    self.containerElem = dojo.byId(self.containerID);
    if (!self.containerElem) return false;
    dojo.query(self.mainQuery,self.containerElem).forEach(self.transform);
    return true;
  };
  //
  this.transform = function(node,index) {
    self.textsArr = new Array();
    dojo.query('br',node.parentNode).forEach(function(node,index){
      node.parentNode.removeChild(node);
    });
    dojo.query('span',node).forEach(function(node,index){
      self.textsArr[index] = node.innerHTML.replace(/<[^>]*>/g,'');
      dojo.style(node,'display','none');
    });
    dojo.query('input',node).forEach(function(node,index){
      var newButton = dojo.create('img', { className: '', alt: '', src: self.directory+node.title+'-off.gif', title: self.textsArr[index]}, node.parentNode);
      newButton.iconsTransform = new Object();
      newButton.iconsTransform.checkElem = node;
      newButton.iconsTransform.parentClassName = self.mainQuery.replace(/^(.+)\./,'');
      if (node.checked) {
        newButton.src = newButton.src.replace('-off.','-on.');
      } else {
        newButton.onmouseover = self.onMouseOver;
        newButton.onmouseout = self.onMouseOut;
      }
      dojo.connect(newButton,'onclick',self.onClick);
      dojo.style(node,'display','none');
    });
  };
  //
  this.onClick = function (e,thisElem) {
    if (!thisElem) thisElem = this;
    var parentElem = thisElem.parentNode;
    while (parentElem.className!=thisElem.iconsTransform.parentClassName) {
      parentElem = parentElem.parentNode;
    }
    dojo.query('img',parentElem).forEach(function(node,index){
      node.src = node.src.replace('-on.','-off.');
      node.onmouseover = self.onMouseOver;
      node.onmouseout = self.onMouseOut;
    });
    thisElem.src = thisElem.src.replace('-off.','-on.');
    thisElem.iconsTransform.checkElem.checked = true;
    thisElem.onmouseover = null;
    thisElem.onmouseout = null;
  };
  //
  this.onMouseOver = function (e,thisElem) {
    if (!thisElem) thisElem = this;
    thisElem.src = thisElem.src.replace('-off.','-on.');
  };
  //
  this.onMouseOut = function (e,thisElem) {
    if (!thisElem) thisElem = this;
    thisElem.src = thisElem.src.replace('-on.','-off.');
  };
  //
  return self.init();
}

//  end of document