﻿if (typeof (velir) == "undefined")velir = {};

var debugGoogleTracking = false;

velir.atlas = {

	//MAILING LIST
	isValidEmail: function(email) {
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		return filter.test(email);
	},

	validateAndSubmitEmail: function(email, url, divSuccess, divIntro, divError, spanSubmitButton) {
		if (velir.atlas.isValidEmail(email)) {
			//Post an email address to the local web service which will then post the email to Dartmouth's mailinglist.php script
			$.post(url, { 'email': email });
			divSuccess.show();
			divIntro.hide()
			divError.hide();
			spanSubmitButton.html('Email Submitted');
		}
		else {
			divError.show();
			divSuccess.hide();
		}
	},

	submitGoogleAnalyticsEvent: function(category, action, optional_label, optional_value) {
		//submit the event to GA
		var success = false;
		if (optional_label === undefined && optional_value === undefined) {
			success = pageTracker._trackEvent(category, action);
		}
		else if (optional_value === undefined) {
			success = pageTracker._trackEvent(category, action, optional_label);
		}
		else {
			success = pageTracker._trackEvent(category, action, optional_label, optional_value);
		}

		//show a popup if we're in debug mode
		if (debugGoogleTracking) {
			alert("Event submission was successful? " + success + "\n\n" +
					  "category: " + category + "\n" +
					  "action: " + action + "\n" +
					  "optional_label: " + optional_label + "\n" +
					  "optional_value: " + optional_value + "\n");
		}
	},

	locationTypes: { State: 2, HRR: 3, HSA: 4, Hospital: 5, County: 6, PCSA: 7 },

	isPrinterFriendly: function() {
		return $(document).url().param(urlParams.printerFriendlyParam) == "1";
	},

	isChrome: function() {
		return /chrome/.test(navigator.userAgent.toLowerCase());
	},

	isSafari: function() {
		var userAgent = navigator.userAgent.toLowerCase();
		return /webkit/.test(userAgent) && !/chrome/.test(userAgent);
	},

	uploadBase64Image: function(base64, fileExtension, onSuccess) {
		var parms = JSON.stringify({
			base64Image: base64,
			fileExtension: fileExtension
		});

		$.ajax({
			type: "POST",
			url: "/Services/FlashPrinter.asmx/UploadBase64Image",
			data: parms,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: onSuccess,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("Error connecting to FlashPrinter service.\n\n" + textStatus);
			}
		});
	},

	showTooltip: function(text, target, title, maxWidth, targetPosition, tipPosition) {

		if (typeof (title) == "undefined") {
			title = false;
		}
		if (typeof (targetPosition) == "undefined") {
			targetPosition = "topMiddle";
		}
		if (typeof (tipPosition) == "undefined") {
			tipPosition = "bottomMiddle";
		}
		if (typeof (tipPosition) == "undefined") {
			maxWidth = 250;
		}

		$(target).qtip({
			content: {
				text: text,
				title: {
					text: title,
					button: "X"
				}
			},
			position: {
				corner: {
					tooltip: tipPosition,
					target: targetPosition
				},
				adjust: {
					screen: true
				}
			},
			show: {
				delay: 0,
				solo: true,
				when: false,
				ready: true
			},
			hide: {
				when: {
					event: "unfocus"
				}
			},
			style: {
				background: "#ffd",
				color: "#413803",
				border: {
					color: "#fcf2be",
					width: 2,
					radius: 5
				},
				title: {
					"background-color": "#fcf2be",
					"padding": "0 6px 2px",
					"color": "#413803"
				},
				width: {
					max: maxWidth
				},
				padding: 10,
				textAlign: "left",
				tip: {
					corner: tipPosition,
					color: false
				}
			}
		});
	}
}


// Convenience
String.isNullOrEmpty = function(val) {
    return val == null || val == '';
   };

// Little plugin to add search box behavior to text inputs
(function($) {

    $.fn.searchbox = function(instructions, classToggle, onEnterPressed) {
        return this.each(function() {
            var searchbox = $(this);

            if (!searchbox.is('input[type="text"]')) {
                return;
            }

            // only set the instructions if an initial value has not been set already
            if (String.isNullOrEmpty(searchbox.val())) {
                searchbox.val(instructions);
            }

            searchbox.focus(function() {
                if ($(this).val() != instructions) {
                    return;
                }
                $(this).val('');
                if (!String.isNullOrEmpty(classToggle)) {
                    $(this).toggleClass(classToggle);
                }
            });

            searchbox.blur(function() {
                if ($(this).val() != '') {
                    return;
                }
                $(this).val(instructions);
                if (!String.isNullOrEmpty(classToggle)) {
                    $(this).toggleClass(classToggle);
                }
            });

            if (onEnterPressed != null) {
                searchbox.keypress(function(e) {
                    if (e.keyCode == 13) {                        
                        onEnterPressed(e);
                    }
                });
            }

        });
    };

})(jQuery);
