var MONITUS = {
	_version: 5,
	_objectQueue: new Array(),
	_interactionTracker: null,
	internal: {
		_test_object: function(pObjectName) {
			try {
				eval("var vObject = window." + pObjectName + ";");
				return vObject;
			} catch(e) {
				return null;
			}
		},
		_wait_object: function(pQueueIndex) {
			if((pQueueIndex >= 0) && (pQueueIndex < MONITUS._objectQueue.length)) {
				var vSpecs = MONITUS._objectQueue[pQueueIndex];
				var vObject = MONITUS.internal._test_object(vSpecs.object);
				if(!vObject) setTimeout("MONITUS.internal._wait_object(" + pQueueIndex + ");", 10);
				else {
					MONITUS._objectQueue.splice(pQueueIndex, 1);
					vSpecs.args.unshift(vObject);
					vSpecs.callback.apply(this, vSpecs.args);
				}
			}
		}
	},
	
	community: {
		utilities: {
			non_null_string: function(pString, pDefault) {
				if(!pDefault) pDefault = "";
				return (pString ? pString : pDefault);
			},
			wait_object: function(pObjectName, pCallBack, pArguments) {
				var vObject = MONITUS.internal._test_object(pObjectName);
				if(vObject) {
					pArguments.unshift(vObject);
					pCallBack.apply(this, pArguments);
				} else {
					var vIndex = MONITUS._objectQueue.length;
					MONITUS._objectQueue[vIndex] = {object: pObjectName, callback: pCallBack, args: pArguments};
					setTimeout("MONITUS.internal._wait_object(" + vIndex + ");", 10);
				}
			},
			make_readable: function(pString) {
				return pString.replace(/[_\-]/g, " ");
			}
		},
		
		ga: {
			track_event: function(pTrackerObject, pEventCategory, pEventAction, pEventLabel) {
				if(pEventCategory && pEventAction && (pEventCategory != "") && (pEventAction != "")) {
					if(!pTrackerObject || (typeof(pTrackerObject) == "undefined") || (typeof(pTrackerObject._trackEvent) != "function")) MONITUS.community.utilities.wait_object(pTrackerObject, MONITUS.community.ga.track_event, [pEventCategory, pEventAction, MONITUS.community.utilities.non_null_string(pEventLabel)]);
					else pTrackerObject._trackEvent(pEventCategory, pEventAction, MONITUS.community.utilities.non_null_string(pEventLabel));
				}
			}
		},
		
		gwo: {
			track_variation_as_event: function(pTrackerObject, pExperimentName, pVariationName) {
				MONITUS.community.ga.track_event(pTrackerObject, pExperimentName, pVariationName);
			}
		},
		
		yahoo: {
			page_id: function() {
				var vTokens;
				if(window.location.search && (window.location.search.match(/sectionId\=([^&]*)/) || window.location.pathname.match(/\/wg\-order/))) {
					vTokens = window.location.search.match(/sectionId\=([^&]*)/);
					if(vTokens) return vTokens[1];
					else return "ysco.cart";
				} else {
					vTokens = window.location.pathname.match(/([^\/\.]+)\..+?$/);
					if(vTokens) return vTokens[1];
					else return "homepage";
				}
			},
			track_add_to_cart: function(pTrackerObject, pOptionalEventAction, pOptionalEventLabel, pOptionalEventCategory) {
				MONITUS.community.ga.track_event(pTrackerObject, MONITUS.community.utilities.non_null_string(pOptionalEventCategory, "Add to Cart"), MONITUS.community.utilities.non_null_string(pOptionalEventAction, MONITUS.community.yahoo.page_id()), MONITUS.community.utilities.non_null_string(pOptionalEventLabel));
				return true;
			},
			track_click_to_enlarge: function(pTrackerObject, pOptionalEventAction, pOptionalEventLabel, pOptionalEventCategory) {
				MONITUS.community.ga.track_event(pTrackerObject, MONITUS.community.utilities.non_null_string(pOptionalEventCategory, "Click to enlarge"), MONITUS.community.utilities.non_null_string(pOptionalEventAction, "clicked"), MONITUS.community.utilities.non_null_string(pOptionalEventLabel, MONITUS.community.yahoo.page_id()));
				return true;
			},
			track_field_focus: function(pEvent, pField) {
				MONITUS.community.ga.track_event(MONITUS._interactionTracker, MONITUS.community.utilities.make_readable(pField.id), "Focused", MONITUS.community.yahoo.page_id());
			},
			track_field_blur: function(pEvent, pObject) {
				MONITUS.community.ga.track_event(MONITUS._interactionTracker, MONITUS.community.utilities.make_readable(pField.id), "Unfocused",  MONITUS.community.yahoo.page_id());
			},
			track_field_interactions: function(pTrackerObject) {
				try {
					MONITUS._interactionTracker = pTrackerObject;
					YAHOO.util.Event.addListener(window, "load", MONITUS.community.yahoo.install_field_interaction_listeners);
				} catch(e) { }
			},
			install_field_interaction_listeners: function() {
				try {
					var vForm = document.forms['CheckoutForm'];
					for(var vLoop = 0; vLoop < vForm.elements.length; vLoop++) {
						YAHOO.util.Event.addListener(vForm.elements[vLoop], "focus", MONITUS.community.yahoo.track_field_focus, vForm.elements[vLoop]);
						YAHOO.util.Event.addListener(vForm.elements[vLoop], "blur", MONITUS.community.yahoo.track_field_blur, vForm.elements[vLoop]);
					}
				} catch(e) { }
			}
		}
	}
};

