/* 2011-12-20 */
/*global $, document, window, alert, navigator */
var accessKeyModifier;





function readCookie(name) {
	"use strict";
	var nameEQ = name + '=', ca = document.cookie.split(';'), i, c;
	for (i = 0; i < ca.length; i++) {
		c = ca[i];
		while (c.charAt(0) === ' ') {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) === 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}





function setCookie(name, value, days) {
	"use strict";
	var expires, date;
	if (days) {
		date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = '; expires=' + date.toGMTString();
	} else {
		expires = '';
	}
	document.cookie = name + '=' + value + expires + '; path=/' + '; domain=' + (document.domain.substr(0, document.domain.lastIndexOf('.')).indexOf('.') > 0 ? document.domain : '.' + document.domain);
}





function brokenSafari() {
	"use strict";
	return ((navigator.appVersion.indexOf('Version/5.1') > -1 && navigator.appVersion.indexOf('Safari') > -1) || navigator.appVersion.indexOf('Version/5.0.6 Safari') > -1);
}





function setCSS(title) {
	"use strict";
	var ss = document.getElementsByTagName('link'), i, cookieExpire;

	/* Safari 5.1 sucks. */
	if (!brokenSafari()) {
		for (i = 0; i < ss.length; i++) {
			if (ss[i].rel.indexOf('style') > -1 && ss[i].title) {
				ss[i].disabled = true;
				if (ss[i].title === title) {
					ss[i].disabled = false;

					cookieExpire = (ss[i].rel === 'stylesheet' ? 0 : 365);

					setCookie('style', title, cookieExpire);
				}
			}
		}
	}
}

if (readCookie('style') !== null) {
	setCSS(readCookie('style'));
}





/*Internet Explorer sucks*/
function addEvent(thisElement, thisEvent, thisAction, bubble, allowDefault) {
	"use strict";
	if (thisElement !== undefined) {
		if (navigator.appName !== 'Microsoft Internet Explorer') {
			thisElement.addEventListener(thisEvent, function (e) {
				if (allowDefault === false) {
					e.preventDefault();
				}

				thisAction();

			}, bubble);
		} else {
			thisElement.attachEvent('on' + thisEvent, function (e) {
				thisAction();

				if (allowDefault === false) {
					e.returnValue = false;
				}
			});
		}
	}
}




// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
	"use strict";
	var vars = [], hash, hashes, i;
	hashes = (window.location.href.slice(window.location.href.indexOf('?') + 1) + '&sf=all').split('&');

	for (i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}

	return vars;
}





function addClassName(item, thisName) {
	"use strict";
	if (!item.className.match(thisName)) {
		item.className = (item.className + ' ').replace('  ', ' ') + thisName;
	}
}





function removeClassName(item, thisName) {
	"use strict";
	if (item.className.match(thisName)) {
		item.className = item.className.replace(thisName, '').replace('  ', ' ');
	}
}





