/*
 * onUserExit jQuery Plugin (http://www.userfirstinteractive.com/)
 * @author Scott D. Brooks 
 * @created by UserFirst Interactive (creations@userfirstinteractive.com)
 * 
 * @version 1.0
 * 
 * @changelog
 * v 1.0 	->	Starting release [Dec. 27, 2008]
 * 
 */


var movingWithinSite 	= false;  // this is the var that determines if the unload was caused by a user leaving, or navigating in the site.
var codeToExecute		= function() {};

function userMovingWithinSite() {
	movingWithinSite = true;
}

(function($){	
	$.fn.onUserExit = function(options) {		
		var defaults = {
			execute:			"",	 // no function assigned by default
			internalURLs:		""		// used to detect whether the url is internal or not (you can add subdomains to this list so when a user hops between sites, they are still considered to be internal.
		};
		var options 			= $.extend(defaults, options);
		
		if (options.execute == "") {
			alert("The onUserExit jQuery Plugin has been misconfigured.  Please add the function you wish to execute.");
		}
		if (options.internalURLs == "") {
			alert("The onUserExit jQuery Plugin has been misconfigured.  Please add internal URLs so it know when the user is navigating internally.");
		}
		codeToExecute = options.execute;
				
		// add onClick function to all internal links
		$("a").each(function() {
			var obj = $(this);
			var linkIsInternal = false;
			
			var myInternalURLs = options.internalURLs.split("|");

			for (i = 0; i < myInternalURLs.length; i++) {
				if (obj.attr("href").indexOf(myInternalURLs[i]) !== -1) {
					linkIsInternal = true;
				}
				// if it's a relative or absolute URL, so it's internal.
				if (obj.attr("href").indexOf("http://") == -1) {
					linkIsInternal = true;
				}
			}

			if (linkIsInternal == true) {				
				obj.bind("click", function(){
					userMovingWithinSite(); 
    			});
			}
		});
		$("input").each(function() {
			var obj = $(this);
			var linkIsInternal = false;

			if (obj.attr("type").indexOf("submit") !== -1 || obj.attr("type").indexOf("button") !== -1) {
				linkIsInternal = true;
			}

			if (linkIsInternal == true) {				
				obj.bind("click", function(){
					userMovingWithinSite(); 
    			});
			}
		});
		$("area").each(function() {
			var obj = $(this);
			var linkIsInternal = false;
			if (obj.attr("onClick") != "") {
				linkIsInternal = true;
			}

			if (linkIsInternal == true) {				
				obj.bind("click", function(){
					userMovingWithinSite(); 
    			});
			}
		});
	};
	
	$(window).unload(function() {
		// unloading the page when the user is leaving
		if (movingWithinSite == false) { 
			codeToExecute();
		}
	});
	
		
})(jQuery);
/*$(document).ready(function() {								
	$().onUserExit({ 
		execute:	function()
		{
            var str = window.location.href;
            str = str.substring(str.lastIndexOf("/") + 1, str.indexOf("?"));
            respondentID = "" + respondentID;
            if (str != "end_survey.aspx" && str != "exitsurvey.aspx" && str != "errorpage.aspx" && str != "congratulations.aspx" && respondentID != "" && respondentKey != "" && respondentID != "0" && respondentKey != "0" && respondentID != "-1" && respondentKey != "-1" && respondentID.indexOf("span") == -1) {
			    //window.open("https://start.insites.eu/exitsurvey.aspx?ID=" + respondentID + "&K=" + respondentKey, "survey");
			    //$.post("https://start.insites.eu/js/shoutboxSendMail.asp", {ID:respondentID, Finished:"No"}, function(data){});
			}
		},
        internalURLs: "survman|start.talktochange.com|start.foodpanel.nl|go.insites.eu|go1.insites.eu|delphi.insites.eu|confirmit.insites.eu|start.insites.eu|talktochange.insites.eu|foodpanel.insites.eu|flyingblue.insites.eu"
	});					
});	*/

