// JavaScript Document
// OBJ FRAMEWORK v 2010-02-16

var obj_tgts = new Array();

String.prototype.css2js = function (){
  var p=this.indexOf('-');
  if(p>0){
    var s = this.toLowerCase();  
    return s.substr(0,p)+s.substr(p+1,1).toUpperCase()+s.substr(p+2);
  }
  return this;  
}
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
}
Function.method('inherits', function (parent) {
    var d = {}, p = (this.prototype = new parent());
    this.method('uber', function uber(name) {
        if (!(name in d)) {
            d[name] = 0;
        }        
        var f, r, t = d[name], v = parent.prototype;
        if (t) {
            while (t) {
                v = v.constructor.prototype;
                t -= 1;
            }
            f = v[name];
        } else {
            f = p[name];
            if (f == this[name]) {
                f = v[name];
            }
        }
        d[name] += 1;
        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
        d[name] -= 1;
        return r;
    });
    return this;
});
Function.method('swiss', function (parent) {
    for (var i = 1; i < arguments.length; i += 1) {
        var name = arguments[i];
        this.prototype[name] = parent.prototype[name];
    }
    return this;
});

function Opt(opt){
  this.options=new Object();
  if(opt!=null){
    if(typeof(opt)=='string'){
      var aP = opt.split(';');
      for(i=0;i<=(aP.length-1);i++){
        var aR = aP[i].split(':');
        this.options[aR[0]]=aR[1];
      }
    }else if (typeof(opt)!='undefined'){
      for(x in opt)this.options[x] = opt[x];
    }
  }
  this.setParams = function (options){
    if(typeof(options)=='string'){
      var aP = options.split(';');
      for(i=0;i<=(aP.length-1);i++){
        var aR = aP[i].split(':');
        this.options[aR[0]]=aR[1];
      }
      return true;
    }
    if (typeof(options)!='undefined'){
      for(x in options)this.options[x] = options[x];
    }
  },
  
  this.applyParams = function(o){
    if(typeof(o)!='object')return o;
    for(x in this.options){
      //alert(typeof(this.options[x]));
    
      if(x=='id'){ o.setAttribute("id",this.options[x]); }   
      //else if(x=='class'){ o.setAttribute("class",this.options[x]); }
      else if(x=='className'){ o.className = this.options[x]; }//fuck you, deBill
      else if(x=='name'){ o.setAttribute("name",this.options[x]); }
      else if(x=='formMethod'){ o.setAttribute("method",this.options[x]); }
      else if(x=='inputType'){ try{ o.setAttribute("type",this.options[x]);}catch(ex){}; }//fuck deBill
      else if(x=='inputValue'){ try{ o.setAttribute("value",this.options[x]);}catch(ex){o.value=this.options[x];}; }//opereta
      else if(x=='CELLPADDING'){ o.setAttribute("cellpadding",this.options[x]); }
      else if(x=='CELLSPACING'){ o.setAttribute("cellspacing",this.options[x]); }
      else if(x=='BORDER'){ o.setAttribute("border",this.options[x]); }
      else if(x=='COLSPAN'){ o.setAttribute("colspan",this.options[x]); }
      else if(x=='ROWSPAN'){ o.setAttribute("rowspan",this.options[x]); }
      else if(x=='ALIGN'){ o.setAttribute("align",this.options[x]); }
      else if(x=='VALIGN'){ o.setAttribute("valign",this.options[x]); }
      else if(x=='MARGINWIDTH'){ o.setAttribute("marginwidth",this.options[x]); }
      else if(x=='MARGINHEIGHT'){ o.setAttribute("marginheight",this.options[x]); }
      else if(x=='VSPACE'){ o.setAttribute("vspace",this.options[x]); }
      else if(x=='HSPACE'){ o.setAttribute("hspace",this.options[x]); }
      else if(x=='ALLOWTRANSPARENCY'){ o.setAttribute("allowTransparency",this.options[x]); }
      else if(x=='FRAMEBORDER'){ o.setAttribute("frameborder",this.options[x]); }
      else if(x=='SCROLLING'){ o.setAttribute("scrolling",this.options[x]); }
      else if(x=='TARGET'){ o.setAttribute("target",this.options[x]); }
      else if(x=='alt'){ o.setAttribute("alt",this.options[x]); }
      else if(x=='title'){ o.setAttribute("title",this.options[x]); }
      else if(x=='src'){ o.setAttribute("src",this.options[x]); }
      else if(x=='href'){ o.setAttribute("href",this.options[x]); }
      else if(x=='innerHTML'){ o.innerHTML=this.options[x]; }
      else if(x=='onabort'){ o.onclick = Function(this.options[x]); }
      else if(x=='onblur'){ o.onblur = Function(this.options[x]); }
      else if(x=='onchange'){ o.onchange = Function(this.options[x]); }
      else if(x=='onclick'){ o.onclick = Function(this.options[x]); }
      else if(x=='ondblclick'){ o.ondblclick = Function(this.options[x]); }
      else if(x=='onerror'){ o.onerror = Function(this.options[x]); }
      else if(x=='onfocus'){ o.onfocus = Function(this.options[x]); }
      else if(x=='onkeydown'){ o.onkeydown = Function(this.options[x]); }
      else if(x=='onkeypress'){ o.onkeypress = Function(this.options[x]); }
      else if(x=='onkeyup'){ o.onkeyup = Function(this.options[x]); }
      else if(x=='onload'){ o.onload = Function(this.options[x]); }
      else if(x=='onmousedown'){ o.onmousedown = Function(this.options[x]); }
      else if(x=='onmousemove'){ o.onmousemove = Function(this.options[x]); }
      else if(x=='onmouseout'){ o.onmouseout = Function(this.options[x]); }
      else if(x=='onmouseover'){ o.onmouseover = Function(this.options[x]); }
      else if(x=='onmouseup'){ o.onmouseup = Function(this.options[x]); }
      else if(x=='onreset'){ o.onreset = Function(this.options[x]); }
      else if(x=='onresize'){ o.onresize = Function(this.options[x]); }
      else if(x=='onselect'){ o.onselect = Function(this.options[x]); }
      else if(x=='onsubmit'){ o.onsubmit = Function(this.options[x]); }
      else if(x=='onunload'){ o.onunload = Function(this.options[x]); }
      else if(x=='draggable'){ o.onmousedown=obj_drag_element;}// = Function(this.options[x]); }
      else if(x=='hideable'){ o.onclick=obj_hide_element;}
      else if(x=='minimalizeable'){ o.ondblclick=obj_minimalize_element;}
      else if(((x=='float')||(x=='cssfloat')||(x=='cssFloat'))&&(this.options[x]!='')){ o.style['cssFloat'] = this.options[x];o.style['styleFloat'] = this.options[x];}
      else if((x!='')&&(this.options[x]!='')){ o.style[x.css2js()] = this.options[x] };
    }
  },
  
  this.applyTo = function(o,opt){
    this.options = new Object();
    if(typeof(o.getO)=='function') o = o.ob;
    if(typeof(o)=='string') o = document.getElementById(o);
    if(o==null)return o;if(typeof(o)!='object')return o;
    this.setParams(opt);
    this.applyParams(o);
    return o;
  }   
};
function Obj_base(opt){
  this.tagName = 'img';
  this.id = '';
  this.cl = '';
  this.ob = null;
  this.inside = new Array();
  this.options=new Opt(opt);
};
Obj_base.method('getO',function(o){
    if(typeof(o)!='object')return null;
    var tagTab=document.createElement(this.tagName);
    if((this.tagName=='input')&&(this.options.options['inputType']))
      tagTab.type=this.options.options['inputType'];
    o.appendChild(tagTab);
    this.options.applyParams(tagTab);
    this.ob = tagTab;
    if(this.ob.id!='undefined')this.id=this.ob.id;
    if(this.ob.className!='undefined')this.cl=this.ob.className;
    if(typeof(obj_tgts)!='undefined')obj_tgts.push(this);
    return tagTab;
  }
);  
Obj_base.method('getIn',function(o){
    if(typeof(o.getO)=='function') o = o.ob;
    if(typeof(o)=='string')o = document.getElementById(o);
    if(o==null)return o;if(typeof(o)!='object')return null;
    return this.getO(o);
  }
);
Obj_base.method('add',function(o){
    if(this.tagName=='img')return false;
    this.inside.push(o);
    return this.inside[this.inside.length-1];
  }
);
Obj_base.method('remove',function(){
    if(!this.ob)return false;
    this.ob.parentNode.removeChild(this.ob);
    this.ob=null;
  }
);
Obj_base.method('measure',function(){
    if(!this.ob)return false;
    p=this.ob;
    this.x=0;
    this.y=0;
    this.h=p.offsetHeight;
    this.w=p.offsetWidth;
    while(p && p.offsetParent)
      {
        this.x += p.offsetLeft;
        this.y += p.offsetTop;
        p = p.offsetParent;
      }
    this.xx=Number(this.x+this.w);
    this.yy=Number(this.y+this.h);
  }
);
function Obj(tagName,opt){
  this.tagName = tagName;
  this.options=new Opt(opt);
}
Obj.inherits(Obj_base);


