
var res_menu_timer          = [];
var arr_menu_timer          = [];
//CorpTicket:45063 - All Pages Popup Disappears in Firefox - changed by ruby on 17 Sep 2008 
//increased the timer so that the popup will not disappears immediately
var int_timer_seconds       = 1.5;
var int_default_menu_zIndex = 4999;
var arr_bit_in_menu         = [];
var arr_bit_timer			= [];
var int_last_menu_id		= 0;

var obj_tracker             = {};
var obj_shim                = null;
var int_shim_count          = 0;
var menuOverlay = function() {/*{{{*/
	
	int_shim_count++;
	
	if (isObject(obj_shim)) {
		//SC.console.log('object built : ' + int_shim_count, obj_shim.id);
		//alert('object built(call:' + int_shim_count + ') : ' + obj_shim.id);
		return obj_shim;
	}
	
	var name = 'sc_menu_cover';
	var shim = $(name);

	if (shim) {
		SC.console.log('found shim', shim.id);
	} else {
		shim                       = document.createElement('iframe');
		shim.id                    = name;
		//shim.src                   = str_path_root + 'graphics/trans.gif';
		shim.src                   = 'javascript:false';
		shim.frameBorder           = 0;
		shim.width                 = 0;
		shim.height                = 0;
		shim.scrolling             = 'no';
		shim.style.position        = 'absolute';
		shim.style.visibility      = 'hidden';
		shim.style.display         = 'block';
		shim.style.backgroundColor = 'transparent';
		//shim.style.backgroundColor = 'blue';
		shim.style.width           = '0px';
		shim.style.height          = '0px';
		shim.style.top             = '0px';
		shim.style.left            = '0px';
		SC.console.log('created shim', shim.id);

		/* iframe = '<iframe id="sc_menu_cover" name="sc_menu_cover" src="' + str_root_path + 'graphics/trans.gif" frameBorder="0" scrolling="no" width="0" height="0" style="background-color:transparent; position:absolute; visibility:hidden; display:block; top:0px; left:0px; width:0px; height:0px;"></iframe>';*/
	}
	
	obj_shim = shim;
	
	return shim;
	
};/*}}}*/

