/**
 * Handle external links in a standards compliant way
 *
 * @author Alec Hill
 */
(function(){
	
com.alechill.ExternalLink = new Class({
    
    el: null,
    target: '_blank',
    
    initialize: function( el, target ){
        this.el = $(el);
		// don't set it up if already has been otherwise will open more than one window
		if(this.el.retrieve('externalised')) return;
        this.target = target || this.target;
        this.el.addEvent('click', this.open.bindWithEvent(this) );
		this.el.store('externalised', true);
    },

	open: function(e){ 
		e.stop();
        window.open(this.el.get('href'), this.target);
    }
    
});

/**
 * static method to parse page for external links and set them up for you
 */
com.alechill.ExternalLink.parseLinks = function( class_to_replace, target, in_element ){
    var class_to_replace = class_to_replace || 'external';
	var container = in_element ? $(in_element) : $(document.body);
    container.getElements('a').each(function(link){
        if( link.hasClass(class_to_replace) || link.rel.test('external')  ) new com.alechill.ExternalLink( link, target );
    });
}

})();
