/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
window.addEvent('domready', function(){    
	// only load simple carousel on the homepage
	if($chk($('hero-feature'))) {
	    slideshow.init();            
	}

        new Carousel({
            'container'             : '.list-box.nett-partners',
            'slides'                : 'ul.partner-scroll',
            'nextController'        : '#next-partner',
            'previousController'    : '#previous-partner'
        });

        $$('ul.side-panel li').each(function(el) {
           el.addEvent('click', function(e) {
               var link = el.getElement('a');
               if ($chk(link)) {
                   window.location.href = link.get('href');
               }
            });
        });
});

var slideshow = {
	init: function() {
		new SimpleCarousel('hero-feature',
                                    $$('#hero-feature .item'),
                                    $$('#slideshow-controls a'),
                                    {
                                    'slideInterval': 5000,
                                    'buttonOnClass': 'active',
                                    'rotateAction': 'click',
                                    'autoplay': true,
                                    'extraButtons' : 'ul.side-panel li',
                                    'extraButtonsAction' : 'mouseover'
                                    }
                                  );
	}
}

Carousel = new Class ({
	Implements: [Options],

	options: {
		'container': 'container',			// the element that will be the outer container for the carousel
                'slides' : '.section-content',
                'nextController' : 'a.next',
                'previousController' : 'a.previous',
                'autoPlay' : true,
                'autoPlayInterval' : 5000
	},

	initialize: function(options) {
		this.setOptions(options);
		this.run();
	},

	slides: null,
	current: 0,
	next: 0,
	previous: 0,
	numberOfSlides: 0,
        autoPlay: true,
        _autoPlayTimer : null,

	run: function(container) {
		var self = this;
		var container = this.options.container;
		self.slides = $$(container + ' ' + this.options.slides);
		self.numberOfSlides = self.slides.length;
		self._setCurrent();
		// add events to the previous and next buttons
		self.nextEl = $$(container + ' ' + this.options.nextController);
		self.previousEl = $$(container + ' ' + this.options.previousController);

		self.nextEl.addEvent('click', function(ev){
			if ($chk(ev)){
                          ev.stop();
                          if ($chk(self._autoPlayTimer)) {
                                $clear(self._autoPlayTimer);
                            }
                        }
                        
			self._showNext();                        
		});
		self.previousEl.addEvent('click', function(ev){
			if ($chk(ev)){
                          ev.stop();
                          if ($chk(self._autoPlayTimer)) {
                                $clear(self._autoPlayTimer);
                            }
                        } 
			self._showPrevious();
		});

                if (this.options.autoPlay) {
                    self._autoPlayTimer = self._setAuto.periodical(this.options.autoPlayInterval, this);
                }

	},
        _setAuto: function() {
            var self = this;
            self.nextEl.fireEvent('click');
        },
	_setCurrent: function() {
		var self = this;
		self.slides.each(function(slide, key){
			if(!slide.hasClass('hidden')) {
				self.current = key;
				self.next = self.current + 1;
				if (self.next % (self.numberOfSlides) == 0) {
					self.next = 0;
				}
				self.previous = self.current - 1;
				if (self.previous < 0) {
					self.previous = self.numberOfSlides - 1;
				}
			}
		});
	},
	_showNext: function() {
		var self = this;

		self.slides.each(function(slide, key){
			if (key == self.current) {
				slide.addClass('hidden');
			} else if(key == self.next) {
				slide.removeClass('hidden');
			} else {
				slide.addClass('hidden');
			}


		});
		self._setCurrent();
	},
	_showPrevious: function() {
		var self = this;

		self.slides.each(function(slide, key){
			if (key == self.current) {
				slide.addClass('hidden');
			} else if(key == self.previous) {
				slide.removeClass('hidden');
			} else {
				slide.addClass('hidden');
			}
		});
		self._setCurrent();
	}
});