/* 2011-12-20 */
/*global addEvent, addAccessKeyHint, navigator, $, readCookie, setCSS, document, addClassName, removeClassName, window, jumpto, switchMenu, alert, brokenSafari, hideOtherMenus */
var links, spans, ss;

links = document.getElementsByTagName('a');





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

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





/*Make thisForm activate on change*/
function jumps() {
	"use strict";
	var addFormJump = function (thisForm) {
		var selectFirst = thisForm.getElementsByTagName('select')[0];

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

		addEvent(selectFirst, 'change', function () {
			if (selectFirst.options[selectFirst.selectedIndex].value !== '-1') {
				thisForm.submit();
			}
		}, false);
	}, formz = document.forms, fz;

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

jumps();





/*Add next and previous accesskeys*/
function nextPrevAccessKeys() {
	"use strict";
	var prevs = 0, nexts = 0, k;

	for (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() {
	"use strict";
	var goTo = function (thisSpan) {
		var thisGoTo = document.createElement('a');
		thisSpan.parentNode.insertBefore(thisGoTo, thisSpan);
		thisGoTo.appendChild(thisSpan);
		addEvent(thisGoTo, 'click', function () {
			jumpto();
		}, false);
	}, i;

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

ss = document.getElementsByTagName("link");





function navStick() {
	"use strict";
	if (readCookie('navFloat') === '1') {
		var checkNavPosition = function () {
			if ($('header').offsetHeight < window.pageYOffset) {
				$('fixednav').className = 'navfloat';
			} else {
				$('fixednav').className = '';
			}
		};

		addEvent(window, 'scroll', function () {
			checkNavPosition();
		}, false, false);
	}

}
navStick();





function makeModMenu() {
	"use strict";
	if ($('modform') !== undefined && $('quick-mod-select') !== undefined) {
		$('modform').getElementsByTagName('select')[0].name = "oldModForm"; /* Necessary in Firefox to prevent Firefox from selecting the form's select element when calling $('modform').action */

		var modUL = document.createElement('ul'), quickModSelect, thisLi, quickmodselectLIs, i;
		modUL.id = 'moderate';

		quickModSelect = $('quick-mod-select');

		quickmodselectLIs = quickModSelect.getElementsByTagName('option');

		for (i = 0; i < quickmodselectLIs.length; i++) {
			thisLi = document.createElement('a');
			thisLi.href = $('modform').action + '&action=' + quickmodselectLIs[i].value;
			thisLi.appendChild(quickmodselectLIs[i].childNodes[0]);
			modUL.appendChild(thisLi);
		}

		addClassName(modUL, 'hide');
		$('menus').appendChild(modUL);
		document.body.removeChild($('modform'));
	}
}
makeModMenu();





function moveMenus() {
	"use strict";
	var menuIDs = ['posts', 'site'], i;

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





function switchMenu(thisParent) {
	"use strict";
	var onChooseStyle = function () {
		addEvent($('switcher'), 'change', function () {
			setCSS($('switcher').value);
		}, false);
	}, showCurrentStyle, switcher, j, switcherOption;

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

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

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

			switcher.appendChild(switcherOption);
		}
	}

	/* Safari 5.1 sucks. */
	if (!brokenSafari()) {
		thisParent.appendChild(switcher);
	}
	onChooseStyle();
	showCurrentStyle();
}

switchMenu($('footer'));











function showMenu(thisMenu, alignment) {
	"use strict";
	/*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);
			}
		}, invisibleWallBehindMenus;



		if (document.getElementById('invisibleWallBehindMenus') === null) {
			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'));
		}
	}, menu2Show;

	hideOtherMenus($(thisMenu));

	buildInvisibleWall();

	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) {
	"use strict";
	var navs = ['search', 'posts', 'site'], i;

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

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

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

	for (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() {
	"use strict";
	var menuLinksClose = function (thisMenuLink) {
		addEvent(thisMenuLink, 'click', function () {
			hideOtherMenus();
		}, false);
	}, menuLinks, l;



	if ($('nav') !== undefined && $('menus') !== 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 ($('sitenav') !== undefined && $('site') !== undefined && $('site').parentNode.id !== undefined) {
			addEvent($('sitenav'), 'click', function () {
				showMenu($('sitenav'), 'left');
			}, false, false);
		}

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

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

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

addMenus();





function addAccessKeys() {
	"use strict";
	var theseNavs = ['siftlabel', 'postsnav', 'profilenav', 'membersnav', 'indexa', 'foruma', 'help'], theseKeys = ['f', 'p', 'e', 'm', '1', '2', '/'], m;

	for (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) {
	"use strict";
	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, '&').replace(/&#38;/g, '&'), name, wxh);
	return false;
}





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

addFindUserSearch();

