// JavaScript Document
//function ThumbnailGallery(dispContainer,ctrlContainer,x,y,width,height,imageNames){
function ThumbnailGallery(dispContainer,ctrlContainer,x,y,width,height){
  this.dispContainer=$(dispContainer);
  this.ctrlContainer=$(ctrlContainer);
  this.x=x;
  this.y=y;
  this.width=width;
  this.height=height;
  this.renderLength=9;
  this.scrnColor='#62625f';
  this.thumbColor='#5b5b50';
  this.thumbHlColor='#e1e1c6';
  this.closeImg='images/close.png';
  this.closeWidth='75';
  this.closeHeight='20';
  this.closeHlColor='#ff6600';
  this.spacerImg='images/spacer.gif';
  this.ctrlWidth=this.ctrlContainer.getStyle('width').toInt();
  this.screenOverlay;
  this.closeButton;
  this.outerImageContainer;
  this.outerImageContainer2;
  this.galleryImg;
  this.galleryImg2;
  this.galVisible=false;
  this.ifPrimary=true;
  this.thumbs;
  this.loading;
  this.cInt=null;
  this.ctrlDir;
//  this.imageNames=imageNames;
  this.imgLoaders = null;
  this.lrgImgPreldr = null;
  this.selectedGallery=null;
  this.selectedImage=null;
  this.tmpImg;
  this.build();
}

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){
    this.loading.fade('show');
    (this.ifPrimary?this.outerImageContainer2:this.outerImageContainer).fade('out');
  }
  this.tmpImg.src=src.replace(/th_/,"");
}

