
var Duplex = Class.create(); 
Duplex.prototype = {

	initialize: function(){
		var defaults = {
			
		}

		this.naviShow  = 1;
		this.actualNr  = 1;
        this.pics = new Array();
        this.steer = 0;
        this.path  = '../../';
	},
	
	setUp: function (path){
		
		this.path = path;
		
		if($('b1')){
		  this.countPic();
		  
		  if(this.pics.size() <2){
		  	
		    if($('contentText')){
		    	this.showDetail();
		    	//$('link').update('<a href="javascript:duplex.hideDetail()" title="Details ausblenden"><img class="animat" src="'+this.path+'image/back.gif" alt="Details ausblenden" /></a>');
		        //$('contentText').show();
		    };
		    return; 
		  }
		  
		  this.startDiaShow();
		  if($('contentText')){$('contentText').hide();};
		  //$('navi').setOpacity(0.8);
		  this.hideNavi.bind(this).delay(6);
		}

	},
	// Zählt Anzahl Bilder der Diashow
	countPic: function(){
 	
		if($('diaShow')){
			$('diaShow').childElements().each(function(s){
				
				var id = s.id.slice(1);
				if(id>0){
					this.pics.push(id);
				}
				if(id>1){$('b'+id).setOpacity(0)};
            }.bind(this));
		}
	},
	
	startDiaShow: function(){

		$('container').setStyle({backgroundColor:'transparent'});
	    this.pe = new PeriodicalExecuter(function(pe){
	    
	     var nr = this.actualNr;
	     nr++;
	     this.showPic(nr);
	      
		}.bind(this), 5);	
	},

	restartDiaShow: function(steer){
		
		if(this.steer == steer){
			
			this.startDiaShow();
		}
	},
	
	// Klick von Hand auf Zahl
	choosePic: function(nr){
		
		if(this.pics.size() <2){return;}
		
		// Diashow unterbrechen
		this.pe.stop();
		
		// Nach 6 Sek erneut starten
		this.steer++
		this.restartDiaShow.bind(this).delay(6, this.steer);
			
		this.showPic(nr);
	},
	
	// Zeigt Foto, blick auf Fotonav
	showPic: function(nr){	
		
		var a = $('b'+this.actualNr);
		if(nr>this.pics.size()){nr=1;}	
		var b = $('b'+nr);
		this.slideOut(a);
		this.slideIn(b);
		//a.hide.delay(2);
	    $('a'+this.actualNr).setStyle({color:'#000000', textDecoration:'none'});
		this.actualNr = nr;
		$('a'+nr).setStyle({color:'#0065bd', textDecoration:'underline'});
	},
	back: function(){
		var nr = this.actualNr;
	    nr--;
	    if(nr==0){nr = this.pics.size();}
	    this.choosePic(nr);
	},
	next: function(){
		var nr = this.actualNr;
	    nr++;
	    this.choosePic(nr);
	},
	
	slideOut: function(a){
		
		var o = 1;
	    new PeriodicalExecuter(function(pe){
	    a.setOpacity(o);
	    o=o-0.1;
	    if(o<0.01){
		    	pe.stop();
		    	a.hide();
		    }
		  }, 0.01);
		
	},
	slideIn: function(a){
		a.show();
	  var o = 0;
      new PeriodicalExecuter(function(pe){
		a.setOpacity(o);
	    o=o+0.1;
		if(o>1.1){
			pe.stop();
	      }
		}, 0.01);
	    
		
	},
	
	// Blendet Navi aus
	hideNavi: function(){

		$('navi').setOpacity(0);
		return;
		
		$('navi').setStyle({backgroundColor:'transparent'});
		return;
		
		if(this.naviShow == 1){
	      this.naviShow = 0;
	      this.slideOut($('navi'));
		}
		
	},
	
	showNavi: function(){
		
		$('navi').setOpacity(1);
		return;
		
		
		$('navi').setStyle({backgroundColor:'#ffffff'});
		return;
		
		if(this.naviShow == 0){
		    this.naviShow = 1;
		    this.slideIn($('navi'));
		}	
		
	},
	naviOver:function(){this.showNavi();},
	naviOut:function(){this.hideNavi();},
	
	// Zeigt Details
	hideDetail: function(){
		$('container').setStyle({backgroundColor:'#ffffff'});
		$('link2').setStyle({color:'#000000'});
		$('link2').href='javascript:duplex.showDetail()';
		$('link').update('<a href="javascript:duplex.showDetail()" title="Details anzeigen"><img class="animat" src="'+this.path+'image/mehr.gif" alt="Details anzeigen" /></a>');
		$('contentText').hide();

	},
	// Blendet Details aus
	showDetail: function(){
		$('container').setStyle({backgroundColor:'transparent'});
		$('link2').setStyle({color:'#0065BD'});
		$('link2').href='javascript:duplex.hideDetail()';
		$('link').update('<a href="javascript:duplex.hideDetail()" title="Details ausblenden"><img class="animat" src="'+this.path+'image/back.gif" alt="Details ausblenden" /></a>');
		$('contentText').show();
	},
	// Suche
	search: function(){

		new Ajax.Updater('searchResults', '../duplex/server_side.php', {
			parameters: { nr:2, text:$F('search')}
		});
	}
};

var duplex = new Duplex();