var ymgLinksPopups =
{
  start: function ()
  {
    // przeszukiwanie wszysktich linków do popupów obrazków    
    var gLinks = document.getElementsByTagName('A');
    
    for( var i=0; i<gLinks.length; i++ )
    {
      var thisObj   = this;
      yTools.addEvent( gLinks[i], 'click', function(e){ return thisObj.imgClick(e); } );
      /*
      if( matches = /[a-zA-Z0-9_-]+pop-([0-9]+)x([0-9]+)/.exec( gLinks[i].className ) )
      {
        var thisObj   = this;
        var imgWidth  = matches[1];
        var imgHeight = matches[2];
        var imgUrl    = gLinks[i];
        var imgTitle  = gLinks[i].title ? gLinks[i].title : gLinks[i].alt ? gLinks[i].alt : '';
        
        alert( imgUrl );
        
        yTools.addEvent( gLinks[i], 'click', function(e){ return thisObj.imgClick( e, imgUrl, imgTitle, imgWidth, imgHeight ); } );
      }
      */
    }
  },
  
  imgClick: function( event )
  {
    event = event || window.event;
    var target = event.target || event.srcElement;
    
    if( target = yTools.getParentByTag( target, 'A' ) )
    {
      if( matches = /[a-zA-Z0-9_-]+pop-([0-9]+)x([0-9]+)/.exec( target.className ) )
      {
        var imgWidth  = matches[1];
        var imgHeight = matches[2];
        var imgUrl    = target.href;
        var imgTitle  = target.title ? target.title : target.alt ? target.alt : '';        
        this.popImage( imgUrl, imgTitle, imgWidth, imgHeight );
        if(event.preventDefault) event.preventDefault()
        return false;
      } 
    }
    return true;
  },
  
  // tworzenie popupu
  popImage: function(imageURL,imageTitle,width,height)
  {
    var AutoClose = false;
    var isW3 = document.getElementByID && document.createTextNode;
    var isOP = window.opera ? true : false;
    var isIE = document.all && !isOP;
    var imgWin=window.open('about:blank','','width='+width+',height='+height+',left='+(screen.width/2-width/2)+',top='+(screen.height/2-height/2)+'resizable=0,scrollbars=no,menubar=no,toolbar=no' );
    with (imgWin.document){
    writeln('<html><head><title>'+imageTitle+'</title><style>html,body{height:100%}body{margin:0px;padding:0;border:0;background-color:#000000;}.max{height:100%;width:100%}</style>');
    writeln('</head><body style="cursor:pointer" bgcolor=000000 scroll="no" onclick="window.close();" onload="this.window.focus();"'+(!AutoClose?'':' onblur="this.window.close()"')+'>');
    writeln('<table class=max cellpadding=0 cellspacing=0 border=0><tr><td align=center><img src='+imageURL+' border="0" style="display:block;border:0;margin:auto;"></td></tr></table></body></html>');
    close();
    }
  }
}

var yTools =
{
  haveClass: function(obj,cls)
  {
    cls=cls.replace( "*","[a-zA-Z0-9-]*")
    r=new RegExp("(^| )"+cls+"($| )")
    return r.test(obj.className)
  },
  
  addClass: function(obj,cls)
  {
    if(!cls||cls=='') return false;    
    if(!this.haveClass(obj,cls))
      obj.className += obj.className.length ? " "+cls : cls;
  },
  
  removeClass: function(obj,cls)
  { 
    var r=RegExp('^'+cls.replace( "*","[a-zA-Z0-9-]*")+'$')
    var split=obj.className.split( /\s/ )
    var newClass = [];
    for( n=0, i=0; i<split.length; i++ )
      if( !r.test(split[i]) )
        newClass[n++] = split[i];    
    obj.className=newClass.join(' ')
  },
  
  getParentByTag: function( srcElement, tagName, className )
  {
    var element = srcElement;
    
    while( element ){
      if( element.nodeName == tagName &&
        ( !className || haveClass(element,className) ) ){
        return element;
        break;
      }
      element = element.parentNode;
    }
    return false;
  },
  
  addEvent: function( obj, evType, fn )
  {
    if( obj.addEventListener ){
      obj.addEventListener( evType, fn, true );
      return true;
    }else if( obj.attachEvent ){
      var r = obj.attachEvent( "on"+evType, fn );
      return r;
    } else {
      return false;
    }
  }
}

yTools.addEvent( window, 'load', function(){ ymgLinksPopups.start(); } );
