function ThumbnailGallery(dispContainer,ctrlContainer,top,left,width,height,spacerImg,thumbnailRender){
  this.dispContainer=$(dispContainer);
  this.ctrlContainer=$(ctrlContainer);
  this.top=top;
  this.left=left;
  this.width=width;
  this.height=height;
  this.offsetX=0;
  this.offsetY=0;
  this.imageBorderWidth=10;
  this.imageBorderColor='#FFF';
  this.scrnColor='rgb(98,98,95)';
  this.closeImg=null;
  this.closeWidth=0;
  this.closeHeight=0;
  this.closeTop=top;
  this.closeLeft=left;
  this.closeHlColor='rgb(255,255,255)';
  this.spacerImg=spacerImg;
  this.screenOverlay;
  this.closeButton;
  this.outerImageContainer;
  this.outerImageContainer2;
  this.galleryImg;
  this.galleryImg2;
  this.galVisible=false;
  this.ifPrimary=true;
  this.thumbs=thumbnailRender;
  this.loading=null;
  this.imgLoaders = new Array();
  this.lrgImgPreldr = null;
  this.selectedGallery=null;
  this.selectedImage=null;
  this.tmpImg;
  this.ctrlWidth=parseInt(this.ctrlContainer.css('width'));
  this.ctrlInt=null;
  this.ctrlDir=1;
  this.ctrlInitDir;
  this.ctrlMoveDist;
  this.ctrlCnt;
  this.ctrlSteps=10;
  this.slideCtrlDir='right';
  this.galCloseCallback=null;
  this.fileListLoaderName='GalleryLoader';
  this.preLoaderName="SequentialPreloader";
  this.selectedIndex = null;
}
ThumbnailGallery.prototype.setDefaultSelected=function(index){
  this.selectedIndex=index;
}
ThumbnailGallery.prototype.setFileListLoaderType=function(fileListLoaderName){
  this.fileListLoaderName=fileListLoaderName;
}
ThumbnailGallery.prototype.setPreLoaderType=function(preLoaderName){
  this.preLoaderName=preLoaderName;
}
ThumbnailGallery.prototype.setCloseCallback=function(callback){
  this.galCloseCallback = callback;
}
ThumbnailGallery.prototype.setImageBorderWidth=function(padding){
  this.imageBorderWidth = padding;
}
ThumbnailGallery.prototype.setImageBorderColor=function(color){
  this.imageBorderColor = color;
}
ThumbnailGallery.prototype.setHorizOffset=function(offset){
  this.offsetX = offset;
}
ThumbnailGallery.prototype.setVertOffset=function(offset){
  this.offsetY = offset;
}
ThumbnailGallery.prototype.setScreenOverlayColor=function(color){
  this.scrnColor=color;
}
ThumbnailGallery.prototype.setCloseButtonImg=function(imgName,top,left,width,height,highlightColor){
  this.closeImg=imgName;
  this.closeWidth=height;
  this.closeHeight=width;
  this.closeTop=top;
  this.closeLeft=left;
  this.closeHlColor=highlightColor;
}
ThumbnailGallery.prototype.setLoadingImage=function(loadingImg,top,left,width,height){
  this.loading=$(new Image());
  this.loading.attr('src',loadingImg);
  this.loading.css({
    'position':'absolute',
    'top':top+'px',
    'left':left+'px',
    'width':width+'px',
    'height':height+'px'
  });
}
ThumbnailGallery.prototype.click=function(newDir){
   this.thumbs.rotate(5*newDir);
}

ThumbnailGallery.prototype.loadImage=function(src,id){
  this.tmpImg.gallery=this.selectedGallery;
  this.selectedImage=this.tmpImg.imgID=id;
  if(this.galVisible){
    if(this.loading!=null)
      this.loading.show();
    (this.ifPrimary?this.outerImageContainer2:this.outerImageContainer).fadeOut(300);
  }
  this.tmpImg.src=src.replace(/th_/,"");
}

