//	document.getElementById ('test').style.filter = 'alpha(opacity=30)';
//
//		style.MozOpacity = '0%';

/**************************************** FLASH DETECTION *****************************************************/

var browser = navigator.userAgent.toLowerCase()

var flashVersion = 0;

function getFlashVersion() {

	var dontKnow = false;

	// NS3+ and Opera3+ (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			if (flashPlugin.description.indexOf('6.') != -1) flashVersion = 6;
			else if (flashPlugin.description.indexOf('5.') != -1) flashVersion = 5;
			else if (flashPlugin.description.indexOf('4.') != -1) flashVersion = 4;
			else if (flashPlugin.description.indexOf('3.') != -1) flashVersion = 3;
		}
	}

	// IE4+ on Win32:  attempt to create an ActiveX object using VBScript
	else if (browser.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && browser.indexOf("win")!=-1 && browser.indexOf("16bit")==-1) {
		document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('if IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6")) then flashVersion = 6 \n');
		document.write('if flashVersion < 6 and IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5")) then flashVersion = 5 \n');
		document.write('if flashVersion < 5 and IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4")) then flashVersion = 4 \n');
		document.write('if flashVersion < 4 and IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3")) then flashVersion = 3 \n');	
		document.write('</scr' + 'ipt\> \n'); 
	}
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
		dontKnow = true;
	}

	return flashVersion;

}

/*************************************** REDIRECT *****************************************************/

var redirected = false;

function redirect (url) {
	if (!redirected) {
		window.location.href = url;
		redirected = true;
	}
}

/************************************ MISC FUNCTIONS ***************************************************/

function replace_string_regexp (remove, replacement, str) {
	return str.replace(remove, replacement);
}

function replace_string (remove, replacement, str) {
	if (replacement.indexOf (str) != -1) return str;
	while (str.indexOf (remove) != -1) str = str.substring (0, str.indexOf (remove)) + replacement + str.substring (str.indexOf (remove) + remove.length, str.length);
	return str;
}

/************************************** CUSTOM HTML ***************************************************/

function fix_custom_html_tags (str) {
	str = replace_string ('&amp;', '&', str);
	str = replace_string ('&lt_c;', '<', str);
	str = replace_string ('&gt_c;', '>', str);
	str = replace_string ('<LineBrake />', ' ', str);
	return str;
}

/*************************************** MENU LOAD ***************************************************/

var menu_data = null;
var iframe_data = {};

