var SL_LinkTweaker = new Class({

	//implements
	Implements: [Options],	

	//options
	options: {
	items:  null,							//Array of elements to scan for links
	currentServer: '',						//for overriding use of document.location.host
	autoExternals: true,					//flag for making all external links set as target=_blank
	externalLinkClass: null,				//class to assign to all external links
	autoFileClasses: true,					//flag for adding a class to specified file extension types (see below)
	autoFileTypes: new Array('zip', 'pdf'),	//array of file types to add class
	classPrefix: 'file_',					//prefix for filetype classes  (i.e. creates 'file_zip', 'file_pdf' with default options)
	autoStart: true							//autostart flag
	},

	//initialization
	initialize: function(options) {
		
		this.setOptions(options);
		if(this.options.autoStart == true)	this.start();
	
	},

	//startup method
	start: function() {
		
		var self = this;
		var server = self.setLocation();
		
		self.options.items.each(function(el, i){
						
			//get all links from each item
			var links = $$(el.getElements('a'));
			
			//if true, adds a class to the link if matches a filetype in array
			if(self.options.autoFileClasses == true){
				
				links.each( function(link){
					var theHref = link.get('href');
				
					// get the extension from the href
					var hrefArray = theHref.split('.');
					var extension = hrefArray[hrefArray.length - 1];
					extension = extension.toLowerCase();
					
					var fileTypes = self.options.autoFileTypes;
					if(fileTypes.contains(extension)){
						var newClass = self.options.classPrefix + extension;
						link.addClass(newClass);
					}
					
				});
				
			}
			
			//if true, adds target='_blank' and adds a class to the link (which is set via options above)
			if(self.options.autoExternals == true){
				links.each( function(item) {
					self.setLink(item,server);
				});
			}
			
		 });
		
		
	},
	
	
	
	setLink : function(el, server) {
		var h = el.get('href');
		if( h.indexOf('http') >= 0 && h.indexOf(server.getLocation() ) < 0 ) {
			el.set('target', '_blank');
			if(this.options.externalLinkClass) el.addClass(this.options.externalLinkClass);
		}					
		return el;
	},
	
	
	
	setLocation : function() {
		var s = ( this.options.currentServer !== '' ) ? this.options.currentServer : document.location.host;
		return {
			getLocation : function() { return s; }
		}
	}



});