/*2009-09-15*/
/*global addEvent, addAccessKeyHint, navigator, $, readCookie, setCSS, document, addClassName, removeClassName, window, jumpto, switchMenu, alert */
var links, spans, ss;

links = document.getElementsByTagName('a');





/*Make all links open new page*/
function postLinksNew()
{
	var linkPop = function (thisLink)
	{
		if (thisLink.className.match('postlink'))
		{
			addEvent(thisLink, 'click', function ()
			{
				window.open(thisLink.href);
			}, false, false);
		}
	};

	if (readCookie('linksPop') === '1')
	{
		for (var k = 0; k < links.length; k++)
		{
			linkPop(links[k]);
		}
	}
}
postLinksNew();





/*Make thisForm activate on change*/
function jumps() {
	var addFormJump = function (thisForm)
	{
		addEvent(thisForm, 'submit', function (e)
		{
			if (thisForm.f.options[thisForm.f.selectedIndex].value === '-1') {
				if (navigator.appName !== 'Microsoft Internet Explorer')
				{
					e.preventDefault();
				}
				else
				{
					e.returnValue = false;
				}
			}
		}, false);

		addEvent(thisForm.f, 'change', function (e)
		{
			if (thisForm.f.options[thisForm.f.selectedIndex].value !== '-1') {
				thisForm.submit();
			}
		}, false);
	};

	var formz = document.forms;

	for (var fz = 0; fz < formz.length; fz++)
	{
		if (formz[fz].className === 'jumpform')
		{
			addFormJump(formz[fz]);
		}
	}
}

jumps();





/*Add next and previous accesskeys*/
function nextPrevAccessKeys()
{
	var prevs = 0;
	var nexts = 0;

	for (var k = 0; k < links.length; k++)
	{
		if(prevs === 0 && links[k].className === 'previouspage')
		{
			prevs++;
			links[k].accessKey = '[';
			addAccessKeyHint(links[k]);
		}
		if(nexts === 0 && links[k].className === 'nextpage')
		{
			nexts++;
			links[k].accessKey = ']';
			addAccessKeyHint(links[k]);
		}
	}
}
nextPrevAccessKeys();

spans = document.getElementsByTagName('span');





function goToAdds()
{
	var goTo = function (thisSpan)
	{
		var thisGoTo = document.createElement('a');
		thisSpan.parentNode.insertBefore(thisGoTo, thisSpan);
		thisGoTo.appendChild(thisSpan);
		addEvent(thisGoTo, 'click', function ()
		{
			jumpto();
		}, false);
	};

	for (var i = 0; i < spans.length; i++)
	{
		if (spans[i].className === 'gotopage')
		{
			goTo(spans[i]);
		}
	}
}
goToAdds();

ss = document.getElementsByTagName("link");





function moveMenus()
{
	var menuIDs = ['posts', 'help'];

	if ($('menus') !== undefined)
	{
		for (var i = 0; i < menuIDs.length; i++)
		{
			if ($(menuIDs[i]) !== undefined)
			{
				addClassName($(menuIDs[i]), 'hide');
				$('menus').appendChild($(menuIDs[i]));
			}
		}
	}
}
moveMenus();





function switchMenu(thisParent)
{
	var onChooseStyle = function ()
	{
		addEvent($('switcher'), 'change', function ()
		{
			setCSS($('switcher').value);
		}, false);
	};

	var showCurrentStyle = function ()
	{
		if (readCookie('style') !== null)
		{
			$('switcher').value = readCookie('style');
		}
	};

	var switcher = document.createElement('select');
	switcher.id = 'switcher';

	for (var j = 0; j < ss.length; j++)
	{
		if (ss[j].title && ss[j].rel.match('style'))
		{
			var switcherOption = document.createElement('option');
			switcherOption.value = ss[j].title;
			switcherOption.appendChild(document.createTextNode(ss[j].title));

			switcher.appendChild(switcherOption);
		}
	}

	thisParent.appendChild(switcher);
	onChooseStyle();
	showCurrentStyle();
}

switchMenu($('footer'));











