




//<!--
/* <![CDATA[ */

/*
 * ID:EXTERNAL_WIDGET_FITANALYTICS_CORE
 */


jQuery(document).bind("track:onview", function(event) {

	var reportPurchaseURL = "//collector.fitanalytics.com/report_purchase.js";

	// report confirmation page
	var reportPurchase = function() {
		// get country and language from pagecontext
		var shopCountry = (PageContext.get('global.constants.locale.country') == "")?"DE":PageContext.get('global.constants.locale.country');
		var language = (PageContext.get('global.constants.locale.language') == "")?"de":PageContext.get('global.constants.locale.language');

		jQuery.each(PageContext.get('order.allProductLineItems'),function(key,pli) {
			var jsonProduct = new Object();
			jsonProduct.productId = PageContext.get('global.constants.brand.name') + "-" + pli.masterId;
			jsonProduct.orderId = PageContext.get('order.orderNo');
			jsonProduct.userId = PageContext.get('customer.id');
			jsonProduct.purchasedSize = pli.size;
			jsonProduct.price = pli.price.value;
			jsonProduct.currency = pli.price.currencyCode;
			jsonProduct.shopCountry = shopCountry;
			jsonProduct.language = language;
			jsonProduct.hostname = '';
			var hostnameSuffix = '';
			if (PageContext.get('global.instanceType') != 'prd') {
				hostnameSuffix = '-test';
			}

			/**
			 * function for app (Android, iOS)
			 * check for iOS
			 */
			function isIOS() {
				if (window.navigator.userAgent.indexOf("soliiosapp") != -1) {
					return true;
				}
				return false;
			};

			/**
			 * function for app (Android, iOS)
			 * check for Android
			 */
			function isAndroid() {
				if (window.navigator.userAgent.indexOf("soliandroidapp") != -1) {
					return true;
				}
				return false;
			};

			if(isAndroid()) {
				jsonProduct.hostname = "embed-android" + hostnameSuffix;
			} else if(isIOS()) {
				jsonProduct.hostname = "embed-ios" + hostnameSuffix;
			}
			_sendPurchaseInformation(jsonProduct);
		});
	}

	if (PageContext.get('global.pageType') == PageContext.get('global.availablePages.coconfirmation')) {
		CoreUtils.loadExternalScript(reportPurchaseURL, function(){return true;}, reportPurchase);
	}

});

/* ]]> */
// -->


//<!--
/* <![CDATA[ */

jQuery(document).bind("track:onview", function(event) {
	if (PageContext.get('global.pageType') == PageContext.get('global.availablePages.productdetails') || 
		PageContext.get('global.pageType') == PageContext.get('global.availablePages.productquickview')) {		
		var ordersDay = PageContext.get('product.custom.activeData.ordersDay');
		if (ordersDay != null) {
			
			if(ordersDay >= 7){
				
				var setRender = function(ordersDay){
					var renderParams = {};
					renderParams =	{ ordersDay: ordersDay };
					var jsRenderTemplate = jQuery.templates(jQuery('.jsRenderTemplate[data-jsrender-tpl="JSRENDERTPL_SOCIALPROOF"]').html());
					var socialProofHTML = jsRenderTemplate.render(renderParams);
					jQuery('.jsSocialProofContainer').replaceWith(socialProofHTML);	
				};
						
				setRender(ordersDay);						
			}					
		}
	}
});

/* ]]> */
// -->


//<!--
/* <![CDATA[ */

