var atavistStore = {
	root:"read.atavist.com",
	template:"\t<div class=\"atavist atavist-wrapper current-process-choose\">\n\t\t<div class=\"atavist-overlay\">&nbsp;<\/div>\n\t\t<div class=\"atavist-offset allow-touchmove\">\n\t\t\t<div class=\"atavist-sheet\">\n\t\t\t\t<div id=\"atavist-header\">\n\t\t\t\t\t<button class=\"close\" data-dismiss=\"alert\" href=\"#\">\u00d7<\/button>\n\t\t\t\t\t<div class=\"atavist-internal\">\n\t\t\t\t\t\t<div class=\"organization_image\"><\/div>\n\t\t\t\t\t\t<span class=\"atavist-purchase-description\">\n\t\t\t\t\t\t\t<span class=\"purchase-description\">You are purchasing <span class=\"story-title\"><\/span> from <span class=\"story-seller\"><\/span>.<\/span>\n\t\t\t\t\t\t\t<span class=\"purchase-description-gift\">You are purchasing <span class=\"story-title\"><\/span> from <span class=\"story-seller\"><\/span> as a gift.<\/span>\n\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t<span class=\"atavist-access-description\">Access <span class=\"story-title\"><\/span> by <span class=\"story-seller\"><\/span>.<\/span>\n\t\t\t\t\t\t<span class=\"atavist-success-description\">You purchased <span class=\"story-title\"><\/span> from <span class=\"story-seller\"><\/span>.<\/span>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class='process-container choose'>\n\t\t\t\t\t<div class='process process-choose'>\n\t\t\t\t\t\t<a href=\"#\" class=\"atavist-toggle atavist-toggle-login\">Sign In<\/a>\n\t\t\t\t\t\t<a href=\"#\" class=\"atavist-toggle atavist-toggle-register\">Register<\/a>\n\t\t\t\t\t\t<h1 class=\"atavist-toggle-login\">Register<\/h1>\n\t\t\t\t\t\t<h1 class=\"atavist-toggle-register\">Sign In<\/h1>\n\t\t\t\t\t\t<div class='message on'>Create an account to complete your purchase<\/div>\n\t\t\t\t\t\t<form action=\"https:\/\/read.atavist.com\/cms\/\" method=\"POST\" class=\"atavist-form\">\n\t\t\t\t\t\t\t<section class=\"atavist-form-elements\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<input class='process-username' type='email' name='username' placeholder='Email' \/>\n\t\t\t\t\t\t\t\t\t<label class=\"under-label\">Email<\/label>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<input class='process-password' type='password' name='password' placeholder='Password' \/>\n<label class=\"under-label\">Password<\/label>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/section>\n\t\t\t\t\t\t\t<section class=\"atavist-footer\">\n\t\t\t\t\t\t\t\t<div class=\"form-row atavist-button-container\">\n\t\t\t\t\t\t\t\t\t<button class='btn atavist process-button process-login' value=\"login\">\nSign In\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/button>\n\t\t\t\t\t\t\t\t\t\t<button class='btn atavist process-button process-signup' value=\"signup\">Register<\/button>\n\t\t\t\t\t\t\t\t\t\t<p class=\"atavist-forgot\"><a href=\"https:\/\/read.atavist.com\/cms\/forgot\/\">Forgot your password?<\/a><\/p>\t\n\t\t\t\t\t\t\t\t\t<p class=\"atavist-powered-by \">Powered By Atavist<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/section>\n\t\t\t\t\t\t<\/form>\n\t\t\t\t\t<\/div>\t\t\t\t\t<div class='process process-payment'>\n\t<h1>Payment Details<\/h1>\n\t<div class='message'><\/div>\n\t<div class=\"atavist-coupon\">\n\t\t<form action=\"\" method=\"POST\" class=\"atavist-form\">\n\t\t\t<a href=\"#\" class=\"atavist-coupon-button\">x<\/a>\n\t\t\t<div class=\"atavist-coupon-widget\">\n\t\t\t\t<div class=\"form-row coupon\">\n\t\t\t\t\t<input type=\"text\" size=\"2\" size=\"2\" class=\"process-store-coupon\" \/>\n\t\t\t\t\t<button type=\"submit\" class=\"btn atavist process-coupon-submit submit-button\">Apply<\/button>\n\t\t\t\t\t<label class=\"under-label\">Coupon code<\/label>\n\t\t\t\t<\/div>\n\t\t\t\t<!--div class=\"form-row coupon\">\n\t\t\t\t\t<button type=\"submit\" class=\"btn atavist process-coupon-submit submit-button\">Add coupon<\/button>\n\t\t\t\t\t<span> Or <\/span>\t\n\t\t\t\t\t<button type=\"submit\" class=\"btn atavist atavist-toggle-coupon submit-button\">Cancel<\/button>\n\t\t\t\t<\/div-->\t \t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\n\t\t\t<\/div>\n\t\t<\/form>\t\n\t\t<a href=\"#\" class=\"atavist-coupon-button add-coupon\">Add a coupon code...<\/a>\n\t\t<a href=\"#\" class=\"atavist-coupon-button edit-coupon\">Change coupon code...<\/a>\n\t<\/div>\t<form action=\"\" method=\"POST\" class=\"atavist-form process-payment-choice-form\">\n\t<section class=\"atavist-form-elements\">\n\t\t<div class=\"form-row payment-details\">\n\t\t\t<span class=\"story-payment\"><\/span>\n\t\t\t<a class=\"atavist-login atavist-change-card\">(Change)<\/a>\n\t\t<\/div>\n\t\t<div class=\"payment-confirm\">\n\t\t\t<div class=\"form-row\">\n\t\t\t\t<input type=\"text\" size=\"20\" autocomplete=\"off\" class=\"card-number\" placeholder=\"\u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022\" \/>\n\t\t\t\t<label class=\"under-label\">Card Number<\/label>\n\t\t\t<\/div>\n\t\t\t<div class=\"atavist-inline-form-set\">\n\t\t\t\t<div class=\"form-row cvc\">\n\t\t\t\t\t<input type=\"text\" size=\"4\" autocomplete=\"off\" length=\"4\" class=\"card-cvc\" placeholder=\"\u2022\u2022\u2022\" \/>\n\t\t\t\t\t<label class=\"under-label\">CVC<\/label>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"form-row expiry\">\n\t\t\t\t\t<input type=\"text\" size=\"2\" maxlength=\"2\" class=\"card-expiry-month\" placeholder=\"MM\" \/>\n\t\t\t\t\t<input type=\"text\" size=\"4\" maxlength=\"4\" class=\"card-expiry-year\" placeholder=\"YYYY\" \/>\n\t\t\t\t\t<label class=\"under-label\">Expiration<\/label>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"form-row zip atavist-inline-form-set-last-item last\">\n\t\t\t\t\t<input type=\"text\" size=\"5\" placeholder='\u2022\u2022\u2022\u2022\u2022' size=\"5\" class=\"card-zip\" \/>\n\t\t\t\t\t<label class=\"under-label\">Zip \/ Postal Code<\/label>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"atavist-clearboth\"><\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t<div class=\"gift-inputs\">\n\t\t\t<div class=\"form-row\">\n\t\t\t\t<input type=\"email\" size=\"5\" placeholder='email address' size=\"5\" class=\"gift-recipient\" \/>\t\t\n\t\t\t\t<label class=\"under-label\">Gift Recipient Email<\/label>\t\t\n\t\t\t<\/div>\t\n\t\t\t<div class=\"form-row\">\n\t\t\t\t<input type=\"text\" size=\"5\" placeholder='your name' size=\"5\" class=\"gift-gifter\" \/>\t\t\t\t\n\t\t\t\t<label class=\"under-label\">Your Name<\/label>\n\t\t\t<\/div>\t\t\n\t\t\t<div class=\"form-row\">\n\t\t\t\t<input type=\"text\" size=\"5\" placeholder='message' size=\"5\" class=\"gift-message\" \/>\t\t\t\t\n\t\t\t\t<label class=\"under-label\">Gift Message<\/label>\n\t\t\t<\/div>\t\t\n\t\t\t<div class=\"form-row\">\n\t\t\t\t<input type=\"text\" id=\"atavist-datepicker\"><\/input>\n\t\t\t\t<label class=\"under-label\">Gift delivery date<\/label>\n\t\t\t<\/div>\t\t\t\t\t\t\n\t\t<\/div>\n\t<\/section>\n\t<section class=\"atavist-footer\">\n\t\t<div class=\"form-row atavist-button-container\">\n\t\t\t<button type=\"submit\" class=\"btn atavist process-payment-submit submit-button\">Proceed To Purchase<\/button>\n\t\t<\/div>\n\t\t<p class=\"atavist-powered-by\">Powered By Atavist<\/p>\n\t<\/section>\n\t<\/form>\n\t<\/div>\n\t\t\t\t\t<div class='process process-confirm'>\n\t<h1>Complete Purchase<\/h1>\n\t<div class='message'><\/div>\n\t\t<form action=\"\" method=\"POST\" class=\"atavist-form\">\n\t\t\t<section class=\"atavist-form-elements\">\n\t\t\t\t<div class=\"form-row payment-details\">\n\t\t\t\t\t<span class=\"story-payment\"><\/span>\n\t\t\t\t\t<a class=\"atavist-login atavist-change-card\">(Change)<\/a>\n\t\t\t\t<\/div>\n\t\t\t<div class=\"story-totals\"><\/div>\n\t\t<\/section>\n\t\t<section class=\"atavist-footer\">\n\t\t\t<div class=\"form-row atavist-button-container\">\n\t\t\t\t<button type=\"submit\" class=\"btn atavist submit-button process-finish\" data-redeem-coupon-text=\"Redeem Coupon\">Purchase<\/button>\n\t\t\t<\/div>\n\t\t\t<p class=\"atavist-powered-by\">Powered By Atavist<\/p>\n\t\t<\/section>\n\t<\/form>\n<\/div>\n\t\t\t\t\t<div class='process process-success'>\n\t<h1>Success!<\/h1>\n\t<section class=\"atavist-form-elements\"><p>We sent you an email with a receipt.<\/p><p class=\"success-message-story\">You can read it on any device at this URL: <a class=\"story-url\"><\/a><\/p><p class=\"success-message-gift\">Your gift will be delivered to <span class=\"gift-destination\"><\/span> on <span class=\"gift-date\"><\/span><\/a><\/section>\n\t<section class=\"atavist-footer\">\n\t\t<div class=\"form-row atavist-button-container\">\n\t\t\t<button type=\"submit\" class=\"btn atavist process-success-submit submit-button\">Read Now<\/button>\n\t\t<\/div>\n\t\t<p class=\"atavist-powered-by\">Powered By Atavist<\/p>\n\t<\/section>\n<\/div>\n\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n\t<\/div>",
	loginTemplate:"",
	localizations : {"incorrect_number":"The card number is incorrect.","invalid_number":"The card number is not a valid credit card number.","invalid_expiry_month":"The card's expiration month is invalid.","invalid_expiry_year":"The card's expiration year is invalid.","invalid_cvc":"The card's security code is invalid.","expired_card":"The card has expired.","incorrect_cvc":"The card's security code is incorrect.","incorrect_zip":"The card's zip code failed validation.","card_declined":"The card was declined.","missing":"There is no card on a customer that is being charged.","processing_error":"An error occurred while processing the card.","rate_limit":" An error occurred due to requests hitting the API too quickly. Please let us know if you're consistently running into this error.","credit_required":"All credit card fields are required"}};
