/* 
 * Biblioteka przydatnych funkcji opracowana
 * przez DT Studio s.c.
 */

/**
 * Funckja zwraca obiekt o podanym id,
 * lub obiekt jeśli parametr jest obiektem
 */
function el(elementName) {
  return $(elementName);
}

function $(elementName) {
    var isString = (elementName === elementName.toString());
    if (isString) return document.getElementById(elementName);
    else return elementName;
}

function $$(elementName) {
    return document.getElementsByName(elementName);
}


/**
 * Wykonuje podobne zadanie co innerHTML, ale tutaj dla elementów XML
 * służy tylko do odczytu
 * @param xmlElement - element, któremu należy odczytać zawartość
 */
function innerXML(xmlElement) {
      var inner = "";
      for (var child = xmlElement.firstChild;
          child; child = child.nextSibling) {
          inner += child.nodeValue;
      }
      return inner;
}


/**
 * Umożliwia edycję komórki TD tabeli
 * oraz odpowiada oraz aktualizuje tabelę po edycji
 *
 * td - edytowalna komórka
 * id - id lub inne wskazanie elementu w bazie danych
 * columnName - określenie edytowalnej kolumny w bazie danych
 * type - typ edytowanej wartości
 */
function CellEditor(td, id, columnName, type, updateFunction) {

  if (CellEditor.currentEditor) { 
      if (confirm("Zapisać zmiany w edytowalnym polu?")) {
          CellEditor.currentEditor.send();
          CellEditor.currentEditor = null;
      }
      else {
          CellEditor.currentEditor.input.focus();
          return;
      }
  }

  CellEditor.currentEditor = this;

  this.onclick = td.onclick;
  td.onclick = '';

  this.cell = td.innerHTML;
  var instance = this;
  
  td.innerHTML = this.getEditor(columnName);
    
  this.input = td.firstChild;
  this.input.value = this.cell;
  this.input.focus();
  this.input.select();
  
  CellEditor.prototype.keyPress = function(event) {

    var klawisz = event.keyCode;

        switch(klawisz) {

            case 13:
                this.value = event.target.value;
                updateFunction(instance);
                break;

            case 27:
                td.innerHTML = instance.cell;
                td.onclick = instance.onclick;
                break;

            default:
//                alert('Testujemy: ' + klawisz);
        }
  }
  
  input.onkeypress = this.keyPress;
}


/**
 * Metoda prywatna, służy do pobrania właściwego dla tabeli edytora
 */
CellEditor.prototype.getEditor = function(type) {
    
    var inputHTML;
    var max;
    
    switch(type) {

        case 'date':
          max = 10;
          break;
        
        case 'time':
          max = 8;
          break;

        case 'text':

        default:
    }
    
    inputHTML = '<input type="text" ';
    if (max) inputHTML += ' maxlength="' + max;
    inputHTML += '" />';
    
    
    return inputHTML;
}


CellEditor.prototype.currentEditor = null;

CellEditor.prototype.send = function() {
    alert("Zatwierdź ENTEREM zmiany w edytowanym polu lub anuluj przez ESC");
    return false;
}

CellEditor.prototype.input = null;


/**
 * ustawia domyślne wartości dla zmiennych
 */
function xxxdef(variable, defValue) {
    if (!variable) variable = defValue;
}

function getBodySize() {
    var w = document.body.offsetWidth;
    var h = document.body.offsetHeight;

    var size = {width : w, height : h};
    return size;
}


function WebElement(elementName, tagName, parentElement) {

    var elem = el(elementName);

    if (!elem) {
        if (!tagName) tagName = 'div';
//        if ()
//        elem = document.createElement(tagName);
        parentElement.appendChild(elem);
    }
    else alert('mam element');
}
//
//function prototype DTElement_setX = function(x) {
////    var left = this.e.
//    this.elem.style.left = x;
//}



/**
 * Klasa timera
 */
function Timer(period, actionListener) {
    var instance = this;
    this._period = period;
    this._actionListener = actionListener;
    this._time = 0;
    
    if (!Timer.prototype.fire) 
      Timer.prototype.fire = function() {
          instance._time += instance._period;

          var event = new Object;
          event.
          instance._time += instance._period;
          event.time = instance._time;
          instance._actionListener.actionPerformer(instance._time);
      }
}

Timer.prototype.start;
Timer.prototype.stop;
Timer.prototype.restart;


Timer.prototype.setPeriod = function(period) {
    this._period = period;
}

Timer.prototype.addEctionListener = function(actionListener) {
    this._actionListener = actionListener;
}