ThumbnailGallery.prototype.toggleGallery=function(dir,gal){
  if(dir > 0 && !this.galVisible){
    this.galVisible=true;
    this.selectedGallery=gal;
    if(this.loading!=null)
      this.loading.show();
    this.loadGallery(gal);
    this.screenOverlay.fadeIn(450);
    if(this.closeButton!=null)
      this.closeButton.fadeIn(300);
    this.ctrlContainer.show('slide',{direction:this.slideCtrlDir},500);
  }else if(dir < 0 && this.galVisible){
    var currentGal = this.selectedGallery;
    this.galVisible=false;
    this.selectedGallery=null;
    this.ctrlContainer.hide('slide',{direction:this.slideCtrlDir},500);
    this.thumbs.init();
    this.outerImageContainer.fadeOut(300);
    this.outerImageContainer2.fadeOut(300);
    this.screenOverlay.fadeOut(450);
    if(this.closeButton!=null)
      this.closeButton.fadeOut(300);
    if(this.galCloseCallback != null)
      this.galCloseCallback(currentGal);
  }
}
ThumbnailGallery.prototype.preloadGallery=function(gal,newSize)
{
  var obj = this;
  var imgLoader = this.imgLoaders[gal]; 
  if(imgLoader == null || (imgLoader.size != null && imgLoader.size != (newSize==null?-1:newSize))){
      this.imgLoaders[gal] = eval('new '+obj.preLoaderName+'()');
      var loader = eval('new '+this.fileListLoaderName+'()');
      loader.getFileList(gal,function(retId,fileList){
      if($(fileList).size() > 0){
        for(var fName in fileList){
          if($(fileList[fName]).size() > 0){
            var tmp = obj.imgLoaders[retId];
            tmp.setId(retId);
            tmp.setImages(fileList[fName]);
            tmp.setCallbackObj(obj);
            tmp.init();
            tmp.size = $(fileList[fName]).size(); 
          }
        }
      }
    });
  }
}
ThumbnailGallery.prototype.preload=function(imgs,startIndex,loadLength){
  tmpLrgImgs=new Array();
  for(var i=0,ri=startIndex;i<loadLength;i++){
    if(ri>=imgs.length)
      ri-=imgs.length;
    else if (ri<0)
      ri+=imgs.length;
    tmpLrgImgs[i]=imgs[ri].src.replace(/th_/,"");
    ri++;
  }
  if(this.lrgImgPreldr == null)
    this.lrgImgPreldr = new SequentialPreloader(0,tmpLrgImgs);
  else
    this.lrgImgPreldr.setImages(tmpLrgImgs);
}
ThumbnailGallery.prototype.loadGallery=function(gal){
  this.thumbs.setImages(null);
  this.thumbs.init(true);
  this.ctrlContainer.children('#arrowcnt').hide();
  if(this.imgLoaders != null){
    if(this.imgLoaders[gal] == null){
      this.preloadGallery(gal);
    }else{
      for(var i in this.imgLoaders){
        this.imgLoaders[i].stop();
      }
      this.imgLoaders[gal].go();
    }
  }
}
ThumbnailGallery.prototype.onload=function(id,images){
  if(id == this.selectedGallery){
    var index = 0;
    if(this.selectedIndex != null)
      index = this.selectedIndex; 
    this.loadImage(images[index].src.replace(/th_/,""),index);
    if(images.length > 5)
      this.ctrlContainer.children('#arrowcnt').show();
    this.thumbs.setDefaultSelected(index);
    this.thumbs.setImages(images);
    this.thumbs.init(true);
    for(var i in this.imgLoaders){
      if(i!=id)
        this.imgLoaders[i].go();
    }
  }
}
ThumbnailGallery.prototype.build=function(){
  var obj=this;
//  var zIndex=this.dispContainer.css('z-index');
  this.ctrlContainer.click(function(){obj.toggleGallery(-1)});
  this.screenOverlay=$(document.createElement('div'));
  this.screenOverlay.css({
  	'position':'absolute',
  	'top':this.top,
  	'left':this.left,
  	'width':this.width,
  	'height':this.height,
  	'background-color': this.scrnColor
//  	'z-index':zIndex
  });
  this.screenOverlay.fadeTo(1,0.8).hide();
  this.screenOverlay.click(function(){obj.toggleGallery(-1)});
  if(this.loading != null)
    this.screenOverlay.append(this.loading);
  this.outerImageContainer=$(document.createElement('div'));
  this.outerImageContainer.css({
  	'position':'absolute',
  	'top':'100px',
  	'left':'100px',
    'margin':'0 auto',
    'display':'none',
  	'background-color':this.imageBorderColor
//  	'z-index':zIndex
  });
  var imageContainer=$(document.createElement('div'));
  imageContainer.css('padding',this.imageBorderWidth+'px');
  this.galleryImg=new Image();
  this.galleryImg.src=this.spacerImg;
  imageContainer.append(this.galleryImg);
  this.outerImageContainer.append(imageContainer);
  this.outerImageContainer2=$(document.createElement('div'));
  this.outerImageContainer2.css({
  	'position':'absolute',
  	'top':'100px',
  	'left':'100px',
    'margin':'0 auto',
    'display':'none',
  	'background-color':this.imageBorderColor
//  	'z-index':zIndex
  });
  imageContainer=$(document.createElement('div'));
  imageContainer.css('padding',this.imageBorderWidth+'px');
  this.galleryImg2=new Image();
  this.galleryImg2.src=this.spacerImg;
  imageContainer.append(this.galleryImg2);
  this.outerImageContainer2.append(imageContainer);
  this.dispContainer.append(this.screenOverlay);
  if(this.closeImg != null){
    this.closeButton=$(document.createElement('div'));
    this.closeButton.css({
      'position':'absolute',
      'top':this.closeTop+'px',
      'left':this.closeLeft+'px',
      'width':this.closeWidth+'px',
      'height':this.closeHeight+'px',
    	'background-color': this.scrnColor,
    	'visibility':'hidden'
//    	'z-index':zIndex
    });
    this.closeButton.hide();
    this.closeButton.mouseenter(function(event){event.stop();this.css('background-color','rgb(255,255,255)').animate({'background-color':obj.closeHlColor},300);});
    this.closeButton.mouseleave(function(event){event.stop();this.animate({'background-color':obj.scrnColor},300);});
    this.closeButton.click(function(event){event.stop();obj.toggleGallery(-1);});
    var closeImg=$(new Image);
    closeImg.attr({
      src:this.closeImg,
      width:this.closeWidth,
      height:this.closeHeight
    });
    this.closeButton.append(closeImg);
    this.dispContainer.append(this.closeButton);
  }
  this.dispContainer.append(this.outerImageContainer).append(this.outerImageContainer2);
  var thumbDiv = $(document.createElement('div'));
  thumbDiv.css({
  	'position':'absolute',
  	'top':'0px',
  	'left':'0px'
//  	'z-index':zIndex
  });
  this.thumbs.setParentObj(this);
  this.thumbs.setContainer(thumbDiv);
  this.thumbs.build();
  this.thumbs.init(true);
  this.ctrlContainer.prepend(thumbDiv);
  $(document).mousewheel(function(e,delta){
    if(obj.galVisible)
        obj.thumbs.queue((delta<0?Math.floor(delta):Math.ceil(delta)));
  });
  this.tmpImg=new Image();
	this.tmpImg.onload=function(){
    if(obj.galVisible && this.gallery==obj.selectedGallery && this.imgID==obj.selectedImage){
      if(obj.loading != null)
        obj.loading.hide();
	    if(obj.ifPrimary){
        var cName=obj.outerImageContainer;
        var iName=obj.galleryImg;
        obj.outerImageContainer2.fadeOut(300);
        obj.ifPrimary=false;
      }else{
        var cName=obj.outerImageContainer2;
        var iName=obj.galleryImg2;
        obj.outerImageContainer.fadeOut(300);
        obj.ifPrimary=true;
      }
      var padding = obj.imageBorderWidth*2;
      cName.css({
        'width':this.width+padding,
        'height':this.height+padding,
        'top':obj.top+(obj.height-(this.height+padding))/2+obj.offsetY,
        'left':obj.left+(obj.width-(this.width+padding))/2+obj.offsetX
      });
      iName.src=this.src;
      $(iName).attr({'width':this.width,'height':this.height});
      cName.fadeIn(300);
    }
	}
}