var VERSION  = '0.05';

var DOM  = ( document.getElementById ) ? true : false;
var OP   = ( window.opera ) ? true : false;
var IE   = ( document.all && !OP ) ? true : false;
var IE4  = IE && !DOM;
var NS4  = ( document.layers ) ? true : false;
var NS6  = ( navigator.vendor == ( "Netscape6" ) || navigator.product == ( "Gecko" ) );
var Mac  = ( navigator.appVersion.indexOf( "Mac" ) != -1 );
var IE4M = IE4 && Mac;

var onloadFunctions = [];

var SA_DOM = 0;   /* style access via dom */
var SA_IE  = 1;   /*              via ie  */

var styleAccess   = ( IE == true ) ? SA_IE : SA_DOM;
var styleTemplate = [];

styleTemplate[ SA_DOM ] = new Object();
styleTemplate[ SA_DOM ][ 'set_string' ] = "element.style.{attr} = '{value}'";
styleTemplate[ SA_DOM ][ 'set_other'  ] = "element.style.{attr} = {value}";
styleTemplate[ SA_DOM ][ 'get' ]        = "document.defaultView.getComputedStyle( element, null ).{attr}";

styleTemplate[ SA_IE ] = new Object();
styleTemplate[ SA_IE ][ 'set_string' ] = "element.style.{attr} = '{value}'";
styleTemplate[ SA_IE ][ 'set_other'  ] = "element.style.{attr} = {value}";
// styleTemplate[ SA_IE ][ 'set_string' ] = "element.currentStyle.{attr} = '{value}'";
// styleTemplate[ SA_IE ][ 'set_other'  ] = "element.currentStyle.{attr} = {value}";
styleTemplate[ SA_IE ][ 'get' ]        = "element.style.{attr}";

function getElementById( _id ) {

  var retVal;

  if ( DOM ) {
    retVal = document.getElementById( _id );
  } else if ( IE4 || IE ) {
    retVal = document.all( _id );
  } else if ( NS4 ) {
    retVal = document.layers[ _id ];
  }

/*
  if ( document.getElementById ) {
    // DOM 1
    if ( window.HTMLElement ) {
      retVal = document.getElementById( _id );
    } else {
      retVal = document.all( _id );
    }
  } else if ( document.all ) {
    retVal = document.all( _id );
  } else if ( document.layers ) {
    // FIXME: Netscape 4.x
    retVal = document.layers[ _id ];
  }
*/
  return retVal;
}

function setAttribute( _id, _attr, _value ) {
  var element = getElementById( _id );
  if ( element ) {
    element.setAttribute( _attr, _value );
  }
}

function getAttribute( _id, _attr ) {
  var element = getElementById( _id );
  var retVal  = "";
  if ( element ) {
    retVal = element.getAttribute( _attr );
  }
  return retVal;
}

// for internal use only
function __autodetectElement( _mixed ) {
  return typeof( _mixed ) == 'string' ? getElementById( _mixed ) : _mixed;
}

// for internal use only
function __autodetectStyleTemplate( _value, _access ) {
  if ( _access == 'get' ) {
    return styleTemplate[ styleAccess ][ _access ];
  } else {
    var idx = typeof( _value );
    if ( idx != 'string' ) {
      idx = 'other';
    }
    idx = 'set_' + idx;
    return styleTemplate[ styleAccess ][ idx ];
  }
}

// for internal use only
function __prepareEvalString( _attr, _value, _access ) {
  var evalString = __autodetectStyleTemplate( _value, _access );
  evalString = evalString.replace( /{attr}/, _attr );
  if ( _value != null ) {
    evalString = evalString.replace( /{value}/, _value );
  }
  return evalString;
}

function setStyle( _mixed, _attr, _value ) {
  var element = __autodetectElement( _mixed );
  if ( element ) {
    eval( __prepareEvalString( _attr, _value, 'set' ) );
  }
}

function setStyles( _mixed, _styles ) {
  var element = __autodetectElement( _mixed );
  if ( element ) {
    for ( var attr in _styles ) {
      eval( __prepareEvalString( attr, _styles[ attr ], 'set' ) );
    }
  }
}

function getStyle( _mixed, _attr ) {
  var element = __autodetectElement( _mixed );
  var retVal  = "";
  if ( element ) {
    retVal = eval( __prepareEvalString( _attr, null, 'get' ) );
  }
  return retVal;
}

function executeOnloadFunctions() {
  for ( i = 0; i < onloadFunctions.length; i++ ) {
    eval( onloadFunctions[ i ] + '()' );
  }
}