function addSiftBox(thisParent, searchName, advancedSearchName) {
	"use strict";
	if ($('siftbox') === undefined) {
		var siftDiv = document.createElement('div'), siftbox, siftLabel, searchTerms, j, searchTerm, siftreset, advancedSearch, siftform;
		siftDiv.id = 'sift';

		siftbox = document.createElement('input');
		siftbox.id = 'siftbox';
		siftbox.className = 'notactiveyet';
		siftbox.type = 'text';
		siftbox.size = '128';
		siftbox.value = searchName;
		addEvent(siftbox, 'focus', function () {
			if (thisParent.id === 'header') {
				removeClassName(advancedSearch, 'hide');
			}

			if ($('siftbox').className.match('notactiveyet')) {
				removeClassName($('siftbox'), 'notactiveyet');
				$('siftbox').value = '';
			}
		}, false);

		addEvent(siftbox, 'blur', function () {
			if ($('siftbox').value === '') {
				siftbox.value = searchName;
				addClassName($('siftbox'), 'notactiveyet');
			}
		}, false);

		siftLabel = document.createElement('label');
		siftLabel.id = 'siftlabel';
		siftLabel.appendChild(siftbox);
		siftDiv.appendChild(siftLabel);

		searchTerms = getUrlVars(), searchTerm;

		for (j = 0; j < searchTerms.length; j++) {
			if (!searchTerms[j].match(window.location.href) && !searchTerms[j].match('view')) {
				searchTerm = document.createElement('input');
				searchTerm.type = 'hidden';
				if (searchTerms[j] === 'f') {
					searchTerm.name = 'fid[]';
				} else {
					searchTerm.name = searchTerms[j];
				}
				searchTerm.value = searchTerms[searchTerms[j]];
				siftDiv.appendChild(searchTerm);
			}
		}

		siftreset = document.createElement('input');
		siftreset.id = 'siftreset';
		siftreset.type = 'reset';
		siftreset.value = 'X';
		siftDiv.appendChild(siftreset);
		if (thisParent.id === 'header' && advancedSearchName !== undefined) {
			siftDiv.appendChild(document.createElement('br'));
			advancedSearch = document.createElement('a');
			advancedSearch.href = './search.php';
			advancedSearch.appendChild(document.createTextNode(advancedSearchName));
			addClassName(advancedSearch, 'hide');
			siftDiv.appendChild(advancedSearch);
		}

		addEvent(siftreset, 'click', function () {
			$('siftreset').style.visibility = 'hidden';
			$('siftbox').focus();
		}, false);
		addEvent(siftbox, 'keyup', function () {
			$('siftreset').style.visibility = (siftbox.value === '' ? 'hidden' : 'visible');
		}, false);


		siftform = document.createElement('form');
		siftform.id = 'siftform';
		siftform.method = 'post';
		siftform.appendChild(siftDiv);

		if (thisParent.childNodes[0] !== undefined) {
			thisParent.insertBefore(siftform, thisParent.childNodes[0]);
		} else {
			thisParent.appendChild(siftform);
		}
	}
}





function closeButton() {
	"use strict";
	if (window.opener !== null) {
		var closeX = document.createElement('a');
		closeX.appendChild(document.createTextNode('X'));
		addClassName(closeX, 'closebutton');
		addEvent(closeX, 'click', function () {
			window.close();
		}, true);
		$('close').insertBefore(closeX, $('close').childNodes[0]);
	}
}







function topicMenus() {
	"use strict";
	var divs = document.body.getElementsByTagName('div'), i, j = 0, k, createTopicMenu = function (beforeThis) {
		var topicMenuDiv, topicMenuSelect, optionThis, topicMenuLinks = $('watch').getElementsByTagName('a'), l, pageNew;
		topicMenuDiv = document.createElement('div');

		topicMenuDiv.className = 'topicMenu';

		topicMenuSelect = document.createElement('select');

		topicMenuSelect.id = 'topic-menu-before-' + beforeThis;

		for (l = -1; l < topicMenuLinks.length; l++) {
			optionThis = document.createElement('option');

			if (l === -1) {
				optionThis.value = l;
				optionThis.appendChild(document.createTextNode(String.fromCharCode('160')));
			} else {
				optionThis.value = topicMenuLinks[l].href;
				optionThis.appendChild(document.createTextNode(topicMenuLinks[l].title));
			}

			addEvent(topicMenuSelect, 'change', function () {
				if ($('topic-menu-before-' + beforeThis).options[$('topic-menu-before-' + beforeThis).selectedIndex].value !== l) {
					pageNew = $('topic-menu-before-' + beforeThis).options[$('topic-menu-before-' + beforeThis).selectedIndex].value;
					$('topic-menu-before-' + beforeThis).selectedIndex = -1;
					window.location.href = pageNew;
				}
			}, false, false);

			topicMenuSelect.appendChild(optionThis);

			topicMenuDiv.appendChild(topicMenuSelect);

			document.body.insertBefore(topicMenuDiv, $(beforeThis));
		}
	};


	for (i = 0; i < divs.length; i++) {
		if (divs[i].className === 'pages') {
			divs[i].id = 'pages' + j;
			j++;
		}
	}

	for (k = 0; k < j; k++) {
		createTopicMenu('pages' + k);
	}

	if (j === 0) {
		createTopicMenu('onlinenow');
		createTopicMenu('quickreplyform');
	}

	$('watch').parentNode.removeChild($('watch'));
}





