/*
Crossbrowser DOM graphical checkboxes
@author Chris Holden
@website http://www.chris-holden.com/
@copyright Chris Holden, 2006
*/
var Checkboxes = {

  ids : 0,

  init : function (name, on, off) {

    var inputs = (name == "*") ? document.getElementsByTagName("input") : document.getElementsByName(name);

    for (i = 0; i < inputs.length; i++) {
      if (inputs[i].nodeName == "INPUT") {
        inputs[i].onclick = function () { Checkboxes.doCheck(this.id); }
        inputs[i].onchange = function () { Checkboxes.doCheck(this.id); }

        var img = document.createElement("img");
        img.src = (inputs[i].checked) ? on : off;
        img.alt = (inputs[i].checked) ? "wybrany" : "nie wybrany";
        img.title = (inputs[i].checked) ? "wybrany" : "nie wybrany";

        newID = "ip_" + Checkboxes.ids;
        imgID = "im_" + Checkboxes.ids;

        if (inputs[i].id != "" && inputs[i].id != null) {
          Checkboxes.changeLabel(inputs[i].id, newID);
        }

        inputs[i].id = newID;
        img.id = imgID;
        img.onclick = function () { Checkboxes.doCheck(this.id); }
        Checkboxes.ids++;

        inputs[i].parentNode.insertBefore(img, inputs[i]);

        //inputs[i].style.position = "absolute";
        //inputs[i].style.left = "-1000px";
        inputs[i].style.display = "none";
      }
    }
  },

  doCheck : function (id) {
    var obj = document.getElementById(id);
    var id = id.replace(/i(m|p)_/gi, "");

    if (obj.nodeName == "IMG") {
      obj = document.getElementById("ip_" + id);
      checked = !obj.checked;
    } else {
      checked = obj.checked;
    }

    Checkboxes.checkImage("im_" + id, checked);
    Checkboxes.checkInput("ip_" + id, checked);

    return true;
  },

  checkInput : function (id, isChecked) {
    document.getElementById(id).checked = isChecked;
    return true;
  },

  checkImage : function (id, isChecked) {
    var img = document.getElementById(id);
    img.src = (isChecked) ? img.src.replace(/_off/, "_on") : img.src.replace(/_on/, "_off");
    img.alt = (isChecked) ? "wybrany" : "nie wybrany";
    img.title = img.alt;

    return true;
  },

  changeLabel : function (oldID, newID) {
    var labels = document.getElementsByTagName("label");

    for (l = 0; l < labels.length; l++) {
      if (labels[l].htmlFor == oldID) {
        labels[l].htmlFor = newID;
        return true;
      }
    }
    return false;
  }
}