if (!jQuery.support.cors && typeof window.XDomainRequest == 'function') {
	var httpRegEx = /^https?:\/\//i;
	var getOrPostRegEx = /^get|post$/i;
	var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
	var xmlRegEx = /\/xml/i;

	// ajaxTransport exists in jQuery 1.5+
	jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){
		// XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
		if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
			var xdr = null;
			var userType = (userOptions.dataType||'').toLowerCase();
			return {
				send: function(headers, complete){
					xdr = new XDomainRequest();
					if (/^\d+$/.test(userOptions.timeout)) {
						xdr.timeout = userOptions.timeout;
					}
					xdr.ontimeout = function(){
						complete(500, 'timeout');
					};
					xdr.onload = function(){
						var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
						var status = {
							code: 200,
							message: 'success'
						};
						var responses = {
							text: xdr.responseText
						};
						/*
						if (userType === 'html') {
							responses.html = xdr.responseText;
						} else
						*/
						try {
							if (userType === 'json') {
								try {
									responses.json = JSON.parse(xdr.responseText);
								} catch(e) {
									status.code = 500;
									status.message = 'parseerror';
									//throw 'Invalid JSON: ' + xdr.responseText;
								}
							} else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
								var doc = new ActiveXObject('Microsoft.XMLDOM');
								doc.async = false;
								try {
									doc.loadXML(xdr.responseText);
								} catch(e) {
									doc = undefined;
								}
								if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
									status.code = 500;
									status.message = 'parseerror';
									throw 'Invalid XML: ' + xdr.responseText;
								}
								responses.xml = doc;
							}
						} catch(parseMessage) {
							throw parseMessage;
						} finally {
							complete(status.code, status.message, responses, allResponseHeaders);
						}
					};
					xdr.onerror = function(){
						complete(500, 'error', {
							text: xdr.responseText
						});
					};
					xdr.open(options.type, options.url);
					var string = jQuery.param(userOptions.data);
					console.log(string);
					xdr.send(string);
					//xdr.send();
				},
				abort: function(){
					if (xdr) {
						xdr.abort();
					}
				}
			};
		}
	});
}/*!
 * Pikaday
 *
 * Copyright Â© 2013 David Bushell | BSD & MIT license | https://github.com/dbushell/Pikaday
 */

