/// <reference path="jquery-vsdoc.js" />
/*
 * jQuery Plugin checkLink v1 & blackOut v1
 * Requires jQuery 1.3.1+ (Not tested with earlier versions).
 * Copyright (c) 2007-2009 Aaron Edwards
 * 
 * !!!!ACHTUNG!!!!
 * This plugin requires the listen plugin created by Ariel Flesler: http://flesler.blogspot.com/2007/10/jquerylisten.html
 * Using version: 1.0.3
 *
 *
 *  Both functions take the same arguments. Outside links is "anchor-centric"
 *	and only applies to anchor tags. 
 *	Options:	
 *		message		- Message you want the user to see.
 *		boxClass	- The class of the confirmation box.
 *		boxStyle	- any style overloads you might want to apply to the confirmation box.
 *		yesBtnText	- Text to display for "Yes" button.
 *		yesBtnValue	- Value of yes button.
 *		yesBtnClass	- Class of yes button.
 *		yesBtnStyle	- Style overloads for yes button.
 *		yesCallBack	- Callback function for yes button.
 *		noBtnText	- Text to display for "No" button.
 *		noBtnVal	- Value of no button.
 *		noBtnClass	- Class of no button.
 *		noBtnStyle	- Style overloads for no button.
 *		noCallBack	- Callback function for no button.
 *	Examples: 
 *		$.checkLink(); //Gives default values.
 *		$.checkLink({message:"Did you remember to check the required boxes?", yesBtnText: "Absolutely!"});
 *		$("span").blackOut(); //When a span is clicked it will display the blackout box.
 */
 
