/*

	References [function, resource]
	
	addBookmarkObj http://
	addEvent       http://www.ejohn.org/projects/flexible-javascript-events/
	externalLinks  http://www.sitepoint.com/article/standards-compliant-world/
	sfHover        http://www.htmldog.com/articles/suckerfish/dropdowns/

*/

// 
function addEvent(obj, type, fn) {
	if(obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function() {
			obj['e'+type+fn](window.event);
		}
		obj.attachEvent('on'+type, obj[type+fn] );
	} else
		obj.addEventListener(type, fn, false);
}

// 
function externalLinks() {
	if(!document.getElementsByTagName)
		return; 
	var anchors = document.getElementsByTagName('a');
	for(var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if(anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
			anchor.setAttribute('target', '_blank');
	}
}

// 
var addBookmarkObj = {
	addTextLink:function(parId) {
		if(!document.getElementById('addBookmark'))
			return null;
		else {
			var a = addBookmarkObj.makeLink(parId);
			a.appendChild(document.createTextNode('Bookmark'));
			a.href = location.href;
			a.setAttribute("class", "bookmark");
			a.setAttribute("className", "bookmark"); // Internet Explorer
			a.title = 'Bookmark';
		}
	},
	makeLink:function(parId) {
		if(!document.getElementById || !document.createTextNode)
			return null;
		parId = ((typeof(parId) == 'string') && !isEmpty(parId)) ? parId :'addBookmark';
		var cont = document.getElementById(parId);
		if(!cont)
			return null;
		var a = document.createElement('a');
		if(window.opera) // Opera 7+
			a.rel = 'sidebar';
		else
			a.onclick = function() {
				addBookmarkObj.exec(this.href, document.title);
				return false;
			}
		return cont.appendChild(a);
	},
	exec:function(url, title) {
		// User agent sniffing is bad in general, but this is one of the times when it's really necessary
		var ua = navigator.userAgent.toLowerCase();
		var isKonq = (ua.indexOf('konqueror') != -1);
		var isSafari = (ua.indexOf('webkit') != -1);
		var isMac = (ua.indexOf('mac') != -1);
		var buttonStr = isMac ? 'Command/Cmd' : 'CTRL';
		// IE4/Win generates an error when you execute "typeof(window.external.AddFavorite)"
		// In IE7 the page must be from a web server, not directly from a local file system, otherwise, you will get a permission denied error
		if(window.external && (!document.createTextNode || (typeof(window.external.AddFavorite) == 'unknown')))
			window.external.AddFavorite(url, title);
		else if(isKonq) // Konqueror
			alert('You need to press CTRL + B to bookmark our site.');
		else if(window.opera) // Opera 7+
			void(0);
		else if(window.home || isSafari) // Firefox, Netscape, Safari, iCab
			alert('You need to press ' + buttonStr + ' + D to bookmark our site.');
		else if(!window.print || isMac) // IE5/Mac and Safari 1.0
			alert('You need to press Command/Cmd + D to bookmark our site.');
		else // Unknown
			alert('In order to bookmark this site you need to do so manually through your browser.');
	}
}

// 
ieHover = function() {
	if(document.getElementById('navigation')) {
		var sfEls = document.getElementById('navigation').getElementsByTagName('li');
		for(var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
   }
}

// 
ieHover2 = function() {
	if(document.getElementById('languages')) {
		var sfEls = document.getElementById('languages').getElementsByTagName('li');
		for(var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
   }
}

// 
sfHover = function() {
	if(document.getElementById('boxes')) {
		var sfEls = document.getElementById('boxes').getElementsByTagName('li');
		for(var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" iehover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" iehover\\b"), "");
			}
		}
	}
}

// 
addEvent(window, "load", addBookmarkObj.addTextLink);
addEvent(window, 'load', externalLinks);
addEvent(window, 'load', ieHover);
addEvent(window, 'load', ieHover2);
addEvent(window, 'load', sfHover);