function photoShow(div,fit){
  this.fit=fit;
  this.container=div;
  var x=div.getElementsByTagName('img')
  if(x) this.img=x[0];
  if(this.img){
    this.oWidth=this.img.getAttribute("owidth");
    this.oHeight=this.img.getAttribute("oheight");
    this.ratio=this.oWidth/this.oHeight;
  }
  this.container.style["overflow"]="hidden";
  this.img.style["position"]="absolute";
  this.process(true);
}

photoShow.prototype.process=function(recalc,marg){
  if(!marg)marg=0;
  if(recalc){
    this.w = parseInt(this.container.offsetWidth);
    this.h = parseInt(this.container.offsetHeight);  
  }
  var iw, ih;
  var r=this.w/this.h;
  if(r<this.ratio){
    if(this.fit){
      iw=this.w-marg*2; ih=iw/this.ratio;
    }else{
      ih=this.h-marg*2; iw=ih*this.ratio;
    }
  }else{
    if(this.fit){
      ih=this.h-marg*2; iw=ih*this.ratio;
    }else{
      iw=this.w-marg*2; ih=iw/this.ratio;
    }
  }
  this.img.style["width"]=parseInt(iw); this.img.style["height"]=ih;
  this.img.style["left"]=parseInt((this.w-iw)/2);
  this.img.style["top"]=parseInt((this.h-ih)/2);
}
photoShow.prototype.resize=function(w,h,marg){
  this.w=w; this.h=h;
  this.container.style["width"]=w;
  this.container.style["height"]=h;
  this.process(false,marg);
}


