// EXTERNAL LINK DISCLAIMER
// @author Joseph Karns
// Additional resources Joel Grannas and Paul Nauman
// Last updated on 4/11/2011 by Joel

(function($) {
		  
	$.fn.linkDisclaimer = function(settings){
		//SETTINGS
		var config = {
			'allowed'	: ['javascript:','mailto:','schoolwires.com','schoolwires.net']
		};
		if (settings){$.extend(true, config, settings);}
		
		// loop thru each matched element
		return this.each(function() {
			
			var element = this;
			var myHREF = String($(element).attr("href"));
			
			//CHECKS AGAINST ARRAY FOR A MATCH
			var extLink = 1;
			for(var i = 0; i < config.allowed.length; i++){
				if(myHREF.match(":")) {
					if(myHREF.match(config.allowed[i]) ){
						extLink = 0;
					}
				} else {
					extLink = 0;
				}
			}
			if (extLink == 1) {
				$(element).addClass("external");
				$(element).click(function(){
					var dialogMessage = config.disclaimer;
					var $dialog = $("<div>" + dialogMessage + "<a style='display:none' class='myLink' href='" + $(this).attr("href") + "' target='" + $(this).attr("target") + "'></a></div>")
					.dialog({
						autoOpen: false,
						modal: true,
						zIndex: 9000,
						title: "Attention Users:",
						dialogClass: "dialogBox",
						buttons: {
							'Accept': function() {
								if($(".myLink",this).attr("target") == "_blank"){
									window.open($(".myLink",this).attr("href"));
									$(this).dialog('close');
								} else {
									window.location = $(".myLink",this).attr("href");
								}
							},
							'Cancel': function() {
							   $(this).dialog('close');
							}
						}
					});
					$dialog.dialog("open");
					// prevent the default action, e.g., following a link
					return false;
				});
			}
			
						
		});
	
		
		
	};
	
	

})(jQuery);
		
