var ImageCarousel = new Class({
	Implements: [Options],
	options: {
		fileLister:'fotogaleria.asp?',
		showDuration: 3500,
		fadeDuration: 900,
		autoPlay: true,
		tipo:0,
		ids: [],
		files: []
	},
	slide: [],
	number: [],
 
	initialize: function(options){
		this.setOptions(options);
		this.options.element = $(this.options.element).grab(
			this.panel = new Element('div', { 'class': 'panel' })
		);
		this.dim = this.options.element.getSize();
        var carga_url  = this.options.fileLister + this.options.source;
		new Request.JSON({
			url: carga_url,
			secure: false, 
			onSuccess: function(datos){
				if($defined(datos.noticias)==true){
				  this.options.tipo=1;//noticias
				  this.options.ids = datos.noticias.split(',');
				  txt_noticias(this.options.ids[0]);
				}
				
				var file = datos.img.split(',');
				
				this.setFiles(file);
				this.loadImage(0);
			}.bind(this)
		}).get();

		if (this.options.autoPlay) this.play();
	},
	
	play: function() {
		this.player = this.nextSlide.periodical(this.options.showDuration, this);
		return this;
	},
	
	stop: function() {
		$clear(this.player);
		return this;
	},
	
	setFiles: function(files) {
		this.options.files = files;
		this.options.files.each(function(f) {
			if ($defined(f)) {
				var slide = {
					file: f,
					element: new Element('div', {
						'class': 'slide',
						styles: {
							width: this.dim.x, height: this.dim.y,
							backgroundImage: 'url('+f+')',
							opacity: 0
						}
					})
				};
				this.options.element.grab(slide.element);
				slide.element.set('tween', {duration: this.options.fadeDuration})
				this.slide.push(slide);
			}
		}.bind(this));
		this.addNumbers();
		return this;
	},
	
	loadImage: function(i) {
		this.currentImage = i;
		this.slide.each(function(s, j) {
			this.number[j].removeClass('active');
			if (i == j) { // show this slide
				s.element.fade(1);
				this.number[j].addClass('active');
			} else { // else hide it
				s.element.fade(0);
			}
		}.bind(this));
	},
	
	addNumbers: function() {
		this.slide.each(function(s, i) {
			var n;
			this.panel.grab(
				n = new Element('div', {
					'class': 'number',
					events: {
						click: function() {
							this.loadImage(i);
							this.stop().play();
							
							if(this.options.tipo==1)//cargamos texto aleatorio de una noticia
		  					  txt_noticias(this.options.ids[i]);
														
						}.bind(this)
					}
				}).grab(new Element('span', { text: (i+1) }))
			);
			this.number.push(n);
		}.bind(this));
	},
	
	nextSlide: function() {
		var t = this.currentImage + 1;
		if (t > this.slide.length - 1) t = 0;
		this.loadImage(t);
		
		if(this.options.tipo==1)//cargamos texto aleatorio de una noticia
		  txt_noticias(this.options.ids[t]);
		
		return this;
	}	
});

ImageCarousel.extend({
	speed: {
		fast: {
			showDuration: 2000,
			fadeDuration: 400
		},
		normal: {
			showDuration: 7400,
			fadeDuration: 900
		},
		slow: {
			showDuration: 5000,
			fadeDuration: 1300
		}
	}
});

window.addEvent('load', function()
{
  carga_fotos();
});

function carga_fotos(rel)
{
  $$('.ImageCarousel').each(function(e){
    var speed = ImageCarousel.speed.normal;
    if (e.hasClass('fast')) speed = ImageCarousel.speed.fast;
    if (e.hasClass('slow')) speed = ImageCarousel.speed.slow;
		
    if($defined(rel)==true){
      $$('.ImageCarousel').set('html','');//limpiamos
      new ImageCarousel($merge({ 
        element: e,
	    source: rel
      }, speed));
    }
    else{
      new ImageCarousel($merge({ 
        element: e,
	    source: $defined(e.get('rel')) ? e.get('rel') : null
      }, speed));
    }    
  });
}
