' + ''; while (root.firstChild) { document.body.appendChild(root.firstChild); } var panel = $('ct-panel'), scrim = $('ct-scrim'), tab = $('ct-tab'); var open = function () { panel.classList.add('on'); scrim.classList.add('on'); panel.setAttribute('aria-hidden', 'false'); tab.setAttribute('aria-expanded', 'true'); }; var close = function () { panel.classList.remove('on'); scrim.classList.remove('on'); panel.setAttribute('aria-hidden', 'true'); tab.setAttribute('aria-expanded', 'false'); }; tab.onclick = open; $('ct-close').onclick = close; scrim.onclick = close; document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && panel.classList.contains('on')) { close(); } }); var head = menu.querySelector('.texto_variacao'); if (head) { var lk = document.createElement('button'); lk.type = 'button'; lk.className = 'ct-link'; lk.textContent = 'Qual \u00e9 o meu tamanho?'; lk.onclick = open; head.appendChild(lk); } var note = function (t) { return t; }; var ctx = function () { $('ct-ctx').textContent = state.tab === 'cal' ? (brandRaw ? 'Marca: ' + brandRaw + '. ' : '') + B.note : 'A numera\u00e7\u00e3o do suti\u00e3 pode variar um pouco de marca para marca.'; }; var setTab = function (t) { state.tab = t; $('ct-tCal').setAttribute('aria-selected', t === 'cal'); $('ct-tSut').setAttribute('aria-selected', t === 'sut'); $('ct-vCal').hidden = t !== 'cal'; $('ct-vSut').hidden = t !== 'sut'; ctx(); render(); }; $('ct-tCal').onclick = function () { setTab('cal'); }; $('ct-tSut').onclick = function () { setTab('sut'); }; var drawMan = function () { var c = $('ct-man'); c.innerHTML = ''; MANS.forEach(function (m) { var b = document.createElement('button'); b.type = 'button'; b.className = 'ct-chip'; b.textContent = m; b.setAttribute('aria-pressed', !state.noMan && state.man === m ? 'true' : 'false'); b.onclick = function () { state.man = m; state.noMan = false; $('ct-hw').hidden = true; $('ct-noMan').setAttribute('aria-pressed', 'false'); drawMan(); render(); }; c.appendChild(b); }); }; $('ct-noMan').onclick = function () { state.noMan = !state.noMan; $('ct-noMan').setAttribute('aria-pressed', state.noMan); $('ct-hw').hidden = !state.noMan; if (state.noMan) { state.man = null; } drawMan(); render(); }; [].forEach.call($('ct-fit').children, function (c) { c.onclick = function () { state.fit = +c.getAttribute('data-v'); [].forEach.call($('ct-fit').children, function (x) { x.setAttribute('aria-pressed', x === c ? 'true' : 'false'); }); render(); }; }); $('ct-alt').oninput = function () { $('ct-altV').textContent = (this.value / 100).toFixed(2).replace('.', ',') + ' m'; render(); }; $('ct-peso').oninput = function () { $('ct-pesoV').textContent = this.value + ' kg'; render(); }; var manFromHW = function () { var h = $('ct-alt').value / 100, w = +$('ct-peso').value, bmi = w / (h * h); return bmi < 20 ? 38 : bmi < 22 ? 40 : bmi < 24 ? 42 : bmi < 26 ? 44 : bmi < 28 ? 46 : bmi < 31 ? 48 : bmi < 34 ? 50 : 52; }; var pickBrandSize = function (hip) { var rows = B.s, idx = -1, i, out = false; for (i = 0; i < rows.length; i++) { if (hip >= rows[i][1] && hip <= rows[i][2]) { idx = i; } } if (idx < 0) { if (hip < rows[0][1]) { idx = 0; } else if (hip > rows[rows.length - 1][2]) { idx = rows.length - 1; out = true; } else { for (i = 0; i < rows.length - 1; i++) { if (hip > rows[i][2] && hip < rows[i + 1][1]) { idx = i + 1; } } } } return { idx: idx, out: out }; }; var usable = function (label) { var o = findOpt(label); return !!(o && !o.off); }; var showPanty = function (idx) { var rows = B.s, s = rows[idx][0], st = $('ct-stockMsg'); state.rec = null; $('ct-big').textContent = s; if (usable(s)) { st.className = 'ct-stock ok'; st.textContent = 'Dispon\u00edvel neste produto.'; state.rec = s; $('ct-apply').hidden = false; return; } var alt = null, d, j; for (d = 1; d < rows.length && !alt; d++) { [idx + d, idx - d].forEach(function (k) { if (!alt && rows[k] && usable(rows[k][0])) { alt = rows[k][0]; } }); } st.className = 'ct-stock warn'; var exists = !!findOpt(s); st.textContent = (exists ? 'O ' + s + ' est\u00e1 esgotado agora.' : 'O ' + s + ' n\u00e3o \u00e9 vendido neste produto.') + (alt ? ' O mais parecido que temos \u00e9 o ' + alt + '.' : ''); if (alt) { state.rec = alt; $('ct-big').textContent = alt; $('ct-apply').hidden = false; } else { $('ct-apply').hidden = true; } }; var grid = function (id, items, cb) { var g = $(id); g.innerHTML = ''; items.forEach(function (it) { var b = document.createElement('button'); b.type = 'button'; b.className = 'ct-opt'; b.textContent = it.label; b.onclick = function () { cb(it.value); }; g.appendChild(b); }); }; var go = function (step) { state.hist.push(step); state.done = false; render(); }; grid('ct-numgrid', NUMS.map(function (n) { return { label: String(n), value: n }; }), function (v) { state.num = v; go('letter'); }); grid('ct-lettergrid', COPAS.map(function (c) { return { label: c, value: c }; }).concat([{ label: 'N\u00e3o lembro', value: null }]), function (v) { state.letter = v; go('band'); }); grid('ct-teegrid', ['P', 'M', 'G', 'GG'].map(function (t) { return { label: t, value: t }; }), function (v) { state.tee = v; go('band'); }); [].forEach.call(document.querySelectorAll('#ct-vSut [data-s="know"] .ct-opt'), function (b) { b.onclick = function () { state.know = b.getAttribute('data-a'); go(state.know === 'yes' ? 'num' : 'tee'); }; }); [].forEach.call(document.querySelectorAll('#ct-vSut [data-s="band"] .ct-opt'), function (b) { b.onclick = function () { state.band = b.getAttribute('data-a'); go('chest'); }; }); [].forEach.call(document.querySelectorAll('#ct-vSut [data-s="chest"] .ct-opt'), function (b) { b.onclick = function () { state.chest = b.getAttribute('data-a'); state.done = true; render(); }; }); $('ct-back').onclick = function () { if (state.done) { state.done = false; } else if (state.hist.length > 1) { state.hist.pop(); } render(); }; $('ct-redo').onclick = function () { state.hist = ['know']; state.done = false; state.know = state.num = state.letter = state.tee = state.band = state.chest = null; render(); }; var braResult = function () { var num, letter, why, tips = []; if (state.know === 'yes') { num = state.num; letter = state.letter || 'B'; why = 'do suti\u00e3 que voc\u00ea usa hoje'; } else { num = { P: 42, M: 44, G: 46, GG: 48 }[state.tee]; letter = 'B'; why = 'do tamanho da sua camiseta'; } if (state.band === 'tight') { num = Math.min(50, num + 2); tips.push('Como a faixa aperta, subimos um n\u00famero.'); } if (state.band === 'loose') { num = Math.max(40, num - 2); tips.push('Como a faixa fica frouxa, descemos um n\u00famero.'); } if (state.chest === 'cupfull') { letter = COPAS[Math.min(3, COPAS.indexOf(letter) + 1)]; tips.push('Como o peito aperta, subimos a letra.'); } if (state.chest === 'cupempty') { letter = COPAS[Math.max(0, COPAS.indexOf(letter) - 1)]; tips.push('Como sobra espa\u00e7o, descemos a letra.'); } return { num: num, letter: letter, why: why, tips: tips }; }; function render() { var res = $('ct-res'); $('ct-explain').hidden = true; $('ct-redo').hidden = true; if (state.tab === 'cal') { var man = state.noMan ? manFromHW() : state.man; if (man == null) { res.hidden = true; return; } var r = pickBrandSize(HIP[man]), rows = B.s; var idx = Math.max(0, Math.min(rows.length - 1, r.idx + state.fit)); var f = state.fit === 1 ? ', para ficar solta' : state.fit === -1 ? ', para ficar bem ajustada' : ''; $('ct-subSize').textContent = 'Na ' + B.name + ', pelo ' + (state.noMan ? 'altura e peso que voc\u00ea informou' : 'seu n\u00famero ' + man) + f + '.'; showPanty(idx); if (r.out) { $('ct-stockMsg').className = 'ct-stock warn'; $('ct-stockMsg').textContent = 'Seu n\u00famero passa da tabela desta marca. Fale com a gente para ver outra op\u00e7\u00e3o.'; } res.hidden = false; return; } var cur = state.hist[state.hist.length - 1]; var steps = { know: 1, num: 1, tee: 1, letter: 1, band: 2, chest: 3 }; [].forEach.call(document.querySelectorAll('#ct-vSut [data-s]'), function (el) { el.hidden = state.done || el.getAttribute('data-s') !== cur; }); [].forEach.call($('ct-dots').children, function (d, i) { d.className = (state.done || i < steps[cur]) ? 'on' : ''; }); $('ct-back').hidden = !(state.done || state.hist.length > 1); if (!state.done) { res.hidden = true; return; } var b = braResult(), label = b.num + b.letter; $('ct-big').textContent = label; $('ct-subSize').textContent = 'Calculado a partir ' + b.why + '.'; var o = findOpt(label) || findOpt(String(b.num)), st = $('ct-stockMsg'); if (o && !o.off) { st.className = 'ct-stock ok'; st.textContent = (b.tips.join(' ') + ' Dispon\u00edvel neste produto.').trim(); state.rec = o.label; $('ct-apply').hidden = false; if (o.label !== label) { st.textContent += ' Neste produto o tamanho \u00e9 s\u00f3 o n\u00famero (' + b.num + '); a letra ' + b.letter + ' \u00e9 a do bojo do modelo: confira na descri\u00e7\u00e3o.'; } } else { st.className = 'ct-stock warn'; st.textContent = (b.tips.join(' ') + (o ? ' Esse tamanho est\u00e1 esgotado agora.' : ' Confira o n\u00famero e a letra entre os tamanhos do produto.')).trim(); state.rec = null; $('ct-apply').hidden = true; } $('ct-explain').hidden = false; $('ct-explain').textContent = 'O n\u00famero (' + b.num + ') \u00e9 a faixa que passa embaixo do peito e fecha nas costas. A letra (' + b.letter + ') \u00e9 o tamanho do bojo, a parte que cobre o peito.'; $('ct-redo').hidden = false; res.hidden = false; } $('ct-apply').onclick = function () { if (state.rec && pickSite(state.rec)) { close(); } }; drawMan(); setTab(state.tab); }); })();