function generate_menu_html (page_ref, page_hierarchy_ref, id, url, level, pos, left, root, use_iframes) {
	var html_data = '';
	var iframe_data = '';
	
	var top = (pos - 1) * item_height + submenu_vertical_displacement;

	var number = 0;
	for (var key in page_hierarchy_ref)
		if (pages [key] != null) number ++;
		
	if (number > 0) {

		var my_id = id + '/' + page_ref.d;
		var my_url = '';
		if (!root) my_url = url + '/' + page_ref.d;		

		var height = number * item_height + header_height + footer_height;
		if (border_color != '') height += 2;
	
		// get sub page data
		var sub_page_html = '';
		var sub_page_iframe = '';
		var count = 0;
		for (var key in page_hierarchy_ref) {
			if (pages [key] != null) {
				count ++;
				var sub_page_data = generate_menu_html (pages [key], page_hierarchy_ref [key], my_id, my_url, (level + 1), count, (menu_width + submenu_horizontal_displacement - 1), false, use_iframes);
				sub_page_html += sub_page_data.html;
				sub_page_iframe += sub_page_data.iframe;			
			}
		}

		//var sub_page_data = generate_menu_html;
	
		// generate html data
		html_data += '<div id="pos' + my_id + '" style="position:absolute; top:' + top + '; left:' + left + ';">';
		html_data += '<div id="m' + my_id + '" style="display:none; position:relative; top:0; left:0;" width="' + menu_width + '" height="' + height + '">';
				
		// create menu contents
		
		// create table for border if applicable
		if (border_color != '')
			iframe_data += '<table border="1" cellpadding="0" cellspacing="0"  style="border-collapse: collapse" bordercolor="' + border_color + '"><tr><td>';
	
		// arrow images must be inserted before the table with menu items because of a bug in IE
		var count = 0;
		for (var key in page_hierarchy_ref) {
			if (pages [key] != null) {
				count ++;
				
				var item_page_ref = pages [key];
				var item_page_hierarchy_ref = page_hierarchy_ref [key];
				
				// if this page has subpages we need the arrow image
				var has_sub_pages = false;
				for (var c in item_page_hierarchy_ref) if (pages [c] != null) has_sub_pages = true;
				if (has_sub_pages)
					iframe_data += '<img border="0" src="/site_resources/' + menu_item_id + '/' + arrow_image_language_id + '/arrow_image/' + arrow_image +
						'" style="position:absolute; top:' + (((count - 1) * item_height) + header_height) + '; left:' + (menu_width + arrow_horizontal_displacement) + ';">';
			}
		}
	
		// define menu table
		iframe_data += '<table class="menu_table" border="0" cellpadding="0" cellspacing="0" width="' + (menu_width - 2) + '">';

		// include header if applicable
		if (header_height > 0)
			iframe_data += '<tr><td class="menu_header"><img src="/spacer.gif" width="' + (menu_width - 2) + '" height="' + header_height + '"/></td></tr>';

		// generate menu items
		var count = 0;
		for (var key in page_hierarchy_ref) {
			if (pages [key] != null) {
				count ++;
				
				var item_page_ref = pages [key];
				var item_page_hierarchy_ref = page_hierarchy_ref [key];

				// create table row for item
				iframe_data += '<tr><td class="menu_item" id="mi' + my_id + '/' + item_page_ref.d + '" onmouseover="parent.menu_item_over(this)" onmouseout="parent.menu_item_out(this)" ';
				iframe_data += 'onclick="parent.menu_open_page(this,' + (use_iframes ? '\\' : '') + '\'' + my_url + '/' + item_page_ref.f + '.html' + (use_iframes ? '\\' : '') + '\')" height="' + item_height + '">';

				// put in spaces before text, if applicable
				for (var c=0; c<spaces_before_text; c++) iframe_data += '&nbsp;';
				
				// put in text
				iframe_data += item_page_ref.m;

				// close the table cell and row
				iframe_data += '</td></tr>';
			}
		}

		// include footer if applicable
		if (footer_height > 0)
			iframe_data += '<tr><td class="menu_footer"><img src="/spacer.gif" width="' + (menu_width - 2) + '" height="' + footer_height + '"/></td></tr>';
					
		// close memu table
		iframe_data += '</table>';

		// close border table if applicable
		if (border_color != '')
			iframe_data += '</td></tr></table>';

		// create iframe string if iframes are used
		if (use_iframes) {
			
			// add iframe to html
			html_data += '<iframe name="i' + my_id + '" width="' + menu_width + '" height="' + height + '" scrolling="no" noresize="noresize" framespacing="0" border="0" frameborder="0" src="">&nbsp;</iframe>';
			
			// add variable declatation and html head to iframe content
			iframe_data = 'iframe_data [\'' + my_id + '\'] = \'' + 
				'<html><head><link rel="stylesheet" type="text/css" href="/menu.css"></head><body style="margin:0;">' +	iframe_data;
			
			// close HTML document
			iframe_data += '</body></html>';
			
			// close string
			iframe_data += '\';';
			
			// add subpage data
			iframe_data += sub_page_iframe;
		
		// if iframes are not used
		} else {
			
			// simply add menu contents to html
			html_data += iframe_data;
		}

		// close html menu div
		html_data += '</div>';
			
		// add subpage data
		html_data += sub_page_html;
	
		// close html pos div
		html_data += '</div>';		
	}

	// return html and iframe data
	return {html:html_data, iframe:iframe_data};
}

function load_menu (menu_ref) {
		
	// decide whether to use iframes or not
	var use_iframes = false;
	switch (menu_ref.use_iframes) {
		case 'always':
			use_iframes = true;
			break;
		case 'never':
			use_iframes = false;
			break;
		case 'when_necessary':
			use_iframes = (browser.indexOf("msie") != -1 ? false : true);
			break;
	}
		
	// aquire menu data as html
	var root_page_id = '';
	
	for (var key in page_hierarchy) root_page_id = key;
	menu_data = generate_menu_html (pages [root_page_id], page_hierarchy [root_page_id], '', '', 1, 1, 0, true, use_iframes);

	// write in menu structure
	document.getElementById (menu_ref.id).innerHTML = menu_data.html;

	// fill iframes if they are used
	if (use_iframes) {
		eval (menu_data.iframe);
		for (var key in iframe_data) {
			// in mozilla an iframe must be 'visible' when written to
			if (!document.all) document.getElementById ('m' + key).style.display = 'block';
			var doc = window.frames ['i' + key].document;

			doc.open ();
			doc.write (iframe_data [key]);
			doc.close ();

			if (!document.all) document.getElementById ('m' + key).style.display = 'none';
		}
	}
}

function load_all_menus () {
	// for each defined menu
	for (c=0; c<menu_divs.length; c++) {
		// load the menu
		load_menu (menu_divs [c]);
	}
	// declare menus as loaded
	menu_loaded = true;
}

function body_onload () {
	// load pop up menus, if they are used anywhere on this page and we are using IE
	if ((document.all)&&(menu_divs.length > 0)) {
		var head = document.getElementsByTagName ("head") [0];
		var script = document.createElement ("script");
		script.src = '/menu.js';
		head.appendChild (script);
	} else menu_loaded = true; // mozilla needs to know menus have been loaded
}


/*************************************** MENU OPERATE ***************************************************/

var menu_loaded = false;

var open_menus_array = new Array();
var open_menus_array_index = 0;

var lit_items_array = new Array();
var lit_items_array_index = 0;

var active_item = null;

var menu_close_timer_id;

var mouse_over_menu = false;
var mouse_over_flash = false;

