/**
 * Main menu
 */

var Skoda = Skoda || {};

Skoda.Menu = {
	m_active_id: 0,
	current_id: 0,
	is_out: true,
	pid: 0,
	showSubmenu: function(id) {
		if(id) {
			this.current_id = id;
			$$('.subTabs .subItem').invoke('hide');
			if($('sub-' + this.current_id)) {
				$('sub-' + this.current_id).setStyle({'display': 'inline'});
			}
		}
	}
};

Event.observe(window, 'load', function() 
{
	$$('#tabs li').each(function(el)
	{
		el.observe('mouseover', function(){
			var id = getIdFromString(el.readAttribute('id'));
			var main_active = $('tab-' + Skoda.Menu.m_active_id);
			
			if(main_active) {
				if(id != Skoda.Menu.m_active_id) {
					if(main_active.hasClassName('active')) {
						main_active.removeClassName('active');
						main_active.addClassName('activeOut');
					}
				} else {
					main_active.removeClassName('activeOut');
					main_active.addClassName('active');
				}
			}
			
			if(Skoda.Menu.current_id > 0) {
				$('tab-' + Skoda.Menu.current_id).removeClassName('current');
			}
			
			el.addClassName('current');
			Skoda.Menu.showSubmenu(id);
		});
	});
});


/**
 * Level 3
 */
var timer = 0;

Event.observe(window, 'load', function() 
{
	$$('#subNav li').each(function(el){
		var id_name = el.readAttribute('id');
		var id = getIdFromString(id_name);
		
		if(id && $('submenu-' + id)) {
			el.observe('mouseover', function(){
				hideDropdowns();
				showDropdown(id);
			});
			
			el.observe('mouseout', function() {
				 timer = setTimeout("$('submenu-" + id+ "').hide();", 500);
			});
		}
	});
});

function showDropdown(id) 
{
	if(timer) {
		clearTimeout(timer);
	}
	if($('submenu-' + id)) {
		var pos = $('nav-' + id).positionedOffset();
		$('submenu-' + id).setStyle({left: pos[0] + 'px', top: pos[1] + 21 + 'px'});
		$('submenu-' + id).show();
	}
}
function hideDropdowns() 
{
	$$('#subNav .subMenu').each(function(el) {
		if(el.getStyle('display') != ' none') {
			el.hide();
		}
	});
}

/**
 * Functions
 */
function getIdFromString(name) 
{
	var a = name.split('-');
	return parseInt(a[1]);
}
