/**
 * Slideshow, based on http://electricprism.com/aeron/slideshow
 * Check the licence !
 * 
 * Author: Eric Jeker <eric.jeker@gmail.com>
 * Copyright (c) 2008 Virtua SA
**/

Slideshow = new Class({
  options: {
    root: '../images',
    width: false,
    height: 'auto',
    duration: 1000,
    delay: 5000,
    resize: true,
    center: true
  },
  
  initialize: function (el, images, options)	{
    // merging the default options + options in parameters
  	this.options = Object.extend(this.options, options || {}) ;

    // make sure image path has a trailing slash
		if (this.options.root.substr(-1) != '/')  {
		  this.options.root += '/' ; 
		}

  	this.slideshow = $(el) ;
  	this.images = images ;

    // test 2: no image array
		if (!this.images.length)	{ 
			return ;
		}
		
		this.preload() ;
		
		this.paused = false ;
		this.nextloop = (new Date).getTime() + this.options.delay ;
		
		// initializing the current position
  	this.go(0) ;
  	
  	// starting the slideshow
  	this.start() ;
  },
  
  start: function() {
    this.timer = 0 ;
    this.loop() ;
  },
  
  loop: function() {
    if (!this.paused) {
      if (this.nextloop <= (new Date).getTime()) {
        this.nextloop = (new Date).getTime() + this.options.delay ;
        this.next() ;
        this.loop.delay(100, this)  ;
      } else {
        this.loop.delay(100, this)  ;
      }
    }
  },
  
  pause: function()	{
    if (this.paused)	{
      this.paused = false ;
      this.nextloop = (new Date).getTime() + this.options.delay ;
      this.loop.delay(100, this) ;
    } else {
      this.paused = true ;
      this.nextloop = (new Date).getTime() + Number.MAX_VALUE ;
    }
  },
  
  /* preloading all the images and building the link */
  preload: function()	{
  	for (i = 0 ; i < this.images.length ; i++)	{
  		this.images[i].img = new Image() ;
	    this.images[i].img.src = this.options.root + this.images[i].src ;
      this.images[i].a = new Element('a', {
        'href': this.images[i].href,
        'styles': {
          'display': 'none',
          'opacity': 0,
          'position': 'absolute',
          'width': this.options.width,
          'height': this.options.height
        }
      }) ;
  	}
  },
  
  show: function()	{
    if (this.images[this.slide].img.complete)	{
	    // creating the link tag to load
	    var toload = this.images[this.slide].a ;
	
	  	// getting the current image to inject inside the link tag
      var image = this.images[this.slide].img ;
      //$('animated_link').href = toload.href ;
      
			// resizing and centering the image
			this._resize(image, image.width, image.height) ;
			this._center(image) ;
			
	    // image injection inside the link tag
	    //image.injectInside(toload) ; // cannot inject inside on IE6
      toload.appendChild(image) ;
	    
	    // link and image injected inside the slideshow
	    toload.injectInside(this.slideshow) ;
	
	  	if (this.loaded)	{
	  	  // image disapear
	  	  if (this.loaded.appear)	{
	        this.loaded.appear.stop() ;	
	  	  }
	  	  this.loaded.disappear = new Fx.Styles(this.loaded, {duration:this.options.duration, wait:false});
				this.loaded.disappear.start({
						'opacity': 0
				}) ;
	    }
	    
	    // replacing by the image to load
	  	this.loaded = toload ;
			
			// image appear
	    this.loaded.setStyle('display', 'block') ;
	    this.loaded.appear = new Fx.Styles(this.loaded, {duration:this.options.duration, wait:false});
			this.loaded.appear.start({
				'opacity': 1
			}) ;
		} else {
		  this.show.delay(100, this) ;
		}
  },
  
  go: function(n)	{
    this.slide = n ;
    this.show() ;
  },
  
  first: function()	{
    this.go(0) ;

  	return false ;
  },

  last: function()	{
    this.go(this.images.length - 1) ;

  	return false ;
  },
  
  next: function(pause)	{
    if (!this.paused && pause)	{
      this.pause() ;
    }
  
  	n = this.slide + 1 > this.images.length - 1 ? 0 : this.slide + 1 ;
  	this.go(n) ;
  	
  	return false ;
  },
  
  prev: function(pause)	{
    if (!this.paused && pause)	{
      this.pause() ;
    }
  
  	n = this.slide - 1 >= 0 ? this.slide - 1 : this.images.length - 1 ;
  	this.go(n) ;

  	return false ;
  },

	_center: function(img) {
		if (this.options.center) {
			img.setStyles({
			  'margin-left' : (img.width - this.options.width) / -2,
				'margin-top' : (img.height - this.options.height) / -2
			}) ;
		}
	},

	_resize: function(img, w, h) {
		if (this.options.resize){
			var dw = this.options.width / w;
			var dh = this.options.height / h;
			
			var new_width = w ;
			var new_height = h ;
			
			if (dw < 1)	{
				new_width = Math.ceil(w * dw) ;
				new_height = Math.ceil(h * dw) ;
				
				dw = this.options.width / new_width ;
				dh = this.options.height / new_height ;
			}

			if (dh < 1)	{
				new_width = Math.ceil(w * dh) ;
				new_height = Math.ceil(h * dh) ;
				
				dw = this.options.width / new_width ;
				dh = this.options.height / new_height ;
			}
			
			if (img.width)	{
			  img.width = new_width ;
			}
			
			if (img.height)	{
			  img.height = new_height ;
			}
			
			img.setStyles({
				'width' : new_width,
			  'height' : new_height
			}) ;
		}	
	}
}) ;