function DHTML_Popup()
{
}


IMPLEMENT_GARBAGE(DHTML_Popup)

DHTML_Popup.DELAY = 500; 
DHTML_Popup.ID = "navigation"; 
DHTML_Popup.FLAGS = DHTML_Popup.NORMAL; 

DHTML_Popup.TIMER = 0;

DHTML_Popup.HIDE = 1; 
DHTML_Popup.SHOW = 2; 
DHTML_Popup.HALT = 4; 
DHTML_Popup.MARK = 8; 


DHTML_Popup.MENUS = null; 
DHTML_Popup.ITEMS = Array(); 

DHTML_Popup.dispose = function() 
{ 
	DHTML_Popup.DELAY = null; 
	DHTML_Popup.ID = null;
	DHTML_Popup.HIDE = null; 
	DHTML_Popup.SHOW = null; 
	DHTML_Popup.HALT = null; 
	
	DHTML_Popup.NORMAL = null;
	DHTML_Popup.SCROLL_IN = null; 
	DHTML_Popup.SCROLL_OUT = null; 
	DHTML_Popup.SCROLL_INOUT = null; 
	DHTML_Popup.FADE_IN = null;
	DHTML_Popup.FADE_OUT = null; 
	DHTML_Popup.FADE_INOUT = null;

	if (DHTML_Popup.TIMER) 
		clearTimeout(DHTML_Popup.TIMER);
		
	DHTML_Popup.MENUS = null; 
	var items = DHTML_Popup.ITEMS; 
	for (var i = 0; i < items.length; i++) 
	{ 
		var item = items[i]; 
		
		item.$holding_menus = null; 
		item.$owner = null; 
		
		item = null; 
	}
	
	DHTML_Popup.ITEMS = null; 
}

DHTML_Popup.install = function() 
{

	var container = DHTML_Popup.get_container(); 
	
	if (container == null)
		return false; 
		
	var menus = container.getElementsByTagName("UL"); 
	
	for (var i = 0; i < menus.length; i++) 
	{
		var menu = menus[i]; 
		
		menu.$status = DHTML_Popup.HIDE; 
		menu.$visible = false; 

		event_install(menu,"onmouseover",DHTML_Popup.mouse_over_menu); 
		event_install(menu,"onmouseout",DHTML_Popup.mouse_out_menu); 

		if (i == 0) 
			menu.$root = true; 
		
		var items = menu.childNodes; 
		var last_valid_item = null; 
		
		for (var n = 0; n < items.length; n++) 
		{ 
			var item = items[n]; 
			
			if (!item.innerHTML) 
				continue; 
			
			last_valid_item = item; 
			
			item.$owner = menu; 
			
			if (DHTML_Popup.item_has_menu(item)) {

				event_install(item,"onmouseover",DHTML_Popup.mouse_over_item); 
				event_install(item,"onmouseout",DHTML_Popup.mouse_out_item); 

				var anchor = item.getElementsByTagName("A"); 
				
				
				if (anchor.length > 0) {
					event_install(anchor[0],"onfocus",DHTML_Popup.item_focused); 
					event_install(anchor[0],"onclick",DHTML_Popup.item_clicked); 
					
					
				}
				DHTML_Popup.ITEMS.push(item); 
			}
			
		} 
		
		if (last_valid_item)
		{
			var anchor = last_valid_item.getElementsByTagName("A"); 
			
			
			
			if (anchor.length > 0) 
				event_install(anchor[0],"onblur",DHTML_Popup.item_blured); 
		}
					

	}	

	DHTML_Popup.MENUS = menus; 
	
	DHTML_Popup.start_worker(); 
	
	container = null; 
	return true; 
}

event_install(window,"onload",DHTML_Popup.install); 


