function nicePopups( icoLinkClass, galleryId, popShadow )
{
  this.galleryId      = galleryId!=null ? galleryId : 'galleryContainer';
  this.icoLinkClass   = icoLinkClass!=null ? icoLinkClass : 'thumb';
  this.popInLayerId   = 'popInDivLayer'; 
  this.popImgId       = 'popDivImg'; 
  this.popXcloseId    = 'popDivX'; 
  this.popAutoClose   = true; 
  this.popCenter      = true;
  this.popMargin      = 10;
  this.popShadow      = popShadow!=null ? popShadow : true;
  this.popPrevNext    = false; 
  this.totalRefreshes = 0; 


  this.broOP  = window.opera ? true : false;
  this.broIE  = document.getElementById && (navigator.userAgent.indexOf("MSIE") !=-1 && !this.broOP );
  this.broMZ  = document.getElementById && !this.broOP && !this.broIE;


  this.popup      = null;

  this.init = function()
  {
    var thisObj = this;
    document.onclick = function(e)
    {
      if (!e) var e = window.event;
      var clickedObj = e.target ? e.target : e.srcElement;
      return thisObj.clicked( clickedObj );
    }
  }

  this.clicked = function( clickedObj )
  {
    var imgLinkObj = this.isIco( clickedObj );

    if( imgLinkObj == false ){ return true; }

    if( this.broOP )
    {
      window.open( imgLinkObj.href );
    }
    else
    {
      if( this.popAutoClose )
      {
        if( this.popup ){ this.popupRemove(); }
      }

      this.popup = new this.popupObj( this, imgLinkObj.href, imgLinkObj.title );
      this.popup.create();
      this.popup.keepRefreshing();
    }

    return false;
  }

  this.isIco = function( obj )
  {
    if( this.isIcoLink( obj ) )
    {
      return obj;
    }
    else
    {
      var parent = obj.parentNode;
      while( this.isIcoLink( parent ) == false )
      {
        parent = parent.parentNode;
        if( !parent ) return false;
      }
      return parent;
    }
  }


  this.isIcoLink = function( obj )
  {
    if( obj.nodeName == 'A' )
    {
      if( this.icoLinkClass != '' && !haveClass( obj, this.icoLinkClass ) )
        return false;
      else
        return true;
    }
    return false;
  }

  this.popupObj = function( parentObj, imgLinkUri, imgLinkTitle )
  {
    this.parent           = parentObj;

    this.imgUri           = imgLinkUri;
    this.imgTitle         = imgLinkTitle;
    this.node             = false;
    this.innerNode        = false;
    this.imgNode          = false;
    this.imgXcloseNode    = false;

    this.stopRefreshing   = false;
    this.imgLoaded        = false;
    this.doOnresize       = false;
    this.lastClick        = 0;
    this.stopRefreshingAfter = null;
    this.allowDblClick    = true;

    this.create = function()
    {
      var prevNextHTML = '';

      if( this.parent.popPrevNext )
      {
        prevNextHTML = '<div class="prevNextBar"><strong>&laquo;</strong> Prev | Next <strong>&raquo;</strong></div>';
      }

      var inLayerHTML =
        '<table id="'+this.parent.popInLayerId+'" cellspacing="0" cellpadding="0"'+
        ' style="width:1px;height:1px;">'+
        '<tr>';

      if( this.parent.popShadow )
      {
        inLayerHTML +=
          '<td class="SdwTL"></td>'+
          '<td class="SdwT"></td>'+
          '<td class="SdwTR"></td>'+
        '</tr>'+
        '<tr>'+
          '<td class="SdwL"></td>';
      }

      inLayerHTML +=
        '<td>'+
         '<div class="outline"><div class="outerImage"><img src="'+this.imgUri+'"'+( this.imgTitle ? ' alt="'+this.imgTitle+'"' : '' )+
         ( this.imgTitle ? ' title="'+this.imgTitle+'"' : '' )+
         ' style="vertical-align:bottom" id="'+this.parent.popImgId+'" /></div>'+
          prevNextHTML+
          '<a href="#" title="close" id="'+this.parent.popXcloseId+'" class="close">X</a>'+
        '</div></td>';

      if( this.parent.popShadow )
      {
        inLayerHTML +=
          '<td class="SdwR"></td>'+
        '</tr>'+
        '<tr>'+
          '<td class="SdwBL"></td>'+
          '<td class="SdwB"></td>'+
          '<td class="SdwBR"></td>';
      }

      inLayerHTML +=
        '</tr>'+
        '</table>';

      this.node = document.createElement( 'DIV' );
      this.node.className = 'nicepopup nicepopupLoading';
      this.node.innerHTML = inLayerHTML;

      if( this.parent.popCenter )
      {
        this.node.style.left     = '50%';
        this.node.style.top      = '50%';
        this.node.style.position = this.parent.broIE ? 'absolute' : 'fixed';
      }

      document.body.appendChild( this.node ); 

      this.innerNode      = byId( this.parent.popInLayerId );
      this.imgNode        = byId( this.parent.popImgId );
      this.imgXcloseNode  = byId( this.parent.popXcloseId );

      var thisObj = this;

      this.node.onmouseover = function(e){ return thisObj.overed( eTrg(e) ); }
      this.node.onmouseout = function(e){ return thisObj.outed( eTrg(e) ); }
      this.node.onclick = function(e){ return thisObj.clicked( eTrg(e) ) }
      this.node.ondblclick = function(e){ return thisObj.dblclicked( eTrg(e) ) }
      this.imgXcloseNode.onclick = function(e){ return thisObj.closeClicked( eTrg(e) ) }
      document.onkeydown = function(e){ return thisObj.closeKeypress( evt(e) ) }

    }

    this.refresh = function()
    {
      if( !this.parent.popup ){ return false; }

      if( !this.imgLoaded && this.imgNode.width > 48 && this.imgNode.height > 48 )
      {
        removeClass( this.node, 'nicepopupLoading' );
        this.imgLoaded = true;
        this.stopRefreshingAfter = 5;
      }

      this.screenHeight     = window.innerHeight ? window.innerHeight : document.body.clientHeight;
      this.screenWidth      = window.innerWidth ? window.innerWidth-16 : document.body.clientWidth;
      this.maxAllowedWidth  = this.screenWidth - ( 2*this.parent.popMargin );
      this.maxAllowedHeight = this.screenHeight - ( 2*this.parent.popMargin );
      this.maxWidth         = this.innerNode.scrollWidth;
      this.maxHeight        = this.innerNode.scrollHeight;
      var scrolTop          = this.parent.broIE ? document.body.scrollTop+this.parent.popMargin : 0;
      var newTopMargin      = 0;
      var thisObj           = this;

      if( this.maxWidth > this.maxAllowedWidth )
      {
        var newWidth = this.maxAllowedWidth>0 ? this.maxAllowedWidth+'px' : '0';
        if( newWidth != this.node.style.width ){
          this.node.style.width = newWidth;
        }

        if( this.parent.popCenter ){
          var newLeftMargin = this.maxAllowedWidth>0 ? '-'+this.maxAllowedWidth/2+'px' : '0';
          if( newLeftMargin != this.node.style.marginLeft ){
            this.node.style.marginLeft = newLeftMargin;
          }
        }

        if( this.parent.broIE ){ this.node.style.overflowX='scroll'; }
        else{
          this.node.style.overflow='scroll';
        }

      }
      else
      {
        if( this.node.style.width != this.maxWidth+'px' )
        {
          this.node.style.width = this.maxWidth+'px';
          if( this.parent.popCenter ){
            this.node.style.marginLeft = '-'+(this.maxWidth/2)+'px';
          }
        }

        if( this.maxHeight <= this.maxAllowedHeight ){
          this.node.style.overflow='hidden';
          this.node.style.overflowX='hidden';
          this.node.style.overflowY='hidden';
        }
      }

      if( this.maxHeight > this.maxAllowedHeight )
      {
        var newHeight = this.maxAllowedHeight>0 ? this.maxAllowedHeight+'px' : '0';
        if( newHeight != this.node.style.height ){
          this.node.style.height = newHeight;
        }

        if( this.parent.popCenter ){
          newTopMargin = (-(this.maxAllowedHeight/2)+scrolTop)+'px';
          if( newTopMargin != this.node.style.marginTop ){
            this.node.style.marginTop = newTopMargin;
          }
        }

        if( this.parent.broIE ){ this.node.style.overflowY='scroll'; }
        else{ this.node.style.overflow='scroll'; }

      }
      else if( this.node.style.height != this.maxHeight+'px' || this.parent.broIE )
      {
        this.node.style.height = this.maxHeight+'px';
        if( this.parent.popCenter ){
          newTopMargin = (-(this.maxHeight/2)+scrolTop)+'px';
          this.node.style.marginTop = newTopMargin;
        }

        if( this.maxWidth > this.maxAllowedWidth ){
          this.node.style.overflow='hidden';
          this.node.style.overflowX='hidden';
          this.node.style.overflowY='hidden';
        }
      }

      if( !this.doOnresize )
      {
        window.onresize = function (){ thisObj.refresh(); };
        this.doOnresize = true;
      }

      if( this.parent.broIE && !this.doScroll )
      {
        window.onscroll = function (){ thisObj.refresh(); };
        this.doScroll = true;
      }

    }

    this.keepRefreshing = function()
    {
      if( !this.stopRefreshing && ( this.stopRefreshingAfter==null || this.stopRefreshingAfter-- > 0 ) )
      {
        this.refresh();
        this.parent.totalRefreshes++;
        var thisObj = this;
        setTimeout( function(){ thisObj.keepRefreshing() }, 100 );
      }
    }


    this.overed = function( overedObj )
    {
      addClass( this.node, 'nicepopupHover' );
      return false;
    }

    this.outed = function( outedObj )
    {
      removeClass( this.node, 'nicepopupHover' );
      return false;
    }

    this.clicked = function( clickedObj )
    {
      this.allowDblClick = true;

      var now = new Date().getTime();
      if( now-this.lastClick <= 250 ){ return dblclicked( clickedObj ); }
      else{ this.lastClick = now; }

      return false;
    }

    this.dblclicked = function( clickedObj )
    {
      if( this.allowDblClick )
      {
        this.parent.popupRemove();
        return false;
      }
    }

    this.closeClicked = function( clickedObj )
    {
      this.parent.popupRemove();
      return false;
    }

    this.closeKeypress = function( ev )
    {
      if( ev.keyCode==27 ){ this.parent.popupRemove(); }
      else{ return true; }
      return false;
    }

  }

  this.popupRemove = function()
  {
    document.body.removeChild( this.popup.node );
    document.onkeydown = null;

    this.node           = null;
    this.popup          = null;
  }

} 


function byId(id)
{
  return document.getElementById(id);
}

function haveClass(obj,cls)
{
  r=new RegExp("(^| )"+cls+"($| )");
  return r.test(obj.className);
}

function removeClass(obj,cls)
{
  r=new RegExp("(^| )("+cls+")($| )");
  obj.className = obj.className.replace(r,"$1$3");
}

function addClass(obj,cls)
{
  if( !cls )
    return false;

  if( !haveClass(obj,cls) )
    obj.className += obj.className.length ? " "+cls : cls;
}

function eTrg(e)
{
  if (!e) var e = window.event;
  return e.target ? e.target : e.srcElement;
}

function evt(e)
{
  if (!e) return window.event;
  else return e
}

function destroyObj( obj )
{
  obj = null;
}
