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

	// quotes behaviour on homepage
	if ($(document.body).hasClass('home') && !$(document.body).hasClass('admin')) {

		// select all quotes in content div
		var quotes = $$('#content .quote, #content .quote-right');

		if (quotes.length > 1) {

			// create quotes container
			var quotesContainer = new Element('div', {
				'class': 'quotes'
			}).inject(quotes.getLast(), 'before');

			// adopt all quotes into quotes container
			quotesContainer.adopt(quotes);

			quotes.current = quotes[0];
			quotes.next    = quotes[1];

			quotes.each(function(quote) {
				quote.fade = new Fx.Tween(quote, {
					'duration': 'long'
				});

				quote.fade.set('opacity', 0);
			});

			quotes.current.fade.set('opacity', 1);

			// fade function
			var fadeFn = function() {
				quotes.current.fade.start('opacity', 1, 0);
				quotes.next.fade.start('opacity', 0, 1);

				// new current
				quotes.current = quotes.next;

				// get next quote in quotes array
				if (quotes.contains(quotes[quotes.indexOf(quotes.next) + 1])) {
					quotes.next = quotes[quotes.indexOf(quotes.next) + 1];
				} else {
					quotes.next = quotes[0];
				}
			}.periodical(8000);
		}
	}

	// empolyee behaviour
	if (!$(document.body).hasClass('admin')) {

		// select all employees
		var employees = $$('#content .employee');

		if (employees.length > 1) {

			var stopPeriodical = false;

			// create employee container
			var employeesContainer = new Element('div', {
				'class': 'employees'
			}).inject(employees.getLast(), 'before');

			// adopt all employees into employees container
			employeesContainer.adopt(employees);

			employees.current = employees[0];

			employees.each(function(employee, index) {
				employee.fade = new Fx.Tween(employee, {
					'duration': 'long'
				});

				employee.fade.set('opacity', 0);

				// get next employee in employees array
				if (employees.contains(employees[index + 1])) {
					employee.next = employees[index + 1];
				} else {
					employee.next = employees[0];
				}

				var nextEmployeeLink = new Element('a', {
					'text': employee.next.getElement('h2').firstChild.nodeValue + ' »',
					'href': '#',
					'styles': {
						'float': 'right'
					},
					'events': {
						'click': function(e) {
							employees.current.fade.start('opacity', 1, 0);
							employees.current.next.fade.start('opacity', 0, 1);

							// new current
							employees.current = employees.current.next;

							stopPeriodical = true;

							e.stop();
						}
					}
				}).inject(employee.getElement('h2'));
			});

			employees.current.fade.set('opacity', 1);

			// fade function
			var fadeFn = function() {
				if (stopPeriodical != true) {
					employees.current.fade.start('opacity', 1, 0);
					employees.current.next.fade.start('opacity', 0, 1);

					// new current
					employees.current = employees.current.next;
				}
			}.periodical(8000);
		}
	}	

	// remove margin on content columns
	var contentColumns = $$('#content .column');
	contentColumns.each(function(column, index) {
		if ((index % 3) == 0) {
			column.addClass('clear');
		}
	});

	// advice
	if ($(document.body).hasClass('advice') && !$(document.body).hasClass('admin')) {
		var advices = $$('#content .advice');

		advices.each(function(advice) {
			advice.content = advice.getElement('h2').getAllNext();
			advice.content.fade('hide');

			advice.addClass('hide');

			advice.addEvent('click', function(e) {
				advice.content.fade('in');
				advice.removeClass('hide');
			});
		});

		var showAdvicesButton = $('show-advices');
		if (showAdvicesButton) {
			showAdvicesButton.addEvent('click', function(e) {

				if (advices.hasClass('hide').contains(true)) {
					this.fxscroll = new Fx.Scroll($(document.body), {	
						onComplete: function() {
							advices.fireEvent('click');
						}
					}).toElement(advices[0]);
				}

				e.stop();
			});
		}
	}

	// advice game
	if ($(document.body).hasClass('advicegame') && !$(document.body).hasClass('admin')) {

		// result messages
		var resultContentWrong = new Element('p', {
			'class': 'result',
			'html': ($(document.body).hasClass('en') ?
				'Unfortunately, not all advices were placed at the correct entrepreneur. Please try again!' :
				'Helaas, niet alle adviezen staan bij de juiste ondernemer. Probeer het opnieuw!'
			)
		});

		var resultContentRight = new Element('p', {
			'class': 'result',
			'html': ($(document.body).hasClass('en') ?
				'Congratulations, all advices were placed at the correct enterpreneur. Are you interested to share your insight with us? Then <a href="/en/working-at-b-and-o/getting-to-know-us">click here</a>.' :
				'Gefeliciteerd, alle adviezen staan bij de juiste ondernemer. Interesse om jouw inzicht met ons te delen? Leuk! Klik dan <a href="/nl/werken-bij-b-en-o/kennismaken">hier</a>.'
			)
		});

		// change questions elements
		var questions = $$('#content .question');

		var questionsContainer = new Element('div', {
			'class': 'questions'
		}).inject(questions.getLast(), 'after');

		questionsContainer.adopt(questions);

		questions.each(function(question, index) {

			var questionContent = question.getChildren();
			var matchAdvice = question.getProperty('class').match(/advice[0-9]/);

			if (matchAdvice) {
				var relValue = matchAdvice;
			} else {
				var relValue = '';
			}

			// create wrapper question element
			var questionWrapper = new Element('div', {
				'class': 'wpr-question',
				'rel': relValue
			}).inject(question);

			// create wrapper answer element
			var answerWrapper = new Element('div', {
				'class': 'wpr-answer'
			}).inject(question);

			// adopt column content into question element
			questionWrapper.adopt(questionContent);
		});

		// change answer elements
		var answers = $$('#content .answer');

		// create container for all answers
		var answersContainer = new Element('div', {
			'class': 'answers'
		}).inject(answers.getLast(), 'after');

		// adopt all ansers in answer container
		answersContainer.adopt(answers);

		answers.each(function(answer, index) {

			// add drag
			answer.drag = new Drag.Move(answer, {
				onStart: function(element) {
					// save current position
					this.currentPosition = element.getParent();
					this.currentDroppable = element.getParent('.question');

					element.setStyles({
						'position': 'absolute',
						'z-index': 999
					});

					// remove results marks
					questions.removeClass('right');
					questions.removeClass('wrong');

					resultContentWrong.addClass('hide');
					resultContentRight.addClass('hide');

					// set new droppables
					var newDroppables = $$('.question').erase(this.currentDroppable);
					this.droppables = newDroppables;
					newDroppables.addClass('droppable');
				},
				onComplete: function(element) {
					questions.removeClass('droppable');
				},
				droppables: '.question',
				onEnter: function(element, droppable) {
					droppable.addClass('mouseenter');
				},
				onLeave: function(element, droppable) {
					droppable.removeClass('mouseenter');
				},
				onDrop: function(element, droppable) {
					if (droppable) {

						if (droppable.getElement('.wpr-answer').getChildren().length > 0) {
							this.currentPosition.adopt(droppable.getElement('.wpr-answer .answer'));
						}

						droppable.removeClass('mouseenter');
						droppable.getElement('.wpr-answer').adopt(element);

						element.setStyles({
							'position': 'static',
							'top': 0,
							'left': 0,
							'z-index': 0
						});

						// set next answer on
						var answers = $$('.answers .answer');
						if (answers.length > 0) {
							if (!answers.getStyle('display').contains('block')) {
								answers.getRandom().setStyle('display', 'block').highlight('#b4133c');
							}
						} else {

							if (!$('check-results')) {

								var answersContainerContent = new Element('div', {
									'class': 'content'
								}).inject(answersContainer);

								// set rel attribute ont .answer div
								var answers = $$('.wpr-answer .answer');
								answers.each(function(answer) {
									var matchAdvice = answer.getProperty('class').match(/advice[0-9]/);

									if (matchAdvice) {
										answer.set('rel', matchAdvice);
									}
								});

								// show 'check' button
								var checkResultsButton = new Element('a', {
									'id': 'check-results',
									'class': 'button',
									'href': '#',
									'text': ($(document.body).hasClass('en') ?
										'Check the result' :
										'Bekijk het resultaat'
									),
									'events': {
										'click': function(e) {
											var questions = $$('.question');
											questions.each(function(question, index) {
												var relQuestion = question.getElement('.wpr-question').get('rel');
												var relAnswer = question.getElement('.answer').get('rel');

												if (relQuestion == relAnswer) {
													question.addClass('right');
												} else {
													question.addClass('wrong');
												}
											});

											if (questions.hasClass('right').contains(false)) {
												resultContentWrong.inject(answersContainerContent);
												resultContentWrong.removeClass('hide');
											} else {
												resultContentRight.inject(answersContainerContent);
												resultContentRight.removeClass('hide');
											}

											e.stop();
										}
									}
								}).inject(answersContainerContent);
							}
						}
					} else {
						// reset position of answer
						this.currentPosition.adopt(element);
						element.setStyles({
							'position': 'static',
							'top': 0,
							'left': 0
						});
					}
				}
			});

			answer.setStyles({
				'display': 'none',
				'position': 'static'
			});

			answer.addEvent('mouseenter', function() {
				answer.setStyle('cursor', 'move');
			});
		});

		// init first answer
		answers.getRandom().setStyle('display', 'block').highlight('#b4133c');
		;
	}

	// cm subscribe form behaviour
	var formCmSubscribe = $('form_cm_subscribe');

	if (formCmSubscribe) {
		formCmSubscribe.slideFx = new Fx.Slide(formCmSubscribe);
		formCmSubscribe.slideFx.hide();

		$('wpr-form-cm-subscribe').getElement('a').addEvent('click', function(e) {
			formCmSubscribe.slideFx.toggle();
			e.stop();
		});
	}

	// shadowbox
	var s_exts  = ['jpg', 'png', 'gif'];
	var s_links = $$('.content a');

	s_links.each(function(link) {
		var split_slash = link.getProperty('href').split('/');
		var split_ext   = split_slash[split_slash.length - 1].split('.');
		var ext         = split_ext[split_ext.length - 1];

		if (split_ext.length > 1) {
			if (s_exts.contains(ext)) {
				link.addClass('clear');
				link.set('rel', 'shadowbox[all]');
			}
		}
	});

	// shadowbox init
	Shadowbox.init();

	// ie6 double class and image margin
	if (Browser.Engine.trident && Browser.Engine.version == 4) {

		$$('.content').each(function(element) {

			if (element.hasClass('column') && element.hasClass('clear')) {
				element.addClass('column-clear');
			}
		});

		// remove margin on image that are 100% of the parent div.content
		var contentImages = $$('#content .content img');

		contentImages.each(function(image) {
			var contentContainer = image.getParent('.content');

			if (image.getSize().x > (contentContainer.getSize().x - 50)) {
				image.setStyle('margin-right', 0);
				image.addClass('nomargin');
			}

		});
	}
});