var SimpleSlideShowDemo = new Class({
	options: {
		slides: [],
		startIndex: 0,
		wrap: true,
		onShow: $empty, //Mootools 1.2: $empty
		onRotate: $empty,
		onStop: $empty,
		onAutoPlay: $empty,
		onShowSlide: $empty,
		slideInterval: 2500,
		auto:false,
		transitionDuration: 700,
		//splitter: "&middot;",
		splitter: "&#0149;",
		prev:"&laquo; ",
		next:" &raquo",
		play:"&infin;",
		stop:"&phi;",
		bigNavigation : true,
		smallNavigation: true,
		browser:true,
		addBarInside:true,
		hideText:false,
		ww_resizeImages: false
				
	},
	initialize: function(container,options){
	
	// WW old(1.1): getTag()	
	if (container[0].getParent().get('tag')=='a') {
		this.containerNest = container[0].getParent().getParent();
    } else {
		this.containerNest = container[0].getParent();
    }
	
		
	/* WW: begin  */
	
	//alert(this.containerNest.getStyle('width'));
	
	/* finde alle bilder der Slideshow */
	
	var childs = this.containerNest.getElements('img');
	
	var captions = this.containerNest.getElements('p.csc-caption');
	
	/* captions finden und verstecken */
	captions.each(function(caption){
	
		caption.setStyle('display', 'none');
	
	});
	
	
	/* finde das Bild mit der groessten Hoehe */
	
	var ww_height = 0;
	var ww_width = 0;
	var img_height = 0;
	
	for (i = 0; i < childs.length; i++)
	{
		img_height = childs[i].getAttribute('height');
		img_width = childs[i].getAttribute('width');
	
		if (parseInt(img_height) > parseInt(ww_height))
		{
			ww_height = img_height;
		}
		
		
		if (parseInt(img_width) > parseInt(ww_width))
		{
			ww_width = img_width;
		}
		
		
	}
	
	
	/* setzte die Hoehe der Slideshow, entspricht dem hoechsten Bild */
	this.containerNest.setStyle('height', ww_height + 'px');
	//this.containerNest.setStyle('width', ww_width + 'px');
	
	/* WW: end */
	
	
    this.container = container;
		this.setOptions(options);
		this.slides = [];
		this.title = [];
		this.descr = [];
		this.effects = [];
		this.browserArr = [];
		this.playStatus = false;
		if (this.options.auto) this.playStatus = true;

    this.createStructure();
	
		/* WW: begin - new 2nd parameter width and 3rd parameter height (to define the width of the title and the height of the image) */
		//this.addSlides(this.container);
		this.addSlides(this.container);
		/* WW: end */
		
		if(this.slides.length) this.showSlide(this.options.startIndex);
		if (this.options.auto) {
      		this.autoplay();
			// this.playStatus = true;
    	}
	},
	
	createFx: function(){
		this.slideFx = new Fx.Elements(this.slides, {duration: this.options.transitionDuration});
		this.slides.each(function(slide){
			slide.setStyle('opacity',0);
		});
	},	
	createStructure: function() {

	var oldClass = this.containerNest.getProperty("class");
	this.containerNest.setProperty('class', '');
	this.containerNest.addClass('rgslideshow');
	  
	
	
	    
	this.test = new Element('div').addClass(oldClass).injectBefore(this.containerNest);
	this.containerNest.injectInside(this.test);
	  
	  if (!this.options.hideText) {
	  
      if (this.options.addBarInside) {
        this.nestAll = new Element('div').setProperty('class', 'rgsnest').injectTop(this.containerNest);    
        this.title = new Element('div').setProperty('class', 'rgstitle').injectInside(this.nestAll);
        this.description = new Element('div').setProperty('class', 'rgsdescription').injectInside(this.nestAll);
      }
      
      //	  this.nestAll = new Element('div').setProperty('class', 'rgslideshow').injectAfter(this.containerNest);
      
      // if (this.options.addBarInside) this.titleOp = new Element('div').setProperty('class', 'optitle').injectInside(this.nestAll);
    //  this.nestDescription = new Element('div').setProperty('class', 'descr').injectInside(this.nestAll);
    //  if (this.options.addBarInside) this.descriptionOp = new Element('div').setProperty('class', 'opdescr').injectInside(this.nestAll);
      
   
       if(this.options.browser) {
       
        this.navigation = new Element('div').setProperty('class', 'rgslideshownav').injectAfter(this.containerNest);
        this.innerNavigation = new Element('div').setProperty('class', 'rgsnav2').injectInside(this.navigation);
        
        if(this.options.smallNavigation) this.prevEl = new Element('span').setProperty('class', 'rgsprev').injectInside(this.innerNavigation).set('html', this.options.prev).addEvent('click', this.cycleBack.bind(this));
        this.browser = new Element('div').setProperty('class', 'rgsbrowser').injectInside(this.innerNavigation);
       if(this.options.smallNavigation)  this.nextEl = new Element('span').setProperty('class', 'rgsnext').injectInside(this.innerNavigation).set('html', this.options.next).addEvent('click', this.cycleForward.bind(this));
  
        this.play = new Element('span').setProperty('class', 'rgsplay').injectInside(this.innerNavigation).addEvent('click', this.togglePlay.bind(this));    
  
        if (!this.playStatus) {
          this.play.set('html', this.options.play).setProperty('title',"Play");
        } else {
          this.play.set('html', this.options.stop).setProperty('title',"Stop");
        }
       } 
    }

    if (this.options.bigNavigation) {
      this.bigPrev = new Element('a').setProperties({'class': 'rgsbigprev', 'href': "javascript:void(0);"}).injectInside(this.containerNest).addEvent('click', this.cycleBack.bind(this));
      this.bigNext = new Element('a').setProperties({'class': 'rgsbignext', 'href': "javascript:void(0);"}).injectInside(this.containerNest).addEvent('click', this.cycleForward.bind(this));
    }    
    
    this.createFx();

  },
	addSlides: function(slides){

		var i =1;

		slides.each(function(slide){
		
			this.slides.include($(slide));
			
			/* WW: begin */
			
			var act_height = parseInt(slide.getStyle('height').replace('px', ''));
			var act_width = parseInt(slide.getStyle('width').replace('px', ''));
			
			var width =  this.containerNest.getSize();
			
			
			/* if images should be resized if image width is min. 20 Pixels smaller */
			
			//alert(width.y);
			
			//alert(parseInt(slide.getStyle('height').replace('px', '')));
			
			//alert(act_height);
			
			if(this.options.ww_resizeImages == true)
			{
				/* act_hoehe ist kleiner => weite vergroessern */
				if ((act_height < (width.y - 20)) || (act_width < (width.x - 20)))
				{
					var factor_x = width.x / act_width;
					var factor_y = width.y / act_height;
					
					if (factor_x < factor_y)
					{
						var new_width = act_width * factor_x;
						var new_height = act_height * factor_x;
						var margin_top = parseInt((width.y - act_height) / 2);
						
						/* bild auf optische Mitte */
						slide.setStyle('margin-top', margin_top - 10 + 'px');
						
					}
					else
					{
						var new_width = act_width * factor_y;
						var new_height = act_height * factor_y;
						
					}
					
					slide.setStyle('height', new_height + 'px');
					slide.setStyle('width', new_width + 'px');
					
					
				}
				
				
			}
			
					
			/* WW: end */
			
			
			//this.effects[this.slides.indexOf(slide)] = new Fx.Style(slide, 'opacity');
			
			/* WW: begin - mootools 1.1 */
			
			//this.effects[this.slides.indexOf(slide)] = new Fx.Tween(slide, 'opacity');
			this.effects[this.slides.indexOf(slide)] = new Fx.Morph(slide, 'opacity');
			
			/* WW: end  */
      
    		slide.addClass('rgssimg');
		
			
			var title=" ";
			var descr="";
		
			if (slide.getProperty("title")) {
			
        		split = slide.getProperty("title").split("|");
        	
				if (split[0]) {
            		title = split[0];
		            slide.setProperty("title",title);
    		 	}
        	  	if (split[1]) descr = split[1];
        	}	
		
			// WW: begin
			
			
			/* add p-Tag (to avoid empty transparent title-layer) */
			
			if (descr.length != 0)
			{
				this.descr[this.slides.indexOf(slide)] = '<p>' + descr + '</p>';
			}
			else
			{
				this.descr[this.slides.indexOf(slide)] = '';
			}
  			
			
			if (title.length > 1)
			{
				this.title[this.slides.indexOf(slide)] = '<p>' + title + '</p>';
			}
			else
			{
				this.title[this.slides.indexOf(slide)] = '';
			}
  			
			// WW: end
			
      if(this.options.browser) {  			
        this.browserEl = new Element('span').setProperties({'class': 'rgsbrowserEl', 'title': title}).set('html', this.slides.indexOf(slide)+1).injectInside(this.browser);
    //    this.browserEl.setProperty('id',this.options.paginationSelector+i);
  			this.browserEl.addEvent('click', this.cycleTo.bind(this,this.slides.indexOf(slide)));
  			this.browserArr[this.slides.indexOf(slide)] = this.browserEl;
  		}
			
	if (i!=1) {
		
    	if(this.options.browser)		
		{
			this.splitter = new Element('span').setProperty('class', 'rgssplitEl').set('html', this.options.splitter).injectBefore(this.browserEl);
		}
    	slide.setStyle('display','none');
		
    } else {

     	if (!this.options.hideText && this.options.addBarInside) {
        	var nestDiv = this.nestAll.getSize();
        } else {
          	var nestDiv = this.containerNest.getSize();
        }
        var img = slide.getSize();

    	if(this.options.bigNavigation) {          
        	this.bigPrev.setStyle('height',(slide.getProperty("height")-nestDiv.size.y)+'px');
       		this.bigNext.setStyle('height',(slide.getProperty("height")-nestDiv.size.y)+'px');
			
        	if(!this.options.addBarInside) {
	        	this.bigPrev.setStyle('height',(slide.getProperty("height"))+'px');
	        	this.bigNext.setStyle('height',(slide.getProperty("height"))+'px');
			}					 
        }
    		if(this.options.addBarInside) { 
			
    		  /* WW: begin */
			  //this.nestAll.setStyle('width',slide.getProperty("width")+'px');
			  this.nestAll.setStyle('width', width + 'px');
			  /* WW: end */
			  
    		}
    	  if(this.options.browser) {
       	 	
			var width =  this.containerNest.getSize();
			
			/* WW: begin - mootools 1.1 */
			
    		//this.navigation.setStyle('width',width.size.x-1+'px');
			this.navigation.setStyle('width',width.x-1+'px');
			
			/* WW end */
			
			
			
        }
		
		
		
      
    }
			i++;
		}, this);
	},
	addSlide: function(slide){
		this.addSlides([slide]);
	},
	cycleForward: function(){
		if($chk(this.now) && this.now < this.slides.length-1) this.showSlide(this.now+1);
		else if ((this.now) && this.options.wrap) this.showSlide(0);
		else if(!$defined(this.now)) this.showSlide(this.options.startIndex);
	},
	cycleBack: function(){
		if(this.now > 0) this.showSlide(this.now-1);
		else if(this.options.wrap) this.showSlide(this.slides.length-1);
	},
	cycleTo: function(i){
		if(this.playStatus) {
      this.togglePlay();
    }	
    this.showSlide(i);
	},
	togglePlay: function() {
    if (this.playStatus) {
      this.stop();
      this.playStatus = false;
      this.play.innerHTML = this.options.play;
      this.play.setProperty('title',"Play");
    }
    else {
      this.autoplay();
      this.playStatus = true;
      this.play.innerHTML = this.options.stop;
      this.play.setProperty('title',"Stop");
    }
  },
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
		this.fireEvent('onAutoPlay');
	},
	stop: function(){
		clearInterval(this.slideshowInt);
		this.fireEvent('onStop');
	},  	
	rotate: function(){
		this.cycleForward();
		this.fireEvent('onRotate');
	},	
	showSlide: function(iToShow){

		var now = this.now;		
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];

		function fadeIn(s){
		
			s.setStyles({
				display:'block',
				visibility: 'visible',
				opacity: 1.0
			});
			
			//this.effects[this.slides.indexOf(s)].start('opacity');
			
			//alert(this.slides);
			
			this.effects[this.slides.indexOf(s)].start(0, 1.0);
			
			this.fireEvent('onShow', [slide, iToShow]);
		};
    
    if (!this.options.hideText) {
      if (this.options.addBarInside) this.description.innerHTML = this.title.innerHTML = "&nbsp;";
      if(this.options.browser) {
        this.browserArr.each(function(el) {
          el.removeClass("rgsact");
        });
      }
    }

		if(slide) {    
			if($chk(now) && now != iToShow){
				this.effects[now].start(0).chain(function(){
					this.slides[now].setStyle('display', 'none');
					fadeIn.apply(this, [slide]);
          if (!this.options.hideText && this.options.addBarInside) {
            this.description.innerHTML  = this.descr[iToShow];
            this.title.innerHTML  = this.title[iToShow];
            if(this.options.browser) this.browserArr[this.now].addClass("rgsact");
          }
				}.bind(this));
			} else {
        fadeIn.apply(this, [slide]);
          if (!this.options.hideText && this.options.addBarInside) {
  		      this.description.innerHTML  = this.descr[iToShow];
            this.title.innerHTML  = this.title[iToShow];
            if(this.options.browser) this.browserArr[0].addClass("rgsact");
          }
      }
			this.now = iToShow;
		}
		

	}
});
SimpleSlideShowDemo.implement(new Options, new Events);
