function galleryLib( bigimage_box , title_box , preview_box , counter_box , current_box , pages_box ,  prev_suf , suf  , galleryNum ){
  this.galleryNum = (galleryNum ? galleryNum : '');
  this.image_sml_suf = prev_suf;
  this.image_suf = suf;
  this.current = -1;
  this.current_page = -1;
  this.per_page = 999;
  this.disp  = 3;
  this.bigimage_box = bigimage_box;
  this.title_box = title_box;
  this.preview_box = preview_box;
  this.counter_box = counter_box;
  this.current_box = current_box;
  this.pages_box = pages_box;
  this.image_src = new Array();
  this.image_sml = new Array();
  this.image_big = new Array();
  this.image_span = new Array();
  this.image_title = new Array();
  this.image4lightbox = new Array();
}
galleryLib.prototype.addImage = function( src , title ){
  var i = this.image_src.length;
  this.image_title[ i ] = title;
  this.image_src[ i ] = src;
  this.image_sml[ i ] = new Image;
  this.image_sml[ i ].src = this.cachedURL( this.image_src[ i ] , this.image_sml_suf );
  this.image_sml[ i ].title = this.image_title[ i ];
  this.image_span[ i ] = document.createElement('span');
  this.image_span[ i ].appendChild(this.image_sml[ i ]);
  this.image_span[ i ].ids = i;
  this.image_span[ i ].GL = this;
  this.image_span[ i ].onclick = function(){ this.GL.showImage(this.ids); }
  this.image_span[ i ].title = '#'+(i+1);
  this.image_span[ i ].style.cursor='pointer';
  this.counter_box.innerHTML = parseInt(i)+1;
  this.image4lightbox[ this.image4lightbox.length ] = new Array ( this.image_src[ i ] , this.image_title[i] );
  this.showPage( 0 );
  this.showImage( 0 );
}

galleryLib.prototype.cachedURL = function ( url , suf ){
  if( suf.match(/(?:(s)\=(\d+))+/g) && suf.match(/(?:([x|y])\=(\d+))+/g).length==2 ){
    l = suf.match(/(?:([x|y])\=(\d+))/g);
    coords = { 'y':0 , 'x':0 };
    tmp1 = l[0].split('=');
    coords[ tmp1[0] ] = tmp1[1];
    tmp2 = l[1].split('=');
    coords[ tmp2[0] ] = tmp2[1];
    return '/i/r/' + coords['x'] + 'x' + coords['y'] + '/' + url.substr(3, url.length-3 );
  }
  return url + suf ;
}

galleryLib.prototype.nextImage = function( ){
  if(this.current<this.image_src.length-1){
    if(this.current_page!= Math.ceil((parseInt(this.current)+2)/this.per_page)-1 ){
      this.showPage(  Math.ceil((parseInt(this.current)+2)/this.per_page)-1 );
    }
    this.showImage(parseInt(this.current)+1);
  }
}
galleryLib.prototype.prevImage = function( ){
  if(this.current>0){
    if(this.current_page!=Math.ceil((parseInt(this.current))/this.per_page)-1){
      this.showPage(  Math.ceil((parseInt(this.current))/this.per_page)-1 );
    }
    this.showImage(parseInt(this.current)-1);
  }
}
galleryLib.prototype.showImage = function( id ){
  id = parseInt(id);

  /*var link = $('#igbig a');
  var img  = $('#igbig a img');
  link.attr('href', this.image_src[ id ]);

  img.attr('src', this.cachedURL( this.image_src[ id ] , this.image_suf ));
  img.attr('title', this.image_title[ id ] + "&nbsp;");*/

  if(id>this.image_src.length-1 || id<0 || id==this.current){
    return '';
  }
  this.title_box.innerHTML = this.image_title[ id ];
  if(!this.image_big[ id ]){
    this.image_big[ id ] = new Image;
    this.image_big[ id ].src = this.cachedURL( this.image_src[ id ] , this.image_suf );
    this.image_big[ id ].title = this.image_title[ id ] + "&nbsp;";
    this.image_big[ id ].style.margin='0px';
    this.image_big[ id ].style.border='none';
  }
  this.bigimage_box.innerHTML = '';
  var a = document.createElement('a');
  a.href = this.image_src[ id ] ;
  this.bigimage_box.appendChild(a);
  a.imgid = id;

  a.gl = this;
  a.title = this.image_big[ id ].title;
  var img = this.image_big[ id ].cloneNode(true);
  a.onclick = function () {
    if(typeof(myLightbox)=='undefined'){ return false;} myLightbox.start(this.gl.image4lightbox,this.href); return false;
  }
  a.rel="unic";
  a.appendChild( img );
  var clo;
  if(this.current>=0){
    clo = document.getElementById('clo_' + this.current/* + '_' + this.galleryNum  */);
    if( clo ){
      clo.className = '';
    }
  }
  clo = document.getElementById('clo_' + id /* + '_' + this.galleryNum */);
  if( clo ){
    clo.className = 'act';
  }
  this.current_box.innerHTML = id+1;
  this.current = id;

  $(function() {
      $('#igbig a').lightBox();
    });
}