DHTML_Popup.item_focused = function() 
{ 
	var item = this.parentNode; 
	
	var menus = DHTML_Popup.MENUS;  
	
	for (var i = 0; i < menus.length; i++) { 
		var menu = menus[i]; 
		
		if (menu.$root) continue; 
		
		if (item.$owner.$root) {	
			menu.$status = DHTML_Popup.HIDE; 
			DHTML_Popup.menu_hide(menu);
		} else { 
			if (menu.$status & DHTML_Popup.HIDE && menu.$visible) 
				DHTML_Popup.menu_hide(menu);
				
			if (menu.$status & DHTML_Popup.SHOW && !menu.$visible) 
				menu.$status = DHTML_Popup.HIDE; 
		}
	}
	
	var holding = item.$holding_menus[0]; 
	
	if (holding) 
		holding.$status = DHTML_Popup.HALT | DHTML_Popup.SHOW | DHTML_Popup.MARK; 
	
}


DHTML_Popup.item_clicked = function() 
{
	var owner = this.parentNode.$owner; 
	if (!owner) 
		return; 
		
	owner.$clicked = true; 
}

DHTML_Popup.item_blured = function() 
{ 
	var owner = this.parentNode.$owner; 
	
	if (!owner) 
		return; 

	if (owner.$clicked) 
	{ 
		owner.$clicked = false; 
		return; 
	}
	
	if (owner.$root) 
		return; 

	var holding = this.parentNode.$holding_menus; 

	owner.style.zIndex = '1'; 
	
	if (holding && holding[0].$status & DHTML_Popup.SHOW) {
		holding[0].$hide = owner; 
		
	} else { 
		if (owner.$hide) 
			owner.$hide.$status = DHTML_Popup.HIDE; 
	
		owner.$status = DHTML_Popup.HALT | DHTML_Popup.HIDE; 
	}

	owner = null; 
}

DHTML_Popup.get_container = function()
{
	var container = document.getElementById(DHTML_Popup.ID); 
	
	if (container == null) 
		ctad_error("DHTML_Popup container can not be found."); 
		
	return container; 
}

DHTML_Popup.start_worker = function() 
{	
	var menus = DHTML_Popup.MENUS; 
	
	if (!menus) 
		return; 
	
	for (var i = 0; i < menus.length; i++) 
	{ 
		
		var menu = menus[i]; 
		
		if (menu.$root) 
			continue; 
			
		if (menu.$status & DHTML_Popup.MARK) {
			DHTML_Popup.mark(menu.parentNode); 
		} else { 
			DHTML_Popup.unmark(menu.parentNode); 
		}
		
		if (menu.$status & DHTML_Popup.HIDE ) {
			DHTML_Popup.menu_hide(menu); 
		} else if (menu.$status & DHTML_Popup.SHOW ) { 
			DHTML_Popup.menu_show(menu); 
		}
	}
	
	DHTML_Popup.TIMER = setTimeout('DHTML_Popup.start_worker()', DHTML_Popup.DELAY ); 
}

DHTML_Popup.menu_hide = function(menu) {
	
	if (!menu.$visible) 
		return; 

	DHTML_Popup.custom_hide(menu); 
	menu.$visible = false; 
}


DHTML_Popup.menu_show = function(menu)
{
	if (menu.$visible) 
		return; 

	var menus = menu.getElementsByTagName("UL"); 
	for (var i = 0; i < menus.length; i++) 
	{ 
		var m = menus[i]; 

		m.style.display = 'block'; 
		m.style.display = 'none'; 
	}

	DHTML_Popup.custom_show(menu); 
	menu.$visible = true; 
}; 


DHTML_Popup.mouse_over_item = function() 
{
	var menu = this.$holding_menus[0]; 

	if (menu.$status & DHTML_Popup.HIDE) 
		menu.$status = (menu.$status & ~DHTML_Popup.HIDE) | DHTML_Popup.SHOW | DHTML_Popup.MARK; 
	
	menu.style.zIndex = '2'; 
	
	DHTML_Popup.restart_worker(); 
	

}

DHTML_Popup.mouse_out_item = function() 
{
	var menu = this.$holding_menus[0]; 
	
	if (menu.$status & DHTML_Popup.SHOW)
		menu.$status = (menu.$status & ~(DHTML_Popup.SHOW | DHTML_Popup.MARK) ) | DHTML_Popup.HIDE; 
	
	menu.style.zIndex = '1'; 

	DHTML_Popup.restart_worker();
}


