Tue Oct 02 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267


/*
This system lets you update mastheads, skus, and bonus offers for all relevant levels on a schedule.
*/




// ********************************************************************************
// SET SCHEDULE AND OFFER TYPES HERE
// ********************************************************************************


var schedule = [
	{
		start_date: '02 Oct 2018 00:00:00',
		end_date: '02 Oct 2018 23:59:59',
		offer_name: 'examplebdi'
	}
];



var offers = {
	'examplebdi': {
		mastheads: {
			'mobile': 'https://www.rosettastone.com/lp/common-modules/assets/modules_2018/tutor-screen.png',
			'desktop': 'https://www.rosettastone.com/lp/common-modules/assets/modules_2018/tutor-screen.png'
		},
		offer_type: 'bonus_months_low'
	}
};



var offer_types = {
	'bonus_months_low':{
		'12': {
			message: '+1 Month Free',
			sku: '12_REPLACEMENT_SKU_HERE'
		},
		'24': {
			message: '+3 Months Free',
			sku: '24_REPLACEMENT_SKU_HERE'
		}
	},
	'bonus_months_high':{
		'12': {
			message: '+3 Months Free',
			sku: '12_REPLACEMENT_SKU_HERE'
		},
		'24': {
			message: '+6 Months Free',
			sku: '24_REPLACEMENT_SKU_HERE'
		}
	}
};






// ********************************************************************************
// RUN OFFERS HERE
//     Either apply one offer directly or run offers on schedule
// ********************************************************************************

apply_offer('examplebdi');
// run_offers_on_schedule(schedule);















// ********************************************************************************
// CORE FUNCTIONALITY
//     No need to edit below here unless you want to change how the offers work
// ********************************************************************************


function run_offers_on_schedule(schedule){
	var date = Date.now();
	for(var i=0; i<schedule.length; i++){
		var start_date = Date.parse(schedule[i].start_date);
		var end_date = Date.parse(schedule[i].end_date);
		if(start_date <= date && date <= end_date){
			apply_offer(schedule[i].offer_name);
			return;
		}
	}
}


function format_offer_message(offer_type, message){
	if((/bonus_months/i).test(offer_type)){}
	return message;
}




function update_masthead(offer){

	if(offer.mastheads){

		var style = document.createElement('style');
		style.id = 'masthead_change';
		var masthead = document.querySelector('#masthead');

		style.innerHTML = ''
			+'body #masthead{'
				+'background-image: url(' + offer.mastheads.mobile + ');'
			+'}'
			+'@media (min-width: 600px) {'
				+'body #masthead{'
					+'background-image: url(' + offer.mastheads.desktop + ');'
				+'}'
			+'}'
		+'';

		masthead.appendChild(style);
	}
}




function _$(selector){
	var elems = Array.prototype.slice.call(document.querySelectorAll(selector));
	return {
		elems: elems,
		attr: function(attr, value){
			elems.forEach(function(elem){
				var original_attr = elem.getAttribute(attr);
				var final_value = typeof value === 'function' ? value(original_attr) : value;
				elem.setAttribute(attr, final_value);
			});
			return this;
		},
		show: function(display_property){
			elems.forEach(function(elem){
				elem.style.display = display_property || 'block';
			});
			return this;
		},
		hide: function(){
			elems.forEach(function(elem){
				elem.style.display = 'none';
			});
			return this;
		},
		html: function(inner_html){
			elems.forEach(function(elem){
				elem.innerHTML = inner_html;
			});
			return this;
		}
	};
}



function apply_offer(offer_name){
	var set_up_offer = (function(offers, offer_name){
		var offer = offers[offer_name];
		update_masthead(offer);

		update_all_cart_buttons(offer);
		show_all_bonuses(offer);
		protect_product_changes_on_lang_change(offer);

		add_bonus_styles();

	}).bind(this, offers);

	set_up_offer(offer_name);
}




function add_bonus_styles(){

	var style = document.createElement('style');
	style.id = 'masthead_change';

	style.innerHTML = ''
		+ '.product-d .header > div.bonus {'
			+ 'font-size: 80%;'
			+ 'font-weight: 100;'
			+ 'position: relative;'
			+ 'bottom: -7px;'
			+ 'display: none;'

			+ 'background-color: #437414;'
			+ 'color: #fff;'
			+ 'padding: 10px;'
			+ 'margin: -30px 20px 0px;'
			+ 'bottom: -41px;'
		+ '}'
	+'';

	document.body.appendChild(style);
}


function show_all_bonuses(offer){
	add_bonus_styles();

	var products = offer_types[offer.offer_type];

	for(var lvl in products){
		show_bonus(lvl, offer);
	}
}


function show_bonus(lvl, offer){
	var message = offer_types[offer.offer_type][lvl].message;
	_$('*[data-lvl="'+lvl+'"] .bonus').html(message).show();
}


function update_all_cart_buttons(offer){
	var products = offer_types[offer.offer_type];
	for(var lvl in products){
		var product = products[lvl];
		update_cart_button(lvl, product.sku);
	}
}


function update_cart_button(lvl, sku){
	_$('*[data-lvl="'+lvl+'"] .add-to-cart').attr('href', function(original_href){
		var new_href = original_href;
		if(sku){
			new_href = original_href.replace(/\/\d+\//, '/'+sku+'/');
		}
		return new_href;
	});
}


function protect_product_changes_on_lang_change(offer){
	window.jQuery && (function($){
		$(document).on('lang_change', function(){
			
			update_all_cart_buttons(offer);
		});
	})(window.jQuery);
}