(function($){

	var _sdkURL = "/d360-sdk.js";
	var _D360PushNotificationActive = false;
	var _deferredNotificationActive = $.Deferred();
	var _deferredTrackOnView = $.Deferred();

	function _trackEvents(event) {
		if(!_D360PushNotificationActive) {
			return;
		}

		var context = event.eventData;
		var globalAvailablePages = PageContext.get('global.availablePages');

		switch (context.global.pageType) {
			// *************
			// productdetails
			// *************
			case globalAvailablePages.productdetails:
			case globalAvailablePages.productquickview:
				var p = context.product;
				var pid = p.id;
				var segment = p.segment;
				var categoryPath = p.categoryPath;
				var pname = p.name;
				var pimg = p.custom.webPushImageURL;
				var purl = event.target.URL;
				var pmasterid = p.masterId;
				var pcolorid = p.colorCode;

				var D360params = ['s.Oliver/Product/' + pid,
					'',
					pid,
					pname,
					{
						'group': categoryPath,
						'mainpath': segment,
						'item_image': pimg,
						'deeplink': purl,
						'item_master_identifier' : pmasterid,
						'item_color_identifier' : pcolorid
					}];

				D360.push('logView', D360params);
				break;

			// add2cart
			case globalAvailablePages.cocartaddproduct:
				var p = context.product;
				var pid = p.id;
				var segment = p.segment;
				var categoryPath = p.categoryPath;
				var pname = p.name;
				var price = parseFloat(p.price.value);
				var currency = p.price.currencyCode;
				var pimg = p.custom.webPushImageURL;
				var purl = event.target.URL;
				var pmasterid = p.masterId;
				var pcolorid = p.colorCode;

				var D360params = [pid,
					pname,
					price,
					currency,
					{
						'group': categoryPath,
						'mainpath': segment,
						'item_image': pimg,
						'deeplink': purl,
						'item_master_identifier' : pmasterid,
						'item_color_identifier' : pcolorid
					}];

				D360.push('logAddToCart', D360params);
				break;

			case globalAvailablePages.coconfirmation:
				var o = context.order;
				var price = parseFloat(o.totalGrossPrice.value);
				var currency = o.totalGrossPrice.currencyCode;
				var orderid = o.orderNo;
				var itemCount = 0;
				for (var pli in o.allProductLineItems) {
					itemCount += o.allProductLineItems[pli].quantity;
				}

				var D360params = [price,
					currency,
					itemCount,
					{
						'order_id': orderid
					}];

				D360.push('logPurchase', D360params);
				break;
			default:
				// nothing to do
				break;
		}
	};

	function _onLoad(data, textStatus, jqXHR) {
		if (textStatus == 'success' || textStatus == 'notmodified') {
			if(typeof(window.D360) != 'undefined') {
				window.D360.push(['onInit', _onInit]);
			}
			return;
		}

		// no luck, initiate cleanup
		AppLog.warn('unable to call 360 dialog sdk');
	};

	function _onInit() {
		if(!window.D360 || !window.D360Push || !window.D360PushState) {
			AppLog.warn('unable to find D360');
			return;
		}
		if(window.D360Push.isPushSupported()) {
			if(window.D360PushState.getActiveSubscription()) {
				// user has already web push notification subscription
				// bypass the soft prompt. go directly to the tracking
				$(document).trigger('d360:requestpush');
			}
			else {
				// control the result of softprompt layer. If answers, save cookie with allow/deny and write timestamp +x time to avoid asking again.

				//AppLog.info("d360:showsoftprompt");
				$(document).trigger('d360:showsoftprompt');
			}
		}
		// else: push notifications not supported. Nothing to do.
	};

	$(document).one('d360:requestpush', function(event) {

		// from a tracking perspective it's o.k. to set it to true here,
		// even if we are (maybe) still waiting for the native confirmation
		//AppLog.info("d360:requestpush");
		_D360PushNotificationActive = true;
		window.D360.push(["requestPushNotification"]);
		_deferredNotificationActive.resolve();

	});

	$(document).one('track:onview', function(event) {
		_deferredTrackOnView.resolve(event);
	});

	$(document).one('page:ready', function(event) {

		var enabled = PageContext.getVariable('global.constants.webpushevents.enabled');
		var appId = null;
		var apiSecret = null;
		if (PageContext.get('global.instanceType') == 'prd') {
			appId = '249';
			apiSecret = 'fcd693a086034d80c3c42af5d0c83ce206431a3bfbf73f078e684f7f66ab9b20';
		} else {
			appId = '250';
			apiSecret = '01ce6af50c31944fc1c37ed5cac9edfaef9520995ff2065a191a58003e8e809e';
		}

		if (!enabled) {
			AppLog.debug('WebPush is disabled.');
			return;
		}

		$.when(_deferredTrackOnView, _deferredNotificationActive).done(function(event) {
			$(document).bind("track:onview", _trackEvents);
			_trackEvents(event);
		});

		window.D360Options = {
			requestPushOnLoad: false,
			disableTrackingWithoutSubscription: false,
			requireInteraction: true,
			appId: appId,
			userId: PageContext.get('customer.hashedMail'),
			apiSecret: apiSecret
		};

		CoreUtils.loadExternalScript(_sdkURL, function(){
			if(typeof(window.D360)!="undefined"){
				_onInit();
				return false;
			}
		}, _onLoad, true, 3000);
	});

})(jQuery);


/* ]]> */
// -->


//<!--
/* <![CDATA[ */

jQuery(document).bind("page:ready", function(event) {

	// check if login modal is enabled
	let loginModalEnabled = PageContext.get('global.constants.loginmodalenabled');
	if (!loginModalEnabled) {
		return;
	}

	// check if current page type is allowed for login modal
	let currentPageType = PageContext.get('global.pageType');
	let avPages = PageContext.get('global.availablePages');
	let notAllowedPages = new Array();

	// add all checkout and myaccount pages to the non allowed pages array
	for (let p in avPages) {
		if (  (p.indexOf('co') == 0 && p.indexOf('contact') == -1)
			|| p.indexOf('myaccount') != -1 ) {
			notAllowedPages.push(avPages[p]);
		}
	}

	if (jQuery.inArray(currentPageType, notAllowedPages) >= 0) {
		return;
	}

	// check if customer is returning
	let customerAuthenticated = PageContext.get('customer.authenticated');
	let customerReturning = PageContext.get('customer.returning');
	if (customerAuthenticated || !customerReturning) {
		return;
	}

	// check if modal was already shown in this session
	let loginModalShown = (CookieUtils.readCookie('loginModalShown') == 1);
	if (loginModalShown) {
		return;
	}
	// create the cookie to control if login modal was already shown
	CookieUtils.createCookie('loginModalShown',1);

	// launch the modal dialog
	let loginModalUrl = PageContext.get('global.constants.url.loginmodal');
	let loginModalOptions = PageContext.get('global.constants.layer.loginmodal');
	ContentUtils.loadDialog(loginModalUrl, loginModalOptions);

});

/* ]]> */
// -->