galleryLib.prototype.showPage = function( page ){
  var pageper = this.per_page;
  var disp = this.disp;
  page = parseInt(page);
  var total = Math.ceil((this.image_src.length)/pageper);
  var from = -(disp)-(total-page<disp?disp-(total-page):0);
  var to = disp+(page<disp?(disp-page):0);
  this.preview_box.innerHTML = '';
  if( (page + to) >= total ){
    to = total-(page+1);
  }
  if( page + from   < 0 ){
    from = -page;
  }
  for(i=page*pageper;i<(page+1)*pageper;i++){
    if(this.image_span[ i ]){
      var clo = this.image_span[ i ].cloneNode(true);
      clo.className = '';
      clo.GL = this; 
      clo.ids = this.image_span[ i ].ids;
      clo.id = 'clo_' + clo.ids /*+ '_' + this.galleryNum */;
      clo.onclick = function(){ this.GL.showImage(this.ids); }
      this.preview_box.appendChild( clo );
    }
  }
  this.pages_box.innerHTML = '';
  for(i=from;i<=to;i++){
    span = document.createElement('span');
    span.GL = this;
    this.pages_box.appendChild(span);
    span.style.cursor='pointer';
    span.i = page + i;
    span.title = span.i;
    if((i==from || i==to) && (page+i!=0 && page+1+i!=total)){
      span.innerHTML = '...';
    }else{
      span.innerHTML = (page+i+1);
    }
    if(i==0){
      span.className='act';
    }else{
      span.onclick = function(){ this.GL.showPage(this.i); }
    }
  }
  this.current_page = page;
}


galleryLib.prototype.showPages = function( id ){
  var span;
  var disp = 2;
  id = parseInt(id);
  var total = this.image_src.length-1;
  this.pages_box.innerHTML = '';
  this.preview_box.innerHTML = '';
  var from = -(disp)-(total-id<=disp?disp-(total-id):0);
  var to = disp+(id<disp?(disp-id):0);
  if( (parseInt(id) + to) > total ){
    to = total-id;
  }
  if( id + from   < 0 ){
    from = -id;
  }
  for(i=from;i<=to;i++){
    span = document.createElement('span');
    span.GL = this;
    this.pages_box.appendChild(span);
    span.style.cursor='pointer';
    span.i = parseInt(id) + parseInt(i);
    if((i==from || i==to) && (id+i!=0 && id+i!=total)){
      span.innerHTML = '...';
    }else{
      span.innerHTML = (id+i+1);
    }
    if(this.image_span[ id + i  ]){
      this.preview_box.appendChild( this.image_span[ id + i ] );
      this.image_span[ id + i ].className = '';
    }
    if( i == 0 ){
      this.image_span[ id ].className = 'act';
      span.className='act';
      if(!this.image_big[ id  ]){
        this.image_big[ id ] = new Image;
        this.image_big[ id ].src = this.cachedURL( this.image_src[ id ] , this.image_suf ) ;
        this.image_big[ id ].title = this.image_title[ id ];
        this.image_big[ id ].style.margin='0px';
        this.image_big[ id ].style.border='none';
      }
      this.title_box.innerHTML = this.image_title[ id ];
      this.bigimage_box.innerHTML = '';
      this.bigimage_box.appendChild(this.image_big[ id ]);
      this.current_box.innerHTML = id+1;
    }else{
      span.onclick = function(){ this.GL.showImage(this.i); }
    }
  }
  this.current = i;
}