//<![CDATA[[
$BTB={s:11180};
$BTB.domain="tiasnimbas.edu";
$BTB.jsh="btbuckets.com/bt.js";
//]]>

$BTB.jshost = (document.location.protocol == "https:")?"https://ssl.":"http://static."; document.write(unescape("%3Cscript src='" + $BTB.jshost + $BTB.jsh + "' type='text/javascript'%3E%3C/script%3E"));


// exit
var exit_notification_nl = "Let op: je hebt aangegeven deel te willen nemen aan de enquete. De enquete is in een nieuw venster geopend. Vergeet niet om de vragen te beantwoorden.";

var exit_notification_en = "Please pay attention: you\'ve indicated that you would like to participate in the online survey. The survey was opened in a new window. Don\'t forget to fill out the questions.";

$(document).ready(function() {
	$().onUserExit({
		execute:	function()
		{
			if($BTB.isUserOnBucket("default_new_visitor") && $BTB.isUserOnBucket("included_for_participation"))
			{
				$BTB.trackEvent("exclude");
				if ($BTB.isUserOnBucket("dutch")) {
					alert(exit_notification_nl);
				} else if ($BTB.isUserOnBucket("english")) {
					alert(exit_notification_en);
				}
			}
		},
		internalURLs: "tiasnimbas.edu"
	});
});

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;
}

// Code to detect refreshing of the page through keyboard use
var ctrlKeyIsDown = false;
function interceptKeyUp(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else 
			return;
	}
	
	keyCode = e.keyCode;	
	if (keyCode == 17){
		ctrlKeyIsDown = false;
	}
}

function interceptKeyDown(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else
			return;
	}
	
	keyCode = e.keyCode;
	// F5 detected
	if ( keyCode == 116 ) {
		userMovingWithinSite();
	}
	
	if (keyCode == 17){
		ctrlKeyIsDown = true;
	}

	// then they are pressing Ctrl+R
	if (ctrlKeyIsDown && keyCode == 82){	
		userMovingWithinSite();
	}	
}

function interceptKeyPress(e) {
	if( !e ) {
		if (window.event)
			e = window.event;
		else
			return;
	}

	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : void 0;
	if(e.charCode == null || e.charCode == 0 ) {
		// F5 pressed
		if ( keyCode == 116 ) {
			userMovingWithinSite();
		}
	}
}

function attachEventListener( obj, type, func, capture ) {
	if(window.addEventListener) {
		//Mozilla, Netscape, Firefox
		obj.addEventListener( type, func, capture );
	} else {
		//IE
		obj.attachEvent( 'on' + type, func );
	}
}

(function($){	
	$.fn.onUserExit = function(options) {		
		var defaults = {
			execute:			"",
			internalURLs:		""
		};
		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, divs and objects
            	$("a,div[class~=hasUrl],object").each(function() {
                  var obj = $(this);
                  var linkIsInternal = false;
                  
                  var myInternalURLs = options.internalURLs.split("|");

                  for (i = 0; i < myInternalURLs.length; i++) {
                        if ($(this).is('a')) {
                             if (obj.attr("href") != undefined) {
                                   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;
                                   }
                             }
                        } else if ($(this).is('div')) {
                             if (obj.attr("url") != undefined) {
                                   if (obj.attr("url").indexOf(myInternalURLs[i]) !== -1) {
                                         linkIsInternal = true;
                                   }
                                   // if it's a relative or absolute URL, so it's internal.
                                   if (obj.attr("url").indexOf("http://") == -1) {
                                         linkIsInternal = true;
                                   }
                             }
                        } else if ($(this).is('object')) {
                             linkIsInternal = true;
                        }
                  }

                  if (linkIsInternal == true) {                  
                        obj.bind("click", function(){
                             userMovingWithinSite();
                  });
                  }
            });


		$("form").each(function() {
			var obj = $(this);
			currentonSubmit = obj.attr("onSubmit");	
			if (currentonSubmit === undefined) {
				currentonSubmit = "";
			}
			obj.attr("onSubmit", currentonSubmit + " userMovingWithinSite();");
		});		

		// for Refresh Detection
		attachEventListener(document,"keydown",interceptKeyDown,true);
		attachEventListener(document,"keyup",interceptKeyUp,true);
		attachEventListener(document,"keypress",interceptKeyPress,true);	
		
	};

	$(window).unload(function() { 
		// unloading the page when the user is leaving
		if (movingWithinSite == false) {
			codeToExecute();
		}
	});
})(jQuery);