function pmAlertMSG(newMSGs, inboxGo, later, pmLink) {
	"use strict";
	var pmlinkimg = document.createElement('img'), pma, pmalert, pma2, pma3, pmdiv2, pmannouncement, pmdiv, pmBG;
	pmlinkimg.src = '/styles/macstack/imageset/messagesnew.png';

	pma = document.createElement('a');
	pma.href = pmLink;
	pma.appendChild(pmlinkimg);

	pmalert = document.createElement('div');
	addClassName(pmalert, 'alert');
	pmalert.appendChild(document.createTextNode(newMSGs));

	pma2 = document.createElement('a');
	pma2.href = pmLink;
	pma2.appendChild(document.createTextNode(inboxGo));

	pma3 = document.createElement('a');
	pma3.id = 'later';
	pma3.appendChild(document.createTextNode(later));

	pmdiv2 = document.createElement('div');
	pmdiv2.appendChild(pmalert);
	pmdiv2.appendChild(document.createElement('br'));
	pmdiv2.appendChild(pma2);
	pmdiv2.appendChild(document.createElement('br'));
	pmdiv2.appendChild(pma3);

	pmannouncement = document.createElement('div');
	pmannouncement.id = 'pmannouncement';
	pmannouncement.appendChild(pma);
	pmannouncement.appendChild(pmdiv2);

	pmdiv = document.createElement('div');
	pmdiv.id = 'pmdiv';
	pmdiv.appendChild(pmannouncement);
	document.body.insertBefore(pmdiv, document.body.childNodes[0]);

	pmBG = document.createElement('div');
	pmBG.id = 'pmbackground';
	pmBG.appendChild(document.createTextNode(String.fromCharCode('160')));
	document.body.insertBefore(pmBG, document.body.childNodes[0]);

	addEvent($('later'), 'click', function () {
		document.body.removeChild($('pmbackground'));
		document.body.removeChild($('pmdiv'));
	}, false, false);
}

/*like innerHTML, but proper XML*/
function getContents(item) {
	"use strict";
	var contents = '', children = item.childNodes, i;
	for (i = 0; i < children.length; i++) {
		if (children[i].childNodes.length !== 0) {
			contents += getContents(children[i]);
		}

		if (children[i].tagName !== undefined && children[i].tagName.toLowerCase() === 'br') {
			contents += '\n';
		}
		if (children[i].nodeValue !== null && children[i].nodeValue !== undefined && children[i].nodeValue !== '') {
			contents += children[i].nodeValue.replace(/(\t)/g, '');
		}
	}

	return contents;
}





function setAccessKeyModifier() {
	"use strict";
	if (navigator.userAgent.match("Opera")) {
		accessKeyModifier = 'shift esc, then';
	} else if (navigator.userAgent.match("Macintosh")) {
		if (navigator.userAgent.match("Safari")) {
			accessKeyModifier = 'ctrl option';
		} else {
			accessKeyModifier = 'ctrl';
		}
	} else if (navigator.userAgent.match("Firefox")) {
		accessKeyModifier = 'alt shift';
	} else {
		accessKeyModifier = 'alt';
	}
}

setAccessKeyModifier();





function addAccessKeyHint(thisElement) {
	"use strict";
	if (thisElement.accessKey !== undefined) {
		if (thisElement.title === '') {
			thisElement.title = accessKeyModifier + ' ' + thisElement.accessKey;
		} else {
			thisElement.title += ' (' + accessKeyModifier + ' ' + thisElement.accessKey + ')';
		}
	}
}





function addJSCSS() {
	"use strict";
	var jsCSS = document.createElement('link');
	jsCSS.rel = 'stylesheet';
	jsCSS.href = 'http://macstack.net/styles/macstack/theme/jscss.css';
	jsCSS.type = 'text/css';

	document.getElementsByTagName('head')[0].appendChild(jsCSS);
}

addJSCSS();
