// JavaScript Document

// Constructor 
// parentObj(optional) - Is a reference to the parent of this object. If specified this object
//                       will try to call two functions if available: 
//                       loadImage(image), preload(images,startIndex,length)
function RoundGallery(container,radius,x,y,slideSize,numVisible,color,hlColor,parentObj){
  this.r=radius;
  this.h=x;
  this.k=y;
  this.r2=Math.pow(this.r,2);
  this.slideSize=slideSize;
  this.images=null;
  this.spacerImg='images/spacer.gif';
  this.color=color;
  this.hlColor=hlColor;
  this.offset=0;
  this.selectedIndex=null;
  this.START_ANGLE=Math.PI*0.653;
  this.END_ANGLE=Math.acos(Math.cos(this.START_ANGLE)+(this.slideSize/this.r));
  this.MOVE_STEPS=3.2;
  this.MOVE_ANGLE=(this.START_ANGLE-this.END_ANGLE)/this.MOVE_STEPS;
  this.z=this.START_ANGLE;
  this.spin=-1;
  this.si=this.offset;
  this.zCounter=0;
  this.totalZ=0;
  this.renderLength=numVisible;
  this.thumbImgs=new Array();
  this.thumbDivs=new Array();
  this.galInt=null;
  this.container=container;
  this.parentObj=parentObj;
  this.lrgImages=new Array();
  this.fadedValue=0.2;
  this.fadeStep=(1-this.fadedValue)/this.MOVE_STEPS;
  this.fadeIn=this.fadedValue;
  this.fadeOut=1;
  this.cmdQ=new Array();
  this.build();
}
RoundGallery.prototype.queue=function(cmd){
  if(this.cmdQ.length<3){
    this.cmdQ.unshift(cmd);
    if(this.galInt==null)
      this.stop();
  }
}
RoundGallery.prototype.setImages=function(images){
  this.images=images;
  if(images == null)
    return;
  this.images.selectedIndex=this.selectedIndex;
  if(this.parentObj!=null && this.parentObj.preload != null)  
    this.parentObj.preload(this.images,this.images.length+this.offset,this.renderLength)
}
RoundGallery.prototype.setStartAngle=function(angle){
  this.START_ANGLE=angle;
}
RoundGallery.prototype.setOffset=function(offset){
  this.offset=offset;
}
RoundGallery.prototype.setDefaultSelected=function(index){
  this.selectedIndex=index;
}
RoundGallery.prototype.rotate=function(step){
  if(this.galInt==null && this.images!=null)
    this.doRotate(step);
}
RoundGallery.prototype.build=function(){
  var divObj,obj=this;
  var table,td,tr,tbody;

  for(var i=0;i<this.renderLength;i++){
    this.lrgImages[i]=new Image(); 
    this.thumbImgs[i]=$(new Image());
    divObj=$(document.createElement('div'));
    this.thumbDivs[i]=$(document.createElement('div'));
    table=$(document.createElement('table'));
    tbody=document.createElement('tbody');
    tr=document.createElement('tr');
    td=$(document.createElement('td'));
    
    this.container.appendChild(this.thumbDivs[i]);
    this.thumbDivs[i].appendChild(divObj);
    divObj.appendChild(table);
    table.appendChild(tbody);
    tbody.appendChild(tr);
    tr.appendChild(td);
    td.appendChild(this.thumbImgs[i]);
    table.setProperties({
      height:'100%',
      width:'100%',
      border:'0',
      cellpadding:'0',
      cellspacing:'0'
    });
    td.setProperties({
      height:'100%',
      width:'100%',
      align:'center',
      valign:'middle'
    });
    
    this.thumbImgs[i].src=this.spacerImg;
    this.thumbImgs[i].index=i;
    this.thumbImgs[i].imgID=null;
    this.thumbImgs[i].addEvent('click',function(e){
      if(e!=null)e.stop();
      if(obj.parentObj!=null&&obj.parentObj.loadImage != null)
        obj.parentObj.loadImage(this.src,this.imgID);
      if(obj.images!=null){
        var tmp=obj.si+this.index;
        if(tmp>=obj.images.length)
          tmp-=obj.images.length;
        obj.images.selectedIndex=tmp;
        obj.drawSlides(obj.r*Math.cos(obj.START_ANGLE)+obj.k,obj.si);
      }
    });
    this.thumbDivs[i].setStyles({
      position:'absolute',
      height:this.slideSize+'px',
      width:this.slideSize+'px',
      'background-color':'#000000'
    });
    divObj.selected=false;
    divObj.setStyles({
      position:'absolute',
      height:this.slideSize-2+'px',
      width:this.slideSize-2+'px',
      top:1+'px',
      left:1+'px',
      'background-color':this.color
    });
    divObj.set('tween',{duration:300,transition: Fx.Transitions.Quad.easeIn});
    divObj.addEvents({
    'mouseenter':function(event){event.stop();this.tween('background-color',['#ffffff',obj.hlColor]);},
    'mouseleave':function(event){event.stop();if(!this.selected)this.tween('background-color',obj.color);}
    });
    td.addEvents({
    'click':function(event){event.stop();this.getFirst('img').fireEvent('click')}
    });
  }
  this.thumbDivs[0].setStyle('opacity',this.fadedValue);
  this.thumbDivs[1].setStyle('opacity',this.fadedValue);
  this.thumbDivs[this.renderLength-2].setStyle('opacity',this.fadedValue);
  this.thumbDivs[this.renderLength-1].setStyle('opacity',this.fadedValue);
}
RoundGallery.prototype.drawSlides=function(y,i){
  var x,tmpX,tmpDiv;

  for(var ri=0;ri<this.renderLength;ri++){
    x=Math.floor(this.h+Math.sqrt(this.r2-Math.pow(y-this.k,2)));
    if(isNaN(x))
      x=0;
    if(ri>0 && x>tmpX){
      y=Math.floor(this.k-Math.sqrt(this.r2-Math.pow(tmpX-this.h,2)));
      x=tmpX;
    }
    tmpDiv=this.thumbDivs[ri].getFirst('div');
    if(this.images != null){
      if(i>=this.images.length)
        i=0;
      else if(i<0){
        i+=this.images.length;
        this.si=i;
      }
      this.thumbImgs[ri].src=this.images[i].src;
      this.thumbImgs[ri].imgID=i;
      this.thumbImgs[ri].setProperties({height:this.images[i].height,width:this.images[i].width})
      tmpDiv=this.thumbDivs[ri].getFirst('div');
      if(this.images.selectedIndex != null && i==this.images.selectedIndex){
        tmpDiv.setStyle('background-color',this.hlColor);
        tmpDiv.selected=true;
      }else{
        tmpDiv.setStyle('background-color',this.color);
        tmpDiv.selected=false;
      }
    }else{
      tmpDiv.setStyle('background-color',this.color);
      this.thumbImgs[ri].src=this.spacerImg;
      this.thumbImgs[ri].imgID=null;
    } 
    this.thumbDivs[ri].setStyles({'top':y+'px','left':x+'px'});

    tmpX=x+this.slideSize;
    y+=this.slideSize;
    i++;
  }
}
RoundGallery.prototype.init=function(draw){
  this.stop();
  this.si=this.offset;
  this.thumbDivs[this.renderLength-3].setStyle('opacity',1);
  this.thumbDivs[2].setStyle('opacity',1);
  if(draw==true)
    this.drawSlides(this.r*Math.cos(this.START_ANGLE)+this.k,this.si);
}
RoundGallery.prototype.stop=function(){
  if(this.galInt != null)
    clearInterval(this.galInt);
  this.galInt=null;
  this.totalZ=0;
  this.zCounter=0;
  this.z=this.START_ANGLE;
  this.fadeIn=this.fadedValue;
  this.fadeOut=1;
  if(this.cmdQ.length>0){
    this.doRotate(this.cmdQ.pop());
  }
}
RoundGallery.prototype.doRotate=function(step){
  var dir=Math.abs(step)/step;
  if(step!=0){
    if(this.galInt==null){
      this.spin=dir;
      var obj=this,tmp=this.si+step;
      this.galInt=setInterval(function(){obj.doRotate(step)},45);
      if(this.parentObj!=null&&this.parentObj.preload!=null)  
        this.parentObj.preload(this.images,tmp>this.images.length?tmp-this.images.length:tmp,this.renderLength);
    }
    if(this.totalZ>=Math.abs(step)){
      this.stop();
      return;
    }
  }else
    return;
  this.z+=this.MOVE_ANGLE*this.spin;
  this.zCounter++;
  this.fadeIn+=this.fadeStep;
  this.fadeOut-=this.fadeStep;
  if(this.zCounter>this.MOVE_STEPS){
    this.z=this.START_ANGLE;
    this.si+=this.spin;
    this.fadeIn=this.fadedValue;
    this.fadeOut=1;
    if(this.spin>0 && this.si>=this.images.length)
        this.si=0
    else if(this.si<0)
        this.si=this.images.length-1;
    this.zCounter=0;
    this.totalZ++; 
  }
  if(dir<0){
    this.thumbDivs[1].setStyle('opacity',this.fadeIn);
    this.thumbDivs[this.renderLength-3].setStyle('opacity',this.fadeOut);
  }else{
    this.thumbDivs[2].setStyle('opacity',this.fadeOut);
    this.thumbDivs[this.renderLength-2].setStyle('opacity',this.fadeIn);
  }
  this.drawSlides(this.r*Math.cos(this.z)+this.k,this.si);
}
