var _show = false;
var _sizex = 600;
var _sizey = 180;
var _time = 15; /* in seconds */
function reveal(what) {
  if(_show) return;
  new Effect.Scale(what,0.1, { 
    scaleContent: false,
    scaleMode: {originalWidth:_sizex, originalHeight:_sizey},
    scaleX:false, scaleY:true, scaleFrom:100.0, duration:1,
    afterUpdate:function(effect){
      Element.setOpacity(what,1-effect.position)
    },
    afterFinish:function(){ _show = true }
  });
}

function hide(what) {
 if(!_show) return;
  new Effect.Scale(what,100, { 
    scaleContent: false,
    scaleMode: {originalWidth:_sizex, originalHeight:_sizey},
    scaleX:false, scaleY:true, scaleFrom:0.1, duration:1,
    afterUpdate:function(effect){
      Element.setOpacity(what,effect.position)
    },
    afterFinish:function(){ _show = false }
  });
}

function cross(front, back) {
	front.style.cursor='default';
	new Effect.Opacity(front, { from: 1.0, to: 0.0, 
			transition: Effect.Transitions.accelerate, 
	        afterFinish: function() {
			front.style.zIndex = 1;
			back.style.zIndex = 2;
			Element.setOpacity(front, 1.0);
			front.style.cursor='auto'; }
	});
	/*front.setStyle( {z-order: '2'});
	back.setStyle( {z-order: '1'});*/
}

/* bilder-kette */
function roll(imageContainer, imageTop, imageBottom) {
	new Effect.MoveBy(imageContainer, -_sizey, 0, {
		transition: Effect.Transitions.sinoidal2,
		duration: 2,
		afterFinish: function() {
			imageContainer.style.top=0; // reset container-position
			imageTop.style.top=_sizey + 'px';
			imageBottom.style.top='0px'; // swap images on container-div

		}
	});
}

	// imagegallery
	var myimg=['gal1.jpg', 'gal2.jpg', 'gal3.jpg', 'gal4.jpg', 'gal5.jpg'];
	var _imgno=-1;
	var _imgcnt=5;
	var _imageloader=null;
	var _timer=null;
	var _mode='cross';
	var _curpane=0;
	function setmode(what) {
	    _mode=what;
	    switch (_mode) {
	    	case 'curtain': 
	    	    Element.hide('picpane1');
	    	    Element.hide('picpane2');
	    	    nextimage();
	    	    break;
	    	case 'cross':
	    	    Element.show('picpane1');
	    	    Element.show('picpane2');
	    	    nextimage();
	    	    break;
	    	case 'roll':
	    	    break;
	    }
	      
	}
	function imageloaded() {
	 switch (_mode) {
	 	case 'curtain': 
			$('imgbox').style.backgroundImage = 'url(/img/gallery/'+myimg[_imgno]+')';
			reveal('s1'); // hintergrundbild
			break;
		case 'cross':
		    // 
		    var front=(_curpane==0)?$('picpane1'):$('picpane2');
		    var back=(_curpane==1)?$('picpane1'):$('picpane2');
		    _curpane=(_curpane+1)%2;
		    back.style.backgroundImage = 'url(/img/gallery/'+myimg[_imgno]+')'; 
		    cross(front, back);
		    break;
		 case 'roll':
		    var imageTop=($('panel1').style.top=='0px')?$('panel1'):$('panel2');
		    var imageBottom=(imageTop==$('panel1'))?$('panel2'):$('panel1');
		    imageBottom.style.backgroundImage = 'url(/img/gallery/' + myimg[_imgno]+')';
		    roll($('panelplane'), imageTop, imageBottom);
		    break;
		}
	}
	function imageleave() {
	 switch (_mode) {
	 	case 'curtain': 
			hide('s1'); // verschwinden lassen
			break;
		case 'cross':
		    break;  // do nothing....
		case 'roll':
		    break;   // do nothing.
		}
	}
	function updateImage() {
		if(_imageloader && _imageloader.complete && !_show) {
			clearInterval(_timer);
			_timer = null;
			_imageloader = null;
			imageloaded();

		}
	}
	
	function nextimage() {
		_imgno++;
		_imgno=_imgno%_imgcnt;
		_imageloader = new Image();
		_imageloader.src = '/img/gallery/'+myimg[_imgno];
		imageleave();
		_timer = setInterval(updateImage, 10);

	}
	var _globaltimer=null;
	function startgallery(){
		if (_globaltimer) {
			clearInterval(_globaltimer);
			_globaltimer=null;
		} else {
			_globaltimer = setInterval(nextimage, _time*1000);
		}	
	}
