var GallerySlideshow = Class.create({
	initialize: function(container) {
        if(!$(container) && Prototype.Browser.IE) return; //IE kludge
		this.container  = $(container) || $("slideshow");
		this.gallery    = this.container.select("div.gallery img");
		this.thumbs     = this.container.select("div.thumbs img");
		this.scrollEl   = this.container.select("div.thumbs")[0];
		this.sets       = this.container.select("div.thumbs ul");
        this.credits    = this.container.select("div.credits p")
		this.setsCount  = this.sets.length;
		this.prev       = this.container.select("a.prev")[0];
		this.next       = this.container.select("a.next")[0];
		this.count      = this.gallery.length;
		this.active     = 0;
		this.currentSet = 0;
		this.setup();
	},
	setup: function() {
		this.thumbs.each(function(el, i) {
			el.observe("click", this.show.bindAsEventListener(this, i)) 
		}.bind(this));

		if(this.count <= 5){
			this.prev.addClassName('disable');
			this.next.addClassName('disable');
		} else {
			if(this.currentSet == 0) this.prev.addClassName('disable');

			this.prev.observe('click', function(event){
				event.stop();
				this.currentSet -= 1;
				if(this.currentSet < 0) this.currentSet = 0;
				this.slide(this.currentSet);
			}.bind(this));

			this.next.observe('click', function(event){
				event.stop();
				this.currentSet += 1;
				if(this.currentSet >= this.setsCount) this.currentSet = this.setsCount-1;
				this.slide(this.currentSet);
			}.bind(this));
		}
	},
	show: function(event, sel) {
		event.stop();
		if(this.active != sel){
			this.deactivate(this.active);
			this.activate(sel);
		}
	},
	deactivate: function(pos) {
		new Effect.Fade(this.gallery[pos], {duration: 0.9, from: 1.0, to: 0.0, afterFinish: function(){
			this.gallery[pos].setStyle({display: 'none'});
		}.bind(this)});
        this.credits[pos].removeClassName("active");
		this.thumbs[pos].removeClassName("active");
	},
	activate: function(pos) {
		new Effect.Fade(this.gallery[pos], {duration: 1.0, from: 0.0, to: 1.0, beforeStart: function(){
			this.gallery[pos].setOpacity(0);
			this.gallery[pos].setStyle({display: 'block'});
			this.active = pos;
		}.bind(this)});
        this.credits[pos].addClassName("active");
		this.thumbs[pos].addClassName("active");
	},
	slide: function(pos) {
		//Needs to Be Refactored
		//console.log(this.sets[pos].offsetLeft);
		var weirdoffset = 47;
		if(Prototype.Browser.IE) weirdoffset = 0;
		new Effect.ScrollHorizontal(this.scrollEl, {duration: 0.8, transition: Effect.Transitions.EaseFromTo, to: (this.sets[pos].offsetLeft-weirdoffset)});
		if(this.currentSet > 0) this.prev.removeClassName('disable');
		if(this.currentSet < this.setsCount) this.next.removeClassName('disable');

		if(this.currentSet <= 0) this.prev.addClassName('disable');
		if(this.currentSet >= this.setsCount-1) this.next.addClassName('disable');
	}
});

/* ---------------------------------------------------------------- */


    document.observe("dom:loaded", function(){
        
        new GallerySlideshow("slideshow");
    });    