function hide_menu () {
	for (var c=0;c<open_menus_array_index;c++) {
		document.getElementById(open_menus_array[c]).style.display='none'; 
	}
	for (c=0;c<lit_items_array_index;c++) {
		lit_items_array[c].className = 'menu_item'; 
	}
	lit_items_array = new Array();
	lit_items_array_index = 0;
	open_menus_array = new Array();
	open_menus_array_index = 0;
}


function test_hide_menu () {
	if ((!mouse_over_menu)&&(!mouse_over_flash)) hide_menu ();
}


function show_menu (menu_id) {
	if (open_menus_array [open_menus_array_index-1] != menu_id) {
		open_menus_array [open_menus_array_index] = menu_id;
		open_menus_array_index ++;
		document.getElementById (menu_id).style.display = 'block';
	}
}

function light_item (i) {
	if ((lit_items_array [lit_items_array_index-1]==null)||(lit_items_array [lit_items_array_index-1].id!=i.id)) {
		if (i.className != 'active_item') {
			lit_items_array [lit_items_array_index] = i;
			lit_items_array_index ++;
			i.className='over_item';
		}
	}
}


function menu_item_over (i) {
	over_div ();
	var level = i.id.split ('/').length;
	var menu_id = 'm'+i.id.substring(2,i.id.length);
	
	while (open_menus_array [open_menus_array_index - 1].split ('/').length > level) {
		document.getElementById (open_menus_array [open_menus_array_index-1]).style.display='none';
		open_menus_array_index--;
	}
	if (menu_id.indexOf (open_menus_array [open_menus_array_index-1])==-1) {
		document.getElementById (open_menus_array [open_menus_array_index-1]).style.display='none';
		open_menus_array_index --;
	}
	var item_id = i.id;
	while ((lit_items_array [lit_items_array_index-1] != null)&&(lit_items_array [lit_items_array_index-1].id.split ('/').length > level)) {
		if (lit_items_array [lit_items_array_index-1].className != 'active_item')
			lit_items_array [lit_items_array_index-1].className = 'menu_item'; 
		lit_items_array_index --;
	}
	if ((lit_items_array [lit_items_array_index-1] != null)&&(item_id.indexOf (lit_items_array [lit_items_array_index-1].id)==-1)) {
		if (lit_items_array [lit_items_array_index-1].className != 'active_item')
			lit_items_array [lit_items_array_index-1].className = 'menu_item'; 
		lit_items_array_index --;
	}
	var sub_menu = document.getElementById (menu_id);
	if (sub_menu!=null) show_menu (sub_menu.id);

	light_item (i);
 }


function menu_item_out (i) {
	out_div ();
}


function over_div () {
	mouse_over_menu = true;
	mouse_over_flash = false;
	clearTimeout (menu_close_timer_id);
}

function out_div () {
	mouse_over_menu = false;
	clearTimeout (menu_close_timer_id);
	menu_close_timer_id = setTimeout('test_hide_menu ();',1000);
}

function activate_item (i) {
	if (active_item!=null) active_item.className = 'menu_item';
	i.className = 'active_item';
	active_item = i;
}



function flash_show_menu (menu_id, left, top) {
	if (menu_loaded) {
		hide_menu ();
		flash_mouse_over ()
		if (document.getElementById ('pos' + menu_id) != null) {
			reset_absolute_position (menu_id);
			document.getElementById ('pos' + menu_id).style.left = left;
			document.getElementById ('pos' + menu_id).style.top = top;
			show_menu ('m' + menu_id);
		}
	}
}

function flash_hide_menu () {
	if (menu_loaded) {
		hide_menu ();
	}
}

function flash_mouse_over () {
	if (menu_loaded) {
		mouse_over_flash = true;  
		mouse_over_div = false;
		clearTimeout (menu_close_timer_id);
	}
}

function flash_mouse_out () {
	if (menu_loaded) {
		mouse_over_flash = false;
		clearTimeout (menu_close_timer_id);
		menu_close_timer_id = setTimeout('test_hide_menu ();',1000);
	}
}


function reset_absolute_position (menu_id) {
	var start_at = 0;
	while (menu_id.indexOf ('/', start_at + 1) != -1) {
		start_at = menu_id.indexOf ('/', start_at + 1); }
	if (start_at != 0) {
		new_id = menu_id.substring (0, start_at);
		document.getElementById('pos' + new_id).style.left = 0;
		document.getElementById('pos' + new_id).style.top = 0;
		reset_absolute_position (new_id);
	}
}

function menu_open_page (i,url) {
	mouse_over_flash = false;
	activate_item (i);
	top.location.href = url;
}


/*************************************** ITEM LIST OPEN ***************************************************/

var open_item_list_item = false;
var open_item_list_item_reset = null;

function item_list_open (url, new_window) {
	if (!open_item_list_item) {
		if (new_window) {
			window.open (url);
		} else {
			location.href = url;
		}
		open_item_list_item = true;
	}
	open_item_list_item_reset = setTimeout ('open_item_list_item = false;', 500);
}
