var SlideShow = Class.create();

SlideShow.prototype = {
  pics: [],
  current: -1,
  running: false,
  insertionNode: 'pictures',
  duration: 3.0,
  timer: null,
  debug: false,

  initialize: function(pictures, options) {
    this.timer = null;
    this.auto = false;
    this.running = false;
    this.current = -1;
    this.prefix = 'pic_';
    
    this.index = 0;
    this.pics = pictures;
    var options = options || { };
    this.updateStatus = options.updateStatus || function() { };
    this.afterEffect = options.afterEffect || function() { };
    this.beforeEffect = options.beforeEffect || function() { };
    
    delete(options.updateStatus);
    Object.extend(this, options);

    return this;
  },

  run: function(){
    this.debug ? console.info('entrée dans _run()') : null;
    if(this.pics.length>1 && this.auto) 
	{
    	this.startAutomaticSlide({start: true});
    }
    else
	{
    	this.current==-1 ? this.show(0) : this.next();
	}	
    delete this.auto;
    this.debug ? console.info('Sortie de _run()') : null;
  },

  // _next: function(options) {
  //   this.debug ? console.info('entrée dans _next()') : null;
  //   // if(!this.running && this.current != -1)
  //   // {
  //   //   this.current = -1
  //   //   return;
  //   // }
  // 
  //   (this.current!= -1 && $(this.prefix+this.current) )? this._hideCurrent() : null;
  //   
  //   this.current = this._nextCalculation();
  //   this.show(this.current, options);
  // },
  
  _nextCalculation: function() {
    return (this.current+1) % this.pics.length;
  },
  
  _previousCalculation: function() {
    return (this.current+this.pics.length-1) % this.pics.length;
  },
  
  _show: function(elt_id, options) {
    this.debug ? console.info('entrée dans _show') : null;
    if(Object.isUndefined(elt_id)) 
      var elt_id = this._nextCalculation();
    if(elt_id==this.current) return;
    if(!$(this.prefix+elt_id.toString()) && this.pics[elt_id])
    {
      var new_node = Builder.node('div', {id: this.prefix+elt_id.toString(), style: "display:none; position: absolute; top:0; left:0; width:0; overflow:hidden"}, [Builder.node('img', {src: this.pics[elt_id]})]);  
      $(this.insertionNode).insert({top: new_node});
      Object.extend($(this.insertionNode), {slideShow: this});
    }

    var element = $(this.prefix+elt_id);
    // if(element.getStyle('width')!=0)
    //   element.setStyle({width: '0px'})
    element.show();
    var current_id = this.current;
	var clone = element.cloneNode(true);
    clone.id = 'show_tmp';
    element.up().insert({top: clone});
    element.setStyle({width: '0px'});
    
	if(Browser.isIE()) {
    	var afterEffect = this.afterEffect.wrap(function(proceed, current_id, next_id){ 
			clone.remove();
			// element.setStyle({'z-index': 'auto'});
			element.setStyle({width: element.down().getDimensions().width+'px'});
    		if(element.id == (this.prefix+this.current))
    			proceed(current_id, next_id);
    		else
    			this._hide(elt_id, { afterEffect: function(){} });
		});
    	element.setStyle({zIndex: 150});
		clone.setStyle({zIndex: 149});
    }
    else {
    	var afterEffect = this.afterEffect.wrap(function(proceed, current_id, next_id){
    		clone.remove();
    		element.setStyle({width: element.down().getDimensions().width+'px'});
    		if(element.id == (this.prefix+this.current))
    			proceed(current_id, next_id);
    		else
    			this._hide(elt_id, { afterEffect: function(){} });
    	});
    }
    // $(this.insertionNode).childElements().each(function(el){
    //   if(el.id!=element.id && el.id!= 'hide_tmp' && el.id!='show_tmp')
    //     this._hide(elt_id);
    // }.bind(this));
    var effect_opts = {
    	style: {width: element.down().getDimensions().width+'px'},
    	beforeStart: this.beforeEffect.bind(this, current_id, elt_id),
    	afterFinish: afterEffect.bind(this, current_id, elt_id)
    };
    if(options && options.queue)
    	effect_opts['queue'] = {scope: 'transition_queue', position: 'end'};
    new Effect.Morph(clone, effect_opts);
    this.current = elt_id;
    this.updateStatus();
    this.debug ? console.info('sortie de _show') : null;
  },

  _hide: function(elt_id, options) {
    this.debug ? console.info('entrée dans _hide()') : null;
    var elt = $(this.prefix+elt_id);
    if(elt_id!=-1 && elt)
    {
		var clone = elt.cloneNode(true);
    	clone.id = 'hide_tmp';
    	elt.up().insert({bottom: clone});
    	elt.setStyle({width: '0px'});
    	if(!Browser.isIE()) {
    		new Effect.SlideUp(clone, Object.extend({scaleY: false, scaleX: true, afterFinish: function(){clone.remove();}}, options || { } ));
    		clone.fade();
    	}
    	else {
    		clone.setStyle({zIndex: 150});
			elt.setStyle({zIndex: 149});
    		new Effect.SlideUp(clone, Object.extend({scaleY: false, scaleX: true, afterFinish: function() {clone.remove();}}, options || { } ));
    		clone.fade();
    	}
    }
  },

  _hideCurrent: function(options) {
    this.debug ? console.info('entrée dans _hideCurrent()') : null;
    this._hide(this.current, options);
  },

  hideCurrent: function(options) {
    this._hideCurrent(options);
  },
  
  stop: function() {
    this.debug ? console.info('entrée dans stop()') : null;
    // this.running = false;
    this.stopAutomaticSlide();
    this.updateStatus();
  },

  updateStatus: function() {
    this.debug ? console.info('entrée dans updateStatus()') : null;
    if(this.current!=-1)
      this.debug ? console.log(pics[this.current].cat) : null;
    this.debug ? console.info('sortie de updateStatus()') : null;
  },
  
  afterEffect: function() {
    
  },
  
  toggleAutomaticSlide: function(options) {
    // this.automaticSlide = !this.automaticSlide;
    var res;
    if(!this.automaticSlide())
      res = this.startAutomaticSlide(options);
    else
      res = this.stopAutomaticSlide(options);
    // this.updateStatus();
    return res;
  },
  
  //Options handled: start (boolean), queue (boolean), id (integer)
  startAutomaticSlide: function(options) {
    this.timer = new PeriodicalExecuter(this.next.bind(this), this.duration);
      if(options && options.start==true) {
        if(!Object.isUndefined(options.id)) 
		{
			var id = options.id;
          	delete options.id;
          	this.show(id, options);
        }
        else
		{
          	this.next(options);
		}
      }
    return this;
  },
  
  stopAutomaticSlide: function() {
    if(this.timer)
      this.timer.stop();
    this.timer = null;
    this.updateStatus();
    return this;
  },
  
  resetAutomaticSlide: function(options) {
    this.stopAutomaticSlide();
    this.startAutomaticSlide(options);
  },

  automaticSlide: function() {
    return this.timer!=null;
  },
  
  show: function(index, options) {
    this.debug ? console.info('entrée dans show()') : null;
    if(this.current == index) return;
    if($(this.prefix+this.current))
      if($(this.prefix+this.current).visible() && $(this.prefix+this.current).getStyle('width')!=0) this._hideCurrent();
    this._show(index, options);
  },
  
  next: function(options) {
    this.debug ? console.info('entrée dans next()') : null;
    if((this.current!= -1 && this.pics.length>1) || this.current==-1)
    {
      var new_id = this._nextCalculation();
      if(this.current!=new_id) {
        this.show(new_id, options);
      }
    }
    this.debug ? console.info('sortie de next()') : null;
  },
  
  previous: function() {
    this.debug ? console.info('entrée dans next()') : null;
    if((this.current!= -1 && this.pics.length>1) || this.current==-1)
    {
      this._hideCurrent({queue: {scope: 'slideshow_transition', position: 'start'}});
      this._show(this._previousCalculation());
    }
    this.debug ? console.info('sortie de next()') : null;
  }
}
