function removeQuery(Href) {
	var NewHref = Href.replace(/\?.*/gi, "");
	return NewHref;
}

$(function () {
	$("a").each(function(ElemIndex, Elem) {	
				var re = new RegExp("(http(s?)://)?" + window.location.host, "gi");
				var IsExitLink = false;
				
				if (Elem.href.match("http(s?):\/\/") && !Elem.href.match(re) && !$(Elem).hasClass("leavingSite") && $(Elem).parents("#leavingSite").length == 0) {
					$(Elem).bind("click", function () {
						var CGP = document.getElementById("ContainingGaPageview");
						var CGPValue = "";
						if (CGP) CGPValue = CGP.value.substring(0, CGP.value.indexOf("?"));
						_gaq.push(["_trackEvent", "Exit Links", "Exit", (CGPValue.length > 0 ? CGPValue : GaPageview) + " - " + removeQuery(this.href)]);		
						if (this.href.match(/lundbeck.com\/.+/gi)) _gaq.push(["_trackPageview", "/outboundlinks" + GaPageview + removeQuery(this.href) + "/"]);
						return true;
					});
					IsExitLink = true;
				} 
				if (Elem.href.match(/mailto:/ig)) {
					$(Elem).bind("click", function () {
								_gaq.push(["_trackEvent", "Mailto Links", "Mailto", this.href.replace("mailto:", "")]);
								return true;
							});
				}
				if (Elem.href.match(/\.(pdf|doc|xls)$/ig)) {
					$(Elem).bind("click", function () {
								if (IsExitLink) _gaq.push(["_trackEvent", "Exit Links", "File Download", this.href]);
								else _gaq.push(["_trackPageview", "/downloads" + GaPageview + this.href + "/"]);
								return true;
							});
				}
				if (Elem.href.match(/#printpage/ig)) {
					$(Elem).bind("click", function () {
						_gaq.push(['_trackEvent', 'Print Page', 'Print', GaPageview]);
						return true;
					});
				} else if (Elem.href.match(/#txtsize/ig)) {
					$(Elem).bind("click", function () {
						/*  this does not work in IE6, works in firefox :
						
							var fn_name = Elem.attributes['onclick'].value;
							alert(fn_name);
						 */	
						var link =  Elem.href;
						var sizeIndex = '';
						if (link.match(/txtsize1/ig)) {
							sizeIndex = '1';
						} else if (link.match(/txtsize2/ig)) {
							sizeIndex = '2';
						} else if (link.match(/txtsize3/ig)) {
							sizeIndex = '3';
						}
						_gaq.push(['_trackEvent', 'Text Adjustment', 'Size', GaPageview + ' - ' + sizeIndex]);
						return true;
					});
				} else if (Elem.href.match(/#/ig) && !$(Elem).hasClass("leavingSite") && $(Elem).parents("#leavingSite").length == 0 && !IsExitLink) {
					$(Elem).bind("click", function () {
								_gaq.push(['_trackEvent', 'Jump To Link', GaPageview, this.innerHTML]);
								return true;
							});
				} 
				if (Elem.href.match(/(lundbeckshare.com)|(seizureresources.com)/ig)) {
					Elem.href = Elem.href + (Elem.href.indexOf("?") >= 0 ? "&" : "?") + "utm_medium=Links&utm_source=" + window.location.host + "&utm_content=" + GaPageview;
				}
			});
	if (typeof(WebForm_OnSubmit) == "function") {
		var tempWebForm_OnSubmit = WebForm_OnSubmit;
		WebForm_OnSubmit = function () {
			var ValidForm = tempWebForm_OnSubmit();
			if (!ValidForm) {
				_gaq.push(["_trackPageview", GaPageview + (GaPageview.match(/tryagain\/$/gi) ? "" : "tryagain/")]);
			}
			return ValidForm;
		};
	}
});

var flash_video_event = function (VideoFile, VideoEvent) {
	VideoFile = VideoFile.replace(/\//gi, "\\");
	var LastSlashIndex = VideoFile.lastIndexOf("\\");
	if (LastSlashIndex >= 0) VideoFile = VideoFile.substr(LastSlashIndex+1);
	
	var page = GaPageview;
	if (VideoEvent == "start") {
		_gaq.push(["_trackEvent", "Video - Plays", VideoFile + " - " + page, "Start"]);				
	} else if (VideoEvent == "end") {
		_gaq.push(["_trackEvent", "Video - Plays", VideoFile + " - " + page, "End"]);				
	} else if (VideoEvent.match(/[1-9].*/gi)) {
		if (VideoEvent.indexOf("s") > 0) _gaq.push(["_trackEvent", "Video - Interval - Seconds", VideoFile + " - " + page, VideoEvent]);
		else if (VideoEvent.indexOf("%") > 0) _gaq.push(["_trackEvent", "Video - Interval - precentage", VideoFile + " - " + page, VideoEvent]);
	}
}

