// JavaScript Document

var SlideShow = Class.create();

SlideShow.prototype = {
	initialize : function(s_class_name, options) {
		this.options = Object.extend({
			delay : 3,
			duration_fade : 1,
			duration_appear : 1,
			morph : false,
			height : 100,
			width : 400,
			textshow: ''
		}, options || {});

	   this.slideshowMethod = this.f_switch_slides.bind(this);
      this.a_slide = $$("div."+s_class_name);
      if(this.options.textshow!='')
      {
         this.a_textshow = $$("div."+this.options.textshow);
      }
//       this.a_slide = $("div."+s_class_name);
      this.i_slide_count = this.a_slide.size();
      this.i_slide = 0;
      if(this.options.morph)
      {
         this.a_height = [];
         this.a_slide.each(function(o_elem, index) {
            if(this.options.textshow!='')
            {
               this.a_textshow[this.i_slide].show();
            }
            o_img = o_elem.select('img')[0];
            if(o_img.height)
            {
               this.a_height[index] = o_img.height;
            }
            else
            {
               o_elem.setStyle({
                  visibility: 'hidden'
               });
               o_elem.show();
               this.a_height[index] = o_img.height;
               o_elem.hide();
               o_elem.setStyle({
                  visibility: 'visible'
               });
            }
         }.bind(this));

         this.a_slide[0].setStyle({
            top: ''+(this.options.height - this.a_height[0])+'px'
         });
      }
      this.a_slide[0].show();
      if(this.slideshowMethod != undefined)
      {
         this.slideshowMethod();
      }
	},
   f_switch_slides : function() {
      var i_next_frame = this.i_slide + 1;
      if (this.i_slide == (this.i_slide_count - 1) )
      {
         i_next_frame = 0;
      }
      if(this.options.morph)
      {
         new Effect.Morph(this.a_slide[this.i_slide], {
            style: 'top:0px;',
            fps: 50,
            delay:1,
            duration:6,
            afterFinish: (function() {
               if(this.options.textshow!='')
               {
                  this.a_textshow[this.i_slide].hide();
               }
               new Effect.Fade(this.a_slide[this.i_slide], {
                  duration: this.options.duration_fade,
                  fps: 50,
                  afterFinish: (function() {
                     var o_img = this.a_slide[i_next_frame].select('img')[0];
                     var i_height = this.a_height[i_next_frame];
                     if(o_img.width!=this.options.width)
                     {
                        i_height = (this.options.width*i_height / o_img.width).ceil();
                     }

                     this.a_slide[i_next_frame].setStyle({
                        top: ''+(this.options.height - i_height)+'px'
                     });
                     if(this.options.textshow!='')
                     {
                        this.a_textshow[i_next_frame].show();
                     }
                     new Effect.Appear(this.a_slide[i_next_frame], {
                     duration: this.options.duration_appear,
                     delay: 2,
                     fps: 50,
                     queue:'end'
                     });
                     this.i_slide = i_next_frame;
                  }).bind(this)
               });
            }).bind(this)
         });
      }
      else
      {
         new Effect.Fade(this.a_slide[this.i_slide], {
            duration: this.options.duration_fade,
            fps: 50,
            afterFinish: (function() {
               if(this.options.duration_appear==0)
               {
                  this.a_slide[i_next_frame].show();
               }
               else
               {
                  new Effect.Appear(this.a_slide[i_next_frame], {
                     duration: this.options.duration_appear,
                     fps: 50,
                     queue:'end'
                  });
               }
               this.i_slide = i_next_frame;
            }).bind(this)
         });
      }
      this.slideshowMethod.delay(this.options.delay + 1);
      return;
   }
}

/*
*/