expandMenu = function(bit_menu_type, str_parent_id) {/*{{{*/
	
	toggleEmbedObjs();	

	if (!isUndefined(obj_browser) && !isNull(obj_browser) && !isNull(browsr) && !isUndefined(browsr)) {
		arr_tmp           = str_parent_id.split('-').reverse();
		int_menu_location = arr_tmp[1];
		int_menu_id       = arr_tmp[1] + arr_tmp[0];
		
		switch (bit_menu_type) {
			case 0 :
			   if (arr_bit_in_menu[int_menu_id]) { // if in the menu then....
					if (arr_bit_timer[int_menu_id]) {
						clearTimer(int_menu_id);
						arr_bit_timer[int_menu_id] = null;
					}
					return;
				} else {
					if (arr_bit_in_menu[int_last_menu_id]) {
						arr_bit_in_menu[int_last_menu_id] = null;
						collapseMenu(int_last_menu_id, 0);
						//collapseMenu unhides the videos, so we need to hide them again.
						toggleEmbedObjs();
					}
					
					// hide select inputs
					arr_nodes = document.getElementsByTagName('select');
					for (i = 0; i < arr_nodes.length; i++) {
						arr_nodes[i].style.visibility = 'hidden';
					}
					
					arr_bit_in_menu[int_menu_id] = true;
					
					adjust_StackOrder(1, str_parent_id);
					toggle_Visibility(str_parent_id, 'hidden');
					toggle_Display(str_parent_id, 'block');
					
					obj_menu = $(str_parent_id);

/*
SC.console.log('Parent Node');
SC.console.log(obj_menu.parentNode);
SC.console.log('Self Node');
SC.console.log(obj_menu);
*/
					if (int_menu_location == 3) {
						//CorpTicket:50822 No Scrollbar with bottom navigation
						//obj_menu.style.top = '-' + (getHeight(str_parent_id) + (browsr.IE ? 0 : 4)) + 'px';
					}
					if (int_menu_location == 1) {
						if (!obj_menu) {
							return;
						}
						var int_menu_offset_y = getHeight(obj_menu.parentNode.id);
						if (!browsr.IE) {
							int_menu_offset_y += 4;
						}
						obj_menu.style.top = '+' + int_menu_offset_y + 'px';		
					}
					arr_menu_timer[int_menu_id] = str_parent_id;
					int_last_menu_id = int_menu_id;
					
					// keeps original menu info needed for overlay and overflow fixes
					// menu overflow
					obj_tracker[int_menu_id] = {
						parent : obj_menu.parentNode,
						shim : {
							active : false
						},
						original : {
							top : Element.getStyle(obj_menu, 'top'),
							left : Element.getStyle(obj_menu, 'left'),
							width : Element.getStyle(obj_menu, 'width'),
							height : Element.getStyle(obj_menu, 'height')
						}
					};
					// overflow menu fix
					// EJF:2007.02.02
					int_menu_margin     = 10;
					int_scrollbar_width = 18;
					int_menu_top        = Position.cumulativeOffset(obj_menu)[1] - Position.realOffset(obj_menu)[1];
					int_menu_height     = $J(obj_menu).height();
					int_menu_width      = $J(obj_menu).width();
					int_content_height  = getWindowHeight();
					int_view_height     = int_content_height - (int_menu_margin * 2);

					// (top && bottom) adjustments
					if ((int_menu_location == 1) || (int_menu_location == 3)) {
						int_menu_top -= browsr.IE ? 20 : 5;
					}
					if ((int_menu_height + int_menu_top) > int_content_height) {
						// the menu has gone off the deep end, save it before it drowns
						if (int_menu_height > int_view_height) {
							int_new_height   = int_view_height;
							int_new_width    = int_menu_width + int_scrollbar_width;
							str_new_overflow = 'auto';
						} else {
							int_new_height   = int_menu_height;
							int_new_width    = int_menu_width;
							str_new_overflow = 'hidden';
						}
						obj_menu.style.backgroundColor = Element.getStyleAbsolute(obj_menu, 'background-color');
						obj_menu.style.overflow        = str_new_overflow;
						obj_menu.style.width           = int2px(int_new_width);
						obj_menu.style.height          = int2px(int_new_height);
						obj_menu.style.zIndex          = '4999';
						obj_menu.style.top             = '-' + int2px((int_new_height + int_menu_top) - (int_content_height - int_menu_margin));
					}
					
					// overlay fix
					// EJF:2007.02.02
					/*{{{*/
					if (!isUndefined(obj_browser.agent) && !obj_browser.SAFARI && !obj_browser.CAMINO) {
						// JRM 34396 02/20/07 - First menu for top nav has incorrect offset in IE
						int_offset_left = Position.cumulativeOffset(obj_menu)[0];
						if ((int_menu_location == 1) && ((browsr.IE) )) {
						    //CorpTicket:43808 - Navigation Dropdowns in IE - changed by ruby on July 18 2008 
							//CorpTicket:44893 - Top Navigation Displaying out of place - changed by ruby on 16 Sep 2008 
							//Commented the lines included for ticket 43808 to make the navigation work in IE correctly
							/*if(navigator.appVersion.match('7'))
								{
								//int_offset_left -= obj_menu.offsetLeft   ;
								}
							else {*/
							if (obj_menu.offsetLeft > 0) {
								// RGB - 42563 - 24-Mar-2008 - offset incorrect for first element on a new line
								int_offset_left -= obj_menu.offsetLeft;
								// RGB - 42563 - 31-Mar-2008 - offset for centered menus 
								if (Element.getStyle(obj_menu.offsetParent, 'text-align') != 'center') {
									int_offset_left -= obj_menu.offsetParent.offsetLeft;
									}
								if (int_offset_left < 0) {
									int_offset_left = 0;
									}
								}
							//}
						
						}
	
						obj_shim = menuOverlay();
						obj_shim.style.top = '-1000px';
						obj_shim.style.left = '-1000px';
						obj_shim.style.backgroundColor = 'transparent';
						obj_tracker[int_menu_id].shim.active = true;					
						obj_shim_params = {
							top : int2px(Position.cumulativeOffset(obj_menu)[1]),
							left : int2px(int_offset_left),
							width : Element.getStyle(obj_menu, 'width'),
							height : Element.getStyle(obj_menu, 'height')
						};
	//alert('top: ' + int2px(Position.cumulativeOffset(obj_menu)[1]));

						for (param in obj_shim_params) {
							value = obj_shim_params[param];
							//Last Changed by JRM on May  1, 2007 - 37850 - Glennbard menus showing shiv							
							//obj_shim.style[param] = value;
							obj_menu.style[param] = value;
						}
						obj_menu = obj_menu.parentNode.removeChild(obj_menu);
						document.body.appendChild(obj_menu);
						Element.setStyle(obj_shim, {'z-index':'50000000', 'visibility':'visible'});
						Element.setStyle(obj_menu, {'z-index':'50000001'});
					}
					/*}}}*/
					
					//nativeBrowserObjects.hide();
					
					toggle_Visibility(str_parent_id, 'visible');
					
					// Bug #23081 - Top Nav Not Working in Safari; RGB; 30-Aug-2006
					// Force a redraw of the window to display menus despite Safari bug.
					if (navigator.appVersion.match(/Konqueror|Safari|KHTML/i)) {
						safariFix();
					}

					return;
				}
				break;
			case 1 :
				//changed swap_Image - SAS on 4-15-2008 to update image swap since arrows now done by htmlentitie
				swap_Image(str_parent_id);
				toggle_Display(str_parent_id);
				toggle_Visibility(str_parent_id);
				break;
			case 2 :
				break;
		}
	}
	return;
};/*}}}*/
do_collapseMenu = function(int_menu_id, bit_menu_type) { /*{{{ try to collapse the menu */
	
	if (!isUndefined(obj_browser) && !isNull(obj_browser) && !isNull(browsr) && !isUndefined(browsr)) {
		switch (bit_menu_type) {
			case 0 :
				if (arr_bit_timer[int_menu_id]) {		     // has timer started?....
				} else {									 // ....timer not started so start it
					setTimer(int_menu_id, bit_menu_type);    // start timer
					arr_bit_timer[int_menu_id] = true;	     // timer is turned on
				}
				break;
			case 1 : // these should never be called, but just in case, or for future use
				break;
			case 2 :
				break;
		}
	}
};/*}}}*/
collapseMenu = function(int_menu_id, bit_menu_type) { /*{{{ acutally collapse the menu */

	var str_parent_id = arr_menu_timer[int_menu_id];
	
	untoggleEmbedObjs();

	switch (bit_menu_type) {
		case 0 :
			if (arr_bit_timer[int_menu_id]) {

				// show select inputs
				arr_nodes = document.getElementsByTagName('select');
				for (i = 0; i < arr_nodes.length; i++) {
					arr_nodes[i].style.visibility = 'visible';
				}
               
				obj_menu = $(str_parent_id);
				Element.setStyle(obj_menu, {'visibility':'hidden'});
				Element.setStyle(obj_menu, {'display':'none'});
				
				// menu shim - put menu back where it was located originally
				if (!isUndefined(obj_browser.agent) && !obj_browser.SAFARI && !obj_browser.CAMINO) {
					if (obj_tracker[int_menu_id]) {
						if (obj_tracker[int_menu_id].shim.active) {
							obj_shim = menuOverlay();
							Element.setStyle(obj_shim, {'visibility':'hidden', 'z-index':'0', 'top':'-1000px', 'left':'-1000px', 'width':'0px', 'height':'0px'});
						}
						if (obj_menu.parentNode.id != obj_tracker[int_menu_id].parent.id) {
							obj_menu = obj_menu.parentNode.removeChild(obj_menu);
							obj_tracker[int_menu_id].parent.appendChild(obj_menu);
							for (param in obj_tracker[int_menu_id].original) {
								obj_menu.style[param] = obj_tracker[int_menu_id].original[param];
							}
						}
						obj_tracker[int_menu_id] = null;
					}
				} else {
					if (obj_tracker[int_menu_id]) {
						for (param in obj_tracker[int_menu_id].original) {
							obj_menu.style[param] = obj_tracker[int_menu_id].original[param];
						}
						obj_tracker[int_menu_id] = null;
					}
				}
				
				//toggle_Visibility(str_parent_id, 'hidden');
				//toggle_Display(str_parent_id, 'none');
				
				adjust_StackOrder(0, str_parent_id)
				clearTimer(int_menu_id);
				arr_bit_in_menu[int_menu_id] = null;
				arr_bit_timer[int_menu_id] = null;
                
				//nativeBrowserObjects.show();
				
				// Bug #23081 - Top Nav Not Working in Safari; RGB; 30-Aug-2006
				// Force a redraw of the window to collapse menus despite Safari bug.
				if (navigator.appVersion.match(/Konqueror|Safari|KHTML/i)) {
					window.resizeTo(self.outerWidth + 1, self.outerHeight);
					window.resizeTo(self.outerWidth - 1, self.outerHeight);
				}
		    } else {
				setTimer(int_menu_id, bit_menu_type);
				arr_bit_timer[int_menu_id] = true;
		    }
			break;
		case 1 :
			toggle_Display(str_parent_id);
			break;
		case 2 :
			break;
	}
};/*}}}*/
setTimer = function(int_menu_id, bit_menu_type) {/*{{{*/
	int_timer_seconds = int_timer_seconds ? int_timer_seconds : 1;
	clearTimer(int_menu_id);
	res_menu_timer[int_menu_id] = setTimeout('collapseMenu(\'' + int_menu_id + '\', ' + bit_menu_type + ')', (int_timer_seconds * 1000));
};/*}}}*/
clearTimer = function(int_menu_id) {/*{{{*/
	if (res_menu_timer[int_menu_id]) clearTimeout(res_menu_timer[int_menu_id]);
};/*}}}*/
swap_Image = function(str_parent_id) {/*{{{*/	
	//#### CHANGED by SAS on April 15, 2008  Instead of swapping gif arrows, we now use html entities, but Javascript thinks of them as \u codes
	//#### changes due to Navigation PRD 4.3.6 - Expanding Menu Alignment
	//\u2228 = v = &or; = \u25bc
	//\u2227 = ^ = &and; = \u25b2
	
	obj_arrow = getReference(getReference(str_parent_id).parentNode.id.replace(/container/, 'arrow'));	
	//### do the up down change for the \u characters
	str_find    = (obj_arrow.firstChild.nodeValue.search(/\u2228/) != -1) ? /\u2228/ : /\u2227/;
	str_replace = (obj_arrow.firstChild.nodeValue.search(/\u2228/) != -1) ? '\u2227' : '\u2228';
	obj_arrow.firstChild.nodeValue = obj_arrow.firstChild.nodeValue.replace(str_find, str_replace);

	str_find    = (obj_arrow.firstChild.nodeValue.search(/\u25bc/) != -1) ? /\u25bc/ : /\u25b2/;
	str_replace = (obj_arrow.firstChild.nodeValue.search(/\u25bc/) != -1) ? '\u25b2' : '\u25bc';
	obj_arrow.firstChild.nodeValue = obj_arrow.firstChild.nodeValue.replace(str_find, str_replace);

};/*}}}*/
adjust_StackOrder = function(bit_expand, str_parent_id) {/*{{{*/
	
	//This function does not appear to do anything anymore except break the toolbar. 
	//if we find out later that it actually fixes something we may revisit it
	//modified by chuckc on 5/7/2010
	return true;

	var elm_menu      = $(str_parent_id); // at the DIV
	if (!elm_menu) {
		return;
	}
	var elm_li        = elm_menu.parentNode;
	var elm_ul        = elm_li.parentNode;
	var elm_container = elm_ul.parentNode; // at the DIV
	
	/*
	for (int_menu_counter = 1; int_menu_counter <= 4; int_menu_counter++) {
		str_menu = 'nav-menu-container-' + int_menu_counter + '-0';
		if (str_menu != elm_container.id) {
			obj_menu = $(str_menu);
			if (obj_menu) {
				SC.console.log('obj_menu.id', obj_menu.id);
				Element.cleanWhitespace(obj_menu);
				elm_other_ul = obj_menu.childNodes[0]; // ul
				Element.cleanWhitespace(elm_other_ul);
				Element.setStyle(elm_other_ul, {'z-index':'100'});
				elm_other_lis = elm_other_ul.childNodes;
				for (j = 0; j < elm_other_lis.length; j++) {
					elm_li = elm_other_lis[j];
					//Element.cleanWhitespace(elm_li);
					//SC.console.log('elm_li.tagName', elm_li.tagName);
					elm_li.style.zIndex = '100';
					//SC.console.log(elm_li.tagName + '.childNodes[0].tagName', elm_li.childNodes[0].tagName);
					elm_li_children = elm_li.childNodes;
					for (k = 0; k < elm_li_children.length; k++) {
						if (elm_li_children.nodeType != 1) {
							//elm_li.childNodes[0].style.display = 'none';
							if (elm_li.childNodes[0].style) {
								elm_li.childNodes[0].style.zIndex = '100';
								elm_li.childNodes[0].style.backgroundColor = 'red';
							}
						}
					}
				}
			}
		}
		
	}
	*/
	var int_zIndex    = bit_expand ? (int_default_menu_zIndex * 5) : (int_default_menu_zIndex / 5);
	
	//Element.setStyle(elm_container.parentNode.parentNode.parentNode, {'z-index':int_zIndex, 'background-color':'green'});
	//Element.setStyle(elm_container.parentNode.parentNode, {'z-index':int_zIndex});
	//Element.setStyle(elm_container.parentNode, {'z-index':int_zIndex, 'background-color':'blue'});
	Element.setStyle(elm_container, {'z-index':int_zIndex});
	Element.setStyle(elm_ul, {'z-index':(int_zIndex + 1)});
	
	//Element.cleanWhitespace(elm_ul);
	
	var arr_child_nodes = elm_ul.childNodes;
	
	for (var i = 0; i < arr_child_nodes.length; i++) {
		//SC.console.log('arr_child_nodes[' + i + ']', arr_child_nodes[i]);
		//SC.console.log('arr_child_nodes[' + i + ']', arr_child_nodes[i].tagName);
		if (arr_child_nodes[i] && arr_child_nodes[i].style && (arr_child_nodes[i].nodeType != 1)) {
			arr_child_nodes[i].style.zIndex = int_zIndex + ((elm_li.id == arr_child_nodes[i].id) ? 3 : 2);
			/*
			var arr_child_anchors = elm_child_nodes.childNodes;
			for (var j = 0; j < arr_child_anchors.length; j++) {
				if (arr_child_anchors[j] && arr_child_anchors[j].style && (arr_child_anchors[j].nodeType != 1)) {
					arr_child_anchors[j].style.zIndex = int_zIndex + ((arr_child_nodes[i].id == arr_child_anchors[j].id) ? 4 : 3);
				}
			}
			*/
		}
	}
	
	Element.setStyle(elm_menu, {'z-index':(int_zIndex + 3)});
	
	return;
	
};/*}}}*/
safariFix = function() { /* {{{ */
						
	// Bug #23081 - Top Nav Not Working in Safari; RGB; 30-Aug-2006
	// Force a redraw of the window to display menus despite Safari bug.

	int_save_width	= new Number(self.outerWidth);
	int_save_height = new Number(self.outerHeight);

	//window.resizeTo(self.outerWidth - 1, self.outerHeight);
	self.resizeTo(int_save_width - 1, int_save_height);
	//alert(screen.availWidth);
//alert("Safarai Fix");
	if (self.outerWidth == int_save_width) {
		
		// The wiggle / resize fix didn't take, possibly because the browser is clipped off-screen, so we need
		// to shrink it to fit on screen, so then it will re-render
		int_clipped_width	= (window.screenLeft + window.outerWidth) - screen.availWidth;
		int_clipped_height	= (window.screenTop + window.outerHeight) - screen.availHeight;

		if (int_clipped_width <= 0) {
			int_clipped_width = 1;
		}

		if (int_clipped_height <= 0) {
			int_clipped_height = 1;
		}

		window.resizeTo(int_save_width - int_clipped_width, int_save_height - int_clipped_height);
		/*
		// Shrink X by offsets until they actually take
		int_offset_x = 0;
		bit_resized = 0;
		do {

			int_offset_x += 10;
			//self.resizeTo(int_save_width - int_offset_x, int_save_height);
			self.resizeTo(int_save_width - int_offset_x, int_save_height);

			if (int_save_width != self.outerWidth) {
				bit_resized = 1;
			}

		} while ((int_offset_x  < 1000) && (!bit_resized));

		if (!bit_resized) {
			// resizing X didn't work, try resizing Y
			int_offset_y = 0;
			bit_resized = 0;
			do {

				int_offset_y += 10;
				//self.resizeTo(int_save_width - int_offset_x, int_save_height);
				self.resizeTo(int_save_width, int_save_height - int_offset_y);

				if (int_save_height != self.outerHeight) {
					bit_resized = 1;
				}

			} while ((int_offset_y  < 1000) && (!bit_resized));

		}
		*/
		//self.resizeTo(int_save_width - 10, int_save_height - 10);
		//self.resizeTo(int_save_width, int_save_height);

	} else {

		self.resizeTo(self.outerWidth + 1, self.outerHeight);
		self.resizeTo(self.outerWidth, self.outerHeight - 1);
		self.resizeTo(self.outerWidth, self.outerHeight + 1);

	}

	/*
	window.resizeTo(self.outerWidth + 1, self.outerHeight);
	window.resizeTo(self.outerWidth, self.outerHeight - 1);
	window.resizeTo(self.outerWidth, self.outerHeight + 1);
	*/

	//window.resizeTo(100, 100);
	//alert(self.outerHeight);


}; /* }}} */

toggleEmbedObjs = function() { /* {{{ */

	arr_nodes = document.getElementsByTagName("embed");	
	var str_src = '';
    for (i = 0; i < arr_nodes.length; i++) {
		var str_src = arr_nodes[i].src;
		if ( !(str_src.indexOf('.swf') > 0) ) {
			arr_nodes[i].style.visibility = "hidden";				
		}
    }

	arr_nodes = $J("object");     
	for (i = 0; i < arr_nodes.length; i++) {
		var str_codebase = arr_nodes[i].codeBase;	
		if (str_codebase.indexOf('qtplugin.cab') > 0) {
			arr_nodes[i].style.visibility = "hidden";				
		}
	}

} /* }}} */
untoggleEmbedObjs = function() { /* {{{ */
	
	arr_nodes = document.getElementsByTagName("embed");
	for (i = 0; i < arr_nodes.length; i++) {
		arr_nodes[i].style.visibility = "visible";
    }

	arr_nodes = document.getElementsByTagName("object");     
	for (i = 0; i < arr_nodes.length; i++) { 
		arr_nodes[i].style.visibility = "visible"; 
	} 

} /* }}} */


