var galleries=new Array();

// CONSTRAINT: (w_std * n) = w_focus + (w_small * (n-1))
// default: n=7, w_std=50, w_focus=200, w_small=25
function Gallery(prefix,n,w_std,w_focus,w_small) {
  this.prefix=prefix;

  this.n=n;
  this.w_std=w_std;
  this.w_focus=w_focus;
  this.w_small=w_small;

  this.divs=new Array();
  this.imgs=new Array();
  this.updates=new Array();
  this.timeout=10;

  this.objectidx=galleries.length;
  galleries[this.objectidx]=this;
}

Gallery.prototype.prefix;
Gallery.prototype.objectidx;

Gallery.prototype.n;
Gallery.prototype.w_std;
Gallery.prototype.w_focus;
Gallery.prototype.w_small;

Gallery.prototype.timer;
Gallery.prototype.divs= new Array();
Gallery.prototype.imgs= new Array();
Gallery.prototype.updates= new Array();
Gallery.prototype.target;
Gallery.prototype.in_focus;
Gallery.prototype.timeout;

Gallery.prototype.init = function(){
  
  var div;
  var img;
  var total_width=0;
  for(i=0;i<this.n;i++){
     div=this.get("div"+i);
     img=this.get("img"+i);
     div.style.width=this.w_std+"px";
     total_width+=this.w_std;
     img.style.width=this.w_focus+"px";
     this.divs[i]=div;
     this.imgs[i]=img;
     
     this.center(i);
  }
  document.getElementById(this.prefix+"keep_images_from_floating").style.display='block';
  document.getElementById(this.prefix+"keep_images_from_floating").style.width=total_width+'px';
}

Gallery.prototype.stabilize = function(){
   var i;var d;var width;
   var total_growth=0;  
   if(this.targetReached()){
	return true;
   }
   for(i=0;i<this.n;i++){
        d=this.getTargetWidth(i)-this.getWidth(i);
 	if(d>0){ 
	   this.updates[i]=1;
	   total_growth++;
	}
	else this.updates[i]=0;
   }
   while(total_growth!=0){
	for(i=0;i<this.n;i++){
           d=this.getTargetWidth(i)-this.getWidth(i);
	   if(d<0){
		this.updates[i]--;
		total_growth--;
	   }
	   if(total_growth==0)break;
 	}
   }
   this.do_updates();
   this.timer=setTimeout("galleries["+this.objectidx+"].stabilize()",this.timeout); 	
}


Gallery.prototype.adapt = function(){

   var i;var d;var width;
   var str="";
   var total_growth=0;
   if(this.targetReached() && this.in_focus!=-1){
        show(this.prefix+'label_cat_'+this.in_focus);
	return true;
   }
   if(this.in_focus>-1){
      d=this.getTargetWidth(this.in_focus)-this.getWidth(this.in_focus);
      this.updates[this.in_focus]=(this.n-1);
      if(this.updates[this.in_focus]>d) this.updates[this.in_focus]=d;
      total_growth=this.updates[this.in_focus];
   }

   for(i=0;i<this.n;i++){
	if(i!=this.in_focus) this.updates[i]=0;
   }
   while(total_growth!=0){
       	for(i=0;i< this.n;i++){
 	   if(i==this.in_focus)continue;
	   d=this.getTargetWidth(i)-this.getWidth(i);
	   if(d<0){
		this.updates[i]-=1;
	        total_growth-=1;
	   }		
	   if(total_growth==0)break;
	}	
   }
   this.do_updates();
   timer=setTimeout("galleries["+this.objectidx+"].adapt()",this.timeout); 	
   
}
Gallery.prototype.center = function(id){
   var offset=parseInt((this.w_focus-this.getWidth(id))/2);
   var left=-offset+"px";
   this.imgs[id].style.left=left; 
}
Gallery.prototype.do_updates = function(){
   var i;
   for(i=0;i<this.n;i++){
	this.update(i,this.updates[i]);
   }
   return true;
}
Gallery.prototype.targetReached = function(){
   var i;
   for(i=0;i<this.n;i++){
	if(this.getWidth(i)!=this.getTargetWidth(i))return false;
   }
   return true;
}
Gallery.prototype.getTargetWidth = function(id){
   if(id==this.in_focus)return this.w_focus;	// if this is the focussed one
   if(this.in_focus==-1)return this.w_std;	// if there is no focussed one
   return this.w_small;			// if this is one that should make space for the focussed one
}
Gallery.prototype.update = function(id,q){
   if(q==0){
	return true;
   }
   var div=this.divs[id];
   var width=this.getWidth(id);
   div.style.width=(width+q)+"px";
   this.center(id);
}

Gallery.prototype.get = function(id){
   return document.getElementById(this.prefix+id);
}

Gallery.prototype.getWidth = function(id){
   var width=this.divs[id].style.width;
   if (width.match(/^([0-9]+)px/)) {
         width = RegExp.$1;
   }
   return parseInt(width);
}
Gallery.prototype.expand = function(id){
   this.in_focus=id;
   clearTimeout(this.timer);
   this.adapt();
}
Gallery.prototype.go_stable = function(){
   hide(this.prefix+'label_cat_'+this.in_focus);
   this.in_focus=-1;
   clearTimeout(this.timer);
   this.stabilize();
}

