//fuggosegek: 
if (typeof USEFUL == "undefined") {
  alert("(EDITINPLACE) USEFUL include missing");
}
//tobbszoros include:
if (typeof EDITINPLACE != "undefined") {
  alert("EDITINPLACE multiple insert!");
}
EDITINPLACE=true;

/** Ez kell, hogy legyen egy statikus osztalyunk
  */
function EditInPlace() {
}

/** Ezzel a statikus metodussal lehet egy inputot hozzaadni a keretrendszerhez, ez beallitja a stilusat, esemenykezelok 
  * megcsinalasa
  * @param cfg a config objektum
  */
EditInPlace.addInput = function (cfg) {
  if (typeof this.inputStates == 'undefined') {
    //default classes
    this.classes={
       edit:             'edit',
       editOn:           'editOn',
       editFocus:        'editFocus'
    };
    this.inputStates=new Array(); 
  }
  
  var node=null;
    
  if (typeof cfg.id == 'string') {
    node=document.getElementById(cfg.id); 
  } else if (null != cfg.node) {
    node=cfg.node;
    cfg.id=node.id;
  }

  if (null == node) {
    throw new Error("EditInPlace: node undefined");
  } else if ("" == cfg.id) {    
    throw new Error("EditInPlace: ID undefined");
  }
  
  this.inputStates[cfg.id]={};
  EditInPlace.setDefaults(cfg);
  EditInPlace.readConfig(cfg);

  if (node) {
    addClass(node,this.inputStates[cfg.id]["classes"]['edit']);
    if (ieDOM) {
      //sajna inherit erteket nem ismeri IE, ezert kell trukkozni
      node.style.backgroundColor=getStyle(node.parentNode,'background-color');
    }
    //esemenykezelok
    addEvent(node,"mouseover",function() {EditInPlace.mouseOver(node);});
    addEvent(node,"mouseout",function() {EditInPlace.mouseOut(node);});
    addEvent(node,"focus",function() {EditInPlace.focus(node);});
    addEvent(node,"blur",function() {EditInPlace.blur(node);});
  }
}

/** Szokasos default parametereket beallito metodus
  * @param cfg a config objektum
  */
EditInPlace.setDefaults = function (cfg) {
  this.inputStates[cfg.id]["classes"]=this.classes;
}

/** Az egyedi beallitasokat vegzi el
  * @param cfg a config objektum
  */
EditInPlace.readConfig = function (cfg) {
  if (typeof cfg.classes == 'object') {
    this.inputStates[cfg.id]["classes"]=mergeObject(this.inputStates[cfg.id]["classes"],cfg.classes);
  }
}

/** Azt az esemenyt dolgozza fel, amikor az eger az input fole kerul
  * @param node az input objektum
  */
EditInPlace.mouseOver = function (node) {
  if (this.inputStates[node.id]["status"] != 'focus') {
//    node.style.border='1px solid #ff0000';

    removeClass(node,this.inputStates[node.id]["classes"]['edit']);
    addClass(node,this.inputStates[node.id]["classes"]['editOn']);
  }
}


/** Azt az esemenyt dolgozza fel, amikor az eger elhagyja az inputot
  * @param node az input objektum
  */
EditInPlace.mouseOut = function (node) {
  if (this.inputStates[node.id]["status"] != 'focus') {
//    node.style.border='none';
    removeClass(node,this.inputStates[node.id]["classes"]['editOn']);
    addClass(node,this.inputStates[node.id]["classes"]['edit']);
//    node.style.backgroundColor=getStyle(node.parentNode,'background-color');
  }
}

/** Azt az esemenyt dolgozza fel, amikor az input ki lesz jelolve
  * @param node az input objektum
  */
EditInPlace.focus = function (node) {
  this.inputStates[node.id]["status"]='focus';
  
//  node.style.border='1px solid #0000ff';
//  node.style.backgroundColor='#ff00ff';
    removeClass(node,this.inputStates[node.id]["classes"]['editOn']);
    addClass(node,this.inputStates[node.id]["classes"]['editFocus']);
    if (ieDOM) {
//      node.style.backgroundColor="#ff00ff";
      node.style.backgroundColor="";  //torolni kell az inline stilust, vagy removestyle is elintezi, de az az egeszet szedne ki
    }
}


/** Azt az esemenyt dolgozza fel, amikor az inputnak megszunik a kijelolese
  * @param node az input objektum
  */
EditInPlace.blur = function (node) {
  //ez inkabb nem hivja meg mouseOut-ot  
  this.inputStates[node.id]["status"]='';
//  node.style.backgroundColor='inherit';
//  node.style.border='none';
  removeClass(node,this.inputStates[node.id]["classes"]['editFocus']);
  addClass(node,this.inputStates[node.id]["classes"]['edit']);
  if (ieDOM) {
    node.style.backgroundColor=getStyle(node.parentNode,'background-color');
  }
}