(function (root, factory)
{
    'use strict';

    var moment;
    if (typeof exports === 'object') {
        // CommonJS module
        // Load moment.js as an optional dependency
        try { moment = require('moment'); } catch (e) {}
        module.exports = factory(moment);
    } else if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(function (req)
        {
            // Load moment.js as an optional dependency
            var id = 'moment';
            moment = req.defined && req.defined(id) ? req(id) : undefined;
            return factory(moment);
        });
    } else {
        root.Pikaday = factory(root.moment);
    }
}(this, function (moment)
{
    'use strict';

    /**
     * feature detection and helper functions
     */
    var hasMoment = typeof moment === 'function',

    hasEventListeners = !!window.addEventListener,

    document = window.document,

    sto = window.setTimeout,

    addEvent = function(el, e, callback, capture)
    {
        if (hasEventListeners) {
            el.addEventListener(e, callback, !!capture);
        } else {
            el.attachEvent('on' + e, callback);
        }
    },

    removeEvent = function(el, e, callback, capture)
    {
        if (hasEventListeners) {
            el.removeEventListener(e, callback, !!capture);
        } else {
            el.detachEvent('on' + e, callback);
        }
    },

    fireEvent = function(el, eventName, data)
    {
        var ev;

        if (document.createEvent) {
            ev = document.createEvent('HTMLEvents');
            ev.initEvent(eventName, true, false);
            ev = extend(ev, data);
            el.dispatchEvent(ev);
        } else if (document.createEventObject) {
            ev = document.createEventObject();
            ev = extend(ev, data);
            el.fireEvent('on' + eventName, ev);
        }
    },

    trim = function(str)
    {
        return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g,'');
    },

    hasClass = function(el, cn)
    {
        return (' ' + el.className + ' ').indexOf(' ' + cn + ' ') !== -1;
    },

    addClass = function(el, cn)
    {
        if (!hasClass(el, cn)) {
            el.className = (el.className === '') ? cn : el.className + ' ' + cn;
        }
    },

    removeClass = function(el, cn)
    {
        el.className = trim((' ' + el.className + ' ').replace(' ' + cn + ' ', ' '));
    },

    isArray = function(obj)
    {
        return (/Array/).test(Object.prototype.toString.call(obj));
    },

    isDate = function(obj)
    {
        return (/Date/).test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime());
    },

    isLeapYear = function(year)
    {
        // solution by Matti Virkkunen: http://stackoverflow.com/a/4881951
        return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
    },

    getDaysInMonth = function(year, month)
    {
        return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
    },

    setToStartOfDay = function(date)
    {
        if (isDate(date)) date.setHours(0,0,0,0);
    },

    compareDates = function(a,b)
    {
        // weak date comparison (use setToStartOfDay(date) to ensure correct result)
        return a.getTime() === b.getTime();
    },

    extend = function(to, from, overwrite)
    {
        var prop, hasProp;
        for (prop in from) {
            hasProp = to[prop] !== undefined;
            if (hasProp && typeof from[prop] === 'object' && from[prop].nodeName === undefined) {
                if (isDate(from[prop])) {
                    if (overwrite) {
                        to[prop] = new Date(from[prop].getTime());
                    }
                }
                else if (isArray(from[prop])) {
                    if (overwrite) {
                        to[prop] = from[prop].slice(0);
                    }
                } else {
                    to[prop] = extend({}, from[prop], overwrite);
                }
            } else if (overwrite || !hasProp) {
                to[prop] = from[prop];
            }
        }
        return to;
    },


    /**
     * defaults and localisation
     */
    defaults = {

        // bind the picker to a form field
        field: null,

        // automatically show/hide the picker on `field` focus (default `true` if `field` is set)
        bound: undefined,

        // the default output format for `.toString()` and `field` value
        format: 'YYYY-MM-DD',

        // the initial date to view when first opened
        defaultDate: null,

        // make the `defaultDate` the initial selected value
        setDefaultDate: false,

        // first day of week (0: Sunday, 1: Monday etc)
        firstDay: 0,

        // the minimum/earliest date that can be selected
        minDate: null,
        // the maximum/latest date that can be selected
        maxDate: null,

        // number of years either side, or array of upper/lower range
        yearRange: 10,

        // used internally (don't config outside)
        minYear: 0,
        maxYear: 9999,
        minMonth: undefined,
        maxMonth: undefined,

        isRTL: false,

        // Additional text to append to the year in the calendar title
        yearSuffix: '',

        // Render the month after year in the calendar title
        showMonthAfterYear: false,

        // how many months are visible (not implemented yet)
        numberOfMonths: 1,

        // internationalization
        i18n: {
            previousMonth : 'Previous Month',
            nextMonth     : 'Next Month',
            months        : ['January','February','March','April','May','June','July','August','September','October','November','December'],
            weekdays      : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
            weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
        },

        // callback function
        onSelect: null,
        onOpen: null,
        onClose: null,
        onDraw: null
    },


    /**
     * templating functions to abstract HTML rendering
     */
    renderDayName = function(opts, day, abbr)
    {
        day += opts.firstDay;
        while (day >= 7) {
            day -= 7;
        }
        return abbr ? opts.i18n.weekdaysShort[day] : opts.i18n.weekdays[day];
    },

    renderDay = function(i, isSelected, isToday, isDisabled, isEmpty)
    {
        if (isEmpty) {
            return '<td class="is-empty"></td>';
        }
        var arr = [];
        if (isDisabled) {
            arr.push('is-disabled');
        }
        if (isToday) {
            arr.push('is-today');
        }
        if (isSelected) {
            arr.push('is-selected');
        }
        return '<td data-day="' + i + '" class="' + arr.join(' ') + '"><button class="pika-button" type="button">' + i + '</button>' + '</td>';
    },

    renderRow = function(days, isRTL)
    {
        return '<tr>' + (isRTL ? days.reverse() : days).join('') + '</tr>';
    },

    renderBody = function(rows)
    {
        return '<tbody>' + rows.join('') + '</tbody>';
    },

    renderHead = function(opts)
    {
        var i, arr = [];
        for (i = 0; i < 7; i++) {
            arr.push('<th scope="col"><abbr title="' + renderDayName(opts, i) + '">' + renderDayName(opts, i, true) + '</abbr></th>');
        }
        return '<thead>' + (opts.isRTL ? arr.reverse() : arr).join('') + '</thead>';
    },

    renderTitle = function(instance)
    {
        var i, j, arr,
            opts = instance._o,
            month = instance._m,
            year  = instance._y,
            isMinYear = year === opts.minYear,
            isMaxYear = year === opts.maxYear,
            html = '<div class="pika-title">',
            monthHtml,
            yearHtml,
            prev = true,
            next = true;

        for (arr = [], i = 0; i < 12; i++) {
            arr.push('<option value="' + i + '"' +
                (i === month ? ' selected': '') +
                ((isMinYear && i < opts.minMonth) || (isMaxYear && i > opts.maxMonth) ? 'disabled' : '') + '>' +
                opts.i18n.months[i] + '</option>');
        }
        monthHtml = '<div class="pika-label">' + opts.i18n.months[month] + '<select class="pika-select pika-select-month">' + arr.join('') + '</select></div>';

        if (isArray(opts.yearRange)) {
            i = opts.yearRange[0];
            j = opts.yearRange[1] + 1;
        } else {
            i = year - opts.yearRange;
            j = 1 + year + opts.yearRange;
        }

        for (arr = []; i < j && i <= opts.maxYear; i++) {
            if (i >= opts.minYear) {
                arr.push('<option value="' + i + '"' + (i === year ? ' selected': '') + '>' + (i) + '</option>');
            }
        }
        yearHtml = '<div class="pika-label">' + year + opts.yearSuffix + '<select class="pika-select pika-select-year">' + arr.join('') + '</select></div>';

        if (opts.showMonthAfterYear) {
            html += yearHtml + monthHtml;
        } else {
            html += monthHtml + yearHtml;
        }

        if (isMinYear && (month === 0 || opts.minMonth >= month)) {
            prev = false;
        }

        if (isMaxYear && (month === 11 || opts.maxMonth <= month)) {
            next = false;
        }

        html += '<button class="pika-prev' + (prev ? '' : ' is-disabled') + '" type="button">' + opts.i18n.previousMonth + '</button>';
        html += '<button class="pika-next' + (next ? '' : ' is-disabled') + '" type="button">' + opts.i18n.nextMonth + '</button>';

        return html += '</div>';
    },

    renderTable = function(opts, data)
    {
        return '<table cellpadding="0" cellspacing="0" class="pika-table">' + renderHead(opts) + renderBody(data) + '</table>';
    },


    /**
     * Pikaday constructor
     */
    Pikaday = function(options)
    {
        var self = this,
            opts = self.config(options);

        self._onMouseDown = function(e)
        {
            if (!self._v) {
                return;
            }
            e = e || window.event;
            var target = e.target || e.srcElement;
            if (!target) {
                return;
            }

            if (!hasClass(target, 'is-disabled')) {
                if (hasClass(target, 'pika-button') && !hasClass(target, 'is-empty')) {
                    self.setDate(new Date(self._y, self._m, parseInt(target.innerHTML, 10)));
                    if (opts.bound) {
                        sto(function() {
                            self.hide();
                        }, 100);
                    }
                    return;
                }
                else if (hasClass(target, 'pika-prev')) {
                    self.prevMonth();
                }
                else if (hasClass(target, 'pika-next')) {
                    self.nextMonth();
                }
            }
            if (!hasClass(target, 'pika-select')) {
                if (e.preventDefault) {
                    e.preventDefault();
                } else {
                    e.returnValue = false;
                    return false;
                }
            } else {
                self._c = true;
            }
        };

        self._onChange = function(e)
        {
            e = e || window.event;
            var target = e.target || e.srcElement;
            if (!target) {
                return;
            }
            if (hasClass(target, 'pika-select-month')) {
                self.gotoMonth(target.value);
            }
            else if (hasClass(target, 'pika-select-year')) {
                self.gotoYear(target.value);
            }
        };

        self._onInputChange = function(e)
        {
            var date;

            if (e.firedBy === self) {
                return;
            }
            if (hasMoment) {
                date = moment(opts.field.value, opts.format);
                date = (date && date.isValid()) ? date.toDate() : null;
            }
            else {
                date = new Date(Date.parse(opts.field.value));
            }
            self.setDate(isDate(date) ? date : null);
            if (!self._v) {
                self.show();
            }
        };

        self._onInputFocus = function()
        {
            self.show();
        };

        self._onInputClick = function()
        {
            self.show();
        };

        self._onInputBlur = function()
        {
            if (!self._c) {
                self._b = sto(function() {
                    self.hide();
                }, 50);
            }
            self._c = false;
        };

        self._onClick = function(e)
        {
            e = e || window.event;
            var target = e.target || e.srcElement,
                pEl = target;
            if (!target) {
                return;
            }
            if (!hasEventListeners && hasClass(target, 'pika-select')) {
                if (!target.onchange) {
                    target.setAttribute('onchange', 'return;');
                    addEvent(target, 'change', self._onChange);
                }
            }
            do {
                if (hasClass(pEl, 'pika-single')) {
                    return;
                }
            }
            while ((pEl = pEl.parentNode));
            if (self._v && target !== opts.trigger) {
                self.hide();
            }
        };

        self.el = document.createElement('div');
        self.el.className = 'pika-single' + (opts.isRTL ? ' is-rtl' : '');

        addEvent(self.el, 'mousedown', self._onMouseDown, true);
        addEvent(self.el, 'change', self._onChange);

        if (opts.field) {
            if (opts.bound) {
                document.body.appendChild(self.el);
            } else {
                opts.field.parentNode.insertBefore(self.el, opts.field.nextSibling);
            }
            addEvent(opts.field, 'change', self._onInputChange);

            if (!opts.defaultDate) {
                if (hasMoment && opts.field.value) {
                    opts.defaultDate = moment(opts.field.value, opts.format).toDate();
                } else {
                    opts.defaultDate = new Date(Date.parse(opts.field.value));
                }
                opts.setDefaultDate = true;
            }
        }

        var defDate = opts.defaultDate;

        if (isDate(defDate)) {
            if (opts.setDefaultDate) {
                self.setDate(defDate, true);
            } else {
                self.gotoDate(defDate);
            }
        } else {
            self.gotoDate(new Date());
        }

        if (opts.bound) {
            this.hide();
            self.el.className += ' is-bound';
            addEvent(opts.trigger, 'click', self._onInputClick);
            addEvent(opts.trigger, 'focus', self._onInputFocus);
            addEvent(opts.trigger, 'blur', self._onInputBlur);
        } else {
            this.show();
        }

    };


    /**
     * public Pikaday API
     */
    Pikaday.prototype = {


        /**
         * configure functionality
         */
        config: function(options)
        {
            if (!this._o) {
                this._o = extend({}, defaults, true);
            }

            var opts = extend(this._o, options, true);

            opts.isRTL = !!opts.isRTL;

            opts.field = (opts.field && opts.field.nodeName) ? opts.field : null;

            opts.bound = !!(opts.bound !== undefined ? opts.field && opts.bound : opts.field);

            opts.trigger = (opts.trigger && opts.trigger.nodeName) ? opts.trigger : opts.field;

            var nom = parseInt(opts.numberOfMonths, 10) || 1;
            opts.numberOfMonths = nom > 4 ? 4 : nom;

            if (!isDate(opts.minDate)) {
                opts.minDate = false;
            }
            if (!isDate(opts.maxDate)) {
                opts.maxDate = false;
            }
            if ((opts.minDate && opts.maxDate) && opts.maxDate < opts.minDate) {
                opts.maxDate = opts.minDate = false;
            }
            if (opts.minDate) {
                setToStartOfDay(opts.minDate);
                opts.minYear  = opts.minDate.getFullYear();
                opts.minMonth = opts.minDate.getMonth();
            }
            if (opts.maxDate) {
                setToStartOfDay(opts.maxDate);
                opts.maxYear  = opts.maxDate.getFullYear();
                opts.maxMonth = opts.maxDate.getMonth();
            }

            if (isArray(opts.yearRange)) {
                var fallback = new Date().getFullYear() - 10;
                opts.yearRange[0] = parseInt(opts.yearRange[0], 10) || fallback;
                opts.yearRange[1] = parseInt(opts.yearRange[1], 10) || fallback;
            } else {
                opts.yearRange = Math.abs(parseInt(opts.yearRange, 10)) || defaults.yearRange;
                if (opts.yearRange > 100) {
                    opts.yearRange = 100;
                }
            }

            return opts;
        },

        /**
         * return a formatted string of the current selection (using Moment.js if available)
         */
        toString: function(format)
        {
            return !isDate(this._d) ? '' : hasMoment ? moment(this._d).format(format || this._o.format) : this._d.toDateString();
        },

        /**
         * return a Moment.js object of the current selection (if available)
         */
        getMoment: function()
        {
            return hasMoment ? moment(this._d) : null;
        },

        /**
         * set the current selection from a Moment.js object (if available)
         */
        setMoment: function(date)
        {
            if (hasMoment && moment.isMoment(date)) {
                this.setDate(date.toDate());
            }
        },

        /**
         * return a Date object of the current selection
         */
        getDate: function()
        {
            return isDate(this._d) ? new Date(this._d.getTime()) : null;
        },

        /**
         * set the current selection
         */
        setDate: function(date, preventOnSelect)
        {
            if (!date) {
                this._d = null;
                return this.draw();
            }
            if (typeof date === 'string') {
                date = new Date(Date.parse(date));
            }
            if (!isDate(date)) {
                return;
            }

            var min = this._o.minDate,
                max = this._o.maxDate;

            if (isDate(min) && date < min) {
                date = min;
            } else if (isDate(max) && date > max) {
                date = max;
            }

            this._d = new Date(date.getTime());
            setToStartOfDay(this._d);
            this.gotoDate(this._d);

            if (this._o.field) {
                this._o.field.value = this.toString();
                fireEvent(this._o.field, 'change', { firedBy: this });
            }
            if (!preventOnSelect && typeof this._o.onSelect === 'function') {
                this._o.onSelect.call(this, this.getDate());
            }
        },

        /**
         * change view to a specific date
         */
        gotoDate: function(date)
        {
            if (!isDate(date)) {
                return;
            }
            this._y = date.getFullYear();
            this._m = date.getMonth();
            this.draw();
        },

        gotoToday: function()
        {
            this.gotoDate(new Date());
        },

        /**
         * change view to a specific month (zero-index, e.g. 0: January)
         */
        gotoMonth: function(month)
        {
            if (!isNaN( (month = parseInt(month, 10)) )) {
                this._m = month < 0 ? 0 : month > 11 ? 11 : month;
                this.draw();
            }
        },

        nextMonth: function()
        {
            if (++this._m > 11) {
                this._m = 0;
                this._y++;
            }
            this.draw();
        },

        prevMonth: function()
        {
            if (--this._m < 0) {
                this._m = 11;
                this._y--;
            }
            this.draw();
        },

        /**
         * change view to a specific full year (e.g. "2012")
         */
        gotoYear: function(year)
        {
            if (!isNaN(year)) {
                this._y = parseInt(year, 10);
                this.draw();
            }
        },

        /**
         * change the minDate
         */
        setMinDate: function(value)
        {
            this._o.minDate = value;
        },

        /**
         * change the maxDate
         */
        setMaxDate: function(value)
        {
            this._o.maxDate = value;
        },

        /**
         * refresh the HTML
         */
        draw: function(force)
        {
            if (!this._v && !force) {
                return;
            }
            var opts = this._o,
                minYear = opts.minYear,
                maxYear = opts.maxYear,
                minMonth = opts.minMonth,
                maxMonth = opts.maxMonth;

            if (this._y <= minYear) {
                this._y = minYear;
                if (!isNaN(minMonth) && this._m < minMonth) {
                    this._m = minMonth;
                }
            }
            if (this._y >= maxYear) {
                this._y = maxYear;
                if (!isNaN(maxMonth) && this._m > maxMonth) {
                    this._m = maxMonth;
                }
            }

            this.el.innerHTML = renderTitle(this) + this.render(this._y, this._m);

            if (opts.bound) {
                this.adjustPosition();
                if(opts.field.type !== 'hidden') {
                    sto(function() {
                        opts.trigger.focus();
                    }, 1);
                }
            }

            if (typeof this._o.onDraw === 'function') {
                var self = this;
                sto(function() {
                    self._o.onDraw.call(self);
                }, 0);
            }
        },

        adjustPosition: function()
        {
            var field = this._o.trigger, pEl = field,
            width = this.el.offsetWidth, height = this.el.offsetHeight,
            viewportWidth = window.innerWidth || document.documentElement.clientWidth,
            viewportHeight = window.innerHeight || document.documentElement.clientHeight,
            scrollTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop,
            left, top, clientRect;

            if (typeof field.getBoundingClientRect === 'function') {
                clientRect = field.getBoundingClientRect();
                left = clientRect.left + window.pageXOffset;
                top = clientRect.bottom + window.pageYOffset;
            } else {
                left = pEl.offsetLeft;
                top  = pEl.offsetTop + pEl.offsetHeight;
                while((pEl = pEl.offsetParent)) {
                    left += pEl.offsetLeft;
                    top  += pEl.offsetTop;
                }
            }

            if (left + width > viewportWidth) {
                left = left - width + field.offsetWidth;
            }
            if (top + height > viewportHeight + scrollTop) {
                top = top - height - field.offsetHeight;
            }
            this.el.style.cssText = 'position:absolute;left:' + left + 'px;top:' + top + 'px;';
        },

        /**
         * render HTML for a particular month
         */
        render: function(year, month)
        {
            var opts   = this._o,
                now    = new Date(),
                days   = getDaysInMonth(year, month),
                before = new Date(year, month, 1).getDay(),
                data   = [],
                row    = [];
            setToStartOfDay(now);
            if (opts.firstDay > 0) {
                before -= opts.firstDay;
                if (before < 0) {
                    before += 7;
                }
            }
            var cells = days + before,
                after = cells;
            while(after > 7) {
                after -= 7;
            }
            cells += 7 - after;
            for (var i = 0, r = 0; i < cells; i++)
            {
                var day = new Date(year, month, 1 + (i - before)),
                    isDisabled = (opts.minDate && day < opts.minDate) || (opts.maxDate && day > opts.maxDate),
                    isSelected = isDate(this._d) ? compareDates(day, this._d) : false,
                    isToday = compareDates(day, now),
                    isEmpty = i < before || i >= (days + before);

                row.push(renderDay(1 + (i - before), isSelected, isToday, isDisabled, isEmpty));

                if (++r === 7) {
                    data.push(renderRow(row, opts.isRTL));
                    row = [];
                    r = 0;
                }
            }
            return renderTable(opts, data);
        },

        isVisible: function()
        {
            return this._v;
        },

        show: function()
        {
            if (!this._v) {
                if (this._o.bound) {
                    addEvent(document, 'click', this._onClick);
                }
                removeClass(this.el, 'is-hidden');
                this._v = true;
                this.draw();
                if (typeof this._o.onOpen === 'function') {
                    this._o.onOpen.call(this);
                }
            }
        },

        hide: function()
        {
            var v = this._v;
            if (v !== false) {
                if (this._o.bound) {
                    removeEvent(document, 'click', this._onClick);
                }
                this.el.style.cssText = '';
                addClass(this.el, 'is-hidden');
                this._v = false;
                if (v !== undefined && typeof this._o.onClose === 'function') {
                    this._o.onClose.call(this);
                }
            }
        },

        /**
         * GAME OVER
         */
        destroy: function()
        {
            this.hide();
            removeEvent(this.el, 'mousedown', this._onMouseDown, true);
            removeEvent(this.el, 'change', this._onChange);
            if (this._o.field) {
                removeEvent(this._o.field, 'change', this._onInputChange);
                if (this._o.bound) {
                    removeEvent(this._o.trigger, 'click', this._onInputClick);
                    removeEvent(this._o.trigger, 'focus', this._onInputFocus);
                    removeEvent(this._o.trigger, 'blur', this._onInputBlur);
                }
            }
            if (this.el.parentNode) {
                this.el.parentNode.removeChild(this.el);
            }
        }

    };

    return Pikaday;

}));/*!
 * EventEmitter v4.2.7 - git.io/ee
 * Oliver Caldwell
 * MIT license
 * @preserve
 */
