function $(id) {
	return document.getElementById(id);
}

function addClass(obj, class_name) {
	if(!hasClass(obj, class_name))
	{
		obj.className = obj.className + ' ' + class_name;
	}
};

function removeClass(obj, class_name) {
	if(hasClass(obj, class_name))
	{
		var r = new RegExp('(^|$| )' + class_name + '(^|$| )');
		obj.className = obj.className.replace(r, ' ');
	}
};

function hasClass(obj, class_name) {
	var r = new RegExp('(^|$| )' + class_name + '(^|$| )');
	return r.test(obj.className);
};

function get_viewport_height()
{
	return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}

function get_viewport_width()
{
	return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}

function get_scroll_offset()
{
	return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
}

function get_position_y( input_element )
{
	position = 0;
	
	while (input_element != null)
	{
		position += input_element.offsetTop;
		input_element = input_element.offsetParent;
	}
	
	return position;
}

function get_position_x( input_element )
{
	position = 0;
	
	while (input_element != null)
	{
		position += input_element.offsetLeft;
		input_element = input_element.offsetParent;
	}
	
	return position;
}

// Top navigation button background hover colors
var MAIN_BAR_HOVER_COLOR = '#0e68db';
var MYACCOUNT_BAR_HOVER_COLOR = '#d6e6fa';

// Will be set to true when mouse is within submenu, otherwise will be false;
// used to determine how to react to a certain mouseout event
var mouseWithinSubmenu = false;

// navBar is hard-coded. Current possibilities: 'main', 'myaccount'
function subOn(navBtn, subNavMenu, navBar) {
	if(navBar == 'main')
		navBtn.style.backgroundColor = MAIN_BAR_HOVER_COLOR;
	else if(navBar == 'myaccount')
		navBtn.style.backgroundColor = MYACCOUNT_BAR_HOVER_COLOR;

	subNavMenu.style.display = 'block';
}

function subOff(navBtn, subNavMenu) {
	if(!mouseWithinSubmenu)
	{
		navBtn.style.backgroundColor = 'transparent';
		subNavMenu.style.display = 'none';
	}
}

function setMouseWithinSubmenu(boolValue) {
	mouseWithinSubmenu = boolValue;
}

// Holds current timeout IDs when mousing over menus (1 for each menu)
var menu_timeouts = [null,null,null,null,null,null,null,null,null];

// IE6?
var IE6 = (navigator.userAgent.indexOf('MSIE 6') >= 0);

// Returns 0-based index of nav_cell in set of nav cells
function getNavIndex(nav_cell) {
	return Number(nav_cell.id.charAt(3)) - 1;
};

// Returns 0-based index of menu in set of menus
function getMenuIndex(menu) {
	return Number(menu.id.charAt(4)) - 1;
};

// Returns menu associated with nav_cell
function menuFromNavCell(nav_cell) {
	return $('menu' + nav_cell.id.charAt(3));
};

// Returns nav_cell associated with menu
function navCellFromMenu(menu) {
	return $('nav' + menu.id.charAt(4));
};

// Called on nav_cell mouseover
function navMouseOver(nav_cell) {
	// Add hover effect
	nav_cell.setAttribute('class', 'hover');
	nav_cell.setAttribute('className', 'hover');

	// Get nav index
	var i = getNavIndex(nav_cell);

	// Clear timeout
	if(menu_timeouts[i])
		clearTimeout(menu_timeouts[i]);

	// Menu available
	var menu = menuFromNavCell(nav_cell);
	if(menu)
	{
		// Set a timeout to show menu
		menu_timeouts[i] = setTimeout(function() { showMenu(menu); }, 200);
	}
};

// Called on nav_cell mouseout
function navMouseOut(nav_cell) {
	// Get nav index
	var i = getNavIndex(nav_cell);

	// Clear timeout
	if(menu_timeouts[i])
		clearTimeout(menu_timeouts[i]);

	// Get menu
	var menu = menuFromNavCell(nav_cell);

	// Menu available and currently shown
	if(menu && menu.style.display == 'block')
	{
		// Set a timeout to hide the menu
		menu_timeouts[i] = setTimeout(function() { hideMenu(menu); }, 200);
	}
	// Menu not available or not currently shown
	else
	{
		// Remove nav cell hover effect
		nav_cell.setAttribute('class', '');
		nav_cell.setAttribute('className', '');
	}
};