/*************************************************************/
var PhotoGalleryList=new Array();
function PhotoGalleryReset(){
  for(var i=0; i<PhotoGalleryList.length; i++) PhotoGalleryList[i].reset();
}
function PhotoGalleryNext(){
  for(var i=0; i<PhotoGalleryList.length; i++){
    if(PhotoGalleryList[i].selectedIndex!=-1){
      var nextIndex=PhotoGalleryList[i].selectedIndex*1+1;
      if(nextIndex>=PhotoGalleryList[i].photos.length){
         var nextGallery=i+1;
         if(nextGallery==PhotoGalleryList.length) nextGallery=0;
         nextIndex=0;
         if(nextGallery!=i){
           PhotoGalleryList[nextGallery].snap(nextIndex);
           var instance=PhotoGalleryList[i];
           pager.scrollThen(false,function(){instance.snapClose();});
        }else{
           PhotoGalleryList[i].slideRight(nextIndex);
        }        
      }else{
        PhotoGalleryList[i].slideLeft(nextIndex);
      }
      break;
    }
  } 
}
function PhotoGalleryPrev(){
  for(var i=0; i<PhotoGalleryList.length; i++){
    if(PhotoGalleryList[i].selectedIndex!=-1){
      var nextIndex=PhotoGalleryList[i].selectedIndex*1-1;
      if(nextIndex<0){
         var nextGallery=i-1;
         if(nextGallery<0) nextGallery=PhotoGalleryList.length-1;
         nextIndex=PhotoGalleryList[nextGallery].photos.length-1;
         if(nextGallery!=i){
           PhotoGalleryList[nextGallery].snap(nextIndex);
           var instance=PhotoGalleryList[i];
           pager.scrollThen(true,function(){instance.snapClose();});         
        }else{
           PhotoGalleryList[i].slideRight(nextIndex);
        }
      }else{
        PhotoGalleryList[i].slideRight(nextIndex);
      }
      break;
    }
  } 
}
function photoGallery(div,x,y,prev,next,fit,margin){
  this.container=div;
  this.finalMargin=margin>0?margin:0;
  this.steps=3;

  DHTML.setOpacity(this.container,0);
  this.container.style["display"]="";

  this.container.style["position"]="absolute";
  this.container.style["overflow"]="hidden";  
  this.res=new HTMLParse(div);  
  this.x=x;
  this.y=y;
  this.selectedIndex=-1;
  this.closingIndex=-1;
  this.galleryIndex=PhotoGalleryList.length;
  this.prev=prev; this.next=next;  
  PhotoGalleryList[this.galleryIndex]=this;
  this.photos=new Array();
  var instance=this;
  this.images=new Array();
  this.container.onmouseout=function(evt){if(instance.selectedIndex==-1){ var ev=new Evt(evt); instance.unhighlight(ev);}};
  for(var i=0; i<this.res.photocontainer.length; i++){
    this.photos[i]=new photoShow(this.res.photocontainer[i].node,fit); 
    this.res.photocontainer[i].node.style["position"]="absolute";
    this.res.photocontainer[i].node.setAttribute("galleryindex",i);
    this.res.photocontainer[i].node.onclick=function(evt){instance.selectPhoto(this.getAttribute("galleryindex"));};
    this.res.photocontainer[i].node.onmouseover=function(evt){instance.highlight(this.getAttribute("galleryindex"));};
    this.photos[i].img.setAttribute('small',this.photos[i].img.src);   
    this.images[i]=ce('img');    
    this.images[i].src=this.photos[i].img.getAttribute('large');
    
  }
  this.process();
  DHTML.setOpacity(this.container,1);
}
photoGallery.prototype.process=function(){
  var instance=this;
  if(this.prev){  this.prev.style["display"]="none"; this.prev.style["zIndex"]=10;}
  if(this.next){  this.next.style["display"]="none"; this.next.style["zIndex"]=10;}
  this.w = parseInt(this.container.offsetWidth);
  this.h = parseInt(this.container.offsetHeight);
  this.photow=parseInt((this.w-1)/this.x);
  this.photoh=parseInt((this.h-1)/this.y);
  var top=1; var left=1;
  var xindex=0; var yindex=0;
  for(var i=0; i<this.res.photocontainer.length; i++){
    this.res.photocontainer[i].node.style["top"]=top;
    this.res.photocontainer[i].node.style["left"]=left;
    this.res.photocontainer[i].node.style["width"]=this.photow;
    this.res.photocontainer[i].node.style["height"]=this.photoh;
    DHTML.setOpacity(this.res.photocontainer[i].node,0.25);
    this.photos[i].process(true);
    xindex++; left+=this.photow+1;
    if(xindex==this.x){ xindex=0; left=1; yindex++; top+=this.photoh+1;}
  }
}
photoGallery.prototype.selectPhoto=function(index){
  if(this.moving) return;
  this.moving=true;
  if(this.selectedIndex!=-1){
    this.reduce(this.selectedIndex);
  }else{
    var photo=this.res.photocontainer[index].node;
    this.highlight(index);    
    this.selectedIndex=index;
    this.originalTop=parseInt(photo.style["top"]);
    this.originalLeft=parseInt(photo.style["left"]);
    this.yStepSize=(this.originalTop-1)/this.steps;   
    this.xStepSize=(this.originalLeft-1)/this.steps;
    this.wStepSize=this.xStepSize+(this.w-2-(this.originalLeft+this.photow))/this.steps;
    this.hStepSize=this.yStepSize+(this.h-2-(this.originalTop+this.photoh))/this.steps;
    photo.style["zIndex"]=9;
    this.photos[index].img.setAttribute('small',this.photos[index].img.src);
    this.photos[index].img.src=this.photos[index].img.getAttribute('large');
    this.expand(index);
  } 
}
photoGallery.prototype.expand=function(index){
  var newTop, newLeft, newWidth, newHeight;
  newTop=parseInt(this.res.photocontainer[index].node.style["top"])-this.yStepSize;
  if(newTop<1)newTop=1;
  newLeft=parseInt(this.res.photocontainer[index].node.style["left"])-this.xStepSize;
  if(newLeft<1)newLeft=1;
  newWidth=parseInt(this.res.photocontainer[index].node.offsetWidth)+this.wStepSize;
  if(newWidth>this.w-1)newWidth=this.w-1;
  newHeight=parseInt(this.res.photocontainer[index].node.offsetHeight)+this.hStepSize;  
  if(newHeight>this.h-1)newHeight=this.h-1;
  this.res.photocontainer[index].node.style["top"]=newTop;
  this.res.photocontainer[index].node.style["left"]=newLeft;
  this.photos[index].resize(newWidth,newHeight,this.finalMargin);
  if(newWidth==this.w-1&&newHeight==this.h-1&&newTop==1&&newLeft==1){
    this.moving=false;
    if(this.prev)  this.prev.style["display"]="";
    if(this.next)  this.next.style["display"]="";  
  }else{
    this.timeOut=window.setTimeout("PhotoGalleryList["+this.galleryIndex+"].expand('"+index+"');",10);
  }
}



