window.addEvent('load', function() {
	if($('current') === null || $('nav') === null) return;

	$('current').addEvent('click', function() {
		var display = $('list').getStyle('display') == 'none' ? 'block' : 'none';
		$('list').setStyle('display', display);
	});


	$('nav').getChildren('ul')[0].getChildren('li').each(function(item) {
		adjustNav(item);
		if(item.hasClass('CURIFSUB') || item.hasClass('ACTIFSUB')) {
			item.getChildren('ul')[0].setStyle('left','auto');
		}

		if(item.hasClass('IFSUB')) {
			item.addEvents({
				'mouseover': function() {
					item.getChildren('ul')[0].setStyle('left','auto');
					$$('#nav ul li.CURIFSUB ul').setStyle('display', 'none');
					$$('#nav ul li.ACTIFSUB ul').setStyle('display', 'none');
				},
			 	'mouseout': function() {
					item.getChildren('ul')[0].setStyle('left','-9999px');
					$$('#nav ul li.CURIFSUB ul').setStyle('display', 'block');
					$$('#nav ul li.ACTIFSUB ul').setStyle('display', 'block');
				}
			});
		}
	});

});

function adjustNav(el) {

	if(el == null) return;

	var ul = el.getChildren('ul');
	if(ul.length == 0) return;
	var li = ul[0].getChildren('li');
	var ulWidth = 1;
	for(i = 0, j = li.length; i < j; ++i) {
		ulWidth += li[i].getCoordinates().width;
	}
	ul[0].setStyle('width', ulWidth);

	var pos = ul[0].getCoordinates().left - $('wrapper').getCoordinates().left;
	pos += 9999;
	pos += ulWidth;

	if(pos > 909) {
		newPos = (pos - 909) * -1;
		ul[0].setStyle('margin-left', newPos);
	}
}