var Gallery = {
	Components : {}
};
Gallery.Components.Slideshow = {};

Gallery.Base = Class.create();
Gallery.Base.prototype = {
  initialize: function(options)
  {
    this.options = Object.extend(
      {
        pictureId   : 'gallery_pic',
        onClick	    : Prototype.emptyFunction,
        onLoad      : Prototype.emptyFunction,
        afterLoad   : Prototype.emptyFunction,
        onChange    : Prototype.emptyFunction,
        beforChange : Prototype.emptyFunction,
        afterChange : Prototype.emptyFunction
      }, options || {}
	);

	this._items = null;
    this._max = 0;
    this._current = 0;
    this.pictureTagId = this.options.pictureId;
  },
  load: function(data)
  {
    this._items = data;
    this._max = this._items.length;

    this.options.onLoad(this._items);
    this.options.afterLoad(this._items);
  },
  previous: function()
  {
    if (this._current==0)
      id = this._max-1;
    else
      id = parseInt(this._current)-1;

  	this.gotoId(id);

  	return false;
  },
  forward: function()
  {
    if (this._current==this._max-1)
      id = 0;
    else
      id = parseInt(this._current)+1;

  	this.gotoId(id);

  	return false;
  },
  gotoId: function(id)
  {
  	if(id==this._max) id = 0;

	this.options.beforChange(id, this._current, this._items);

  	this._current = id;

    var item = this._items[id];

    $(this.pictureTagId).src = item.image;
    $(this.pictureTagId).appear();

    this.options.onChange(id, this._current, this._items)

    this.options.afterChange(id, this._current, this._items);
  }
};

Gallery.Components.Thumbnail = Class.create();
Gallery.Components.Thumbnail.prototype = {
  _createThumbs: function()
  {
  	for (var i = 0; i < this._items.length; i++)
    {
        var element = Builder.node('li', { id: 'ghosttrain' }, [
          Builder.node('img',{ className: 'gallery_thumb', width: this.thumbWidth,
                src: this._items[i].thumb,
                id:this.thumbHolderId+'_'+i}),
        ]);

      $(this.thumbHolderId).appendChild(element);
    }
    Event.observe($(this.thumbHolderId),'click',this.options.onClick.bindAsEventListener(this));
  },
  getThumbElement: function(id)
  {
  	return $(this.thumbHolderId+'_'+id);
  }
};

Gallery.Components.Slideshow.Base = Class.create();
Gallery.Components.Slideshow.Base.prototype = {
  start: function()
  {
  	if (this._started == false)
  	{
  		this._started = true;
  		this._stop = false;
  		this._restart = false;
	    this.counter();
  	}
  },
  counter: function()
  {
  	var obj = this;
  	if (this._stop==false && this._started)
  	{
      setTimeout(function(){
      	return obj.counter();
      }, this.interval*1000);

  	  if(!this._resetTimer)
	    this.forward();
	  else
	  	this._resetTimer = false;
    }
  },
  resetTimer: function()
  {
  	this._resetTimer = true;
  },
  reset: function()
  {
    this.start();
  },
  stop: function()
  {
  	this._started=false;
    this._stop=true;
  }
};

Gallery.Components.Slideshow.Basic = Class.create();
Object.extend(Gallery.Components.Slideshow.Basic.prototype, Gallery.Components.Slideshow.Base.prototype);
Object.extend(Gallery.Components.Slideshow.Basic.prototype, Gallery.Base.prototype);

Gallery.Slideshow = Class.create(Gallery.Components.Slideshow.Basic, {
  initialize: function($super, options)
  {
  	var obj = this;
  	if(Object.isUndefined(options.afterLoad))
  	{
  		options.afterLoad = function(data){
  			if(this.autostart)
	  			obj.start();
  		};
  	}
  	$super(options);
    this.autostart = (Object.isUndefined(this.options.autostart)?false:true);
    this.interval = (Object.isUndefined(this.options.interval)?1:this.options.interval);
    this._stop = false;
    this._started = false;
    this._first = true;
    this._time = this.interval;
  }
});

Gallery.Components.Slideshow.Thumbnail = Class.create();
Object.extend(Gallery.Components.Slideshow.Thumbnail.prototype, Gallery.Components.Slideshow.Base.prototype);
Object.extend(Gallery.Components.Slideshow.Thumbnail.prototype, Gallery.Components.Thumbnail.prototype);
Object.extend(Gallery.Components.Slideshow.Thumbnail.prototype, Gallery.Base.prototype);

Gallery.Thumbnail = Class.create(Gallery.Base,{
  initialize: function($super, options)
  {
  	var obj = this;
  	if(Object.isUndefined(options.afterLoad))
  	{
  		options.afterLoad = function(data){
  			obj._createThumbs();
  		};
  	}
  	$super(options);

  	this.thumbHolderId = 'gallery_thumbs';
  	this.thumbWidth = '60px';
  }
});
Object.extend(Gallery.Thumbnail.prototype, Gallery.Components.Thumbnail.prototype);
Gallery.SlideshowWithThumbnail = Class.create(Gallery.Components.Slideshow.Thumbnail, {
  initialize: function($super, options)
  {
  	var obj = this;
  	if(Object.isUndefined(options.afterLoad))
  	{
  		options.afterLoad = function(data){
  			obj._createThumbs();
  			if(this.autostart)
	  			obj.start();
  		};
  	}
  	$super(options);
    this.autostart = (Object.isUndefined(this.options.autostart)?false:true);
    this.interval = (Object.isUndefined(this.options.interval)?1:this.options.interval);
    this._stop = false;
    this._started = false;
    this._first = true;
    this._time = this.interval;
  	this.thumbHolderId = (Object.isUndefined(this.options.thumbHolderId)?'gallery_thumbs1':this.options.thumbHolderId);
  	this.thumbWidth = '60px';
  }
});

Gallery.Parser = function(element)
{
	var e = element;
	var imgs = e.getElementsByTagName('img');
	var arr = {"items":[]};

	for(i=0; i < imgs.length; i++)
	{
	  imgs[i].style.display='none';
	  var a = {'image':imgs[i].src,'thumb':imgs[i].src}
	  arr.items.push(a);

	}
	return arr;
};