var CARROUSEL_current = 1;
var CARROUSEL_nextItem = 2;
var CARROUSEL_exec = 0;
var CARROUSEL_timer;
var CARROUSEL_auto_timer;
var CARROUSEL_auto_timer_delay = 15000;
var CARROUSEL_move_continue = 0;
var CARROUSEL_move_timer;
var CARROUSEL_move_timer_stop;
var CARROUSEL_move_timer_delay = 30;

window.addEvent('domready', function() 
{
	$$('#carrousel_menu .carrousel_menu_item').each(function(item, index) 
	{
		var a	  = item.getElement('a');
		var nr	  = a.getProperty('rel');
		
		a.addEvent('mouseover', function()
		{
			CARROUSEL_execute(item);
		});
		
		if ( nr == 2 )
		{
			CARROUSEL_auto_timer = setTimeout(function() 
			{
				CARROUSEL_execute(item);
			}, CARROUSEL_auto_timer_delay);
		}
	});
	$$('#carrousel_elements_list').addEvent('mouseover', function()
	{
		if ( CARROUSEL_auto_timer || CARROUSEL_timer )
		{
			clearTimeout(CARROUSEL_timer);
			clearTimeout(CARROUSEL_auto_timer);
		}
	});
	$$('#carrousel_elements_list').addEvent('mouseout', function()
	{
		if ( !CARROUSEL_auto_timer && !CARROUSEL_timer || 1)
		{
			CARROUSEL_auto_timer = setTimeout(function() {
				item = $$('#carrousel_menu_'+ CARROUSEL_nextItem);
				CARROUSEL_execute(item);
			}, CARROUSEL_auto_timer_delay);
		}
	});
	
	for ( i=1; i<=4; ++i )
		CARROUSEL_initScrollbar(i);
});

function CARROUSEL_execute(item)
{
	var a	  = item.getElement('a');
	var p	  = item.getElement('p');
	var nr	  = new String(a.getProperty('rel')).toInt();
	//var nr	  = Math.round(a.getProperty('rel'));
	var block = $('carrousel_elem_'+ nr);
	
	if ( CARROUSEL_current == nr )
		return ;
	if ( CARROUSEL_exec > 0 )
	{
		if ( CARROUSEL_timer )
			clearTimeout(CARROUSEL_timer);
			
		CARROUSEL_timer = setTimeout(function() {
			CARROUSEL_execute(item);
		}, 50);
		
		return false;
	}
	
	if ( CARROUSEL_auto_timer )
		clearTimeout(CARROUSEL_auto_timer);
	
	CARROUSEL_exec = 2;
		
	var curElem = CARROUSEL_current;
	CARROUSEL_current = nr; 
	
	var currentWidth = new String($$('#carrousel_menu_'+ curElem).getStyle('width')).toInt();
	var nextWidth 	  = new String(item.getStyle('width')).toInt();

	if ( nr > curElem )
	{
		var moveWidth = (nr - curElem);
		var moveSens = '-';
	}
	else
	{
		var moveWidth = (curElem - nr);
		var moveSens = '+';
	}
	moveWidth *= (new String($$('#CARROUSEL').getStyle('width')).toInt());
	
	var leftPos   = (new String($$('#carrousel_elements_list').getStyle('left')).toInt());
	var leftLimit = (moveSens == '+' ? (leftPos+moveWidth) : (leftPos-moveWidth));
	
	leftLimit = (Math.round(leftLimit/720) * 720);
				
	CARROUSEL_changeWidthLink($$('#carrousel_menu_'+ curElem), currentWidth, item, nextWidth, 0, currentWidth);
	CARROUSEL_moveImage(leftPos, moveSens, leftLimit);
	
	CARROUSEL_nextItem = (((nr+1)>4) ? 1 : (nr+1));
	
	CARROUSEL_auto_timer = setTimeout(function() {
		item = $$('#carrousel_menu_'+ CARROUSEL_nextItem);
		CARROUSEL_execute(item);
	}, CARROUSEL_auto_timer_delay);
}

function CARROUSEL_changeWidthLink(item1, item1Width, item2, item2Width, i, limit)
{
	if ( i > (item1Width-item2Width) )
		i = item1Width-item2Width;
	
	$$(item1).setStyle('width', (item1Width - i)+'px');
	$$(item2).setStyle('width', (item2Width + i)+'px');
	
	if ( i < (item1Width-item2Width) )
		setTimeout(function() {
			CARROUSEL_changeWidthLink(item1, item1Width, item2, item2Width, (i+20), limit);
		}, 1);
	else
		-- CARROUSEL_exec;
}

function CARROUSEL_moveImage(left, sens, limit)
{
	if ( sens == '+' )
		left += 30;
	else
		left -= 30;
		
	if ( left > 0 )
		left = 0;
	if ( left > 2880 )
		left = 2880;
	
	$$('#carrousel_elements_list').setStyle('left', left +'px');
	
	if ( (left < limit && sens == '+') || (left > limit && sens == '-') )
	{
		setTimeout(function() {
			CARROUSEL_moveImage(left, sens, limit);
		}, 1);
		
		if ( left == limit )
			-- CARROUSEL_exec;
	}
	else
		-- CARROUSEL_exec;
}


function CARROUSEL_initScrollbar(i)
{
	var c = document.getElement('#carrousel_elem_'+ i).getElement('.elements').getElement('.elements-content');
	var content = c.getElement('div.text');
	var scrollbar = c.getElement('div.scrollbar');
	var handleTop = scrollbar.getElement('div.scrollbar-arrow-top');
	var handleBottom = scrollbar.getElement('div.scrollbar-arrow-bottom');
	
	var containerH = c.getSize().size.y;
	var contentH = content.scrollHeight;
	
	handleTop.addEvent('mouseover', function() {
		if ( CARROUSEL_move_timer_stop )
			clearTimeout(CARROUSEL_move_timer_stop);
		
		CARROUSEL_moveContainer(content, 1);
	});
	handleBottom.addEvent('mouseover', function() {
		if ( CARROUSEL_move_timer_stop )
			clearTimeout(CARROUSEL_move_timer_stop);
			
		CARROUSEL_moveContainer(content, -1);
	});
	handleTop.addEvent('mouseout', function() {
		CARROUSEL_move_timer_stop = setTimeout('CARROUSEL_moveContainerStop();', 100);
	});
	handleBottom.addEvent('mouseover', function() {
		CARROUSEL_move_timer_stop = setTimeout('CARROUSEL_moveContainerStop();', 100);
	});
}

function CARROUSEL_moveContainerStop()
{
	clearTimeout(CARROUSEL_move_timer);
}

function CARROUSEL_moveContainer(elem, sens)
{
	nextMargin = elem.getStyle('marginTop').toInt() + (5*sens);
	
	parentHeight = elem.getParent().getStyle('height').toInt();
	elemHeight = elem.getSize().size.y;
	
	if ( sens < 0 && (parentHeight > (elemHeight + nextMargin) ) )
		nextMargin = (parentHeight - elemHeight);
	
	if ( sens > 0 && nextMargin > 0 )
		nextMargin = 0;
	
	elem.setStyle('marginTop', nextMargin);
	
	CARROUSEL_move_timer = setTimeout(function() 
	{
		CARROUSEL_moveContainer(elem, sens);
	}, CARROUSEL_move_timer_delay)
}