/**
 * This Service Class is concerned with interacting with the personalization service 
 * in order to collect and display personalized data for a given user on a page.
 * @author Andrew Southwick
 */
var PersonalizationService = Class.create();
PersonalizationService.prototype = {
	initialize: function() {
	},
	constants: {
		PERSONALIZATION_SERVICE_PATH:"/resources/personalization/v1",
		STRING_TRUE:"true",
		SITE_EXPRESSION_PREFIX:"^https?://[^/]*(",
		LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_SUFFIX:"|gap.com|gap.eu|gapcanada.ca|bananarepublic.ca|oldnavy.ca|thegap.co.uk|bananarepublic.co.uk|bananarepublic.com|oldnavy.com|piperlime.com|athleta.com)",
		LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_SUFFIX:"|gidgol.com|gidgol.ca|gidgol.co.uk|gidapps.com|gidapps.ca|gidapps.eu|gidbrol.com|gidonol.com|gidplol.com|gidatol.com)",
		templates: {
			ANONYMOUS_USER_ACCOUNT_LINK_TEMPLATE:null,
			ANONYMOUS_USER_ACCOUNT_LINK_TEMPLATE_TEXT:"<div class=\"unknownUser\"><a class=\"alt\" href=\"/profile/account_summary.do?locale=#{userContext.localeCode}\">#{resourceBundleValues.profile.yourAccount}</a></div>",
			RECOGNIZED_USER_ACCOUNT_LINK_TEMPLATE:null,
			RECOGNIZED_USER_ACCOUNT_LINK_TEMPLATE_TEXT:"<div class=\"knownUser\"><a class=\"knownUser\" href=\"/profile/account_summary.do?locale=#{userContext.localeCode}\">#{customerAccount.firstName}#{resourceBundleValues.profile.personalizedMyAccount}</a></div>",
			LOGGED_IN_USER_ACCOUNT_LINK_TEMPLATE:null,
			LOGGED_IN_USER_ACCOUNT_LINK_TEMPLATE_TEXT:"<div class=\"knownUser\"><a class=\"knownUser\" href=\"/profile/account_summary.do?locale=#{userContext.localeCode}\">#{customerAccount.firstName}#{resourceBundleValues.profile.personalizedMyAccount}</a></div>",
			ANONYMOUS_USER_SIGN_IN_LINK_TEMPLATE:null,
			ANONYMOUS_USER_SIGN_IN_LINK_TEMPLATE_TEXT:"| <a href=\"/profile/sign_in.do\" onclick=\"return siteNavigation.goSignIn(this.href)\">#{resourceBundleValues.profile.signIn}</a>",
			RECOGNIZED_USER_SIGN_OUT_LINK_TEMPLATE:null,
			RECOGNIZED_USER_SIGN_OUT_LINK_TEMPLATE_TEXT:"| <a href=\"/profile/sign_out.do\" onclick=\"return siteNavigation.goSignOut(this.href)\">#{resourceBundleValues.profile.isRecognizedUser}</a>",
			LOGGED_IN_USER_SIGN_OUT_LINK_TEMPLATE:null,
			LOGGED_IN_USER_SIGN_OUT_LINK_TEMPLATE_TEXT:"| <a href=\"/profile/sign_out.do\" onclick=\"return siteNavigation.goSignOut(this.href)\">#{resourceBundleValues.profile.signOut}</a>",
			ORDER_HISTORY_LINK_TEMPLATE:null,
			ORDER_HISTORY_LINK_TEMPLATE_TEXT:"<a href=\"/profile/order_history.do?isNav=true&locale=#{userContext.localeCode}\">#{resourceBundleValues.profile.orderStatus}</a>",
			GLOBAL_SHIPPING_LINK_TEMPLATE:null,
			GLOBAL_SHIPPING_LINK_TEMPLATE_TEXT:"| <a id=\"showShippingTo\" href=\"#\" onclick=\"return siteNavigation.showShippingOptInOverlay()\">#{resourceBundleValues.profile.shippingTo}: <img id=\"shippingToCountryFlag\" alt=\"#{userContext.globalShippingCountryCode}\" title=\"#{userContext.globalShippingCountryName}\" src=\"/gid/assets/profile/internationalShipping/icons/#{userContext.globalShippingCountryCode}.gif\"/></a>",
			INLINE_BAG_SUMMARY_LINK_TEMPLATE:null,
			INLINE_BAG_SUMMARY_LINK_TEMPLATE_TEXT:"#{shoppingBag.totalItemCount} #{evaluatedMessages.inlineBagQuantityMessage} <a href=\"/buy/shopping_bag.do\">#{resourceBundleValues.inLineBag.yourBag}</a>",
			LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_TEMPLATE:null,
			LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_TEMPLATE_TEXT:"^https?://[^/]*(#{brandUrl}|gap.com|gap.eu|gapcanada.ca|bananarepublic.ca|oldnavy.ca|thegap.co.uk|bananarepublic.co.uk|bananarepublic.com|oldnavy.com|piperlime.com|athleta.com)",
			LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_TEMPLATE:null,
			LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_TEMPLATE_TEXT:"^https?://[^/]*(gidgol.com|gidgol.ca|gidgol.co.uk|gidgol.eu|gidapps.com|gidapps.ca|gidapps.eu|gidbrol.com|gidonol.com|gidplol.com|gidatol.com)"
		},
		elementIds:{
			SIGN_IN_YOUR_ACCOUNT:"signInYourAccount",
			SIGN_IN_SIGN_IN:"signInSignIn",
			SIGN_IN_ORDER_STATUS:"signInOrderStatus",
			SHIPPING_TO:"shippingTo",
			INLINE_BAG_SUMMARY:"inlineBagTopPriceLayer"
		},
		requestParameters:{
			SD_REFERER_PARAMETER:"sdReferer",
			TID_PARAMETER:"tid",
			AP_PARAMETER:"ap",
			SEM_PARAMETER:"sem",
			REFERRER_PARAMETER:"referrer",
			MKWID_PARAMETER:"mkwid",
			CREATIVE_PARAMETER:"creative",
			AKAMAI_COUNTRY_CODE_PARAMETER:"akamaiCountryCode",
			CID_PARAMETER:"cid",
			SIZE_CATEGORY_IDS_PARAMETER:"sizeCategoryIds",
			ORIGIN : "origin"
		}
	},
	constructors: {
	},
	api: {

	},
	model: {
		isDomReady:false,
		isPersonalizationDataReady:false,
		personalizationData:null,
		globalShipping:{
			isGlobalShippingActive:false
		},
		origin : null
	},
	controller: {
		init: {
			main: function(origin) {
				personalizationService.model.origin = origin;
				personalizationService.controller.init.setConstants();
				var domLoadedHandler = personalizationService.controller.handlers.domLoadedHandler;
				Event.observe(document, "dom:loaded", domLoadedHandler);
				var getPersonalizationData =
					personalizationService.controller.getPersonalizationData.bind(personalizationService);
				getPersonalizationData();
			},
			setConstants:function() {
				var constants = personalizationService.constants;
				var resourceBundlePersonalizedMyAccount = resourceBundleValues.profile.personalizedMyAccount;
				var resourceBundleYourAccount = resourceBundleValues.profile.yourAccount;
				constants.templates.ANONYMOUS_USER_ACCOUNT_LINK_TEMPLATE = new Template(constants.templates.ANONYMOUS_USER_ACCOUNT_LINK_TEMPLATE_TEXT);
				constants.templates.RECOGNIZED_USER_ACCOUNT_LINK_TEMPLATE = new Template(constants.templates.RECOGNIZED_USER_ACCOUNT_LINK_TEMPLATE_TEXT);
				constants.templates.LOGGED_IN_USER_ACCOUNT_LINK_TEMPLATE = new Template(constants.templates.LOGGED_IN_USER_ACCOUNT_LINK_TEMPLATE_TEXT);
				
				constants.templates.ANONYMOUS_USER_SIGN_IN_LINK_TEMPLATE = new Template(constants.templates.ANONYMOUS_USER_SIGN_IN_LINK_TEMPLATE_TEXT);
				constants.templates.RECOGNIZED_USER_SIGN_OUT_LINK_TEMPLATE = new Template(constants.templates.RECOGNIZED_USER_SIGN_OUT_LINK_TEMPLATE_TEXT);
				constants.templates.LOGGED_IN_USER_SIGN_OUT_LINK_TEMPLATE = new Template(constants.templates.LOGGED_IN_USER_SIGN_OUT_LINK_TEMPLATE_TEXT);
				
				constants.templates.ORDER_HISTORY_LINK_TEMPLATE = new Template(constants.templates.ORDER_HISTORY_LINK_TEMPLATE_TEXT);
				constants.templates.GLOBAL_SHIPPING_LINK_TEMPLATE = new Template(constants.templates.GLOBAL_SHIPPING_LINK_TEMPLATE_TEXT);
				constants.templates.INLINE_BAG_SUMMARY_LINK_TEMPLATE = new Template(constants.templates.INLINE_BAG_SUMMARY_LINK_TEMPLATE_TEXT);
				
				constants.templates.LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_TEMPLATE = new Template(constants.templates.LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_TEMPLATE_TEXT);
				constants.templates.LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_TEMPLATE = new Template(constants.templates.LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_TEMPLATE_TEXT);
			}
		},
		handlers: {
			domLoadedHandler: function() {
				personalizationService.model.isDomReady = true;
			}
		},
		getPersonalizationData:function() {
			document.fire("personalizationDataRequest:begin");
			var personalizationServiceUrl = this.controller.getPersonalizationServiceUrl();
			new Ajax.Request(
					personalizationServiceUrl,
				{
					method:'get',
					onSuccess:personalizationService.controller.personalizationDataResponseHandlers.successHandler,
					onException:personalizationService.controller.personalizationDataResponseHandlers.exceptionHandler
				}
			);
		},
		getPersonalizationServiceUrl:function() {
			var PERSONALIZATION_SERVICE_PATH = personalizationService.constants.PERSONALIZATION_SERVICE_PATH;
			var requestProtocol = null;
			var requestHost = window.location.host;
			if (window.location.protocol == "http:") {
				requestProtocol = "http:";
			} else {
				requestProtocol = "https:";
			}
			var shopperId = personalizationService.controller.getShopperId();
			var personalizationServiceUrl = requestProtocol + "//" + requestHost + PERSONALIZATION_SERVICE_PATH + "/" + shopperId;
			var queryParametersUrlFormatted = personalizationService.controller.getQueryParametersUrlFormatted();
			if (queryParametersUrlFormatted != null) {
				personalizationServiceUrl = personalizationServiceUrl + "?" + queryParametersUrlFormatted;
			}
			return personalizationServiceUrl;
		},
		getQueryParametersUrlFormatted:function() {
			var constants = personalizationService.constants;
			var requestParameters = constants.requestParameters;
			var model = personalizationService.model;
			var queryParametersUrlFormatted = null;
			var queryParameters = [];
			var isSdReferer = false;
			var constants = personalizationService.constants;
			var STRING_TRUE = constants.STRING_TRUE;
			/* sdReferer */
			var SD_REFERER_PARAMETER = requestParameters.SD_REFERER_PARAMETER;
			var sdRefererParameter = gidLib.getQuerystringParam(SD_REFERER_PARAMETER, true);
			if (sdRefererParameter != null && sdRefererParameter != "") {
				var sdRefererParameterEncoded = encodeURIComponent(sdRefererParameter);
				queryParameters.push(SD_REFERER_PARAMETER + "=" + sdRefererParameterEncoded);
				isSdReferer = true;
			}
			/* referrer */
			var REFERRER_PARAMETER = requestParameters.REFERRER_PARAMETER;
			var isReferrerAThirdPartySite = personalizationService.controller.isReferrerAThirdPartySite();
			if (!isSdReferer && isReferrerAThirdPartySite) {
				var referrerEncoded = encodeURIComponent(model.referrer);
				queryParameters.push(REFERRER_PARAMETER + "=" + referrerEncoded);
			}
			/* tid */
			var TID_PARAMETER = requestParameters.TID_PARAMETER;
			var tidQueryParameter = gidLib.getQuerystringParam(TID_PARAMETER);
			if (tidQueryParameter != null && tidQueryParameter != "") {
				var tidQueryParameterEncoded = encodeURIComponent(tidQueryParameter);
				queryParameters.push(TID_PARAMETER + "=" + tidQueryParameterEncoded);
			}
			/* ap */
			var AP_PARAMETER = requestParameters.AP_PARAMETER;
			var apQueryParameter = gidLib.getQuerystringParam(AP_PARAMETER);
			if (apQueryParameter != null && apQueryParameter != "") {
				var apQueryParameterEncoded = encodeURIComponent(apQueryParameter);
				queryParameters.push(AP_PARAMETER + "=" + apQueryParameterEncoded);
			}
			/* mkwid */
			var MKWID_PARAMETER = requestParameters.MKWID_PARAMETER;
			var mkwidQueryParameter = gidLib.getQuerystringParam(MKWID_PARAMETER);
			if (mkwidQueryParameter != null && mkwidQueryParameter != "") {
				var mkwidQueryParameterEncoded = encodeURIComponent(mkwidQueryParameter);
				queryParameters.push(MKWID_PARAMETER + "=" + mkwidQueryParameterEncoded);
			}
			/* creative */
			var CREATIVE_PARAMETER = requestParameters.CREATIVE_PARAMETER;
			var creativeQueryParameter = gidLib.getQuerystringParam(CREATIVE_PARAMETER);
			if (creativeQueryParameter != null && creativeQueryParameter != "") {
				var creativeQueryParameterEncoded = encodeURIComponent(creativeQueryParameter);
				queryParameters.push(CREATIVE_PARAMETER + "=" + creativeQueryParameterEncoded);
			}
			/* sem */
			var SEM_PARAMETER = requestParameters.SEM_PARAMETER;
			var semParameter = gidLib.getQuerystringParam(SEM_PARAMETER, true);
			if (semParameter == "true") {
				var semParameterEncoded = encodeURIComponent(semParameter);
				queryParameters.push(SEM_PARAMETER + "=" + semParameterEncoded);
			} else if (isReferrerAThirdPartySite){
				queryParameters.push(SEM_PARAMETER + "=" + STRING_TRUE);
			}
			/* akamaiCountryCode */
			var AKAMAI_COUNTRY_CODE_PARAMETER = requestParameters.AKAMAI_COUNTRY_CODE_PARAMETER;
			var akamaiCountryCodeParameter = gidLib.getQuerystringParam(AKAMAI_COUNTRY_CODE_PARAMETER, true);
			if (akamaiCountryCodeParameter != null && akamaiCountryCodeParameter != "") {
				var akamaiCountryCodeParameterEncoded = encodeURIComponent(akamaiCountryCodeParameter);
				queryParameters.push(AKAMAI_COUNTRY_CODE_PARAMETER + "=" + akamaiCountryCodeParameterEncoded);
			}
			/* cid */
			if (window["categoryInfo"]) {
				var CID_PARAMETER = requestParameters.CID_PARAMETER;
				var cidParameter = categoryInfo.businessCatalogItemId;
				var cidParameterEncoded = encodeURIComponent(cidParameter);
				queryParameters.push(CID_PARAMETER + "=" + cidParameterEncoded);
			}
			/* sizeCategoryIds */
			if (window["categoryInfo"]) {
				if (categoryInfo.isShopBySize) {
					var SIZE_CATEGORY_IDS_PARAMETER = requestParameters.SIZE_CATEGORY_IDS_PARAMETER;
					var sizeCategoryIds = categoryInfo.sizeCategoryIds;
					var sizeCategoryIdsEncoded = encodeURIComponent(sizeCategoryIds);
					queryParameters.push(SIZE_CATEGORY_IDS_PARAMETER + "=" + sizeCategoryIdsEncoded);
				}
			}
			
			if (personalizationService.model.origin) {
				queryParameters.push(requestParameters.ORIGIN + "=" + personalizationService.model.origin);
			}
			if (queryParameters.length > 0) {
				queryParametersUrlFormatted = queryParameters.join("&");
			}
			return queryParametersUrlFormatted;
		},
		getShopperId:function() {
			var shopperId = (getCookie("unknownShopperId") + "").replace(/\|\|\|/,"");
			if (shopperId == "" || shopperId == "0") {
				shopperId = "initialize";
			}
			var shopperIdEncoded = encodeURIComponent(shopperId);
			return shopperIdEncoded;
		},
		personalizationDataResponseHandlers:{
			successHandler:function(response) {
				personalizationService.model.personalizationData = response.responseJSON;
				personalizationService.model.isPersonalizationDataReady = true;
				document.fire("personalizationDataRequest:end");
				document.fire("personalizationData:ready");
				var isDomReady = personalizationService.model.isDomReady;
				var updateDomWithPersonalizationData = personalizationService.view.updateDomWithPersonalizationData;
				if (isDomReady) {
					/* Update DOM with Personalization data */
					updateDomWithPersonalizationData();
				} else {
					/* Wait for DOM event to fire */
					Event.observe(document, "dom:loaded", updateDomWithPersonalizationData);
				}
			},
			exceptionHandler:function(response, e) {
				document.fire("personalizationDataRequest:end");
				document.fire("personalizationDataRequest:exception");
				throw(e);
			}
		},
		isReferrerAThirdPartySite:function() {
			var isReferrerAThirdPartySite = false;
			var isReferrerALocalSite = false;
			var isTestEnvironment = false;
			var referrer = document.referrer;
			var brandUrl = brandConst.BRAND_URL;
			var constants = personalizationService.constants;
			var model = personalizationService.model;
			
			
			var expressionContract = {"brandUrl":brandUrl};
			var expressionValue = constants.templates.LOCAL_SITE_PROD_ENVIRONMENT_EXPRESSION_TEMPLATE.evaluate(expressionContract);
			var expressionValueForTestEnvironments = constants.templates.LOCAL_SITE_TEST_ENVIRONMENT_EXPRESSION_TEMPLATE.evaluate(expressionContract);
			var urlExpression = new RegExp(expressionValue, "i");
			var urlExpressionForTestEnvironments = new RegExp(expressionValueForTestEnvironments, "i");
			if (referrer != null && referrer != undefined && referrer != "") {
				isReferrerALocalSite = urlExpression.test(referrer);
				if (isReferrerALocalSite == false) {
					// Test to see if the environment is a test environment - using test environment expression.
					isReferrerALocalSite = urlExpressionForTestEnvironments.test(referrer);
					if (isReferrerALocalSite == true) {
						isTestEnvironment = true;
					}
				}
			} else {
				/* Referrer does not exist so default to local */
				isReferrerALocalSite = true;
			}
			if (!isReferrerALocalSite) {
				isReferrerAThirdPartySite = true;
			}
			model.referrer = referrer;
			model.expressionValue = expressionValue;
			model.isReferrerALocalSite = isReferrerALocalSite;
			model.isReferrerATestEnvironment = isTestEnvironment;
			model.isReferrerAThirdPartySite = isReferrerAThirdPartySite;
			return isReferrerAThirdPartySite;
		}
	},
	view:{
		updateDomWithPersonalizationData:function() {
			var personalizationData = personalizationService.model.personalizationData;
			var constants = personalizationService.constants;
			var STRING_TRUE = constants.STRING_TRUE;
			if (personalizationData != undefined && personalizationData != null) {
				var userContext = personalizationData.personalizationInfoV1.userContext;
				var customerAccount = personalizationData.personalizationInfoV1.customerAccount;
				var shoppingBag = personalizationData.personalizationInfoV1.shoppingBag;
				var isAnonymousUser = userContext.isAnonymousUser;
				var isLoggedInUser = userContext.isLoggedInUser;
				var isRecognizedUser = userContext.isRecognizedUser;
				var personalizationDisplayContract = {
					"userContext":userContext,
					"shoppingBag":shoppingBag,
					"customerAccount":customerAccount,
					"resourceBundleValues":resourceBundleValues,
					commonDomElements:{},
					evaluatedMessages:{},
					"personalizationData":personalizationData
				};
				/* Get elements to update */
				with (personalizationDisplayContract) {
					commonDomElements.signInYourAccountElementId = constants.elementIds.SIGN_IN_YOUR_ACCOUNT;
					commonDomElements.signInYourAccountElement = $(commonDomElements.signInYourAccountElementId);
					commonDomElements.signInSignInElementId = constants.elementIds.SIGN_IN_SIGN_IN;
					commonDomElements.signInSignInElement = $(commonDomElements.signInSignInElementId);
					commonDomElements.signInOrderStatusElementId = constants.elementIds.SIGN_IN_ORDER_STATUS;
					commonDomElements.signInOrderStatusElement = $(commonDomElements.signInOrderStatusElementId);
					commonDomElements.shippingToElementId = constants.elementIds.SHIPPING_TO;
					commonDomElements.shippingToElement = $(commonDomElements.shippingToElementId);
					commonDomElements.inlineBagSummaryElementId = constants.elementIds.INLINE_BAG_SUMMARY;
					commonDomElements.inlineBagSummaryElement = $(commonDomElements.inlineBagSummaryElementId);
				}

				var signInYourAccountElement = personalizationDisplayContract.commonDomElements.signInYourAccountElement;
				var hasStandardDomElementsToUpdate = false;
				if (signInYourAccountElement != null && signInYourAccountElement != undefined) {
					hasStandardDomElementsToUpdate = true;
				}
				if (hasStandardDomElementsToUpdate) {
					/* Update elements based on state */
					if (isAnonymousUser == STRING_TRUE) {
						personalizationService.view.anonymousUserDomDisplayHelper(personalizationDisplayContract);
					} else if (isRecognizedUser == STRING_TRUE) {
						personalizationService.view.recognizedUserDomDisplayHelper(personalizationDisplayContract);
					} else if (isLoggedInUser == STRING_TRUE) {
						personalizationService.view.loggedInUserDomDisplayHelper(personalizationDisplayContract);
					}
					personalizationService.view.allUsersDomDisplayHelper(personalizationDisplayContract);
				}
			}
		},
		anonymousUserDomDisplayHelper:function(personalizationDisplayContract) {
			var constants = personalizationService.constants;
			/* signInYourAccountElement */
			var signInYourAccountContentXhtml = constants.templates.ANONYMOUS_USER_ACCOUNT_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInYourAccountElement.update(signInYourAccountContentXhtml);
			/* signInSignInElement */
			var signInSignInContentXhtml = constants.templates.ANONYMOUS_USER_SIGN_IN_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInSignInElement.update(signInSignInContentXhtml);
		},
		recognizedUserDomDisplayHelper:function(personalizationDisplayContract) {
			var constants = personalizationService.constants;
			/* signInYourAccountElement */
			var signInYourAccountContentXhtml = constants.templates.RECOGNIZED_USER_ACCOUNT_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInYourAccountElement.update(signInYourAccountContentXhtml);
			/* signInSignInElement */
			var signInSignInContentXhtml = constants.templates.RECOGNIZED_USER_SIGN_OUT_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInSignInElement.update(signInSignInContentXhtml);
		},
		loggedInUserDomDisplayHelper:function(personalizationDisplayContract) {
			var constants = personalizationService.constants;
			/* signInYourAccountElement */
			var signInYourAccountContentXhtml = constants.templates.LOGGED_IN_USER_ACCOUNT_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInYourAccountElement.update(signInYourAccountContentXhtml);
			/* signInSignInElement */
			var signInSignInContentXhtml = constants.templates.LOGGED_IN_USER_SIGN_OUT_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			personalizationDisplayContract.commonDomElements.signInSignInElement.update(signInSignInContentXhtml);
		},
		allUsersDomDisplayHelper:function(personalizationDisplayContract) {
			var constants = personalizationService.constants;
			var commonDomElements = personalizationDisplayContract.commonDomElements;
			var evaluatedMessages = personalizationDisplayContract.evaluatedMessages;
			var shoppingBag = personalizationDisplayContract.shoppingBag;
			/* signInOrderStatusElement */
			var signInOrderStatusContentXhtml = constants.templates.ORDER_HISTORY_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
			commonDomElements.signInOrderStatusElement.update(signInOrderStatusContentXhtml);
			/* shippingToElement */
			var shippingToElement = commonDomElements.shippingToElement;
			if (shippingToElement != null) {
				/* shippingToElement may be null if globalShipping is not enabled. */
				var shippingToContentXhtml = constants.templates.GLOBAL_SHIPPING_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
				shippingToElement.update(shippingToContentXhtml);
			}
			/* inlineBagSummaryElement */
			/* This element may be null on the shopping bag page */
			var inlineBagSummaryElement = commonDomElements.inlineBagSummaryElement;
			if (inlineBagSummaryElement != null) {
					
				if (shoppingBag.totalItemCount == "1") {
					evaluatedMessages.inlineBagQuantityMessage = resourceBundleValues.inLineBag.inlineBagItemSingular;
				} else {
					evaluatedMessages.inlineBagQuantityMessage = resourceBundleValues.inLineBag.inlineBagItemPlural;
				}
				var inlineBagSummaryContentXhtml = constants.templates.INLINE_BAG_SUMMARY_LINK_TEMPLATE.evaluate(personalizationDisplayContract);
				inlineBagSummaryElement.update(inlineBagSummaryContentXhtml);
			}
		}
	}
}

var personalizationService = new PersonalizationService();
