//----------------------------------------------------------------------------
// cb.js
//    some cross browser approaches
//----------------------------------------------------------------------------

var cbDbgFlag = false;

function isDef(p)  { return typeof(p) != 'undefined'; }
function allDef( )  {
   for(var i=0; i<arguments.length; ++i){
      if(typeof(arguments[i]) == 'undefined' ) return false;
   }
   return true;
}
function isStr(s)  { return typeof(s) == 'string'; }
function isNum(n)  { return typeof(n) == 'number'; }
function isBool(b) { return typeof(b) == 'boolean'; }
function isHex(nn, digits, prefix) {
   var n = Math.ceil(nn);
   n = n.toString(16);
   var p = prefix ? prefix : '';
   for (var i=0; i < digits - n.length; ++i) {
      p += '0';
   }
   return p + n;
}

function isCompatMode( ) {
   return( document.compatMode == 'CSS1Compat' && !window.opera &&
           document.documentElement );
}

function isDocument(e) {
   return( e == document || e.tagName.toLowerCase() == 'html' ||
           e.tagName.toLowerCase() == 'body' );
}

//----------------------------------------------------------------------------
// ... the very basic one ...
//----------------------------------------------------------------------------
function xGetElementById(e) {
   return   (typeof(e) != 'string')   ? e
          : (document.getElementById) ? document.getElementById(e)
          : (document.all)            ? document.all[ e ]
          : (document.layers)         ? xLayer(e)
          :                             null;
}

function xGetElementsByTagName(t, p, f) {
   var l = null;
   t = t || '*'; p = xGetElementById(p) || document;

   l = ( document.getElementById )
     ? ( p.getElementsByTagName ? p.getElementsByTagName(t) : new Array() )
     : ( ( t == '*' ) ? p.all : p.all.tags( t ) );
   if ( isDef( f ) && l && l.length ) {
      var maxind = l.length - 1;
      for ( var i = maxind; i >= 0; i-- )
         f( l[ i ] );
   }
   return l || new Array();
}

function xGetElementsByClassName(c, p, t, f) {
   var l  = new Array();
   var re = new RegExp( '\\b' + c + '\\b', 'i' );
   var tEls = xGetElementsByTagName( t, p );

   for ( var i = 0; i < tEls.length; i++ ) {
      if ( tEls[ i ].className && tEls[ i ].className.search( re ) != -1 ) {
         l[ l.length ] = tEls[ i ];
         if ( f ) f( tEls[ i ] );
      }
   }
   return l;
}

function xLayer(id, base) { 
   var i, layer, found = null;
   if (!base) base = window;
   for (i=0; i < base.document.layers.length; i++) {
      layer = base.document.layers[i];
      if (layer.id == id)	return layer;
      if (layer.document.layers.length) found = xLayer(id, layer);
      if (found) return found;
   }
   return null;
}

function xName(e) {
    if ( !e ) return e;
    try {
        return   e.id       && e.id       != "" ? e.id
               : e.name     && e.name     != "" ? e.name
               : e.nodeName && e.nodeName != "" ? e.nodeName
               : e.tagName  && e.tagName  != "" ? e.tagName
               :                                  e;
    }
    catch( err ) {
        return e;
    }
}

//----------------------------------------------------------------------------
// ... the visibility
//----------------------------------------------------------------------------
function xShow(e) {
   if (!(e = xGetElementById(e))) return;
   if (e.style && isDef(e.style.visibility)) e.style.visibility = 'visible';
   else if (isDef(e.visibility))             e.visibility = 'show';
}

function xHide(e) {
   if (!(e = xGetElementById(e))) return;
   if (e.style && isDef(e.style.visibility)) e.style.visibility = 'hidden';
   else if (isDef(e.visibility))             e.visibility = 'hide';
}

function xUnsetVis(e) {
   if (!(e = xGetElementById(e))) return;
   if (e.style && isDef(e.style.visibility)) e.style.visibility = '';
   else if (isDef(e.visibility))             e.visibility = '';
}

