var browser = get_browser();

function tt(event,text,css){

  var e = getE('kTT');

  if( e ){

    e.style.display = 'none';
    e.style.height  = '';
    e.style.width   = '';

    //  Default CSS
    //e.style.border          = '2px solid #5A4B38';
    //e.style.padding         = '2px';
    //e.style.backgroundColor = 'Black';
    //e.style.position        = 'absolute';
    //e.style.zIndex          = 10000;

    var winWidth   = document.body.clientWidth  + parseInt(document.body.scrollLeft);
    var winHeight  = document.body.clientHeight + parseInt(document.body.scrollTop);

    var H = e.offsetHeight;
    var W = e.offsetWidth;

    var topPos   = event.pageY || event.clientY;
    var leftPos  = event.pageX || event.clientX;
    var rightPos = leftPos + W;

    if( browser == 'ie' ){
      topPos = topPos + parseInt(document.documentElement.scrollTop);
    }

    if( rightPos+20 > winWidth ){
      leftPos = leftPos - (W+20);
    }

    if( topPos+H+20 > winHeight ){
      topPos = topPos - (H+20);
    }

    e.style.top  = topPos  + 5 + 'px';
    e.style.left = leftPos + 20 + 'px';
    
    e.innerHTML = text;
    
    e.style.display = '';

  }

}

function untt(){
  var e = getE('kTT');
  if( e ){
    e.style.display = 'none';
  }   
}

function popup(ev,img,w,h){

  if( !ev ){
    var ev = window.event;
  }

  var H = h || 252;
  var W = w || 450;

  var winWidth   = parseInt(document.body.clientWidth)  + parseInt(document.body.scrollLeft);
  var winHeight  = parseInt(document.body.clientHeight) + parseInt(document.body.scrollTop);

  var topPos   = ev.pageY || ev.clientY;
  var leftPos  = ev.pageX || ev.clientX;
  var rightPos = leftPos + W;

  if( browser == 'ie' ){
    topPos = topPos + parseInt(document.documentElement.scrollTop);
  }

  if( rightPos+20 > winWidth ){
    leftPos = leftPos - (W+20);
  }

  if( topPos+H+20 > winHeight ){
    topPos = topPos - (H+20);
  }

  loadWindow('popup',img,W,H,leftPos,topPos);

}

function loadWindow(id,img,w,h,x,y,bg){

  var check = getE(id);

  if( check ){

    var e = check;

  }else{

    var e = document.createElement('div');

    if( e ){

      e.id = id;

      e.className = 'window';

      e.style.position = 'absolute';
      e.style.zIndex   = 19;

      e.style.textAlign = 'left';

      e.style.overflow = 'hidden';

      if( bg ){
        e.style.backgroundColor = 'transparent';
        e.style.border = 'none';
        e.style.background = 'url('+bg+') no-repeat';
      }else{
        e.style.backgroundColor = 'white';
        e.style.border = '1px solid #003f6f';
      }

      document.body.appendChild(e);

    }

  }

  if( e ){
  
    if( !x ) var x = 200;
    if( !y ) var y = 30;

    if( !w ) var w = 900;
    if( !h ) var h = 580;  
    
    e.style.width  = w + 'px';
    e.style.height = h + 'px';

    e.style.top  = y + 'px';
    e.style.left = x + 'px';    

    e.innerHTML = '<div style=text-align:right;height:20px;><a nohref=nohref style=cursor:pointer; onclick=unloadElement("popup");>Close</a>&nbsp;</div><div><img src="'+img+'" border=0 onclick="unloadElement(\'popup\')"></div>';
    focusWindow(id);
    
  }

}

function focusWindow(id){

  var e = getE(id);

  if( e ){

    var els = document.getElementsByClassName('window');

    var count = els.length;

    if( count > 1 ){

      var obj = null;

      for( var i=0; i<count; i++ ){

        if( els[i] ){

          obj = els[i];

          if( obj.id != e.id ){
            obj.style.zIndex = 18;
          }

        }

      }

    }

    e.style.zIndex = 19;

  }

}

function unloadElement(id){
  var e = getE(id);
  if( e ){
    document.body.removeChild(e);
  }
}

document.getElementsByClassName = function(cl){

  var retnode = [];
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = this.getElementsByTagName('*');

  for (var i = 0; i < elem.length; i++) {
    var classes = elem[i].className;
    if( myclass.test(classes) ) retnode.push(elem[i]);
  }

  return retnode;

};

function getE(e){
 if( typeof(e) == 'string' ){
   return document.getElementById(e);
 }else{
   return e;
 }
 return null;
}

function get_browser(){

  var thebrowser = navigator.appName;

  if( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 )
    return 'chrome';

  if( thebrowser == 'Microsoft Internet Explorer' ||  thebrowser == 'Internet Explorer' )
    return 'ie';
  else
    return 'ff';

}




