/*
 *  Status box/area mangling © 2006-2011, Horus Web Engineering Ltd
 *
 *  $Id: status.js,v 1.24 2011-12-15 18:04:10 horus Exp $
 *
 *  licensed under the terms of the GNU Lesser General Public License:
 *    http://www.opensource.org/licenses/lgpl-license.php
 *
 *  needs horus.js, dom.js
 *  uses popover.js if popover is enabled
 *
 */

horus.script.load('dom');


horus.status         = function () { horus.status._add(arguments, true, false) };
horus.status.at      = function () { horus.status._add(arguments, true, true) };
horus.status.add     = function () { horus.status._add(arguments, false, false) };
horus.status.add.at  = function () { horus.status._add(arguments, false, true) };

horus.status.show    = function () { return horus.status._show(horus.status.box()) };
horus.status.hide    = function () { return horus.status._hide(horus.status.box()) };
horus.status.visible = function () { return horus.status.box()._visible };


horus.status.clear=
  function ( classname ) {
    horus.status._clear(horus.status.box(classname));
  };


horus.status.pending=
  function ( classname ) {
    horus.status._clear(horus.status.box(), true, classname);
  };


horus.status.group=
  function ( grouped, clear ) {
    if (grouped==null) grouped=true;
    if (clear==null) clear=grouped;
    var visible=horus.status.visible();

    if (clear) {
      var classname;
      if (typeof clear!='boolean') classname=clear;
      horus.status.clear(classname);
    }

    horus.status.grouped=grouped;
    horus.status.displayed={};
    return visible;
  };


horus.status.box=
  function ( classname ) {
    var status=horus.status.node;

    if (!status) {
      status=document.getElementById('statusbox');

      if (!status) {
	status=document.getElementById('content');
	if (!status) status=document.getElementById('admincontent');
	if (!status) status=document.body;

	status=horus.insertChild
	  (status,
	   [ 'div',
	     { id: 'statusbox', classname: 'error', style: { display: 'none' } } ]);

      }

      if (status._content==null) {
	status._content=horus.firstTag
	  (horus.firstTag(status, 'form') || status, 'div.error') || status;

	status._popover=horus.hasClass(status, 'popover');
      }

      horus.status.node=status;
    }

    if (classname!=null) status._content.className=classname;
    return status;
  };


horus.status._show=
  function ( status ) {
    if (status._visible) return;

    if (status._popover) {
      var pos={ moveable: horus.status._moveable, focus: horus.status._focus, level: 2 };

      if (horus.status._reference)
	pos.reference=horus.status._reference;
      else {
	pos.y=horus.status.yoffset;
	pos.x=horus.status.xoffset;
      }

      horus.popover(status, pos);
    } else
      status.style.display='block';

    status._visible=true;
    return false;
  };


horus.status._hide=
  function ( status ) {
    if (!status.initialised)
      status.initialised=true;
    else if (!status._visible)
      return;

    if (status._popover)
      horus.popover.hide(status);
    else
      status.style.display='none';

    status._visible=false;
    return false;
  };


horus.status._clear=
  function ( status, pending, classname ) {
    if (pending) {
      horus.status._pending=true;
      horus.status._classname=classname;
    } else {
      horus.status._pending=false;

      if (horus.status._classname!=null) {
	status._content.className=horus.status._classname;
	horus.status._classname=null;
      }

      horus.removeContents(status._content);
      horus.status._hide(status);
    }
  };


horus.status._add=
  function ( argv, clear, options ) {
    var argc=argv.length;
    var offset=0;
    var reference=false;
    var focus=false;
    var moveable=true;
    var classname=clear ? 'error' : null;

    if (options) {
      options=argv[offset++];

      if (options!=null)
	switch (horus.typeOf(options)) {

	case 'boolean':
	  focus=options;
	  break;

	case 'string':
	  if (options=='')
	    classname='';
	  else if (options.left(1)=='.')
	    classname=options.right(-1);
	  else
	    reference=options;

	  break;

	case 'object':
	  if (horus.isNode(options))
	    reference=options;
	  else if (horus.callable(options.control))
	    reference=options.control();
	  else {
	    if ('reference' in options) reference=options.reference;
	    if ('focus' in options) focus=options.focus;
	    if ('moveable' in options) moveable=options.moveable;
	    if ('classname' in options) classname=options.classname;
	  }

	}

    }

    var status=horus.status.box(classname);
    horus.status.reference(reference);
    horus.status._focus=focus;
    horus.status._moveable=moveable;

    if (horus.status.grouped) {
      var tag=horus.toString(horus.toArray(argv).slice(offset));
      if (horus.status.displayed[tag]) return;
      horus.status.displayed[tag]=true;
    }

    if (clear && !horus.status.grouped || horus.status._pending)
      horus.status._clear(status);

    if (argc<=offset) return;

    if (horus.childText(status._content, true)!='')
      horus.appendChild(status._content, horus.BR);

    var statustext=argv[offset++];

    if (offset==argc && horus.isSimpleValue(statustext) && /</.test(statustext)) {
      horus.appendChild(status._content, [ 'div', null ]).innerHTML=
	new String(statustext).replace(/\n/g, '<br/>');

      horus.status._show(status);
      return;
    }

    for (;;) {
      if (horus.isSimpleValue(statustext)) {
	statustext=new String(statustext).split(/(\n)/, true);

	for (var i=0; i<statustext.length; i++)
	  if (statustext[i]=='\n') statustext[i]=horus.BR;

	horus.appendChild(status._content, statustext, true);
      } else
	horus.appendChild(status._content, statustext);

      if (offset==argc) break;
      statustext=argv[offset++];
    }

    horus.status._show(status);
  };


horus.status.reference=
  function ( node ) {
    if (!node)
      horus.status._reference=null;
    else {
      var node=horus.getElement(node);
      if (node) horus.status._reference=node;
    }
  };


horus.status.init=
  function () {
    var status=document.getElementById('statusbox');

    if (status) {
      horus.status.box();

      if (status._popover) {
	status.style.top=horus.status.yoffset+'px';
	status.style.left=horus.status.xoffset+'px';
	status._visible=status.style.visibility=='visible';
      } else
	status._visible=status.style.display!='none';

    }
  };


horus.status.yoffset=100;
horus.status.xoffset=200;

horus.onLoad(horus.status.init);
horus.script.loaded('status');