DHTML_Popup.mouse_over_menu = function() 
{
	if (this.$status & DHTML_Popup.HALT)
		return; 

	this.$status |= DHTML_Popup.HALT; 
	DHTML_Popup.restart_worker(); 
}


DHTML_Popup.mouse_out_menu = function() 
{
	if ( this.$status & DHTML_Popup.HALT) 
		this.$status &= ~DHTML_Popup.HALT; 
	
	DHTML_Popup.restart_worker(); 
}



DHTML_Popup.restart_worker = function() 
{ 
	clearTimeout(DHTML_Popup.TIMER); 
	DHTML_Popup.TIMER = setTimeout('DHTML_Popup.start_worker()',DHTML_Popup.DELAY ); 
}

DHTML_Popup.item_has_menu = function (item) 
{ 
	var menus = item.getElementsByTagName("UL"); 
	
	if (menus == null || menus.length == 0) 
		return false; 
	
	item.$holding_menus = menus; 
	
	return true; 
	
}

DHTML_Popup.mark = function (item) 
{
}

DHTML_Popup.unmark = function (item) 
{
}


DHTML_Popup.custom_show = function(menu) 
{
	menu.style.display = 'block'; 
}

DHTML_Popup.custom_hide = function(menu) 
{
	menu.style.display = 'none'; 
}

function Scrolling() 
{
}

IMPLEMENT_GARBAGE(Scrolling); 

Scrolling.$hidden = []; 
Scrolling.$active_out = null; 

Scrolling.$show = null; 

Scrolling.$step_size = 10; 


Scrolling.dispose = function() 
{ 
	Scrolling.$hidden = null; 
	Scrolling.$active_out = null; 
	Scrolling.$show = null; 
	Scrolling.$step_size = null; 
}

Scrolling.do_scrolling_in = function() 
{
	var menu = Scrolling.$show; 
	if (!menu) 
		return; 
	
	if (menu.clientHeight < menu.$original_height) 
	{
		menu.style.height = (menu.clientHeight + Scrolling.$step_size) + 'px';
		setTimeout('Scrolling.do_scrolling_in()',10); 
		
	} else { 
		
		menu.style.height =  'auto'; 
		menu.style.overflow = 'visible'; 
		
		Scrolling.$show = null; 	
	}
}

Scrolling.do_scrolling_out = function() 
{
	var menu = Scrolling.$active_out; 
	
	if (!menu)
		return; 
		
	menu.$height -= Scrolling.$step_size;
	
	if (menu.$height > 0) 
	{
		menu.style.height = menu.$height + 'px'; 
		setTimeout('Scrolling.do_scrolling_out()',10); 
	} else { 
		
		menu.style.display = 'none'; 

		menu.style.overflow = 'visible'; 
		menu.style.height = menu.$original_height + 'px'; 

		Scrolling.$active_out = Scrolling.$hidden.pop(); 
		
		if (Scrolling.$active_out) 
			Scrolling.do_scrolling_out();
	}
}

Scrolling.scroll_in = function(menu) 
{ 
	menu.style.display = 'block'; 
	
	if (!menu.$original_height) 
		menu.$original_height = menu.clientHeight; 
	
	menu.style.overflow = 'hidden'; 
	menu.style.height = '1px'; 
	
	Scrolling.$show = menu; 
	
	Scrolling.do_scrolling_in(); 
}

Scrolling.scroll_out = function(menu) 
{
	menu.style.overflow = 'hidden'; 
	
	if (!menu.$original_height) 
		menu.$original_height = menu.clientHeight;

	menu.$height = menu.$original_height; 
	
	Scrolling.$hidden.push(menu); 

	if (!Scrolling.$active_out) 
	{
		Scrolling.$active_out = Scrolling.$hidden.pop(); 
		Scrolling.do_scrolling_out(); 
	}
}

