var prettyCheckbox = new Object();
prettyCheckbox.boxes = Array();
prettyCheckbox.inputs = Array();
prettyCheckbox.addBox = function (el) {
  idata = el.name.split('|');
  el.iname = idata[0];
  el.ivalue = idata[1];
  var input = new Element('input',
    {
    'type': 'hidden',
    'name': el.iname,
    'value': ''
    }
  );
  el.adopt(input);
  this.boxes.push(el);
  this.inputs.push(input);

  el.style.backgroundPosition = '0px 0px';
  el.checked = el.rel.test(/checked/i);
  el.disabled = el.rel.test(/disabled/i);
  if (el.checked) this.check(el);
  if (el.disabled) this.disable(el);

  el.className = 'checkbox';


  el.addEvents(
    {
    'mouseover': function() {
        prettyCheckbox.mouseover(this);
      },
    'mouseout': function() {
        prettyCheckbox.mouseout(this);
      },
    'click': function() {
        prettyCheckbox.click(this);
      }
    }
  );
}

prettyCheckbox.mouseover = function (el) {
  pos = el.style.backgroundPosition.split(' ');
  posLeft = pos[0].toInt();
  posTop = pos[1].toInt();
  el.style.backgroundPosition = (posLeft+15)+'px '+posTop+'px';
}

prettyCheckbox.mouseout = function (el) {
  pos = el.style.backgroundPosition.split(' ');
  posLeft = pos[0].toInt();
  posTop = pos[1].toInt();
  el.style.backgroundPosition = (posLeft-15)+'px '+posTop+'px';
}

prettyCheckbox.click = function (el) {
  if (el.disabled) return false;

  if (el.checked) {
    this.uncheck(el);
  } else {
    this.check(el);
  }
}

prettyCheckbox.check = function (el) {
  pos = el.style.backgroundPosition.split(' ');
  posLeft = pos[0].toInt();

  el.checked = true;
  if (el.disabled) {
    el.style.backgroundPosition = posLeft+'px -30px';
  } else {
    el.style.backgroundPosition = posLeft+'px -15px';
  }
  prettyCheckbox.inputs[prettyCheckbox.boxes.indexOf(el)].value = el.ivalue;
}

prettyCheckbox.uncheck = function (el) {
  pos = el.style.backgroundPosition.split(' ');
  posLeft = pos[0].toInt();

  el.checked = false;
  if (el.disabled) {
    el.style.backgroundPosition = posLeft+'px -15px';
  } else {
    el.style.backgroundPosition = posLeft+'px -0px';
  }

  prettyCheckbox.inputs[prettyCheckbox.boxes.indexOf(el)].value = '';
}

prettyCheckbox.disable = function (el) {
  pos = el.style.backgroundPosition.split(' ');
  posLeft = pos[0].toInt();

  if (el.checked) {
    el.style.backgroundPosition = posLeft+'px -45px';
  } else {
    el.style.backgroundPosition = posLeft+'px -30px';
  }
}

prettyCheckbox.getBox = function (name) {
  var ret = false;
  prettyCheckbox.boxes.each(function(item, index)
    {
      if (item.name == name) ret = prettyCheckbox.boxes[index];
    }
  );
  return ret;
}

prettyCheckbox.getBoxInput = function (name) {
  var ret = false;
  prettyCheckbox.boxes.each(function(item, index)
    {
      if (item.name == name) {
        ret = prettyCheckbox.inputs[index];
      }
    }
  );

  return ret;
}

prettyCheckbox.getElementForm = function (el) {
  return el.getParent('form');
}

prettyCheckbox.load = function() {
  $$(document.links).filter(function(el)
    {
      if (el.rel && el.rel.test(/^checkbox/i)) {
        prettyCheckbox.addBox(el);
      }
    }
  )
}

