/*
 *  graphic checkbox/tristate/quadstate control code, client-end part
 *  © 2001-2010, Horus Web Engineering Ltd
 *
 *  $Id: tristate.js,v 2.11 2010-06-04 11:33:14 horus Exp $
 *
 *  licensed under the terms of the GNU Lesser General Public License:
 *    http://www.opensource.org/licenses/lgpl-license.php
 *
 *  needs horus.js
 *  uses tooltip.js
 *
 */

horus.tick=new Object;
horus.tick.images=new Array(1);

horus.tick.tags=
  [ 'unset', 'yes', 'yesyes', 'no', 'delete', 'yesish', 'noish',
    'query', 'show', 'noshow' ];


horus.tick.control=
  function ( tick ) {
    if (typeof tick=='string') return document.getElementById(tick).firstChild;
    if (tick instanceof horus.event) tick=tick.control();
    var tagName=tick.tagName.toLowerCase();
    if (tagName=='input') return tick;
    if (tagName=='a') return tick.firstChild;
    if (tagName=='img') return tick.previousSibling;
    throw 'page error: bad parameter ('+tagName+') passed to horus.tick.control()';
  };


horus.tick.load=
  function ( imageset, width, height, type ) {
    var images=Array(20);

    for (var over=0; over<2; over++)
      for (var index=0; index<horus.tick.tags.length; index++) {
	var ptr=over*6+index;
	var image=new Image(width, height);

	image.src=
	  '/common/tristate/'+imageset+'-'+horus.tick.tags[index]+'.'+over+'.'+type;

	images[ptr]=image;
      }

    horus.tick.images[imageset]=images;
  };


horus.tick.set=
  function ( tick, imagesetname ) {
    var image=horus.tick.image(tick);

    if (!image.imagesetname) {
      var ident=image.src.match(/([\w\.-]+)-\w+\.\d\.(\w+)$/);
      image.imagesetname=ident[1];
      image.imagesettype=ident[2];
    }

    if (imagesetname && imagesetname!=image.imagesetname) {
      image.src=image.src.replace(/[\w\.-]+(-\w+\.\d\.\w+)$/, imagesetname+'$1');
      image.imagesetname=imagesetname;
    }

    if (!horus.tick.images[image.imagesetname])
      horus.tick.load(image.imagesetname, image.width, image.height, image.imagesettype);

    image.imageset=horus.tick.images[image.imagesetname];
    return image;
  };


horus.tick.image=
  function ( tick ) {
    var theNode=horus.tick.control(tick);
    do theNode=theNode.nextSibling; while (theNode.nodeType!=1);
    return theNode;
  };


horus.tick.form    = function ( tick ) { return horus.tick.control(tick).form };
horus.tick.name    = function ( tick ) { return horus.tick.control(tick).name };
horus.tick.value   = function ( tick ) { return Number(horus.tick.control(tick).value) };
horus.tick.checked = function ( tick ) { return horus.tick.value(tick)!=0 };
horus.tick.select  = function ( tick ) { return horus.tick.control(tick).select };


horus.tick.key=
  function ( event ) {
    event=new horus.event(event);
    var tick=event.target;
    if (!tick.onkeyup) tick.onkeyup=horus.tick.key;

    if (event.keyCode==32 || event.keyCode==13) {
      event.preventDefault();
      if (event.type=='keyup') tick.onclick(event);
      return false;
    }

    return true;
  };


horus.tick.over=
  function ( event, tick, tooltip )  {
    var image=horus.tick.image(tick);

    if (tooltip) {
      image._tooltip=true;
      horus.tooltip(event, tooltip);
    }

    horus.mouseover(image, 1);
  };


horus.tick.out=
  function ( tick ) {
    var image=horus.tick.image(tick);
    if (image._tooltip) horus.tooltip();
    horus.mouseover(image);
  };


horus.tick.label=
  function ( tick, newlabel ) {
    tick=horus.tick.control(tick);
    var theNode=tick;
    do theNode=theNode.nextSibling; while (theNode.nodeType!=3);
    var oldlabel=theNode ? theNode.nodeValue : '';

    if (newlabel!=null)
      if (theNode)
	theNode.nodeValue=newlabel;
      else
	tick.parentNode.insertBefore
	  (document.createTextNode(newlabel), tick.nextSibling);

    return oldlabel;
  };


horus.tick.click=
  function ( tick, value, select ) {
    tick=horus.tick.control(tick);

    if (arguments.length==1)
      tick.parentNode.onclick();
    else {
      tick.value=horus.isNumber(value) ? value : value ? 1 : 0;
      tick.select=select==null ? tick.value : select;
      var image=horus.tick.set(tick);

      image.src=image.src.replace
	(/-\w+(\.\d\.\w+)$/, '-'+horus.tick.tags[tick.select]+'$1');

    }
  };


horus.tick.cycle=
  function ( argv, subs ) {
    var wheels=subs.length;
    var name=argv[0];
    var tick=name;
    var count=1;

    if (name instanceof Array) {
      tick=name[0];
      count=name.length;
    }

    tick=horus.tick.control(tick);
    tick.oldvalue=parseInt(tick.value);
    var images=new Array(wheels);
    for (var i=0; i<wheels; i++) images[i]=i;
    var value=(tick.oldvalue+1)%wheels;
    var argc=argv.length;

    if (argc>1) {
      var alt=argv[1];

      if (alt instanceof Array)
	for (i=0; i<alt.length; i++) images[subs[i]]=alt[i];
      else if (alt!=null)
	images[subs[0]]=alt;

      if (argc>2) {
	value=argv[2];
	if (wheels==2) value=value ? 1 : 0;
      }
    }

    value%=wheels;
    tick.oldselect=images[tick.oldvalue];

    if (tick.firstvalue==null) {
      tick.firstvalue=tick.oldvalue;
      tick.firstselect=tick.oldselect;
    }

    var select=images[value];
    horus.tick.click(tick, value, select);
    while (count>1) horus.tick.click(name[--count], value, select);
  };


horus.tick.undo=
  function ( tick ) {
    tick=horus.tick.control(tick);

    if (tick.oldvalue!=null)
      horus.tick.click(tick, tick.oldvalue, tick.oldselect);

  };


horus.tick.reset=
  function ( tick ) {
    tick=horus.tick.control(tick);

    if (tick.firstvalue!=null)
      horus.tick.click(tick, tick.firstvalue, tick.firstselect);

  };


horus.bicycle   = function () { horus.tick.cycle(arguments, [ 0, 1 ]) };
horus.tricycle  = function () { horus.tick.cycle(arguments, [ 2, 0, 1 ]) };
horus.quadcycle = function () { horus.tick.cycle(arguments, [ 3, 2, 0, 1 ]) };

horus.script.loaded('tristate.js');