(function(){"use strict";function t(){}function r(t,n){for(var e=t.length;e--;)if(t[e].listener===n)return e;return-1}function n(e){return function(){return this[e].apply(this,arguments)}}var e=t.prototype,i=this,s=i.EventEmitter;e.getListeners=function(n){var r,e,t=this._getEvents();if(n instanceof RegExp){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.flattenListeners=function(t){var e,n=[];for(e=0;e<t.length;e+=1)n.push(t[e].listener);return n},e.getListenersAsObject=function(n){var e,t=this.getListeners(n);return t instanceof Array&&(e={},e[n]=t),e||t},e.addListener=function(i,e){var t,n=this.getListenersAsObject(i),s="object"==typeof e;for(t in n)n.hasOwnProperty(t)&&-1===r(n[t],e)&&n[t].push(s?e:{listener:e,once:!1});return this},e.on=n("addListener"),e.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},e.once=n("addOnceListener"),e.defineEvent=function(e){return this.getListeners(e),this},e.defineEvents=function(t){for(var e=0;e<t.length;e+=1)this.defineEvent(t[e]);return this},e.removeListener=function(i,s){var n,e,t=this.getListenersAsObject(i);for(e in t)t.hasOwnProperty(e)&&(n=r(t[e],s),-1!==n&&t[e].splice(n,1));return this},e.off=n("removeListener"),e.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},e.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},e.manipulateListeners=function(r,t,i){var e,n,s=r?this.removeListener:this.addListener,o=r?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(e=i.length;e--;)s.call(this,t,i[e]);else for(e in t)t.hasOwnProperty(e)&&(n=t[e])&&("function"==typeof n?s.call(this,e,n):o.call(this,e,n));return this},e.removeEvent=function(e){var t,r=typeof e,n=this._getEvents();if("string"===r)delete n[e];else if(e instanceof RegExp)for(t in n)n.hasOwnProperty(t)&&e.test(t)&&delete n[t];else delete this._events;return this},e.removeAllListeners=n("removeEvent"),e.emitEvent=function(r,o){var e,i,t,s,n=this.getListenersAsObject(r);for(t in n)if(n.hasOwnProperty(t))for(i=n[t].length;i--;)e=n[t][i],e.once===!0&&this.removeListener(r,e.listener),s=e.listener.apply(this,o||[]),s===this._getOnceReturnValue()&&this.removeListener(r,e.listener);return this},e.trigger=n("emitEvent"),e.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},e.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},e._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},e._getEvents=function(){return this._events||(this._events={})},t.noConflict=function(){return i.EventEmitter=s,t},"function"==typeof define&&define.amd?define(function(){return t}):"object"==typeof module&&module.exports?module.exports=t:this.EventEmitter=t}).call(this);
var creatavistStore = (atavistStore) ? atavistStore : {};
(function(as, $) {
	var list = {},
		username = null,
		cash = '$',
		ids = [],
		config = (as) ? as : {},
		store = this,
		injectionTargets = ['story-title', 'story-price', 'atavist-form'];
	if (typeof $ == 'undefined' || !config.root || !config.template || !window.Stripe) return; //you never know
	config.root = 'https://' + config.root + '';
	$.support.cors = true;
	window.addEventListener('load', function() {
		document.addEventListener('touchmove', function(e) {
			if (document.body.classList.contains('overflow-no-scroll')) {
				e.preventDefault();
			}
		}, false);
	});

	function getParameterByName(name) {
		name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
		var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
			results = regex.exec(location.search);
		return results == null ? false : decodeURIComponent(results[1].replace(/\+/g, ' '));
	};

	var product = function(opts) {
		if (!opts) opts = {};
		var me = this,
			product_id = (typeof opts === 'string') ? opts : opts.product_id;
		if (!product_id || !product_id.length) throw 'Invalid product_id';
		this.product_id = product_id;
		this.gift = opts.gift || false;
		this.ref = getParameterByName('ref') || opts.ref || false;
		this.success_url =  opts.success_url || false;
		this.data = {
			user: null
		};

		for (var i = 0; i < this.stringSettings.length; i++) {
			if (opts[this.stringSettings[i]]) {
				this[this.stringSettings[i]] = opts[this.stringSettings[i]];
			}
		};

		this.owns = false;
		this.loaded = this.loading = false;
		this.on('singlepurchase', this.go);
		return this;
	};

	$.extend(product.prototype, EventEmitter.prototype);
	product.prototype.stringSettings = [
		'signupText',
		'loginText',
		'explanatoryText'
	];
	product.prototype.deferred = function() {
		if (!this.defer) this.defer = $.Deferred();
		return this.defer;
	};

	product.prototype.title = function() {
		return this.data.title;
	};

	product.prototype.available = function() {
		return (this.data.options && this.data.options.wv_availability && (parseInt(this.data.options.wv_availability) !== 0 && parseInt(this.data.options.wv_availability) !== 4)) ? true : false;
	};

	product.prototype.availability = function() {
		return this.data.options && this.data.options.wv_availability ? parseInt(this.data.options.wv_availability) : 0;
	};

	product.prototype.accessible = function() {
		if (this.gift === true) return false;
		if (this.data.owned == true) return true;
		if (parseInt(this.data.options.wv_availability) === 2 || parseInt(this.data.options.wv_availability) === 3) return true;
		if (this.data.accessible === true) return true;
		return false;
	};

	product.prototype.load = function(data, user) {
		this.data = data;
		this.owns = data.owned;
		this.url = data && data.url ? data.url : false;
		if (user) {
			this.user(user);
		};

		if (data.stripe_publishable_key) this.stripe_publishable_key = data.stripe_publishable_key;

		this.type = (this.data.product_type && this.data.product_type == 'subscription') ? 'subscription' : 'story';
		this.emit('loaded');
		this.loaded = true;
		this.loading = false;
		this.deferred().resolve();
	};

	product.prototype.go = function() {
		if (this.gift === true) return; // ?
		this.emit('read');
	};

	product.prototype.success = function() {

	};

	product.prototype.click = function() {
		if (!this.data) return;
		if (this.accessible() === true && this.gift === false) {
			this.go();
		} else { // begin purchase flow
			new modal(this);
		}
	};

	var modal = function(p, opts) {
		$('body').addClass('overflow-no-scroll');
		if (!opts) opts = {};
		this.mode = 'choose'; // starts with login or signup
		this.product = p;
		this.urls = {
			login:config.root + '/api/v2/store/login.php',
			signup:config.root + '/api/v2/store/signup.php',
			customer:config.root + '/api/v2/store/customer.php',
			coupon:config.root + '/api/v2/store/coupon.php',
			purchase:config.root + '/api/v2/store/purchase.php'
		};

		this.$wrapper = $('.atavist-wrapper');
		this.gift = false;
		if (this.product instanceof product) {
			this.gift = this.product.gift;
		};

		this.use_coupons = true;
		var owns_all = true;
		this.products(function ownsCheck(product) {
			if (product.owns !== true) owns_all = false;
		});

		if (this.gift !== true && owns_all === true) return; // if we own everything, return before creating?
		this.create();
	};

	$.extend(modal.prototype, EventEmitter.prototype);
	modal.prototype.clear = function() {
		$('body').removeClass('overflow-no-scroll');
		$('.atavist-wrapper')
			.removeClass('atavist-show')
			.remove();
		this.emit('close');
	};

	modal.prototype.products = function(cb) {
		if (this.product instanceof product) {
			cb.call(this, this.product);
		} else {
			for (var i = 0; i < this.product.length; i++) {
				cb.call(this, this.product[i]);
			};
		}
	};

	modal.prototype.free = function() {
		var free = true;
		this.products(function(product) {
			if (product.data.free !== true) free = false;
		});

		return free;
	};

	modal.prototype.go = function() {
		this.products(function(product) {
			product.emit('purchased');
		});

		if (this.product instanceof product) {
			this.product.emit('singlepurchase');
		};

		this.emit('owns', this.product, this.email);
		if (this.success_url && this.success_url.length) {
			window.location.href = this.success_url;
			return;
		};
	};

	modal.prototype.title = function() {
		var title = [];
		this.products(function(product) {
			title.push(product.data.title);
		});

		return (title.length > 1)
			? '<ul><li>' + title.join('</li><li>') + '</li></ul>'
			: title;
	};

	modal.prototype.product_ids = function() {
		var list = [];
		this.products(function(product) {
			list.push(product.data.product_id);
		});

		return (list.length > 1) ? list : list[0];
	};

	modal.prototype.fromFloat = function(num) {
		if (typeof num === 'string') {
			num = num.replace('.', '');
		};

		return parseInt(num);
	};

	modal.prototype.price = function(text) {
		var price = 0,
			symbol = cash;
		if (text) {
			price = text;
		} else {
			this.products(function(product) {
				price += this.fromFloat(product.data.web_price);
				if (product.data.symbol) symbol = product.data.symbol;
			});
		};

		$('.story-price').text(symbol + (price / 100));
	};

	modal.prototype.create = function(data) {
		var me = this,
			cfg = ($.parseHTML) ? $.parseHTML(config.template) : $(config.template);
		this.$el = $(cfg).appendTo($('body'));
		this.$process = this.$el.find('.process-container');
		this.$message = $('.message');
		this.$sheet = $('.atavist-sheet');
		if (this.product instanceof product) {
			this.$el.toggleClass('atavist-subscription', (this.product.type === 'subscription'));
		};

		setTimeout(function() {me.$el.addClass('atavist-show');}, 0); // fade in
		$('.allow-touchmove').on('touchmove', function(e) {
			e.stopPropagation();
		});

		$('.atavist-overlay, #atavist-header button.close').click(function() {
			me.clear();
		});

		$('.atavist-offset').click(function() {
			me.clear();
		});

		$('.atavist-sheet').click(function(e) {
			if (e.target.tagName === 'A') {
				e.preventDefault();
			};

			e.stopPropagation();
		});

		$('.atavist-forgot a').click(function(e) {
			e.stopPropagation();
			return true;
		});

		$('.atavist-sheet .story-title').html(this.title());
		$('.atavist-sheet .story-seller').html(this.product.data.organization_name);
		$('.atavist-sheet .story-url').html(this.product.data.url).attr('href', this.product.data.url);
		$('.atavist-sheet .organization_image').css('background-image', 'url("' + this.product.data.organization_image + '")');
		this.price();
		if (as.username) {
			$('.process-username').val(as.username);
			$('.atavist-sheet .process-choose').addClass('atavist-is-login').removeClass('atavist-is-register');
			$('.atavist-sheet .message').removeClass('on');
			this.email = as.username;
		};

		if (as.password) {
			this.password = as.password;
		};

		$('.process-login')
			.attr('type', 'button')
			.click($.proxy(this.login, this)).text(this.product.loginText);
		$('.process-signup')
			.click($.proxy(this.signup, this));
		$('.process-success')
			.attr('type', 'button')
			.click($.proxy(this.success, this));
		$('.process-choose form').submit(function(e) {
			return false;
		});

		$('.atavist-sheet').on('click', '.atavist-toggle', function() {
			if ($(this).hasClass('atavist-toggle-login')) {
				$(this).closest('.process').addClass('atavist-is-login').removeClass('atavist-is-register');
			} else if ($(this).hasClass('atavist-toggle-register')) {
				$(this).closest('.process').addClass('atavist-is-register').removeClass('atavist-is-login');
			}
		});

		// New coupon handling code
		$('.atavist-coupon-button').click(function() {
			$(this).closest('.atavist-coupon').toggleClass('show');
		});

		$('.atavist-coupon').on('input', 'input[type="text"]', function() {
			var charCount = $(this).val().length;
			$coupon = $(this).closest('.atavist-coupon');
			$coupon.toggleClass('dirty', (charCount > 0));
		});

		$('.atavist-coupon form').unbind().submit(function(event) {
			var data = me.user();
			data.code = $couponInput.val();
			me.ajax(me.urls.coupon, {
				data:data,
				success: $.proxy(me.couponCheck, me),
				failure: $.proxy(me.couponFailure, me)
			});
			return false;
		});

		if (this.email && this.password && as.cached_user_data) {
			this.userCheck(as.cached_user_data);
		};

		this.$el.toggleClass('atavist-gift', (this.gift === true));
	};

	modal.prototype.update = function(mode) {
		this.$message.text('').removeClass('on');
		this.$el
			.removeClass('current-process-' + this.mode)
			.addClass('current-process-' + mode);
		this.$sheet
			.removeClass(this.mode)
			.addClass(mode);
		this.$process
			.removeClass(this.mode)
			.addClass(mode)
			.find('input')
			.val('');
		this.disable(false);
		this.mode = mode;
	};

	modal.prototype.disable = function(bool) {
		if (bool === true) {
			$('.process button').attr('disabled', 'disabled');
		} else {
			$('.process button').removeAttr('disabled');
		};

		return this;
	};

	modal.prototype.ajax = function(url, opts) {
		var me = this;
		this.disable(true);
		opts.type = 'POST';
		opts.async = true;
		opts.crossDomain = true;
		opts.dataType = 'json';
		opts.url = url;
		var success = opts.success,
			failure = opts.failure;
		opts.success = function(data) {
			if (data.error) {
				if (opts.custom_error) {
					data.error_description = opts.custom_error(data);
				};

				me.error(data.error_description);
				if (failure) failure(data);
			} else {
				if (success) success(data);
			}
		};

		opts.error = $.proxy(this.error, this);
		return $.ajax(opts);
	};

	modal.prototype.owns = function(ownsData) {
		var owns = true;
		if (!ownsData) return false;
		if (ownsData === true || ownsData === false) {
			this.product.owns = ownsData;
			return ownsData; // for singular product purchases, singular ownership details
		};

		for (var i in ownsData) {
			if (ownsData[i] === false) owns = false;
			for (var j = 0; j < this.product.length; j++) {
				if (this.product[j].product_id === i) {
					this.product[j].owns = ownsData[i];
				}
			};
		};

		return owns;
	};

	modal.prototype.userCheck = function(data) {
		var me = this,
			checked_user = this.user(),
			owns = false,
			go = false;
		this.disable(true);
		if (data.user && data.user.username) {
			as.username = data.user.username;
		};

		if (checked_user && checked_user.password) {
			as.password = checked_user.password;
		};

		as.cached_user_data = data;
		this.credentials(this.user(), function() {
			if (go === true) me.go();
		});

		if (this.owns(data.owns) === true && me.gift === false) {
			if (this.product && this.product.url) {
				window.location.href = this.product.url;
			} else {
				this.message(data.already_owned_message);
			}
		} else {
			if (this.$el.find('.atavist-sheet').hasClass('store-button-type-login')) {
				$('body').addClass('logged-in');
				this.clear();
			} else {
				this.payment(data);
			}
		};

		return false;
	};

	modal.prototype.login = function() {
		var me = this;
		this.updateUserFields();
		this.ajax(this.urls.login, {
			data: this.user(),
			success: $.proxy(this.userCheck, this)
		});
		return false;
	};

	modal.prototype.signup = function() {
		if (this.$el.find('.process.atavist-is-login').length > 0) {
			return this.login();
		};

		this.updateUserFields();
		this.ajax(this.urls.signup, {
			data: this.user(),
			success: $.proxy(this.userCheck, this)
		});
		return false;
	};

	modal.prototype.updateUserFields = function() {
		var $username = $('.process-username'),
			$password = $('.process-password');
		if ($username && $username.val().length) {
			this.email = $username.val();
		};

		if ($password && $password.val().length) {
			this.password = $password.val();
		};
	};

	modal.prototype.success = function() {
		if (this.product.data.url && this.product.data.length) {
			window.location.href = this.product.data.url;
		}
		return false;
	};

	modal.prototype.changeCard = function() {
		var me = this;
		$('.atavist-login').unbind().click(function() {
			me.payment();
		});
	};

	modal.prototype.error = function(error, b, c) {
		this.message(error);
		this.changeCard();
		return this;
	};

	modal.prototype.message = function(message) {
		this.disable(false);

		$('.message')
			.html(message)
			.addClass('on')
			.click(function() {
				$(this).removeClass('on');
			});

		if (message === '') {
			$('.message').removeClass('on');
		};
	};

	modal.prototype.credentials = function(user, cb) { // cookie-based hack
		var self = this;
		this.email = user.email;
		this.password = user.password;
		$('<iframe class="login-frame" name="login-frame"></iframe>')
			.appendTo('body')
			.load(function() {
				if (cb) cb.call(self); // iframe load
			})
			.attr('src', this.urls.login);
		var product_id_html = (user.product_id instanceof Array)
			? $.map(user.product_id, function(val) { return '<input type="hidden" name="product_id[]" value="' + val + '"/>'; }).join('') // multiple
			: '<input type="hidden" name="product_id" value="' + user.product_id + '"/>', // single
			$form = $('<form method="POST" action="' + this.urls.login + '" target="login-frame">' +
				'<input type="hidden" name="iframe" value="true" />' +
				'<input type="hidden" name="password" value="' + user.password + '"/>' +
				'<input type="hidden" name="email" value="' + user.email + '"/>' +
				product_id_html +
			'</form>')
			.appendTo('body')
			.submit();
		return true;
	};

	modal.prototype.confirm = function(details) { // confirm payment details
		var me = this;
		this.update('confirm');
		this.changeCard();
		this.doTotals(details);
		var text = details.payment,
			redeemText = $('.process-finish').data('redeem-coupon-text'),
			completeText = $('.process-finish').text();
		if (this.gift === true) {
			text += '<br />' + [
				'To: ' + this.gift_recipient,
				'From: ' + ((this.gift_gifter.length) ? this.gift_gifter : 'not provided'),
				'Message: ' + ((this.gift_message.length) ? this.gift_message : 'none'),
				'Delivered on: ' + this.gift_timestamp
			].join('<br />');
		};

		if (this.code) {
			text += '<br />Coupon Code: <em>' + this.code + '</em>';
		};

		$('.story-payment').html(text);
		$('.process-finish')
			.text((details && details.free && details.free === true) ? redeemText : completeText)
			.unbind('click')
			.click(function(event) {
				var data = me.user(),
					purchases = [];
				me.products(function(product) {
					data.product_id = product.product_id;
					purchases.push(me.ajax(me.urls.purchase, {
						data: data,
						custom_error: function(data) {
							var message = '<a class="atavist-login">You may need to change your credit card information.</a>';
							if (data.error_description) message = data.error_description + '. ' + message;
							return message;
						},

						success: function() {
							product.owns = true;
						}
					}));
				});

				$.when // when deferreds are done
					.apply($, purchases)
					.done(function() {
						var errorCount = 0,
							x;
						for (x = 0; x < purchases.length; x++) {
							if (purchases[x].responseJSON && purchases[x].responseJSON.error) {
								errorCount++;
							}
						};

						if (!errorCount) {
							me.update('success');
							me.go();
						}
					});

				return false;
			});
	};

	modal.prototype.doTotals = function(data) {
		$('.story-totals').empty();
		var $ul = $('<ul/>').appendTo('.story-totals'),
			total = 0,
			symbol = '$';

		this.products(function(product) {
			var web_price = product.data.discounted_web_price ? product.data.discounted_web_price : product.data.web_price,
				productPrice = this.fromFloat(web_price),
				productPriceFormatted;
			if (product.data.symbol) symbol = product.data.symbol;
			if (product.free === true) {
				productPriceFormatted = 'Free';
			} else {
				total += productPrice;
				productPriceFormatted = symbol + (productPrice / 100);
				if (product.data) {
					if (product.data.discounted_price) {
						productPriceFormatted = product.data.discounted_price;
					} else if (product.data.price) {
						productPriceFormatted = product.data.price;
					}
				}
			};

			$('<li/>')
				.addClass('line-item')
				.html('<span>' + product.data.title + '</span><span class="amount">' + productPriceFormatted + '</span>')
				.appendTo($ul);
		});

		$ul.append($('<li/>').html('<span class="total-label">Total</span><span class="amount">' + symbol + (total / 100) + '</span>').addClass('payment-total'));
	};

	modal.prototype.attachPayment = function(response) {
		var me = this,
			data = this.user();
		data.stripe_token = response.id;
		this.ajax(this.urls.customer, {
			data: data,
			custom_error: function(data) {
				return data.error_description + ' Please try a different card.';
			},

			success: function(data) {
				me.stripe_token = response.id;
				me.hasPayment(data);
				me.confirm(data);
			}
		});
	};

	modal.prototype.hasPayment = function(data) {
		var hasPayment = (data && data.payment) ? true : false;
		$('.payment-confirm').toggle(!hasPayment);
		$('.payment-details').toggle(hasPayment);
		if (hasPayment) {
			$('.story-payment').html(data.payment);
			var confirmText = ($('button.process-payment-submit').length) ? $('button.process-payment-submit').text() : 'Confirm Credit Card';
			$('.process-payment-submit').text(confirmText);
		};

		if (as.cached_user_data && data && data.payment) {
			as.cached_user_data.payment = data.payment;
		};

		return hasPayment;
	};

	modal.prototype.stripe_publishable_key = function() {
		var stripe_key;
		this.products(function(product) {
			stripe_key = product.stripe_publishable_key;
		});

		return stripe_key;
	};

	modal.prototype.payment = function(data) { // process card info
		var me = this,
			$choiceForm = $('.process-payment-choice-form');
		this.update('payment');
		var hasPayment = false;
		if (this.hasPayment(data)) {
			hasPayment = true;
			this.changeCard();
		};

		$choiceForm.unbind().submit(function(event) {
			me.disable(true);
			if (me.gift === true) {
				me.gift_recipient = $('.gift-recipient').val();
				me.gift_message = $('.gift-message').val();
				me.gift_gifter = $('.gift-gifter').val();
				if (!me.gift_recipient.length) {
					me.error('You must enter a recipient for your gift.');
					return false;
				};

				me.$el.find('.gift-destination').text(me.gift_recipient);
				me.$el.find('.gift-date').text(me.gift_timestamp);
			};

			if (hasPayment === true) {
				me.confirm(data);
			} else {
				var cc = me.card();
				for (var i in cc) {
					if (!cc[i]) {
						me.error(config.localizations.credit_required);
						return false;
					}
				};

				Stripe.setPublishableKey(me.stripe_publishable_key());
				Stripe.createToken(cc, function(status, response) {
					if (response.error) {
						var message = (response.error.type !== 'invalid_request_error') ? response.error.message : 'Invalid request.';
						if (response.error.code && config.localizations[response.error.code]) {
							message = config.localizations[response.error.code];
						};

						me.error(message);
					} else {
						me.attachPayment(response);
					}
				});
			};

			return false;
		});

		var $couponForm = $('.process-coupon-form'),
			$couponGroup = $('.atavist-coupon-group').hide(),
			$couponToggle = $('.atavist-toggle-coupon');
		$('#atavist-add-coupon').css({display:'block', clear:'both'});
		if (this.use_coupons !== false) {
			$couponInput = $('.process-store-coupon');
			$couponToggle.unbind().click(this.toggleCoupon);
			$couponInput
				.unbind()
				.focus(function() {
					$couponForm.addClass('focus');
				})
				.blur(function() {
					$couponForm.removeClass('focus');
				});

			$couponForm.unbind().submit(function(event) {
				var data = me.user();
				data.code = $couponInput.val();
				me.ajax(me.urls.coupon, {
					data:data,
					success: $.proxy(me.couponCheck, me),
					failure: $.proxy(me.couponFailure, me)
				});
				return false;
			});
		} else {
			$couponForm.hide();
			$couponToggle.hide();
		};

		var $giftInputs = $('.gift-inputs');
		if (this.gift === true) {
			this.$el.addClass('atavist-gift');
			var today = new Date();
			if (!this.datepicker) { // ?
				this.datepicker = new Pikaday({
					field: document.getElementById('atavist-datepicker'),
					firstDay: 1,
					minDate: today,
					yearRange: [2013, 2014],
					defaultDate:new Date(),
					setDefaultDate:true,
					onSelect:function(date) {
						me.gift_timestamp = me.dateToYMD(date);
					}
				});
			};

			if (this.gift_recipient) {
				$('.gift-recipient').val(this.gift_recipient);
			};

			if (this.gift_message) {
				$('.gift-message').val(this.gift_message);
			};

			if (this.gift_gifter) {
				$('.gift-gifter').val(this.gift_gifter);
			};

			me.gift_timestamp = this.dateToYMD(today);
			$('.process-payment-submit').text('Confirm Gift Details');
			$giftInputs.show();
		} else {
			$giftInputs.hide();
		}
	};

	modal.prototype.dateToYMD = function(date) {
		return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
	};

	modal.prototype.toggleCoupon = function() {
		$('.process-coupon-form').toggleClass('atavist-show-coupon');
		$('.atavist-coupon-group').toggle();
		return false;
	};

	modal.prototype.couponCheck = function(data) {
		this.disable(false);
		if (data.code) this.code = data.code;
		if (data.price) {
			this.price(data.price);
		};

		this.toggleCoupon();
		$('.atavist-coupon').removeClass('show').addClass('has-coupon');
		if (data.free && data.free === true) {
			data.payment = 'Free via Coupon';
			if (this.product instanceof product) {
				this.product.free = true;
			};

			if (!this.gift) {
				this.confirm(data);
			}
		} else {
			this.products(function(product) {
				if (data.product_id === product.product_id) {
					product.data.discounted_web_price = data.web_price;
					product.data.discounted_price = data.price;
				}
			});
		};

		this.message(''); // Clear any bad coupon messaging
	};

	modal.prototype.couponFailure = function(data) {
		$('.atavist-coupon').removeClass('has-coupon');
		this.code = null;
		this.products(function(product) {
			product.data.discounted_web_price = null;
			product.data.discounted_price = null;
		});
	};

	modal.prototype.user = function() {
		var data = {
			email: (this.email) ? this.email : $('.process-username').val(),
			password: (this.password) ? this.password : $('.process-password').val(),
			product_id: this.product_ids() // product for ownership stats
		};
		if (this.code) data.code = this.code;
		if (this.gift === true) {
			data.gift_timestamp = this.gift_timestamp;
			data.gift_recipient = this.gift_recipient;
			data.gift_message = this.gift_message;
			data.gift_gifter = this.gift_gifter;
		};

		if (this.ref) {
			data.ref = this.ref;
		};

		return data;
	};

	modal.prototype.card = function() {
		return {
			number: $('.card-number').val(),
			cvc: $('.card-cvc').val(),
			exp_month: $('.card-expiry-month').val(),
			exp_year: $('.card-expiry-year').val(),
			address_zip:  $('.card-zip').val()
		};
	};

	// jQuery adapter
	function atavistButtonOwned($button, product) {
		$button
			.trigger('product.owned', product)
			.addClass('atavist-product-owned')
			.attr('disabled', 'disabled');
	}

	$.fn.creatavistStore = function(options) {
		return this.each(function() {
			var self = this,
				$self = $(this),
				is_gift = $self.data('gift'),
				product_list = $self.data('store'),
				success_url = $self.data('success-url'),
				product;
			if (!$.data(this, 'creatavist-store')) {
				$self.attr('disabled', 'disabled');
				as.load(product_list, function(product) {
					if (is_gift !== true) {
						if (product.length === 1 && product[0].owns === true) {
							atavistButtonOwned($self, product[0]);
							return;
						}
					};

					$self.removeAttr('disabled');
					$.data(self, 'creatavist-store', product);
					$self.click(function(e) {
						var modal = as
							.modal(product, is_gift)
							.on('owns', function() {
								if (!is_gift) {
									atavistButtonOwned($self, product);
								}
							});

						if (success_url) {
							modal.success_url = success_url;
						};

						if ($self.data('store-button-type')) {
							modal.$el.find('.atavist-sheet').addClass('store-button-type-' + $self.data('store-button-type'));
							modal.$el.find('.message').removeClass('on');
							modal.$el.find('.process-choose').addClass('atavist-is-login').removeClass('atavist-is-register');
						};

						return false;
					});

					$self.trigger('product.loaded', product);
				});
			}
		});
	};

	as.products = {};
	as.username = null;
	as.callbackCount = 1;
	as.fetch = function(product_ids, cb) {
		$.ajax({
			url: config.root + '/api/v2/store/store.php',
			dataType:'jsonp',
			cache:true,
			jsonpCallback: 'productsCallback' + as.callbackCount,
			data: {product_id: product_ids },
			error:function(err1, err2, err3) {
				// console.log("ERR?", err1,err2,err3)
			},

			success:function(result) {
				if (result.products) {
					if (cb) cb(result.products);
				};

				if (result.user) {
					as.username = result.user.username;
					$('body').addClass('logged-in');
				}
			}
		});

		as.callbackCount++;
	};

	as.get = function(product_id) {
		if (this.products[product_id]) {
			return this.products[product_id];
		};

		return null;
	};

	as.modal = function(products, is_gift) {
		var modalProducts = [];
		if (products instanceof Array === false) {
			products = [products];
		};

		for (var i = 0; i < products.length; i++) {
			if (typeof products[i] === 'string') { // string, e.g. 'com.whatever'
				products[i] = this.products[ products[i] ];
			};

			if (products[i] instanceof product) { // an actual product itself
				products[i].gift = (is_gift === true) ? true : false;
				modalProducts.push(products[i]);
				products[i].emit('modal');
			}
		};

		if (modalProducts.length < 1) return;
		return new modal(modalProducts.length > 1 ? modalProducts : modalProducts[0]);
	};

	as.flat = function(product_ids) {
		var flat = [],
			product_ids = (typeof product_ids === 'string') ? [product_ids] : product_ids;
		for (var i in this.products) {
			if (this.products[i].available() === true && product_ids.indexOf(i) !== -1) {
				flat.push(this.products[i]);
			}
		};

		return flat;
	};

	as.load = function(product_ids, cb) {
		var deferreds = [],
			unloaded = [],
			product_ids = (typeof product_ids === 'string') ? [product_ids] : product_ids;

		for (var i = 0; i < product_ids.length; i++) {
			var product_id = product_ids[i],
				prod = as.products[product_id];
			if (!prod) {
				as.products[product_id] = prod = new product({
					product_id: product_ids[i]
				});
			};

			if (prod.loaded === false && prod.loading !== true) {
				prod.loading = true;
				unloaded.push(product_id);
			};

			deferreds.push(
				as.products[product_id].deferred().promise()
			);
		};

		if (unloaded.length > 0) { // some of these may need loading
			as.fetch(unloaded, function(loaded_products) {
				for (var product_id in loaded_products) {
					if (as.products[product_id]) {
						as.products[product_id].load(loaded_products[product_id]);
					}
				};
			});
		}

		return $.when // when deferreds are done
			.apply($, deferreds)
			.done(function() {
				if (cb) cb.call(as, as.flat(product_ids));
			});

	};

	$(document).ready(function() {
		$('[data-store]').creatavistStore();
	});

})(creatavistStore, jQuery);