function showMenu(thisMenu, alignment)
{
	/*Put a wall behind menus. If the wall is clicked, hide the menus*/
	var buildInvisibleWall = function ()
	{
		var invisibleWallAddEvent = function ()
		{
			if (document.getElementById('invisibleWallBehindMenus') !== null)
			{
				addEvent($('invisibleWallBehindMenus'), 'click', function ()
				{
					hideOtherMenus();
					
				}, false, false);
			}
		};
	
	
	
		if (document.getElementById('invisibleWallBehindMenus') === null)
		{
			var invisibleWallBehindMenus = document.createElement('div');
			invisibleWallBehindMenus.id = 'invisibleWallBehindMenus';
			invisibleWallBehindMenus.appendChild(document.createTextNode(String.fromCharCode('160')));
			document.body.insertBefore(invisibleWallBehindMenus, document.body.childNodes[0]);
			invisibleWallAddEvent();
		}
		else if (document.getElementById('invisibleWallBehindMenus') !== undefined)
		{
			document.body.removeChild(document.getElementById('invisibleWallBehindMenus'));
		}
	};

	hideOtherMenus($(thisMenu));

	buildInvisibleWall();

	var menu2Show = $(thisMenu.id.substr(0, thisMenu.id.length - 3));

	if (thisMenu.className === undefined)
	{
		thisMenu.className = '';
	}

	if (thisMenu.className.match('active'))
	{
		removeClassName(thisMenu, 'active');
		addClassName($(menu2Show), 'hide');
	}
	else
	{
		addClassName(thisMenu, 'active');
		removeClassName($(menu2Show), 'hide');

		if (alignment === 'left')
		{
			$(menu2Show).style.left = thisMenu.offsetLeft + 'px';
		}

		if ($(menu2Show).id === 'search' || $(menu2Show).id === 'login')
		{
			$(menu2Show).getElementsByTagName('input')[0].focus();
		}
	}

	thisMenu.blur();
}





function hideOtherMenus(notThisMenu)
{
	var navs = ['search', 'posts', 'help'];

	if( $('profile') !== undefined )
	{
		navs[navs.length] = 'profile';
	}

	if( $('login') !== undefined )
	{
		navs[navs.length] = 'login';
	}

	for (var i = 0; i < navs.length; i++)
	{
		if ($(navs[i] + 'nav') !== undefined)
		{
			if (notThisMenu !== $(navs[i] + 'nav') && !$(navs[i]).className.match('hide'))
			{
				showMenu($(navs[i] + 'nav'));
			}
		}
	}
}







/*Add menus now that the page has loaded*/
function addMenus()
{
	var menuLinksClose = function (thisMenuLink)
	{
		addEvent(thisMenuLink, 'click', function ()
		{
			hideOtherMenus();
		}, false);
	};



	if ($('nav') !== undefined)
	{

		if ($('searchnav') !== undefined)
		{
			addEvent(document.forms.searchform, 'reset', function ()
			{
				showMenu($('searchnav'));
			}, false);

			addEvent(document.forms.searchform, 'submit', function ()
			{
				showMenu($('searchnav'));
			}, false);

			addEvent($('searchnav'), 'click', function ()
			{
				showMenu($('searchnav'), 'left');
			}, false, false);
		}

		if ($('postsnav') !== undefined)
		{
			addEvent($('postsnav'), 'click', function ()
			{
				showMenu($('postsnav'), 'left');
			}, false, false);
		}

		if ($('helpnav') !== undefined && $('help') !== undefined && $('help').parentNode.id !== undefined)
		{
			addEvent($('helpnav'), 'click', function ()
			{
				showMenu($('helpnav'), 'left');
			}, false, false);
		}

		var menuLinks = $('menus').getElementsByTagName('a');

		for (var l = 0; l < menuLinks.length; l++)
		{
			menuLinksClose(menuLinks[l]);
		}
	}
}

addMenus();





function addAccessKeys()
{
	var theseNavs = ['searchnav', 'postsnav', 'helpnav', 'profilenav', 'membersnav', 'indexa', 'foruma'];
	var theseKeys = ['f', 'p', '/', 'e', 'm', '1', '2'];

	for (var m = 0; m < theseKeys.length; m++)
	{
		if ($(theseNavs[m]) !== undefined)
		{
			$(theseNavs[m]).accessKey = theseKeys[m];
			addAccessKeyHint($(theseNavs[m]));
		}
	}
}

addAccessKeys();

/*Open a popup window*/
function popup(url, name, width, height)
{
	if (!name)
	{
		name = '_popup';
	}

	var wxh = 'resizable=yes,scrollbars=yes';

	if (!isNaN(width))
	{
		wxh += ',width=' + width;
	}

	if (!isNaN(height))
	{
		wxh += ',height=' + height;
	}

	window.open(url.replace(/&amp;/g, '&'), name, wxh);
	return false;
}





/*Find a member*/
function addFindUserSearch()
{
	if ($('findUser') !== undefined)
	{
		addEvent($('findUser'), 'click', function ()
		{
			popup($('findUser').href, '_usersearch');
		}, false, false);
	}
}

addFindUserSearch();





/*Hide the top "jump to top" link until someone has scrolled(so it doesn't display when users first mouse onto the page)*/
function showTopJump()
{
	$('topjump').style.visibility = 'visible';
}

function topJumpUnHide()
{
	if ($('topjump') !== undefined)
	{
		addEvent(window, "scroll", function (e)
		{
			showTopJump();
		}, false);

		$('topjump').accessKey = 'a';
		addAccessKeyHint($('topjump'));

		if ($('bottomjump') !== undefined)
		{
			addEvent($('bottomjump'), "click", function (e)
			{
				showTopJump();
			}, false);

			$('bottomjump').accessKey = 'z';
			addAccessKeyHint($('bottomjump'));
		}
	}
}

topJumpUnHide();