var browser = get_browser();

function popup(ev,img,w,h){

  if( !ev ){
    var ev = window.event;
  }

  var H = h || 570;
  var W = w || 600;

  var winWidth   = parseInt(document.body.clientWidth)  + parseInt(document.body.scrollLeft);
  var winHeight  = parseInt(document.body.clientHeight);

  var topPos   = ev.pageY || ev.clientY;
  var leftPos  = ev.pageX || ev.clientX;
  
  leftPos += 20;
  topPos  -= 20;
  
  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 = 'whiteSmoke';
        e.style.border = '1px solid #4D4D4D';
        e.style.color  = '#fff';
      }

      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;color:#fff; onclick=unloadElement("popup");><u>Close</u></a>&nbsp;</div><div><img src="'+img+'" border=0 onclick="unloadElement(\'popup\')"></div>';
    
    e.innerHTML = '<div style=text-align:right;height:20px;background-color:#4D4D4D;><a nohref=nohref style=cursor:pointer;color:#fff; onclick=unloadElement("popup");><u>Close</u></a>&nbsp;</div><object width="600" height="550"><param name="movie" value="flash/main.swf"><embed src="flash/main.swf" width="600" height="550"></embed></object>';
    
    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';

}



