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

	/**
	 * Langue picker element
	 */
	var languagePicker = new Dropdown(
	{
		'container': 'frameHeader',
		'className': 'frameLanguage'
	});
	
	languagePicker.addItem('Nederlands', 'nl', currentLanguage == 'nl');
	languagePicker.addItem('English', 'en', currentLanguage == 'en');
	languagePicker.addItem('Deutsch', 'de', currentLanguage == 'de');
	
	languagePicker.addEvent('change', function ()
	{
		var urlSuffix = '';
		if (languagePicker.getCurrentValue() != 'nl')
			urlSuffix = languagePicker.getCurrentValue() + '/';
			
		window.location = 'http://www.eurojuris.nl/' + urlSuffix;
	});
	
	/**
	 * Tabs
	 */
	 
	if (document.id('frameRight'))
	{
		var tabSearch = document.id('frameRight').getElements('a.tab')[0];
		var frameSearch = document.id('frameRight').getChildren('div')[1];
		var tabMap = document.id('frameRight').getElements('a.tab')[1];
		var frameMap = document.id('frameRight').getChildren('div')[2];
		var tabInternational = document.id('frameRight').getElements('a.tab')[2];
		var frameInternational = document.id('frameRight').getChildren('div')[3];
	}
	
	if (tabSearch && frameSearch)
	{
		tabSearch.addEvent('click', function (event)
		{
			event.stop();
			tabSearch.addClass('active');
			tabMap.removeClass('active');
			tabInternational.removeClass('active');
			
			frameSearch.removeClass('hide');
			frameMap.addClass('hide');
			frameInternational.addClass('hide');
		});
		
		tabMap.addEvent('click', function (event)
		{
			event.stop();
			tabMap.addClass('active');
			tabSearch.removeClass('active');
			tabInternational.removeClass('active');
			
			frameSearch.addClass('hide');
			frameMap.removeClass('hide');
			frameInternational.addClass('hide');
		});
		
		tabInternational.addEvent('click', function (event)
		{
			event.stop();
			tabInternational.addClass('active');
			tabMap.removeClass('active');
			tabSearch.removeClass('active');
			
			frameSearch.addClass('hide');
			frameMap.addClass('hide');
			frameInternational.removeClass('hide');		
		});
	}
	
	/**
	 * Search filters
	 */
	
	var selectElements = document.id(document.body).getElements('select.replace');
	if (selectElements)
		selectElements.each(function (element)
		{
			new Dropdown(
			{
				replace: element,
				useMooScroller: true
			});
		});
		
	/**
	 * Area of law quick-switch
	 */
	var areaList = Dropdown.getInstance('rechtsgebied');
	if (areaList)
		areaList.addEvent('change', function ()
		{
			if (areaList.getCurrentValue())
				window.location = 'module/?lawyers&organisations&area=' + areaList.getCurrentValue();
		});
		
	/**
	 * Office quick-switch
	 */
	var officeList = Dropdown.getInstance('kantoor');
	if (officeList)
		officeList.addEvent('change', function ()
		{
			if (officeList.getCurrentValue())
				window.location = 'module/?lawyers&organisation&id=' + officeList.getCurrentValue();
		});
		
	/**
	 * International websites
	 */
	var countryList = Dropdown.getInstance('land');
	if (countryList)
		countryList.addEvent('hide', function ()
		{
			if (countryList.getCurrentValue())
				window.open(countryList.getCurrentValue());
		});

	/**
	 * This part handles the forms included by the form component.
	 * When these forms are submitted, the default event is prevented,
	 * and a XHR is send with the data of the form attached, using the POST method.
	 * The receiving script will handle the incoming data and send a JSON message
	 * confirming successful processing or an error message.
	 */

	// First get a list of all the active `auto'-forms on the page.
	var formList = document.id(document.body).getElements('form.autoForm');
	if (formList && formList.length)
	{

		// Attach the event handler to each form
		formList.each(function (form)
		{

			// Get the form's ID
			var formID = form.get('id').replace('form:', '').toInt();
			
			// Prepend an notice/error element
			var noticeElement = new Element('p', {'class': 'formNotice', styles: {display: 'none'}}).inject(form, 'before');

			// The XHR request
			var httpRequest = new Request.JSON(
			{
				url: 'forms.ajax.php?form=' + formID,
				method: 'post',
				onRequest: function ()
				{
					noticeElement.setStyle('display', 'none');
				},
				onSuccess: function (response)
				{
					// Check the status code; 1 means success, 0 means error
					if (response && response.status)
					{
						if (response.redirect)
							window.location = response.redirect;
						else if (response.message)
						{
							noticeElement.set('text', response.message);
							noticeElement.setStyle('display', '');
						}
					}
					else
					{
						noticeElement.set('text', response ? response.error : 'Er is een onbekende fout opgetreden.');
						noticeElement.setStyle('display', '');
					}
				}
			});

			// The submit event handler
			form.addEvent('submit', function (event)
			{
				if (event) event.preventDefault();

				// Gather the data
				var data = {};
				var items = form.getElements('input[type=radio]');
				items.each(function (item)
				{
					alert(item.get('value'));
				});
				
				var items = form.getElements('select').combine(form.getElements('input')).combine(form.getElements('textarea'));
				items.each(function (item)
				{
					if (item.get('type') == 'submit')
						return;
					data[item.get('name')] = item.get('value');
				});

				// Send the request with the data attached.
				httpRequest.send({data: data});
			});

		});

	}

});