ThumbnailGallery.prototype.toggleGallery=function(dir,gal){
  if(dir > 0 && !this.galVisible){
    this.galVisible=true;
    this.selectedGallery=gal;
    this.loading.fade('show');
    this.loadGallery(gal);
    this.screenOverlay.fade(0.95);
    this.closeButton.fade('in');
    this.slideCtrls(1);
  }else if(dir < 0 && this.galVisible){
    this.galVisible=false;
    this.selectedGallery=null;
    this.slideCtrls(-1);
    this.thumbs.init();
    this.outerImageContainer.fade('out');
    this.outerImageContainer2.fade('out');
    this.screenOverlay.fade('out');
    this.closeButton.fade('out');
  }
}
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);
  if(this.imgLoaders != null){
    for(var i=1;i<this.imgLoaders.length;i++){
      this.imgLoaders[i].stop();
    }
    this.imgLoaders[gal].go();
  }
}
ThumbnailGallery.prototype.setImgLoaders=function(imgLoaders){
  this.imgLoaders = imgLoaders;
}
ThumbnailGallery.prototype.onload=function(id,images){
  if(id == this.selectedGallery){
    this.loadImage(images[0].src.replace(/th_/,""),0);
    this.thumbs.setImages(images);
    this.thumbs.init(true);
    for(var i=1;i<this.imgLoaders.length;i++){
      if(i!=id)
        this.imgLoaders[i].go();
    }
  }
}
ThumbnailGallery.prototype.slideCtrls=function(dir){
  if(dir!=null)
    this.ctrlDir=dir;
  if(this.cInt == null){
    var obj=this;
    this.cInt=setInterval(function(){obj.slideCtrls()},50);
    return;
  }
  var cLeft = this.ctrlContainer.getStyle('left').toInt();
  cLeft+=(this.ctrlWidth/10)*this.ctrlDir;
  if((this.ctrlDir>0 && cLeft>0) || (this.ctrlDir <0 && cLeft <-this.ctrlWidth)){
    clearInterval(this.cInt);
    this.cInt=null;
    return;
  }
  this.ctrlContainer.setStyle('left',cLeft);
}
ThumbnailGallery.prototype.build=function(){
  var obj=this;
  this.screenOverlay=$(document.createElement('div'));
  this.screenOverlay.setStyles({
  	position:'absolute',
  	top:this.y,
  	left:this.x,
  	width:this.width,
  	height:this.height,
  	visibility:'hidden',
  	'background-color': this.scrnColor
  });
  this.screenOverlay.set('tween',{duration:450}).fade('hide');
  this.screenOverlay.addEvent('click',function(){obj.toggleGallery(-1)});
  this.loading=$(new Image);
  this.loading.src='images/loading.gif';
  this.loading.setStyles({
    position:'absolute',
    top:(this.height-35)/2+'px',
    left:'524px',
    width:'103px',
    height:'29px'
  });
  this.screenOverlay.grab(this.loading);
  this.closeButton=$(document.createElement('div'));
  this.closeButton.setStyles({
    position:'absolute',
    top:this.y,
    left:this.width-this.closeWidth+'px',
    width:this.closeWidth+'px',
    height:this.closeHeight+'px',
  	'background-color': this.scrnColor,
  	visibility:'hidden'
  });
  this.closeButton.set('tween',{duration:300}).fade('hide');
  this.closeButton.addEvents({
    'mouseenter':function(event){event.stop();this.tween('background-color',['#fffd00',obj.closeHlColor]);},
    'mouseleave':function(event){event.stop();this.tween('background-color',obj.scrnColor);},
    'click':function(event){event.stop();obj.toggleGallery(-1);}
  });
  var closeImg=$(new Image);
  closeImg.setProperties({
    src:this.closeImg,
    width:this.closeWidth,
    height:this.closeHeight
  });
  this.closeButton.grab(closeImg);
  this.outerImageContainer=$(document.createElement('div'));
  this.outerImageContainer.setStyles({
  	position:'absolute',
  	top:'100px',
  	left:'100px',
    margin:'0 auto',
    visibility:'hidden',
  	'background-color':'#fff',
    'opacity':0
  });
  this.outerImageContainer.set('tween',{duration:300});
  var imageContainer=$(document.createElement('div'));
  imageContainer.setStyle('padding','10px');
  this.galleryImg=$(new Image);
  this.galleryImg.src=this.spacerImg;
  imageContainer.grab(this.galleryImg);
  this.outerImageContainer.grab(imageContainer);
  this.outerImageContainer2=$(document.createElement('div'));
  this.outerImageContainer2.setStyles({
  	position:'absolute',
  	top:'100px',
  	left:'100px',
    margin:'0 auto',
    visibility:'hidden',
  	'background-color':'#fff',
    'opacity':0
  });
  imageContainer=$(document.createElement('div'));
  imageContainer.setStyle('padding','10px');
  this.galleryImg2=$(new Image);
  this.galleryImg2.src=this.spacerImg;
  imageContainer.grab(this.galleryImg2);
  this.outerImageContainer2.grab(imageContainer);
  this.dispContainer.adopt(this.screenOverlay,this.closeButton,this.outerImageContainer,this.outerImageContainer2);
  var thumbDiv=$(document.createElement('div'));
  thumbDiv.setStyles({
  	position:'absolute',
  	top:'0px',
  	left:'0px'
	});
  this.thumbs = new RoundGallery(thumbDiv,500,-430,250,90,this.renderLength,this.thumbColor,this.thumbHlColor,this);
  this.thumbs.setStartAngle(Math.PI*0.756);
  this.thumbs.setOffset(-2);
  this.thumbs.setDefaultSelected(0);
  this.thumbs.init(true);
  this.ctrlContainer.grab(thumbDiv,'top');
  $(document).addEvent('mousewheel',function(e){
    if(obj.galVisible)
        obj.thumbs.queue(-(e.wheel<0?Math.floor(e.wheel):Math.ceil(e.wheel)));
  });
  this.tmpImg=new Image();
	this.tmpImg.onload=function(){
    if(obj.galVisible && this.gallery==obj.selectedGallery && this.imgID==obj.selectedImage){
      obj.loading.fade('hide');
	    if(obj.ifPrimary){
        var cName=obj.outerImageContainer;
        var iName=obj.galleryImg;
        obj.outerImageContainer2.fade('out');
        obj.ifPrimary=false;
      }else{
        var cName=obj.outerImageContainer2;
        var iName=obj.galleryImg2;
        obj.outerImageContainer.fade('out');
        obj.ifPrimary=true;
      }
      cName.setStyles({width:this.width+20,
        height:this.height+20,
        top:(591-(this.height+20))/2,
        left:(750-(this.width+20))/2+200}
      );
      iName.src=this.src;
      iName.setStyles({width:this.width,height:this.height});
      cName.set('tween',{duration:300}).fade('in');
    }
	}
}

