var detect = navigator.userAgent.toLowerCase()
var OS,browser,version,total,thestring
var browsers = {ff:'Firefox', ie:'Internet Explorer', safari:'Safari', opera:'Opera'}

if (checkIt('konqueror')){ browser = "khtml"; OS = "Linux" }
else if (checkIt('safari')) browser = "safari"
else if (checkIt('omniweb')) browser = "omniweb"
else if (checkIt('opera')) browser = "opera"
else if (checkIt('icab')) browser = "icab"
else if (checkIt('msie')) browser = "ie"
else if (!checkIt('compatible')) { browser = "ff"; version = detect.charAt(8) }
else browser = "An unknown browser"

if (!version) version = detect.charAt(place + thestring.length)

if (!OS) {
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system"
}
// get previous/next non-text node
function previousElement(o) {
	if(o.previousSibling) { while (o.previousSibling.nodeType != 1) o = o.previousSibling; return o.previousSibling }
	else return false
}
function nextElement(o) {
	if(o.nextSibling) { while (o.nextSibling.nodeType != 1) o = o.nextSibling; return o.nextSibling }
	else return false
}
// styling functions
function isA(o,klass){ return new RegExp('\\b'+klass+'\\b').test(o.className) }
function addClass(o,klass){ if(!isA(o,klass)) o.className += ' ' + klass }
function rmClass(o,klass){ o.className = o.className.replace(new RegExp('\\s*\\b'+klass+'\\b'),'') }
function swapClass(o,klass,klass2){ var swap = isA(o,klass) ? [klass,klass2] : [klass2,klass]; rmClass(o,swap[0]); addClass(o,swap[1]) }
function getStyle(o,s) {
	if (document.defaultView && document.defaultView.getComputedStyle) return document.defaultView.getComputedStyle(o,null).getPropertyValue(s)
	else if (o.currentStyle) { return o.currentStyle[s.replace(/-([^-])/g, function(a,b){return b.toUpperCase()})] }
}
// get elements by class name
function $c(c,o,t) { o=o||document;
	if (!o.length) o = [o]
	var elements = []
	for(var i = 0, e; e = o[i]; i++) {
		if(e.getElementsByTagName) {
			var children = e.getElementsByTagName(t || '*')
			for (var j = 0, child; child = children[j]; j++) if(isA(child,c)) elements.push(child)
	}}
	return elements
}
// shorter names for grabbing stuff
function $id(id){ return document.getElementById(id) }
function $tags(t,o){ o=o||document; return o.getElementsByTagName(t) }
function $tag(t,o,i) { o=o||document; return o.getElementsByTagName(t)[i||0] }
function checkIt(string) {
	place = detect.indexOf(string) + 1
	thestring = string
	return place
}
function addClass(o,klass){ if(!isA(o,klass)) o.className += ' ' + klass }

function bookmarkletInstructions() {
	for(var i in browsers) {
		var div = $id('instructions-' + i)
		if(div) {
			var first = true, content = '&nbsp; (for other browsers: '
			var others = document.createElement('span'); others.style.fontWeight = 'normal'; addClass(others,'smaller')
			for(var j in browsers) {
				if (i != j) {
					if(first) first = false
					else content += ', '
					content += '<a href="javascript:showInstructions(\''+j+'\')">'+browsers[j]+'</a>'
			}}
			others.innerHTML = content + ')'
			$tag('h4',div).appendChild(others)
	}}
	showInstructions(browser)
	makeHiddenHelp($id('bookmarklet-instructions'), '(show me how...)', '(hide instructions)')
}

function showInstructions(b) {
	for(var i in browsers) {
		var div = $id('instructions-' + i)
		if(div) {
			if(b == i) div.style.display = 'block'
			else div.style.display = 'none'
}}}

function blurb() {
	ieFavorites($id('blurb'))
	makeHiddenHelp($id('blurb'), '(what are these?)', '(hide explanation)')
}

function makeHiddenHelp(o, showText, hideText) { // make show/hide links for help
	var elements = $c('hidden-help', o)
	for(var i=0, e; e=elements[i]; i++) {
		var hide = document.createElement('a'), show = document.createElement('a'), p = previousElement(e)
		hide.onclick = hideFunc(e, show); show.onclick = showFunc(e, hide)
		hide.innerHTML = hideText; show.innerHTML = showText
		hide.className = 'expand-help'; show.className = 'expand-help'
		e.style.display = hide.style.display = 'none'
		p.appendChild(show); p.appendChild(hide);
}}

function hideFunc(e, show) { return (function(){show.style.display = 'inline'; e.style.display = 'none'; this.style.display = 'none'}) }
function showFunc(e, hide) { return (function(){hide.style.display = 'inline'; e.style.display = 'block'; this.style.display = 'none'}) }