function Obj_td(opt){
  this.tagName = 'td';
  this.options=new Opt(opt);
}
Obj_td.inherits(Obj_base);


function Obj_tr(opt){
  this.tagName = 'tr';
  this.options=new Opt(opt);
  this.cols = new Array();
}
Obj_tr.inherits(Obj_base);
Obj_tr.method('addCol',function(opt){
    this.cols.push(new Obj_td(opt));
    this.cols[this.cols.length-1].getIn(this);
    return this.cols[this.cols.length-1];
  }
);
Obj_tr.method('addCols',function(cols,opt){
    for(var i=0;i<cols;i++){
      this.cols.push(new Obj_td(opt));
      this.cols[this.cols.length-1].getIn(this);
    }
    return this.cols;
  }
);


function Obj_tbody(opt){
  this.tagName = 'tbody';
  this.options=new Opt(opt);
  this.rows = new Array();
}
Obj_tbody.inherits(Obj_base);
Obj_tbody.method('addRow',function(opt){
    this.rows.push(new Obj_tr(opt));
    this.rows[this.rows.length-1].getIn(this);
    return this.rows[this.rows.length-1];
  }
);
Obj_tbody.method('addCol',function(opt){
    return this.rows[this.rows.length-1].addCol(opt);
  }
);
Obj_tbody.method('addRowsAndCols',function(rows,cols,opt){
    for(var i=0;i<rows;i++){
      this.rows.push(new Obj_tr(opt));
      this.rows[this.rows.length-1].getIn(this);
      this.rows[this.rows.length-1].addCols(cols,opt);
   }   
    return this.rows;
  }
);

function Obj_table(opt){
  this.tagName = 'table';
  this.options=new Opt(opt);
  this.tbody = null;
}
Obj_table.inherits(Obj_base);
Obj_table.method('addBody',function(opt){
    this.tbody = new Obj_tbody(opt);
    this.tbody.getIn(this);
    return this.tbody;
  }
);
Obj_table.method('addRowsAndCols',function(rows,cols,opt){
    if(!this.tbody)this.addBody(null);
    return this.tbody.addRowsAndCols(rows,cols,opt);
  }
);