
$(function() {
	// Initialisation
	$('ul#topMenu select').hide();
	$('div#language-dropdown').show();
	$('div#language-dropdown ul li').each(function() {
		// Change Link text from Search Engine-Friendly
		$theLink = $(this).children('a');
		var $label = $theLink.children('span').attr('title');
		$theLink.html($label).attr('title', '');

		// Add the flag background-images
		$theLink.css('background-image', 'url(./img/flags/' + $theLink.attr('rel') + '.png)');
	});

	// Set the default option
	updateSelected($('div#language-dropdown ul li.default a'));

	// Drop down button click
	$('div#language-dropdown a#language-button').click(function() {
		// Change animated effect based on action... removes small jump on select box rounded corners
		if ($('div#language-dropdown').hasClass('active')) {
			$('div#language-dropdown ul').slideToggle('normal', function() { $('div#language-dropdown').toggleClass('active') });
		} else {
			$('div#language-dropdown ul').slideToggle('normal');
			$('div#language-dropdown').toggleClass('active');
		}
		$(this).blur();
		return false;
	});

	// "<option>" click function
	$('div#language-dropdown ul a').click(function() {
		if (!$(this).hasClass('inactive')) {
			updateSelected($(this));
			$(this).blur();
			$('div#language-dropdown a#language-button').trigger('click');
		}
		return false;
	});

	// General function to update the box status
	function updateSelected($newObject) {
		var $theValue = $newObject.attr('rel');
		$('div#language-dropdown span#language-selected')
			.html($newObject.html())
			.css('background-image', 'url(./img/flags/' + $theValue + '.png)');
		$('div#language-dropdown input#language-value').val($theValue)
	}
});
