window.addEvent('domready', function() {
	/* OVERTEXT */
	$$('.overtext').each(function(i){ new OverText(i); });

	/* MENU */
	var menu = document.id('mainmenu');
	if(menu) {
		var menuitems = menu.getElements('li');
		menuitems.addEvents({
			'mouseenter': function(e) {
				var submenu = this.getElement('.submenu');
				if(submenu) {
					submenu.setStyle('display', 'block');
					this.getElement('a').addClass('active');
				}
			},
			'mouseleave': function(e) {
				var submenu = this.getElement('.submenu');
                		if(submenu) {
					submenu.setStyle('display', 'none');
					this.getElement('a').removeClass('active');
                		}
			}
		});
	}


	/* TICKER */
	var ticker = document.id('ticker');
	if(ticker) {
		var perStep = 1;
		var speed = 40;
		var ticker_elements = ticker.getElements('span');
		var ticker_current = 0;
		var ticker_max = ticker_elements.length || 0;
		if(ticker_elements) {
			var lock = false,
			doScroll = function(){
				scrollitem(ticker.getFirst('span'));
			},
			stopScroll = function() { lock = true; },
			startScroll = function() { lock = false; },
			scrollitem = function(item) {
				var width = item.getSize().x;
				var i = 1;
				var loop = (function(){
					item.setStyle('margin-left', -i);
					if(i <= width+3) {
						if(!lock) i += perStep;
					} else {
						item.setStyle('margin-left', 0);
						item.inject(ticker, 'bottom');
						clearInterval(loop);
						doScroll();
					}
				}).periodical(speed);
			};

			
			// alle elementen bij elkaar opgeteld + eerste >= width
			if(ticker_max >= 1) {
				var calc = 0;
				ticker_elements.each(function(item) { calc += item.getSize().x; });
				if(calc >= 960) {
					// Double op messages and begin scrolling
					ticker_elements.each(function(e){ e.clone().inject(ticker,'bottom'); });
					doScroll();
					ticker.addEvents({
						'mouseenter':stopScroll,
						'mouseleave':startScroll
					});
				} else {
					ticker_elements[0].setStyle('margin-left', 10);
				}
			}
			
		}
	}

});
