window.addEvent('domready', function() {

	var swfLinks = $('page').getElements('a[href$=".swf"]');

	if (swfLinks) {
	    swfLinks.each(function(link) {
	        var path  = link.getProperty('href');
	        var image = link.getElement('img');
	        if (image) {

                var flashBlock = new Swiff(path, {
                    width: image.width,
                    height: image.height
                });
                flashBlock.replaces(link);
	        }
	    });
	}

	// tickerTape
	var marginLeft = 0;
	if (Cookie.read('tickerTapeMarginLeft')) {
		marginLeft = Cookie.read('tickerTapeMarginLeft').toInt();
	}

	var tickerTape            = $('tickertape');
	var tickerTapeMoveElement = tickerTape.getElement('table');

	// clone movement element
	tickerTapeMoveElement.clone().inject(tickerTape.getElement('.content'));

	var tickerTapeWidth       = tickerTapeMoveElement.getStyle('width').toInt();
	var pageWidth             = $('page').getStyle('width').toInt();
	var baseSpeed             = 20000;
	var tickerTapeSpeed       = (((marginLeft.toInt() + tickerTapeWidth.toInt()) * baseSpeed) / tickerTapeWidth.toInt()).round();
	var tickerTapeMoveTo      = -1 * (tickerTapeMoveElement.getStyle('width').toInt());

	// tickerTapeFx fx on tickerTapeMoveElement
	var tickerTapeFx = new Fx.Tween(tickerTapeMoveElement, {
		duration: baseSpeed,
		transition: 'linear',
		onComplete: function() {
			this.options.duration = baseSpeed;
			this.start('margin-left', 0, tickerTapeMoveTo);
		}
	});

	tickerTape.addEvent('mouseenter', function() {
		tickerTapeFx.pause();
	});
	tickerTape.addEvent('mouseleave', function() {
		tickerTapeFx.resume();
	});

	tickerTapeFx.options.duration = tickerTapeSpeed;
	marginLeft = marginLeft == tickerTapeMoveTo ? 0 : marginLeft;

	tickerTapeFx.start('margin-left', marginLeft, tickerTapeMoveTo);

	// remove margin-right on column contents
	var columnContents = $$('.column');
	columnContents.each(function(columnContent, index) {
		if ((index % 2) == 1) {
			columnContent.setStyle('margin-right', 0);
		}
	});

	// back to top link
	var backToTopLinks = $$('.content a');

	backToTopLinks.each(function(link) {

		if (link.getProperty('href') == '#top') {

			link.addEvent('click', function(e) {
				var scroll = new Fx.Scroll(window).toTop();
				new Event(e).stop();
			});
		}
	});

	// Accordion
	var isProgrammaInitiatief = $(document.body).hasClass('programma-initiatief');

	if (isProgrammaInitiatief) {

		var initiativeTitles = $('content').getElements('div.initiatief h2');
		var initiativeInfo   = $('content').getElements('div.initiatief div.info');

		initiativeTitles.each( function(question) {
			initiativeTitles.setStyle('cursor', 'pointer');
			initiativeTitles.addEvent('mouseover', function() { this.toggleClass('rollover'); });
			initiativeTitles.addEvent('mouseout', function() { this.toggleClass('rollover'); });
		});

		var accordion = new Fx.Accordion(initiativeTitles, initiativeInfo, {
			opacity: true,
			display: null,
			alwaysHide: true,
			onActive: function(toggler, element) {
				toggler.addClass('active');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');
			}
		});
	}
});

window.addEvent('unload', function() {
	// set cookie for tickerTape
	var tickerTapeMarginLeft = $('tickertape').getElement('table').getStyle('margin-left').toInt();
	Cookie.write('tickerTapeMarginLeft', tickerTapeMarginLeft, {
		path: '/'
	});
});