function xZIndex(e, uZ) {
   if (!(e = xGetElementById(e))) return 0;
   if (e.style && isDef(e.style.zIndex)) {
      if (isNum(uZ))    e.style.zIndex = uZ;
      uZ = parseInt(e.style.zIndex);
   }
   else if (isDef(e.zIndex)) {
      if (isNum(uZ))    e.zIndex = uZ;
      uZ = e.zIndex;
   }
   return uZ;
}

function xShowAllOf(A) { for (var i = 0; i < A.length; i++ ) xShow(A[i]); }
function xHideAllOf(A) { for (var i = 0; i < A.length; i++ ) xHide(A[i]); }

function xDisable(e, b) {
   if ( !(e = xGetElementById(e)) ) return 0;
   if ( e.style && isDef(e.style.disabled )) {
      if (isBool(b))    e.style.disabled = b;
      else              b = e.style.disabled;
   } 
   if ( isDef(e.disabled )) {
      if (isBool(b))    e.disabled = b;
      else              b = e.disabled;
   }
   return b;
}

function xShowByClassName(c, p, t) {
   xGetElementsByClassName(c, p, t, xShow );
}

function xHideByClassName(c, p, t) {
   xGetElementsByClassName(c, p, t, xHide );
}

function xSetStyle(e, prop, val) {
   if (!(e = xGetElementById(e))) return;

   var eprop = eval("e." + prop);
   var estyleprop = eval("e.style." + prop);

   if ( allDef(e.style, estyleprop))   estyleprop = val;
   else if (isDef(eprop))              eprop = val;
}

//----------------------------------------------------------------------------
//	... positions ...
//----------------------------------------------------------------------------

function xMoveTo(e,iX,iY) { 
   xLeft(e,iX); 
   xTop(e,iY);
 }

function xLeft(e, l) {
   if (!(e = xGetElementById(e))) return 0;
   if (isDef(e.style) && isStr(e.style.left)) {
      if (isNum(l))     e.style.left = l + 'px';
      else {
         l = parseInt(e.style.left);
         if (isNaN(l))  l = 0;
      }
   } else if (isDef(e.style) && isDef(e.style.pixelLeft)) {
      if (isNum(l))     e.style.pixelLeft = l;
      else              l = e.style.pixelLeft;
   } else if (isDef(e.left)) {
      if (isNum(l))     e.left = l;
      else              l = e.left;
   }
   return l;
}

function xTop(e, t) {
   if (!(e = xGetElementById(e)))   return 0;
   if (isDef(e.style) && isStr(e.style.top)) {
      if (isNum(t))     e.style.top = t +'px';
      else {
         t = parseInt(e.style.top);
         if (isNaN(t))  t = 0;
      }
   } else if (isDef(e.style) && isDef(e.style.pixelTop)) {
      if (isNum(t))     e.style.pixelTop = t;
      else              t = e.style.pixelTop;
   } else if (isDef(e.top)) {
      if (isNum(t))     e.top = t;
      else              t = e.top;
   }
   return t;
}

// Anchors - page positions
function xPageX(e) {
   // if (isDef(e.pageX)) { return e.pageX; }
   var x = 0;
   for (e=xGetElementById(e); e; e=isDef(e.offsetParent)?e.offsetParent:null) {
      if (isDef(e.offsetLeft)) { x += e.offsetLeft; }
   }
   return x;
}

function xPageY(e) {
   // if (isDef(e.pageY)) { return e.pageY; }
   var y = 0;
   for (e=xGetElementById(e); e; e=isDef(e.offsetParent)?e.offsetParent:null) {
      if (isDef(e.offsetTop)) { y += e.offsetTop; }
   }
   return y;
}

//----------------------------------------------------------------------------
//	... scrolls and Client widths
//----------------------------------------------------------------------------

// convert css property name --> IE object property name

function xCamelize( cssprop ) {
   var a = cssprop.split( '-' );
   var iep = a[0];
   for ( var i = 1; i < a.length; i++ ) {
      var c = a[i].charAt(0);
      iep  += a[i].replace( c, c.toUpperCase( ) );
   }
   return iep;
}

