$(window).addEvent('domready', function() {
	if($$('.slideshow').length > 0) {
		$$('.slideshow').each(function(slideshow){
			if(slideshow.getElements('img').length > 1) {
				ce_slideshow(slideshow, 'img', 10000);
			}
		});
	}
	if($('newsticker')) {
		objNewsticker = new Newsticker({
			'wrapper': $('newsticker'),
			'container': $('newsticker_inner'),
			'item': '.item',
			'delay': 10000,
			'reverse': true,
			'fx': {
				'duration': 500,
				'transition': Fx.Transitions.Sine.easeOut
			}
		});
	}
	
	$$('#main_nav ul', '#main_nav li', '#main_nav a').addEvents({
		'mouseenter': function(){
			this.addClass('hover');
		},
		'mouseleave': function(){
			this.removeClass('hover');
		}
	});
	
});


function ce_slideshow(container, type, length) {
	var length = length ? length : 4000 ;
	var elements = container.getElements(type);
	var elementcount = elements.length - 1;
	var visible = elementcount;
	elements.fade('hide');
	elements[visible].fade('show');
	
	var fader = (function(){
		elements[visible].fade(0);
		if(visible == elementcount) {
			visible = 0;
		} else {
			visible = visible + 1;
		}
		elements[visible].fade(1);
	}).periodical(length);
}

var Newsticker = new Class({
	'options': {
		'wrapper': {},
		'container': {},
		'item': {},
		'delay': 0
	},
	'timer': 0,
	'current': {},
	'fx': {},
	'initialize': function(options){
		this.options.wrapper = $(options.wrapper);
		this.options.container = $(options.container);
		this.options.item = options.item;
		this.options.delay = options.delay;
		this.options.reverse = options.reverse;
		this.fx = new Fx.Scroll(this.options.wrapper, options.fx);
		this.current = this.options.container.getLast(this.options.item);
		this.fx.set(0, this.current.getSize().y * this.options.container.getChildren(this.options.item).length - this.current.getSize().y);
		this.start.delay(this.options.delay, this);
	},
	'start': function(){
		if(this.options.reverse) {
			this.timer = this.prev.periodical(this.options.delay, this);
			this.prev();
		} else {
			this.timer = this.next.periodical(this.options.delay, this);
			this.next();
		}
	},
	'stop': function(){
		this.timer = $clear(this.timer);
	},
	'next': function(){
		if(next = this.current.getNext(this.options.item)) {
			this.current = next;
		} else {
			this.current = this.options.container.getFirst(this.options.item);
		}
		this.fx.toElement(this.current);
	},
	'prev': function(){
		if(next = this.current.getPrevious(this.options.item)) {
			this.current = next;
		} else {
			this.current = this.options.container.getLast(this.options.item);
		}
		this.fx.toElement(this.current);
	}
});