photoGallery.prototype.reduce=function(index){
  var newTop, newLeft, newWidth, newHeight;
  newTop=parseInt(this.res.photocontainer[index].node.style["top"])+this.yStepSize;
  if(newTop>this.originalTop)newTop=this.originalTop;
  newLeft=parseInt(this.res.photocontainer[index].node.style["left"])+this.xStepSize;
  if(newLeft>this.originalLeft)newLeft=this.originalLeft;
  newWidth=parseInt(this.res.photocontainer[index].node.offsetWidth)-this.wStepSize;
  if(newWidth<this.photow)newWidth=this.photow;
  newHeight=parseInt(this.res.photocontainer[index].node.offsetHeight)-this.hStepSize;
  if(newHeight<this.photoh)newHeight=this.photoh;
  this.res.photocontainer[index].node.style["top"]=newTop;
  this.res.photocontainer[index].node.style["left"]=newLeft;
  this.photos[index].resize(newWidth,newHeight);
  if(newWidth==this.photow&&newHeight==this.photoh&&newTop==this.originalTop&&newLeft==this.originalLeft){
    this.moving=false;
    this.selectedIndex=-1
    this.res.photocontainer[index].node.style["zIndex"]=1;    
    this.photos[index].img.src=this.photos[index].img.getAttribute('small');    
    if(this.prev)  this.prev.style["display"]="none";
    if(this.next)  this.next.style["display"]="none";
  }else{
    this.timeOut=window.setTimeout("PhotoGalleryList["+this.galleryIndex+"].reduce('"+index+"');",10);
  }
}
photoGallery.prototype.highlight=function(index){
  if(this.selectedIndex>=0)return;
  for(var i=0; i<this.res.photocontainer.length; i++){
    if(i==index) DHTML.setOpacity(this.res.photocontainer[i].node,1);
    else DHTML.setOpacity(this.res.photocontainer[i].node,0.25);
  } 
}
photoGallery.prototype.unhighlight=function(ev){
  if(ev){
    var posbox=Geometry.offsetBox(this.container);    
    var mouse=new Coordinate(ev.x,ev.y);
    if(mouse.inside(posbox))return;
  }
  for(var i=0; i<this.res.photocontainer.length; i++){
    DHTML.setOpacity(this.res.photocontainer[i].node,0.25);
  } 
}
photoGallery.prototype.reset=function(){
  this.unhighlight();
  if(this.selectedIndex!=-1){
    this.reduce(this.selectedIndex);
  }
}
photoGallery.prototype.slideLeft=function(index){
  if(this.selectedIndex==-1){
    this.selectPhoto(index);
  }else{
    var photo=this.res.photocontainer[index].node;
    var oldphoto=this.res.photocontainer[this.selectedIndex].node;
    DHTML.setOpacity(photo,1);
    this.tmp_originalTop=parseInt(photo.style["top"]);
    this.tmp_originalLeft=parseInt(photo.style["left"]);
    this.tmp_selectedIndex=index;
    this.slideStepSize=(this.w-1)/this.steps;
    this.yStepSize=(this.tmp_originalTop-1)/this.steps;
    this.xStepSize=(this.tmp_originalLeft-1)/this.steps;
    this.wStepSize=this.xStepSize+(this.w-2-(this.tmp_originalLeft+this.photow))/this.steps;
    this.hStepSize=this.yStepSize+(this.h-2-(this.tmp_originalTop+this.photoh))/this.steps;    
    photo.style["top"]=1;    
    photo.style["left"]=this.w;    
    this.photos[index].resize(this.w-1,this.h-1,this.finalMargin);        
    photo.style["zIndex"]=9;
    oldphoto.style["zIndex"]=5;
    this.photos[index].img.setAttribute('small',this.photos[index].img.src);
    this.photos[index].img.src=this.photos[index].img.getAttribute('large');
    this.continueLeft();
  }
}
photoGallery.prototype.continueLeft=function(){
  var photo=this.res.photocontainer[this.tmp_selectedIndex].node;
  var oldphoto=this.res.photocontainer[this.selectedIndex].node;
  var newLeft, newLeft2;
  newLeft2=parseInt(photo.style["left"])-this.slideStepSize;
  if(newLeft2<1)newLeft2=1;
  photo.style["left"]=newLeft2;    
  if(newLeft2==1){
    this.moving=false; 
    oldphoto.style["zIndex"]=1;    
    DHTML.setOpacity(oldphoto,.25);
    oldphoto.style["top"]=this.originalTop;
    oldphoto.style["left"]=this.originalLeft;   
    this.photos[this.selectedIndex].resize(this.photow,this.photoh);
    this.selectedIndex=this.tmp_selectedIndex;
    this.originalTop=this.tmp_originalTop;
    this.originalLeft=this.tmp_originalLeft;       
    if(this.prev)  this.prev.style["display"]="";
    if(this.next)  this.next.style["display"]="";  
  }else{
    this.timeOut=window.setTimeout("PhotoGalleryList["+this.galleryIndex+"].continueLeft();",10);
  }
}
photoGallery.prototype.slideRight=function(index){
  if(this.selectedIndex==-1){
    this.selectPhoto(index);
  }else{
    var photo=this.res.photocontainer[index].node;
    var oldphoto=this.res.photocontainer[this.selectedIndex].node;
    DHTML.setOpacity(photo,1);
    this.tmp_originalTop=parseInt(photo.style["top"]);
    this.tmp_originalLeft=parseInt(photo.style["left"]);
    this.tmp_selectedIndex=index;
    this.slideStepSize=(this.w-1)/this.steps;
    this.yStepSize=(this.tmp_originalTop-1)/this.steps;
    this.xStepSize=(this.tmp_originalLeft-1)/this.steps;
    this.wStepSize=this.xStepSize+(this.w-2-(this.tmp_originalLeft+this.photow))/this.steps;
    this.hStepSize=this.yStepSize+(this.h-2-(this.tmp_originalTop+this.photoh))/this.steps;    
    photo.style["top"]=1;
    photo.style["left"]=0-this.w;    
    this.photos[index].resize(this.w-1,this.h-1,this.finalMargin);    
    photo.style["zIndex"]=9;
    oldphoto.style["zIndex"]=5;
    this.photos[index].img.setAttribute('small',this.photos[index].img.src);
    this.photos[index].img.src=this.photos[index].img.getAttribute('large');
    this.continueRight();
  }
}
photoGallery.prototype.continueRight=function(){
  var photo=this.res.photocontainer[this.tmp_selectedIndex].node;
  var oldphoto=this.res.photocontainer[this.selectedIndex].node;
  var newLeft, newLeft2;
  newLeft2=parseInt(photo.style["left"])+this.slideStepSize;
  if(newLeft2>1)newLeft2=1;
  photo.style["left"]=newLeft2;    
  if(newLeft2==1){
    this.moving=false; 
    oldphoto.style["zIndex"]=1;    
    DHTML.setOpacity(oldphoto,.25);
    oldphoto.style["top"]=this.originalTop;
    oldphoto.style["left"]=this.originalLeft;   
    this.photos[this.selectedIndex].resize(this.photow,this.photoh);
    this.selectedIndex=this.tmp_selectedIndex;
    this.originalTop=this.tmp_originalTop;
    this.originalLeft=this.tmp_originalLeft;       
    if(this.prev)  this.prev.style["display"]="";
    if(this.next)  this.next.style["display"]="";  
  }else{
    this.timeOut=window.setTimeout("PhotoGalleryList["+this.galleryIndex+"].continueRight();",10);
  }
}
photoGallery.prototype.snap=function(index){
  if(this.selectedIndex>=0){
    var oldphoto=this.res.photocontainer[this.selectedIndex].node;
    oldphoto.style["zIndex"]=1;    
    DHTML.setOpacity(oldphoto,.25);
    oldphoto.style["top"]=this.originalTop;
    oldphoto.style["left"]=this.originalLeft;     
    this.photos[this.selectedIndex].resize(this.photow,this.photoh);      
  }
    var photo=this.res.photocontainer[index].node;
    this.highlight(index);    
    this.selectedIndex=index;
    this.originalTop=parseInt(photo.style["top"]);
    this.originalLeft=parseInt(photo.style["left"]);
    this.yStepSize=(this.originalTop-1)/this.steps;   
    this.xStepSize=(this.originalLeft-1)/this.steps;
    this.wStepSize=this.xStepSize+(this.w-2-(this.originalLeft+this.photow))/this.steps;
    this.hStepSize=this.yStepSize+(this.h-2-(this.originalTop+this.photoh))/this.steps;
    photo.style["zIndex"]=9;
    this.photos[index].img.setAttribute('small',this.photos[index].img.src);
    this.photos[index].img.src=this.photos[index].img.getAttribute('large');
  this.res.photocontainer[index].node.style["top"]=1;
  this.res.photocontainer[index].node.style["left"]=1;
  this.photos[index].resize(this.w-1,this.h-1,this.finalMargin);
  if(this.prev)  this.prev.style["display"]="";
  if(this.next)  this.next.style["display"]="";  
}
photoGallery.prototype.snapClose=function(){
  if(this.selectedIndex>=0){
    var oldphoto=this.res.photocontainer[this.selectedIndex].node;
    oldphoto.style["zIndex"]=1;    
    DHTML.setOpacity(oldphoto,.25);
    oldphoto.style["top"]=this.originalTop;
    oldphoto.style["left"]=this.originalLeft;     
    this.photos[this.selectedIndex].resize(this.photow,this.photoh);      
  }
  this.selectedIndex=-1;
}