function xIEProperty( cssprop ) { return xCamelize( cssprop ); }

function xGetComputedStyle(e, p, toInt) {
   var val = 'undefined';
   var dv = document.defaultView;
   if ( dv && dv.getComputedStyle ){
      var s = dv.getComputedStyle( e, '' );
      if ( s ) val = s.getPropertyValue( p );
   } else if( e.currentStyle ) {    // IE, needs its own property name
      val = e.currentStyle[ xCamelize( p ) ];
   } else
      return null;
  
   return toInt ? (parseInt(val) || 0) : val;
}

// e       element id, string or object reference.
// bWin   if true, ele is assumed to be a reference to a window object

function xScrollLeft(e, bWin) {
   if ( !isDef( e ) || bWin || isDocument( e ) ) {
      var wd = bWin && e ? e.document : window.document;
      return ( wd.documentElement && wd.documentElement.scrollLeft
               ? wd.documentElement.scrollLeft
               : wd.body && isDef( wd.body.scrollLeft )
                  ? wd.body.scrollLeft
                  : 0 );
   } else {
      e = xGetElementById(e);
      return( e && isNum(e.scrollLeft) ? e.scrollLeft : 0 );
   }
}

function xScrollTop(e, bWin) {
   if ( !isDef( e ) || bWin || isDocument( e ) ) {
      var wd = bWin && e ? e.document : window.document;
      return ( wd.documentElement && wd.documentElement.scrollTop
               ? wd.documentElement.scrollTop
               : wd.body && isDef( wd.body.scrollTop )
                  ? wd.body.scrollTop
                  : 0 );
   } else {
      e = xGetElementById(e);
      return( e && isNum(e.scrollTop ) ? e.scrollTop : 0 );
   }
}

function xClientWidth() {
   var x = 0, d = document, w = window;
   if ( isCompatMode( ) && d.documentElement.clientWidth ) {
      x = d.documentElement.clientWidth;
   } else if ( d.body && d.body.clientWidth ) {
      x = d.body.clientWidth;
   } else if ( allDef(w.innerWidth, w.innerHeight, d.height ) ) {
      x = w.innerWidth;
      if ( d.height > w.innerHeight ) x -= 16;
   }
   return x;
}

function xClientHeight() {
   var y = 0, d = document, w = window;
   if ( isCompatMode( ) && d.documentElement.clientHeight ) {
      y = d.documentElement.clientHeight;
   } else if ( d.body && d.body.clientHeight ) {
      y = d.body.clientHeight;
   } else if ( allDef( w.innerWidth, w.innerHeight, d.width ) ) {
      y = w.innerHeight;
      if ( d.width > w.innerWidth ) y -= 16;
   }
   return y;
}

function xIntersects( f, e ) {
   var f_l = xPageX( f );
   var f_r = f_l + xWidth( f );
   var f_t = xPageY( f );
   var f_b = f_t + xHeight( f );
   var e_l = xPageX( e );
   var e_r = e_l + xWidth( e );
   var e_t = xPageY( e );
   var e_b = e_t + xHeight( e );
   // horizontal
   if ( ( f_l <= e_l && f_r < e_l ) || ( e_l <= f_l && e_r < f_l ) ||
        ( f_b >= e_b && f_t > e_b ) || ( e_b >= f_b && e_t > f_b ) )
      return false;
   else
      return true;
}

//----------------------------------------------------------------------------
// ... sizes ...
//----------------------------------------------------------------------------

function xResizeTo(e, wval, hval) {
   xWidth(e, wval);
   xHeight(e, hval);
}

