
// @param e : string or array of string for target id
function $( e ){
  if( arguments.length > 1 ){
    for( var i = 0, X = arguments.length,  r = [] ; i < X ; i++ )
      r.push( $( arguments[i] ) );
    return r;
  }
  else
    return typeof e == "string" ? document.getElementById( e ) : false ;
}

// get a new element
function $E( t ){ return document.createElement( t || "div" ); }

// get a new textnode
function $T( c ){ return document.createTextNode( c || "" ); }

// delete by id
function $D( d ){
  var n = $( d );
  try{ n.parentNode.removeChild( n ); }catch( e ){ return false }
}

// @param cname : class of the elements you want, say 'MENUITEM'
// @param elType : type of the elements you want, say 'div','span','a'..
function getElementsByClass( cName, elType ){
  var tb = document.getElementsByTagName( elType );
  for( var i = 0, rtab = [], X = tb.length, j = 0 ; i < X; i++ ){
    if(tb[i].className == cName){
      rtab[j] = tb[i];
      j++;
    }
  }
  return rtab;
}

//-----------------------------------------------------------------------6>
// Ajax ...
//-----------------------------------------------------------------------6>

//-----------------------------------------------------------------------6>
// get That xmlHTTPObject
//-----------------------------------------------------------------------6>
function getXhr(){
  return Try.these(
		   function(){return new XMLHttpRequest()},
		   function(){return new ActiveXObject('Msxml2.XMLHTTP')},
		   function(){return new ActiveXObject('Microsoft.XMLHTTP')}
		   ) || false;
}

//-----------------------------------------------------------------------6>
// things you might want to do with an xmlHTTPObject
//-----------------------------------------------------------------------6>
// @param method : 'POST' or 'GET'
// @param url : whatever url you want, but on your server only
// @param data : data to POST or GET, like 'ID=12&NAME=foo&COLOR=BLUE'
function sendData( method, url, data ){
  var x = getXhr();
  if( !x ) return false;
  if( method == "GET" ){
    var t = !data ?  url : url + '?' + data; 
      x.open( "GET", t, true );
      x.send( null );
  }else if( method == "POST" ){
    x.open( "POST", url, true );
    x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    x.send( data );
  }
  return x;
}

var Try = {
  these: function(){
    var r;
    for(var i = 0, X = arguments.length; i < X ; i++ ){
      try{
        r = arguments[i]();
        break;
      }catch(e){}
    }
    return r;
  }
};

//-----------------------------------------------------------------------6>
// Prototype-like Browser detection
//-----------------------------------------------------------------------6>
var Browser = {
  IE : !!(window.attachEvent && !window.opera),
  Opera : !!window.opera,
  WebKit : navigator.userAgent.indexOf('AppleWebKit/') > -1, // safari
  Gecko : navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('AppleWebKit/') == -1, // all Moz
  MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
};

function getClientSize(){
  if( Browser.IE ) 
    return [document.body.clientWidth,document.body.clientHeight];
  else
    return [window.innerWidth,window.innerHeight] || false;
}

// get the cursor position 
// @param e: the event
function getCurPos(e){
  if( !e ) var e = window.event;
  if( Browser.IE )
    return [event.clientX,event.clientY];
  if( Browser.Opera )
    return [e.pageX,e.pageY];
  else //  if( Browser.Gecko || Browser.WebKit ) // all others
    return [e.clientX,e.clientY] || false;
}

function getScrollXY(){
  return Try.these(
		function(){return [document.body.scrollLeft,document.body.scrollTop];},
		function(){return [document.documentElement.scrollLeft,document.documentElement.scrollTop];},
		function(){return [window.pageXOffset,window.pageYOffset];}
		) || false;
}

function closePop( id ){
  $( id ).style.display = "none";
  $D( 'TPCTT_'+id );
}

function oPop( id, s, ctt, apd ){
  this.unit = "px";

  if( 'object' === typeof id ) this.c = id; 
  else{
    this.c = $( id ) || $E() ;
    this.c.id = id;
  }
  this.c.style.width = s[0] + this.unit;
  this.c.style.height = s[1] + this.unit;
  this.c.style.left = s[2] + this.unit;
  this.c.style.top = s[3] + this.unit;
  this.c.style.position = "absolute";
  this.c.style.overflow = "auto";
  document.body.appendChild( this.c );
  this.draw = function( ctt ){
    var m = $( "TPCTT_" + this.c.id ),
    e = $E();
    if( m && !apd ) this.c.removeChild( m );    
    e.id = "TPCTT_" + this.c.id;
    "object" == typeof ctt ?  e.appendChild( ctt ) : e.innerHTML = ctt ;
    this.c.appendChild( e );
    this.c.style.display = "block";
  };
  this.draw( ctt );
  return this.c;
}

// @param id : either an id of an existing container or a new id ( and new container !)
// @param hf : horizontal flex, like [1,2,1]
// @param vf : vertical flex, like [1.5,2,1.5]
// @param ctt : content, either string or Element
// @param apd : def to false => remove the last ctt from container, if true append
function flexPop( id, hf, vf, ctt, apd ){
  var cs = getClientSize(),
    scr = getScrollXY(),
    sh = hf[0] + hf[1] + hf[2],
    sv = vf[0] + vf[1] + vf[2],
    w = parseInt( ( hf[1] / sh ) * cs[0] ),
    h = parseInt( ( vf[1] / sv ) * cs[1] ),
    x = parseInt( ( ( hf[0] / sh ) * cs[0] ) + scr[0] ),
    y = parseInt( ( ( vf[0] / sv ) * cs[1] ) + scr[1] );
  return new oPop( id, [w,h,x,y], ctt, apd );
}

// attach an event to el, crossbrowser
function addEvent( el, et, funk ){
  return el.addEventListener ?
    el.addEventListener( et, funk, false ) :
    el.attachEvent( et, funk );
}

// detach an event to el, crossbrowser
function removeEvent( el, et, funk ){
  return el.removeEventListener ?
    el.removeEventListener( et, funk, false ) :
    el.detachEvent( et, funk );
}

// append one or more childs
// Node.prototype.appendChilds = function( e ){
//   if( arguments.length > 1 )
//     for(var i = 0, X = arguments.length ; i < X; i++ )
//       this.appendChild( arguments[i] );
//   else
//     this.appendChild( e );
// }

function dump( o ){
  var dmp = "";
  for( p in o ){
    if( o[p] instanceof Array ){
      for(var i=0,X=o[p].length,v='['; i < X ; i++ ) v += "i : "+ o[p][i] +", " ;
      v += "]{"+X+"}";
    }else
      v = o[p];
    dmp += p +": "+ v +"\n";
  }
  return dmp;
}

function pdump( o ){
  var f = flexPop("IPOP",[1,5,1],[1,7,1], "<pre>" + dump( o ) + "</pre>" );
  f.style.backgroundColor = "#CC3377";
}

function getCurProp( el, prop ){
  if(el.currentStyle) // IE
    return el.currentStyle[prop];
  if(document.defaultView) // GECKO
    return document.defaultView.getComputedStyle(el, '').getPropertyValue( prop );
  return false;
}

