

function Picture(id,url,title,legend)
{
	this.id = id;
	this.url = url;
	this.title = title;
	this.legend = legend;
}

function ImageSwitcher() 
{
        this.arr = new Array();
		this.aktindex = 0;
		this.imageId = "containerimg";
		this.legendId = "commentdiv";
		this.titleId = "titlediv";
		this.statusId = "statusdiv";
		this.intervalID = "";
		this.allowCycle = true;
}

/* constructor */
ImageSwitcher.prototype.initialize = function(imgid, commentdivid, titledivid, statusdivid) {
		this.arr = new Array();
		this.aktindex = 0;
		this.imageId = imgid;
		this.legendId = commentdivid;
		this.titleId = titledivid;
		this.statusId = statusdivid;
		
		if(this.imageId == null)
		    this.imageId = "containerimg";
		if(this.legendId == null)
		    this.legendId = "commentdiv";
		this.titleId = "titlediv";
		this.statusId = "statusdiv";
		
	
   }
   
	/* add new picture */	
   ImageSwitcher.prototype.add = function(id,url,title,legend){
		
		var pic = new Picture(id,url,title,legend);
		this.arr[this.arr.length] = pic;
   }
   
   ImageSwitcher.prototype.remove = function(index){
		
   }
   
   ImageSwitcher.prototype.first = function(){
		this.aktindex = 0;
		this.setImage();
   }
   
   ImageSwitcher.prototype.previous = function(){
	
	this.aktindex--;
	
	if(this.aktindex < 0)
	{
	    if(this.allowCycle == true)
		    this.aktindex =  this.arr.length - 1;
		else 
		    this.aktindex++;
    }
		
	 this.setImage();
   }
   
   ImageSwitcher.prototype.next = function(){
	
	this.aktindex++;
	
	if(this.aktindex >= this.arr.length){
	    if(this.allowCycle == true)
		    this.aktindex = 0;
		else
		    this.aktindex--;
    }
		
	 this.setImage();
   }
   
   ImageSwitcher.prototype.last = function(){
	  this.aktindex = this.arr.length - 1;
	  this.setImage();
   }
   
   ImageSwitcher.prototype.setImage = function()
   {
   
	 var img = $(this.imageId);
	 var legend = $(this.legendId);
	 var title = $(this.titleId);
	 var status = $(this.statusId);
	
	 var pic = this.arr[this.aktindex];
	 
	 if(pic == null)
	     return;
	     
//	 if(pic.legend == null || pic.legend == "")
//	    pic.legend = pic.title;
	        
	 //if(img != null) img.src = pic.url;
	 if(legend != null) legend.innerHTML = pic.legend;
	 if(title != null) title.innerHTML = pic.title;
	 if(status != null) status.innerHTML = (this.aktindex + 1) + "/" + this.arr.length;
	 
	 if(onImageSet)
		onImageSet(pic.id);
   }
   
   ImageSwitcher.prototype.setImageByIndex = function(index)
   {
	 this.aktindex = index;
	 this.setImage();
   }
   
   ImageSwitcher.prototype.setImageById = function(picid)
   {
	 for(var i=0;i<this.arr.length;i++)
	 {
		if(this.arr[i].id == picid)
			this.aktindex = i;
	 }
	 this.setImage();
   }

   ImageSwitcher.prototype.getImageById = function(picid)
   {
	 for(var i=0;i<this.arr.length;i++)
	 {
		if(this.arr[i].id == picid)
			return this.arr[i];
	 }
	 return null;
   }
   
   ImageSwitcher.prototype.play = function(switcherVarName, ms)
   {
      window.clearInterval(this.intervalID);
      this.intervalID =  window.setInterval(switcherVarName + ".next()", ms);
   }

   ImageSwitcher.prototype.stop = function()
   {
        window.clearInterval(this.intervalID);
   }