(function($) {
	var ver = "1";
	var gujs = new Guid();

	$.extend({
		checkLink: function(args) {
			if (!args) { args = {}; }

			var disabled = ReadStopCookie();
			var address = window.location.href.toString().split("/");
			//From a browser, index 2 should always be the hostname.
			address = address[2];

			var defAction = {
				message: "You are about to navigate away from this site.<br/>Are you sure you wish to continue?"
				, yesCallBack: function() {
					var oid = $(this).attr("caller");
					if (!udf(oid)) {
						var obj = $("[gujs='" + oid + "']");
						if (!udf(obj)) {
							var target = obj.get(0).target;
							window.open(obj.attr("href"), target || '_self');
						}
						BlackIn();
					}
				}
			};

			args = $.extend({}, defAction, args);

			var linkCheck = function(event) {

				var o = $(this);
				if (udf(o.data("clicked"))) {
					var tolink = o.attr("href");
					var outside = o.attr("outsideLink");
					var onclick = o.attr("onclick");

					if (!disabled && udf(onclick)
					&& !udf(tolink) && tolink.toLowerCase().indexOf("javascript") == -1) {
						if (tolink.indexOf(address) > -1
						|| (tolink.indexOf("http") == -1
						&& (outside == "False" || udf(outside)))) {
							var target = this.target || '_self';
							o.data("clicked", true); //Added because some bound events get called multiple times, this prevents multiple calls.
							o.data("reset", setTimeout(function() { o.removeData("clicked"); }, 1000)); //Added in case link was local, and user wants to click again.
							window.open(this.href, target);
							if (!udf(o.data("clicked"))) {
								return false;
							}
						} else {
							$(this).blackOut(args);
							return false;
						}
					}
				} else {
					if (!udf(o.data("reset"))) {
						clearInterval(o.data("reset"));
						o.data("reset", setTimeout(function() { o.removeData("clicked"); }, 1000));
					}
					return false;
				}
			}

			$("a").bind("click", linkCheck);
			$.listen("click", "a", linkCheck);

			$.listen("click", "li", function() {
				var o = $(this);
				if (o.children().size() == 1 && o.children("a").size() == 1) {
					o.children("a").trigger('click');
				}
			});

		}
	});

	var defaults = {
		message: "Are you sure?"
		, boxClass: "Skin-ExternalLinkPopup"
		, boxStyle: "position:absolute;z-index:1001;"
		, yesBtnText: "Yes"
		, yesBtnValue: "Yes"
		, yesBtnClass: "Form-Button"
		, yesBtnStyle: "margin-right:6px;cursor:pointer;padding:2px 8px;height:20px;width:40px;"
		, yesCallBack: function() { }
		, noBtnText: "No"
		, noBtnVal: "No"
		, noBtnClass: "Form-Button"
		, noBtnStyle: "cursor:pointer;padding:2px 8px;height:20px;width:40px;"
		, noCallBack: function() { BlackIn(); }
		, disableBoxMsg: "Please don't show me this again:"
	};

	$.fn.blackOut = function(args) {
		if (!args) { args = {}; }
		args = $.extend({}, defaults, args);

		var hgt, wth;
		hgt = $(document).height();
		wth = $(window).width();

		var yesClass = args.yesBtnClass || defaults.yesBtnClass;
		var yesBtnstyle = args.yesBtnStyle || defaults.yesBtnStyle;
		var yesBtnValue = args.yesBtnValue || defaults.yesBtnValue;
		var yesBtnText = args.yesBtnText || defaults.yesBtnText;
		var noClass = args.noBtnClass || defaults.noBtnClass;
		var noBtnstyle = args.noBtnStyle || defaults.noBtnStyle;
		var noBtnValue = args.noBtnValue || defaults.noBtnValue;
		var noBtnText = args.noBtnText || defaults.noBtnText;
		var boxClass = args.boxClass || defaults.boxClass;
		var boxStyle = args.boxStyle || defaults.boxStyle;
		var msg = args.message || defaults.message;
		var disableBoxMsg = args.disableBoxMsg || defaults.disableBoxMsg;

		var yesBtn = "<span id=\"confirmyes\" name=\"yesno\" class=\"" + yesClass + "\" value=\"" + yesBtnValue + "\" style=\"" + yesBtnstyle + "\">" + yesBtnText + "</span>";
		var noBtn = "<span id=\"confirmno\" name=\"yesno\" class=\"" + noClass + "\" value=\"" + noBtnValue + "\" style=\"" + noBtnstyle + "\">" + noBtnText + "</span>";
		var popupTitle = "<div class=\"Skin-ExternalLinkPopupTitle\">External Link Notice</div>";
		var dontDisplay = "<input type=\"checkbox\" id=\"stopTheMadness\">";

		//Size returns at least 1 if it exists.
		if ($("#cfrmbox").size() == 0) {
			$("body").append("<div id=\"cfrmbox\" class=\"" + boxClass + "\" style=\"display:none;" + args.boxStyle + "\">" + popupTitle + "<div id=\"bkMsg\" class=\"Skin-ExternalLinkPopupMsg\">" + msg + "</div><div class=\"Skin-ExternalLinkButtonBar\">" + yesBtn + noBtn + "</div><div class=\"Skin-ExternalLinkButtonBar\">" + disableBoxMsg + dontDisplay + "</div></div>");
		}

		if ($("#blkout").size() == 0) {
			$("body").append("<div id=\"blkout\" style=\"display:none;overflow:hidden;left:0;top:0;position:absolute;height:" + hgt + ";width:" + wth + ";background-color:black;z-index:800;\"></div>");
			$("#blkout").css("opacity", 0.7);
			BlackoutSizer();
		}

		if ($("#stopTheMadness").size() > 0) {
			$("#stopTheMadness").click(function() {
				document.cookie = 'dontCheckExternal=true; expires=Thu, 5 Aug 2100 23:59:59 UTC; path=/';
			});
		}

		$("#confirmno").click(BlackIn);
		$(window).bind('resize', BlackoutSizer);

		return this.each(function() {
			var o = $(this);

			if (udf(o.attr("gujs"))) {
				o.attr("gujs", gujs.New());
				o.data("cfbxArgs", args);
			}

			ConfirmBox(o.attr("gujs"));
			o.click(function() {
				var t = $(this);
				ConfirmBox(t.attr("gujs"));
				return false;
			});
			return false;
		});
	}

	function ReadStopCookie() {
		if (document.cookie.length > 0) {
			return document.cookie.indexOf("dontCheckExternal") > -1;
		}
		return false;
	}

	function ConfirmBox(oid) {
		var wdo;
		wdo = $(window);
		var caller = $("[gujs='" + oid + "']");
		var cArgs = caller.data("cfbxArgs");
		$("#blkout").show();
		$("#cfrmbox").show();
		ScreenCenter($("#cfrmbox"));
		wdo.bind('scroll', function() { ScreenCenter($("#cfrmbox")); });
		//Caller is assigned to the GUID of the calling object. Allows reversal back to parent object for processing. 
		$("#confirmyes").attr("caller", oid).one("click", cArgs.yesCallBack).attr("class", cArgs.yesBtnClass).attr("style", cArgs.yesBtnStyle).html(cArgs.yesBtnText);
		$("#confirmno").attr("caller", oid).one("click", cArgs.noCallBack).attr("class", cArgs.noBtnClass).attr("style", cArgs.noBtnStyle).html(cArgs.noBtnText);
	}

	function BlackIn() {
		var wdo = $(window);
		$("#blkout").hide();
		$("#cfrmbox").hide();
		wdo.unbind('scroll');
		wdo.unbind('resize');
	}

	function BlackoutSizer() {
		var hgt, wth;
		hgt = $(document).height();
		wth = $(window).width();
		var o = $("#blkout");
		if (o != null && o.size() > 0) {
			o.height(hgt).width(wth);
		}
		o.show();
		ScreenCenter($("#cfrmbox"));
	}

	function udf(val) {
		return typeof (val) == 'undefined' ? true : val == null ? true : false;
	}

	function ScreenCenter(elm) {
		var hgt, wth, tp, lft, srch, wdo;
		wdo = $(window);
		hgt = wdo.height();
		wth = wdo.width();
		scrh = wdo.scrollTop();
		scrw = wdo.scrollLeft();
		tp = scrh + ((hgt - elm.outerHeight()) / 2);
		lft = scrw + ((wth - elm.outerWidth()) / 2);
		elm.css({ top: tp, left: lft, position: "absolute" });
		elm.show();
	}

	//Written By Aaron Edwards 06/2009
	//Creates valid guids, verifys guids, checks for empty guids.
	function Guid() {
		var value;

		this.Empty = function() {
			return "00000000-0000-0000-0000-000000000000";
		}

		this.IsEmpty = function(gid) {
			return gid == this.Empty();
		}

		this.IsValid = function(value) {
			rGx = new RegExp("\\b(?:[A-F0-9]{8})(?:-[A-F0-9]{4}){3}-(?:[A-F0-9]{12})\\b");
			return rGx.exec(value) != null;
		}

		if (arguments.length == 1) {
			if (this.IsValid(arguments[0])) {
				value = arguments[0];
			} else {
				value = this.Empty();
			}
		}

		this.New = function() {
			if (arguments.length == 1 && this.IsValid(arguments[0])) {
				value = arguments[0];
			}

			var res = [], hv;
			var rgx = new RegExp("[2345]");
			for (var i = 0; i < 8; i++) {
				hv = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
				if (rgx.exec(i.toString()) != null) {
					if (i == 3) { hv = "6" + hv.substr(1, 3); }
					res.push("-");
				}
				res.push(hv.toUpperCase());
			}
			value = res.join('');
			return value;
		}

		this.value = function() {
			if (value) {
				return value;
			}
			return this.New();
		};
	}
})(jQuery);

function ClearLinkCheckCookie() {
	document.cookie = 'dontCheckExternal=true; expires=Thu, 5 Aug 1980 23:59:59 UTC; path=/';
}

$(function() {
	$.checkLink();
});