function xWidth(e, val) {
   if (!(e = xGetElementById(e))) return 0;
   val = isNum(val) ? ( val < 0 ? 0 : Math.round(val) ) : -1;
   if ( isDocument(e) ) {
      val = xClientWidth( );
   } else if ( allDef(e.style, e.offsetWidth) && isStr(e.style.width) ) {
      if (val >= 0) {
         var pl=0,pr=0,bl=0,br=0;
         if ( document.compatMode == 'CSS1Compat' ) {
            pl = xGetComputedStyle(e,'padding-left',1);
            if ( pl !== null ) {
               pr = xGetComputedStyle(e,'padding-right',1);
               bl = xGetComputedStyle(e,'border-left-width',1);
               br = xGetComputedStyle(e,'border-right-width',1);
            } else if ( allDef(e.offsetWidth, e.style.width) ){
               e.style.width = val + 'px';
               pl = e.offsetWidth - val;
            }
         }
         val -= (pl+pr+bl+br);
         if ( isNaN(val) || val < 0 ) return -1;
         else e.style.width = val + 'px';
      }
      val = e.offsetWidth;
   } else if ( allDef(e.style, e.style.pixelWidth) ) {
      if (val >= 0) e.style.pixelWidth = val;
      val = e.style.pixelWidth;
   }

   return val;
}

function xHeight(e, val) {
   if (!(e = xGetElementById(e))) return 0;
   val = isNum(val) ? ( val < 0 ? 0 : Math.round(val) ) : -1;
   if ( isDocument(e) ) {
      val = xClientHeight();
   } else if ( allDef(e.style, e.offsetHeight) && isStr(e.style.height)) {
      if ( val >= 0 ) {
         var pt = 0, pb = 0, bt = 0, bb = 0;          // paddings and borders
         if (document.compatMode=='CSS1Compat') {
            pt = xGetComputedStyle(e,'padding-top',1);
            if ( pt !== null ) {
               pb = xGetComputedStyle(e,'padding-bottom',1);
               bt = xGetComputedStyle(e,'border-top-width',1);
               bb = xGetComputedStyle(e,'border-bottom-width',1);
            } else if ( allDef(e.offsetHeight, e.style.height) ) {
               e.style.height = val + 'px';
               pt = e.offsetHeight - val;
            }
         }
         val -= (pt+pb+bt+bb);
         if ( isNaN(val) || val < 0 ) return -1;
         else e.style.height = val + 'px';
      }
      val = e.offsetHeight;
   } else if ( allDef(e.style, e.style.pixelHeight) ) {
      if ( val >= 0 ) e.style.pixelHeight = val;
      val = e.style.pixelHeight;
   }
   return val;
}


function xClip(e, t, r, b, l) {
   if ( !(e = xGetElementById(e)) ) return;
   if ( e.style ) {
      e.style.clip = (isNum(l)) ?
         'rect(' + t + 'px ' + r + 'px ' + b + 'px ' + l + 'px)' :
         'rect(0 ' + parseInt(e.style.width) + 'px ' +
                     parseInt(e.style.height) + 'px 0)';
   } else if (e.clip) {
      if ( isNum(l) ) {
         e.clip.top = t; e.clip.right = r; e.clip.bottom = b; e.clip.left = l;
      } else {
         e.clip.top = 0; e.clip.right = xWidth(e);
         e.clip.bottom = xHeight(e); e.clip.left = 0;
      }
   }
}

//----------------------------------------------------------------------------
// ... motions ...
//----------------------------------------------------------------------------

function xSlideTo(e, x, y, tG) {
   if (!(e = xGetElementById(e))) return;
   if (!e.tS) e.tS = 25;
   e.halted = false;                         // to halt from outside
   e.tG = tG;                                // total time ( Gesamt )
   e.xE = x; e.yE = y;                       // end positions
   e.dY = y - xTop(e); e.dX = x - xLeft(e);  // deltaY, deltaX
   e.B = Math.PI / (2 * tG);                 // period
   e.y0 = xTop(e); e.x0 = xLeft(e);          // start positions
   var d = new Date(); e.t0 = d.getTime();
   if (!e.sliding) xSlide(e);
}

function xSlideBy(e, dx, dy, tG) {
   if (!(e = xGetElementById(e))) return;
   var x = e.xE ? e.xE + dx : xLeft(e) + dx;
   var y = e.yE ? e.yE + dy : xTop(e) + dy;
   xSlideTo(e, x, y, tG);
}

