/**
* ExternalLinks - A jQuery plugin
* Description: This plugin opens links in a new browser window
* Author: Kellan Craddock &Jessica Tsuji
* @argument {object} a jquery object used to constrain the area effected
* @constructor this.construct()
* @returns false
*/

(function($) {

	//Create plugin obj
	$.fn.externalLinks = function(options) {
		return this.each(function(i) {
			$.fn.externalLinks.createInstance($(this), options);
		});
	}
	
	//Aquire plugin instance
	$.fn.externalLinks.createInstance = function(element, options) {
		if (element.data('externalLinks')) {
			//Existing Instance
			return element.data('externalLinks');
		} else {
			//New Instance
			var instance = new $.fn.externalLinks.instance(element, options);
			element.data('externalLinks').init(element, options);
			return element.data('externalLinks');
		}
	}
	
	//Instance
	$.fn.externalLinks.instance = function(container, i) {
	
		var self = this;
		this.container;
		this.options;
		
		//Default options
		this.defaults = {
			selector: 'a.external',
			allLinks: false
		}
		
		//Initialize 
		this.init = function(container, options) {
			//Set the container
			self.container = container;
			//Extend the default options obj
			self.options = $.extend({}, self.defaults, options);
			
			if(self.options.allLinks) {
				self.options.selector ='a';
			}
			$(self.options.selector, self.container).live('click', function() {
				var location = $(this).attr('href');
				window.open(location);
				return false;
			});
		}
	    
	    //Set plugin instance to data
	    container.data('externalLinks', this);
	}
	
	
})(jQuery);
