/**
 * Menu handler
 *
 * jQuery 1.6.2 expected
 */
var MenuHandler = {
	
	menu1stItems: '#menu_1st ol > li > a',
	menu1stArrows: '#menu_1st #arrows span',
	menuFlyOut: '',
	
	init: function()
	{
		this.menu1st();
	},
	
	menu1st: function()
	{
		var root = this;
		var items = $(this.menu1stItems);
		var speed = 200;
		var active = 0;
		var toFlyOut;
		
		for (i = 0; i < items.length; i++)
		{
			if ($(items[i]).parent('li').get(0).className.search(/active/) != -1)
			{
				active = i;
				break;
			}
		}
		
		if (i == items.length)
		{
			$('#menu_1st #arrows').css({ display: 'none' });
			$('#menu_1st ol').hover(function(){
				$('#menu_1st #arrows').css({ display: 'block' });
			}, function(){
				$('#menu_1st #arrows').css({ display: 'none' });
			});
		}
		
		this.menu1stPosStartArrows(active * $(items).width() + Math.ceil($(items).width() / 2) + (active * 2) - 11, active);
				
		items.parent('li').hover(function(){
			if ($(this).get(0).className.search(/active/) == -1)
			{
				$(this).find('a').animate({
					paddingTop: '11px',
					height: '42px'
				}, speed);
				$(this).animate({
					marginTop: '0px',
					height: '50px'
				}, speed);
			}
			if ($(this).find('div').get(0) && $(this).find('a').get(0).className.search(/s0/) == -1)
			{
				root.menuFlyOut = $(this).find('div').get(0);
				toFlyOut = setTimeout(showFlyOut, 400);
			}
			root.menu1stPosArrows($(this).index() * $(this).width() + Math.ceil($(this).width() / 2) + ($(this).index() * 2) - 11, $(this).index());
		}, function(){
			if ($(this).get(0).className.search(/active/) == -1)
			{
				$(this).find('a').animate({
					paddingTop: '12px',
					height: '38px'
				}, speed, function(){ $(this).clearQueue(); });
				$(this).animate({
					marginTop: '3px',
					height: '50px'
				}, speed, function(){ $(this).clearQueue(); });
			}
			if ($(this).find('div').get(0))
			{
				hideFlyOut();
			}
			root.menu1stPosArrows(active * $(this).width() + Math.ceil($(this).width() / 2) + (active * 2) - 11, active);
		});
		
		function showFlyOut()
		{
			var speed = 300;
			$(root.menuFlyOut).find('.items .item').height($(root.menuFlyOut).height());
			$(root.menuFlyOut).show(speed);
		}
		
		function hideFlyOut()
		{
			clearTimeout(toFlyOut);
			var speed = 300;
			$(root.menuFlyOut).hide(speed);
		}
	},
	
	menu1stPosStartArrows: function($pos, $index)
	{
		var items = $(this.menu1stArrows);
		items.clearQueue();
		$(items).hide();
		$(items[$index]).show();
		items.css('left', $pos + 'px');
	},
	
	menu1stPosArrows: function($pos, $index)
	{
		var items = $(this.menu1stArrows);
		var speed = 300;	
		
		items.clearQueue();
		$(items).hide();
		$(items[$index]).show();
		
		items.animate({
			left: $pos + 'px'
		}, speed);
	}

}

