$(document).ready(function() {
	/*----------------- Menu -----------------*/
	$('#main-menu>li').mouseover(function() {
		$(this).find('ul').css('display', 'block');
		$(this).addClass('active');
	}).mouseout(function(){
		$(this).find('ul').css('display', 'none');
		$(this).removeClass('active');
	});
	/*----------------- End Menu -----------------*/
	selectButton('langSelect');
});

var selectButton = function(target){
	var div = $(document.createElement('div')),
		ul = $(document.createElement('ul')).css('display', 'none'),
		span = $(document.createElement('span')),
		elem = $('#'+target).parent().html();
		span.text($('#'+target+' option:selected').text());
		list = $('#'+target+'>option').each(function(){
			var rel = $(this).attr('value'),
				text = $(this).text();
				href = $('<a></a>').html(text).attr({href : rel, rel : ''});
			$('<li></li>').html(href).appendTo(ul);
		});
	div.attr('class', 'selectZone').append(span, ul, elem);
	span.click(function(){
		var etat = $(this).parent().children('ul').css('display');
		if(etat=='block'){
			$(this).parent().children('ul').css('display', 'none');
			//$(this).css('background-position', 'right 0');
		}
		else{
			$(this).parent().children('ul').css('display', 'block');
			//$(this).css('background-position', 'right -25px');
		}
	})
	.mouseout(function(){
		$(this).parent().children('ul').css('display', 'none');
		//$(this).css('background-position', 'right 0');
	});
	ul.find('a').click(function(){
		//e.preventDefault();
		var active = $(this);
		ul.parent().find('option').each(function(){
			$(this).attr('selected', false);
			if($(this).attr('value')==active.attr('rel')){
				$(this).attr('selected', true);
			}
		});
		$('#langSubmit').submit();
	});
	ul.mouseenter(function(){
		$(this).css('display', 'block');
		//$(this).parent().find('span').css('background-position', 'right -25px');
	})
	.mouseleave(function(){
		$(this).css('display', 'none');
	});
	
	$('#'+target).parent().html(div);
}

