/**
 * @author thomas
 */
(function(){
    Array.implement({
        'sumup': function(){
            var sum=0;
            this.each(function(val){sum+=(val).toInt();});
            return sum;
        }
    });

	var HeroSlider = new Class({
		Extends: Player,
		
		options: {
			'textCont': null
		},
		
		initialize: function(opts){
			this.setOptions(opts);
			this.sliderCont = this.options.items[0].getParent();
			this.sliderCont.setStyle('width', 942*this.options.items.length);
			this.parent();
		},
		
		slide: function(){
			this.curItem = this.options.items[this.curIndex];
			
			if (!this.curItem.retrieve('loaded')) {
				var img = this.curItem.getElement('img')
				img.addEvent('load', this.slideContainer.bind(this)).set('src', img.get('rel'));
				this.curItem.store('loaded', true);
			}
			else{
				this.slideContainer();
			}
			return this;
		},
		
		slideContainer: function(){
			var cont = this.curItem.getParent();
			var slideTo = this.curItem.getPosition(cont).x;
			cont.set('tween', this.options.fxOpts).tween('margin-left', -slideTo);
			this.updateText();
			return this;
		},
		
		updateText: function(){
			if(this.options.textCont){
				var text = this.curItem.getElement('.hero-text').get('html');
				this.options.textCont.set('html', text);
			}
			return this;
		}
	});
	
	window.addEvent('domready', function(){
		if(document.getElement('.hero')){
			var tsSet = new HeroSlider({
				'autoPlay': false,
				'textCont': document.id('text-cont'),
				'items': document.getElements('.hero-item')
			}).slideTo(0);

			document.getElements('.next').addEvent('click', (function(e){
				e.stop();
				this.next();
			}).bind(tsSet));
			
			document.getElements('.prev').addEvent('click', (function(e){
				e.stop();
				this.prev();
			}).bind(tsSet));	
		}
	});
})();

