/* Morph.js contents */

var MorphList = new Class({   
	
	Implements: [Events, Options],
	
	options: {/*             
		onmouseover: $empty,
		onMorph: $empty,*/
		morph: { 'link': 'cancel' }
	},
	
	initialize: function(menu, options) {
		var that = this;
		this.setOptions(options);
		this.menu = $(menu);
		this.menuitems = this.menu.getChildren();
		this.menuitems.addEvents({
			mouseenter: function(){ that.morphTo(this); },
			mouseleave: function(){ that.morphTo(that.current); },
			mouseover: function(ev){ that.mouseover(ev, this); }
		});       
		//this.bg = new Element('li', {'class': 'background'}).adopt(new Element('div', {'class': 'left'}));
		//this.bg.fade('hide').inject(this.menu).set('morph', this.options.morph);
		this.setCurrent(this.menu.getElement('.current'));
	},          

	mouseover: function(ev, item) {
		this.setCurrent(item, true);
		this.fireEvent('onmouseover', [ev, item]);
	},
	
	setCurrent: function(el, effect){  
		//if(el && ! this.current) {
			//this.bg.set('styles', { left: el.offsetLeft, width: el.offsetWidth, height: el.offsetHeight, top: el.offsetTop });
			//(effect) ? this.bg.fade('hide').fade('in') : this.bg.setOpacity(1);
		//}
		if(this.current) this.current.removeClass('current');
		if(el) this.current = el.addClass('current');    
	},         
         
	morphTo: function(to) {
		if(! this.current) return; 
		//this.bg.morph({
			//left: to.offsetLeft, top: to.offsetTop,
			//width: to.offsetWidth, height: to.offsetHeight
		//});
		this.fireEvent('onMorph', to);
	}

});


/* Slidshow JS*/

var BarackSlideshow = new Class({
  
  Extends: MorphList,
  
  options: {/*
    onShow: $empty,*/
  },
  
  initialize: function(menu, images, loader, options) {
    this.parent(menu, options);
    this.images = $(images);
    this.imagesitems = this.images.getChildren();
	totalImages = this.imagesitems.length;
    this.imagesitems.fade('hide');
    $(loader).fade('in');
    new Asset.images(this.images.getElements('img').map(function(el) { return el.setStyle('display', 'none').get('src'); }), { onComplete: function() {
      this.loaded = true;      
      $(loader).fade('out');
	  this.show(0)
      //if(this.current) this.show(this.menuitems.indexOf(this.current));
    }.bind(this) });
  },
  			
  mouseover: function(ev, item) {
    this.parent(ev, item);
    new Event(ev).stop();
	currentImage = this.menuitems.indexOf(item);
    this.show(this.menuitems.indexOf(item));
  },
  
  show: function(index) {
    if(! this.loaded) return;
    var image = this.imagesitems[index];    
		if(image == this.curimage) return;
    $each(this.imagesitems,function(name,thisIndex){if(index != name){name.fade('out')}})
	image.dispose().fade('hide').inject(this.curimage || this.images.getFirst(), this.curimage ? 'after' : 'before').fade('in');
		image.getElement('img').setStyle('display', 'block');
    $pick(this.curimage, image).get('tween').chain(function() { this.fireEvent('onShow', image); }.bind(this));
    this.curimage = image;
	
	//////
	if(totalImages > 1){
		if(currentImage == totalImages-1){
			$('arrowRight').fade(0);
		}
		if(currentImage > 0){
			$('arrowLeft').fade(1);
		}
		if(currentImage == 0){
			$('arrowLeft').fade(0);
		}
		if(currentImage < totalImages -1){
			$('arrowRight').fade(1);
		}
	}
	/////
	var titleTweener = new Fx.Tween($('pressTitle'),{link:'chain'})
	var descriptionTweener = new Fx.Tween($('pressDescription'),{link:'chain'})
	if($chk($('pressTitle'))){
		$('pressTitle').set('text',image.getElement('img').get('alt'))
		$('pressTitle').fade(0,1);
		titleTweener.start('top',(image.getElement('img').clientHeight + 20)+ "px")
	}
	if($chk($('pressDescription'))){
		$('pressDescription').set('text',image.getElement('img').get('title'))
		$('pressDescription').fade(0,1)
		descriptionTweener.start('top',(image.getElement('img').clientHeight + 40)+ "px")
	}
	return this;
  
  
  }
  
});

/* Initialize */
var galleryInstance
window.addEvent('domready', function() {
galleryInstance = new BarackSlideshow('menu', 'pictures', 'loading', 'picture');
});