function xSlide(e) {
   if ( !(e = xGetElementById(e)) ) return;
   var tnow, s;
   if ( !e.halted ) {
      tnow = new Date() - e.t0;
      if ( tnow < e.tG ) {
         setTimeout("xSlide('" + e.id + "')", e.tS);
         s = Math.sin(e.B * tnow);
         xMoveTo(e, Math.round(e.dX * s + e.x0), Math.round(e.dY * s + e.y0));
         e.sliding = true;
      } else {
         xMoveTo(e, e.xE, e.yE);
         e.sliding = false;
      }
   } else e.sliding = false;
}

//----------------------------------------------------------------------------
//	... dom nodes ...
//----------------------------------------------------------------------------
function xSetTextval(e, text) {
   /*
		* has to stay as it is!!!!
		* implementations rely on it !!!
		* allthough this would bo more appropriate
   if (!(e = xGetElementById(e))) return;
   if (e.firstChild) e.firstChild.nodeValue = text;
   else if (document.all) e.innerText = text;
   */
   xInnerHtml(e, text);
}

function xSetSelByVal(e, val) {
   if (!(e = xGetElementById(e))) return false;
   for (var i = 0; i < e.length; i++) {
      if (e.options[ i ].value.indexOf(val) == 0 ) {
         e.selectedIndex = i;
         return true;
      }
   }
   return false;
}

function xInnerHtml(e, h) {
   if ((e=xGetElementById(e)) && isStr(e.innerHTML)) {
      var old = e.innerHTML;
      if (isStr(h)) e.innerHTML = h;
      return old;
   }
   else
      return null;
}
//----------------------------------------------------------------------------
// ... change display styles for e.c. table rows
//----------------------------------------------------------------------------

function xDisplay(e, s) {
   if (!(e = xGetElementById(e))) return null;
   return ( e.style && isDef(e.style.display) && isStr(s))
            ? (e.style.display = s)
            : null;
}

function xCollapseTableRowById(id) {
   xDisplay(id, 'none');
}

function xExpandTableRowById(id) {
   xDisplay(id, (document.all && !window.opera) ? 'block' : 'table-row');
}

function xDisplayTableRow(t_id, row, b) {
  var t = xGetElementById(t_id);
  if (t && row < t.rows.length) {
    t.rows[row].style.display = b ? '' : 'none';
  }
}

function xIsDisplayed(e) {
   if (!(e = xGetElementById(e))) return false;
   while ( e.nodeType != 9 && e.parentNode) {
      if ( e.style.display == 'none' ) return false;
      e = e.parentNode;
   }
   return true;
}
//----------------------------------------------------------------------------
// ... some debugging stuff ...
//----------------------------------------------------------------------------
function cbDbg( txt ) {
   if ( cbDbgFlag ) alert( txt );
}

function cb_dbgElement(e, txt) {
   if (!(e = xGetElementById(e))) return;
   var s = "" + txt; var c = 0, p;
   for ( p in e ) {
      var t = c++ % 3 ? "\t" : "\n"; s += t + p + ":   " + e.p;
   }
   cbDbg( s );
}

function xLoadLate( url ) {
   if (document.createElement && document.getElementsByTagName ) {
      var h = document.getElementsByTagName( 'head' );
      if ( ! h.length ) return;

      var re_css = new RegExp( '\.css$', 'i' );
      var re_js  = new RegExp( '\.js$', 'i' );

      if ( url.search( re_js ) > -1 ) {
         var s = document.createElement( 'script' );
         if (s) {
            s.src  = url;
            s.type = "text/javascript";
            h[0].appendChild( s );
         }
      }
      else if ( url.search( re_css ) > -1 ) {
         var l = document.createElement( 'link' );
         if ( l ) {
            l.href = url;
            l.type = "text/css";
            l.rel  = "Stylesheet";
            h[0].appendChild( l );
         }
      }
   }
}

//----------------------------------------------------------------------------
// ... the end of cb.js
//----------------------------------------------------------------------------