// Shows menu
function showMenu(menu) {
	// Get nav_cell
	var nav_cell = navCellFromMenu(menu);

	// Show menu
	menu.style.display = 'block';

	// Get right extremity of nav buttons
	var max_right = get_position_x($('nav_new')) + $('nav_new').offsetWidth;

	// Get proposed menu left coordinate
	leftOffset = 1;
	if (/Safari/.test(navigator.userAgent))
	{
		leftOffset = 0;
	}
	else if (/Firefox[\/\s]*3\.[5-9]|Firefox[\/\s]*[4-9]/.test(navigator.userAgent))
	{
		if (getMenuIndex(menu) == 0)
		{
			leftOffset = 1;
		}
		else
		{
			leftOffset = 2;
		}
	}
	
	var menu_left = get_position_x(nav_cell) - leftOffset;

	// If menu would extend past right extremity
	if(menu_left + menu.offsetWidth > max_right)
	{
		// Calculate menu left based on right extremity
		menu_left = max_right - menu.offsetWidth;
	}

	// Position menu
	menu.style.left = menu_left + 'px';
	menu.style.top = (get_position_y(nav_cell) + nav_cell.offsetHeight) + 'px';

	//show iframe for IE6
	show_iframe(getMenuIndex(menu) + 1);
};

// Hides menu
function hideMenu(menu) {
	// Get associated nav cell
	var nav_cell = navCellFromMenu(menu);

	// Remove nav cell hover effect
	nav_cell.setAttribute('class', '');
	nav_cell.setAttribute('className', '');

	// Hide menu
	menu.style.display = 'none';

	//hide iframe for IE6
	hide_iframe(getMenuIndex(menu) + 1);
};

// Called on menu mouseover
function menuMouseOver(menu) {
	// Get menu index
	var i = getMenuIndex(menu);

	// Clear timeout
	if(menu_timeouts[i])
		clearTimeout(menu_timeouts[i]);
};

// Called on menu mouseout
function menuMouseOut(menu) {
	// Get menu index
	var i = getMenuIndex(menu);

	// Set a timeout to hide the menu
	menu_timeouts[i] = setTimeout(function() { hideMenu(menu); }, 200);
};

// Tab stuff
function tab_mouseover(tab_id)
{
	// Not selected tab
	if(tab_id != selected_tab)
	{
		var tab = $('info_tab' + tab_id);
		
		if(tab)
		{
			addClass(tab, 'info_tab_header_hover');
			removeClass(tab, 'info_tab_header_off');
		}
	}
};

function tab_mouseout(tab_id)
{
	// Not selected tab
	if(tab_id != selected_tab)
	{
		var tab = $('info_tab' + tab_id);
		
		if(tab)
		{
			addClass(tab, 'info_tab_header_off');
			removeClass(tab, 'info_tab_header_hover');
		}
	}
};

function tab_select(tab_id)
{
	// Get old and new selected tabs/content
	var old_tab = $('info_tab' + selected_tab);
	var new_tab = $('info_tab' + tab_id);
	var old_tab_content = $('tab_content' + selected_tab);
	var new_tab_content = $('tab_content' + tab_id);
	
	// Elems available
	if(old_tab && new_tab && old_tab_content && new_tab_content)
	{
		// Unselect old tab
		addClass(old_tab, 'info_tab_header_off');
		removeClass(old_tab, 'info_tab_header_on');
		removeClass(old_tab_content, 'show');

		// Select tab
		addClass(new_tab, 'info_tab_header_on');
		removeClass(new_tab, 'info_tab_header_off');
		removeClass(new_tab, 'info_tab_header_hover');
		addClass(new_tab_content, 'show');
		
		// Set new selected tab
		selected_tab = tab_id;
	}
};