/**
 * Marlon website 2008
 * common.js
 * 
 * Description:	common used javascript functions
 *
 * @author: 	Davy De Pauw
 * @created: 	19/03/2008
 * @modified:	28/04/2008
 * @copyright:	Marlon BVBA
 * URI:			http://www.marlon.be
 * 
 * MooTools Javascript Library was used as framework.
 * http://mootools.net/
 * 
 */

/**
 * Graceful E-Mail Obfuscation
 * Based on script originally written by Roel Van Gils
 *
 * URI:	http://www.alistapart.com/articles/gracefulemailobfuscation/ 
 */

function geo()
{
	var links = $$('a');
	
	links.each(function(element) {
		
		element.addEvent('click', function(evt) {			
			geoDecode(element);			
		});		
	});	
}

function geoDecode(anchor) { // function to recompose the orginal address
	var href = anchor.getAttribute('href');

	var address = href.replace(/.*nospam\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
	
	var linktext = anchor.innerHTML; // IE Fix
	if (href != address) {
		anchor.setAttribute('href','mailto:' + address); // Add mailto link	
		anchor.innerHTML = linktext; // IE Fix
	}
}

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.target = "_blank";
 	}
}

/**
 * Highlights in sidebar which creates nicer looking :hover effects.
 * This piece of javascript makes the hovering work in IE 6 as well.
 *
 */

function highlightItems() {
	
	if($$('#blog-recent li')) {
		// Get all li items in specified ul
		var list = $$('#blog-recent li');
		
		// Iterate each li element
		list.each(function(element) {
			
			// Set fx variable
			var fx = new Fx.Styles(element, {duration:200, wait:false});
			
			// Attach event to mouseEnter (change background color)
			element.addEvent('mouseenter', function(){
				fx.start({
					'background-color': '#F5F4EE'
				});
			});
			
			// Attach event to mouseLeave (change background color back to #fff)
			element.addEvent('mouseleave', function(){
				fx.start({
					'background-color': '#FFFFFF'
				});
			});
			
		});
	}
}

/**
 * Attach window listener to call init function
 * on domready event
 * 
 * @param {Object} 'domready'
 * @param {Object} function(init())
 */ 

window.addEvent('domready', function(){
	geo();
	externalLinks();
	
	// Highlight:hover in sidebar list
	// highlightItems();
});