var atavistStore = {
	root:"creatavist.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\">\n\t\t\t<div class=\"atavist-sheet\">\n\t\t\t\t<div id=\"atavist-header\">\n\t\t\t\t\t<div class=\"story-title\"><\/div>\n\t\t\t\t\t<div class=\"story-price\"><\/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<div class='message'><\/div>\n\t\t\t\t\t\t<form action=\"https:\/\/creatavist.com\/cms\/\" method=\"POST\" class=\"atavist-form\">\n\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<label>Email<\/label>\n\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<\/div>\n\t\t\t\t\t\t\t<div class=\"form-row\">\n<label>Password<\/label>\t\t\t\t\t\t\t\t<input class='process-password' type='password' name='password' placeholder='Password' \/>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t<button class='btn atavist process-button process-login' value=\"login\">\nLogin\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<span> Or <\/span>\t\n\t\t\t\t\t\t\t\t\t<button class='btn atavist process-button process-signup' value=\"signup\">Signup<\/button>\t\t\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t<a class=\"atavist-forgot\" href=\"https:\/\/creatavist.com\/cms\/forgot\/\">Forgot your password?<\/a>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/form>\t\t\t\t\t<\/div>\n\t \t\t\t\t<div class='process process-payment'>\n\t\t\t\t\t\t<div class='message'><\/div>\n\t\t\t\t\t\t<form action=\"\" method=\"POST\" class=\"atavist-form process-coupon-form\">\n\t \t\t\t\t\t\t<div class=\"atavist-coupon-group\">\n\t\t\t\t\t\t\t\t<div class=\"form-row coupon\">\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"2\" placeholder='Coupon code' size=\"2\" class=\"process-store-coupon\" \/>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row coupon\">\n\t\t\t\t\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\t\t\t\t<span> Or <\/span>\t\n\t\t\t\t\t\t\t\t\t<button type=\"submit\" class=\"btn atavist atavist-toggle-coupon submit-button\">Cancel<\/button>\n\t\t\t\t\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\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/form>\t\t\t\t\n\t\t\t\t\t\t<form action=\"\" method=\"POST\" class=\"atavist-form process-payment-choice-form\">\n\t\t\t\t\t\t\t<div class=\"form-row payment-details\">\n\t\t\t\t\t\t\t\t<span class=\"story-payment\"><\/span>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"atavist-login\">(change)<\/a>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"payment-confirm\">\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<label>Card Number<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"20\" autocomplete=\"off\" class=\"card-number\"\/>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row cvc\">\n\t\t\t\t\t\t\t\t\t<label>CVC<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"4\" autocomplete=\"off\" length=\"4\" class=\"card-cvc\" \/>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row expiry\">\n\t\t\t\t\t\t\t\t\t<label>Expiration<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"2\" placeholder='MM' size=\"2\" class=\"card-expiry-month\" \/>\n\t\t\t\t\t\t\t\t\t<span class='card-expiry-slash'> \/ <\/span>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" placeholder='YYYY' size=\"4\" class=\"card-expiry-year\" \/>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"form-row zip\" style='float:left;'>\n\t\t\t\t\t\t\t\t\t<label>Zip \/ Postal Code<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"5\" placeholder='' size=\"5\" class=\"card-zip\" \/>\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t \t\t\t\t\t\t<a href=\"#\" class=\"atavist-toggle-coupon\" id=\"atavist-add-coupon\">Add a coupon code...<\/a>\n\t\t\t\t\t\t\t<div class=\"gift-inputs\">\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<label>Gift Recipient Email<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"email\" size=\"5\" placeholder='email address' size=\"5\" class=\"gift-recipient\" \/>\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t<\/div>\t\t\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<label>Gift Message<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" size=\"5\" placeholder='message' size=\"5\" class=\"gift-message\" \/>\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t<\/div>\t\t\n\t\t\t\t\t\t\t\t<div class=\"form-row\">\n\t\t\t\t\t\t\t\t\t<label>Gift delivery date<\/label>\n\t\t\t\t\t\t\t\t\t<input type=\"text\" id=\"atavist-datepicker\"><\/input>\n\t\t\t\t\t\t\t\t<\/div>\t\t\t\t\t\t\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<button type=\"submit\" class=\"btn atavist process-payment-submit submit-button\">Verify Credit Card<\/button>\n\t\t\t\t\t\t<\/form>\n\t\t\t\t\t<\/div>\n\t \t\t\t\t<div class='process process-confirm'>\n\t\t\t\t\t\t<div class='message'><\/div>\n\t \t\t\t\t\t<form action=\"\" method=\"POST\" class=\"atavist-form\">\n\t \t\t\t\t\t\t<div class=\"form-row payment-details\">\n\t \t\t\t\t\t\t\t<span class=\"story-payment\"><\/span>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"atavist-login\">(change)<\/a>\n\t \t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"story-totals\"><\/div>\n\t\t\t\t\t\t\t<button type=\"submit\" class=\"btn atavist submit-button process-finish\" data-redeem-coupon-text=\"Redeem Coupon\">Complete Purchase<\/button>\n\t\t\t\t\t\t<\/form>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n\t<\/div>",
	loginTemplate:"<form action=\"https:\/\/creatavist.com\/cms\/\" method=\"POST\" class=\"atavist-form\">\n\t\t<div class=\"form-row\">\n\t\t<label>Email<\/label>\n\t\t<input class='process-username' type='email' name='username' placeholder='Email' \/>\n\t<\/div>\n\t<div class=\"form-row\">\n<label>Password<\/label>\t\t<input class='process-password' type='password' name='password' placeholder='Password' \/>\n\t<\/div>\n\t<div class=\"form-row\">\n\t\t<button class='btn atavist process-button process-login' value=\"login\">\nLogin\t\t\t\t\t<\/button>\n\t\t\t<span> Or <\/span>\t\n\t\t\t<button class='btn atavist process-button process-signup' value=\"signup\">Signup<\/button>\t\t\n\t<\/div>\n\t<div class=\"form-row\">\n\t\t<a class=\"atavist-forgot\" href=\"https:\/\/creatavist.com\/cms\/forgot\/\">Forgot your password?<\/a>\n\t<\/div>\n<\/form>",
	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;

}));var atavistStore = (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;
	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($el) {
		var me = this,
			barebones = ($el.attr('data-barebones')) ? true : false,
			priceSelector = $el.attr('data-price'),
			textSelector = $el.attr('data-text'),
			readText = $el.attr('data-read-text'),
			buyText = $el.attr('data-buy-text'),
			loginText = $el.attr('data-login-text'),
			signupText = $el.attr('data-signup-text'),
			explanatoryText = $el.attr('data-explanatory-text');
		this.$el = $el;
		this.product_id = $el.attr('data-store');
		this.gift = $el.data('gift') || false;
		this.ref = getParameterByName('ref') || $el.data('ref') || false;
		this.success_url = $el.data('success-url') || false;
		if (barebones === false) {
			this.$text = ($(textSelector).length) ? $(textSelector) : $('<span/>').addClass('text').appendTo(this.$el);
			this.$price = ($(priceSelector).length) ? $(priceSelector) : $('<span/>').addClass('price').appendTo(this.$el);			
		}
		this.readText = (readText) ? readText : "Read";
		this.buyText = (buyText) ? buyText : "Buy";
		if (signupText) {
			this.signupText = signupText;
		}
		if (loginText) {
			this.loginText = loginText;
		}
		if (explanatoryText) this.explanatoryText = explanatoryText;
	};
	product.prototype.price = function(amt) { 
		if (this.$price) this.$price.text(amt); 
	}
	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.loadOptions = function(options,value) {
		if (typeof options === 'undefined') return;
		if (typeof options === 'string') { // methods?
			if (this[options] && typeof this[options] === 'function') this[options](value)
		} else { // proper options?
			if (options.onRead) this.onRead = options.onRead			
			if (options.onLoad) this.onLoad = options.onLoad			
		}
	}
	product.prototype.user = function(username) {
		this.data.username = username;
	}
	product.prototype.close = function() {

	}
	product.prototype.load = function(data) {
		this.data = data;
		this.$el.addClass('atavist-type-'+this.data.product_type);
		if (data.stripe_publishable_key) this.stripe_publishable_key = data.stripe_publishable_key;
		if (this.available() == true) {
			this.$el.click($.proxy(this.click,this));
			var access = this.accessible();
			if ( access !== true ) {
				this.price(this.data.price);
			} else {
				if (this.data.product_type == "subscription") this.$el.attr('disabled','disabled')
			}
			if (this.$text) {
				this.$text.text( (access) ? this.readText : this.buyText );
			}
			this.$el.addClass("atavist-access-"+access);
		} else { // not for sale
			this.$el.attr('disabled','disabled')
			if (this.availability() === 4) {
				this.$el.remove();
			}
			if (this.$text) {
				this.$text.text(
					this.availability() === 4 
						? 'Not available in this country'
						: 'Not for sale'
				);
			}
		}
		this.type = (this.data.product_type && this.data.product_type == "subscription") ? "subscription" : "story";
		if (this.onLoad) this.onLoad.call(this.$el, {
			accessible : this.accessible(),
			available : this.available()
		})
	}
	product.prototype.go = function() {
		if (this.success_url && this.success_url.length) {
			window.location.href = this.success_url;
			return;
		}
		if (this.gift === true) {
			this.close()
			return
		}
		if (this.data.product_type && this.data.product_type == "subscription") {
			window.location.reload();
		} else {
			if (this.onRead) {
				this.onRead.call(null,this.data.url)
			} else {
				window.location.href = this.data.url;				
			}
		}
	}
	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 plan = function($el) {
		var me = this;
		this.$el = $el;
		this.plan = this.product_id  = $el.data('plan')
		this.price = $el.data('price');
		this.title  = $el.data('plan-title');
		this.$el.on('click',$.proxy(this.click,this))
		this.success_url = $el.data('success-url') || false;
		this.data = {
			title: this.title,
			price: this.price
		}
		if (this.price === 0) {
			this.data.price = "Free";
			this.data.free = true;
		}
		this.type = "plan"
	}
	plan.prototype.go = function() {
		if (this.success_url && this.success_url.length) {
			window.location.href = this.success_url;
		} else {
			window.location.href = window.location.href;			
		}
	}
	plan.prototype.click = function() {
		var m = new modal(this);
		m.urls.login = config.root+'/api/v2/plans/login.php';
		m.urls.customer = config.root+'/api/v2/plans/customer.php';
		m.urls.purchase = config.root+'/api/v2/plans/purchase.php';
		return false;
	}
	var modal = function(product) {
		this.mode = 'choose'; // starts with login or signup
		product.close = this.clear
		this.product = product;
		this.create(product.data);
		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.$wrapper.toggleClass("atavist-gift", (this.product.gift === true))
		if (this.product.type === "subscription") this.$wrapper.addClass("atavist-subscription")
		if (this.product.type === "plan" && this.product.plan === "free") {
			$(".story-price").remove();
		}
	}
	modal.prototype.clear = function() {
		$('.atavist-wrapper').removeClass('atavist-show').remove();
	}
	modal.prototype.create = function(data) {
		var me = this;
		var 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');
		setTimeout(function(){ me.$el.addClass('atavist-show'); },0); // fade in
		$('.atavist-overlay').click(this.clear);
		$('.atavist-offset').click(this.clear);
		$('.atavist-sheet').click(function(e){ e.stopPropagation(); });
		$(".story-title").text(data.title);
		if (data.cname) $(".atavist-forgot").attr('href', data.cname + '/cms/forgot/');	
		var symbol = "";
		if (data.free && data.free === true) {
			$(".story-price").addClass("atavist-free");
		} else {
			symbol = (data.symbol) ? data.symbol : cash;
		}
		this.price(data.price);
		if (data.username) $(".process-username").val(data.username);
		$(".process-login").click($.proxy(this.login,this)).text(this.product.loginText);
		$(".process-signup")
			.click($.proxy(this.signup,this));
		if (this.product.signupText) {
			$(".process-signup").text(this.product.signupText);
		}
		var $initialForm = $(".process-choose form").submit(function(e) { return false; });
		if (this.product.explanatoryText) $initialForm.prepend($("<div/>").addClass("form-row form-custom").text(this.product.explanatoryText))
		// for (var i in injectionTargets) {
		// 	var id = injectionTargets[i];
		// 	var $target = $('.atavist .' + id);
		// 	var $fromSet = $(".atavist-injection[data-target='" + id + "']");
		// 	if ($fromSet.length && $target.length) {
		// 		$fromSet.each(function(){
		// 			var fromData = $(this).data();
		// 			if (!fromData.placement) fromData.placement = 'after';
		// 			var $new = $('<div/>').addClass(id + '-' + fromData.placement).html($(this).html());
		// 			if (fromData.placement == 'before') {
		// 				$new.insertBefore($target);	
		// 			} else {
		// 				$new.insertAfter($target);
		// 			}
		// 		});
		// 	}
		// }
	}
	modal.prototype.price = function(text) {
		$(".story-price").text(text);
	}
	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;
		opts.success = function(data) {
			if (data.error) {
				if (opts.custom_error) {
					data.error_description = opts.custom_error(data);
				}
				me.error(data.error_description);
			} else {
				if (success) success(data);
			}
		}
		opts.error = $.proxy(this.error,this);			
		$.ajax(opts);
	}
	modal.prototype.userCheck = function(data) {
		var me = this;
		this.disable(true);
		this.credentials(this.user());
		if (data.owns === true && me.product.gift === false) {
			setTimeout(function() { me.product.go(); }, 3000); //crude
		} else {
			this.payment(data);
		}
		return false;
	}
	modal.prototype.login = function() {
		var me = this;
		this.ajax(this.urls.login,{
			data: this.user(),
			success: $.proxy(this.userCheck,this)
		});
		return false;
	}
	modal.prototype.signup = function() {
		this.ajax(this.urls.signup,{
			data: this.user(),
			success: $.proxy(this.userCheck,this)			
		});
		return false;
	}
	modal.prototype.changeCard = function() {
		var me = this;
		$(".atavist-login").unbind().click(function() { 
			
			me.payment() 
		});
	}
	modal.prototype.error = function(error,b,c) {
		this.disable(false);
		$(".message")
			.html(error)
			.addClass('on')
			.click(function() {
				$(this).removeClass('on')
			});
		this.changeCard();
		return this;
	}
	modal.prototype.credentials = function(user) { // cookie-based hack
		this.email = user.email;
		this.password = user.password;
		$('<iframe class="login-frame" name="login-frame"></iframe>')
			.appendTo("body")
			.load(function() {
				console.log('load?')
			})
			.attr("src", this.urls.login);
		var $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+"'/><input type='hidden' name='product_id' value='"+user.product_id+"'/></form>")
			.appendTo("body")
			.submit();
		return true;
	}
	modal.prototype.confirm = function(details) { // confirm payment details
		var me = this
		this.update('confirm')
		this.changeCard();
		this.checkTax(details)
		var text = details.payment,
			redeemText = $(".process-finish").data('redeem-coupon-text'),
			completeText = $(".process-finish").text();
		if (this.product.gift === true) {
			text += "<br />" + [
				"To: " + this.gift_recipient,
				"Message: " + ((this.gift_message.length) ? this.gift_message : "none"),
				"Delivered on: " + this.gift_timestamp
			].join("<br />")
		}
		$(".story-payment").html(text)
		$(".process-finish")
			.text( (details && details.free && details.free === true) ? redeemText : completeText )
			.unbind('click')
			.click(function(event) {
				var data = me.user();
				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(data) {
						$(".atavist-wrapper").addClass("atavist-finished");
						me.product.go();
					}
				});
				return false;
			})
		if (this.product.type === "plan" && this.product.plan === "free") {
			$(".process-finish").text("Switch to Free")
		}
	}
	modal.prototype.checkTax = function(data) {
		if (data.tax) {
			$(".story-totals")
				.empty()
				.append( $("<div/>").text(data.cost+" + "+ data.tax+" (NY state sales tax)") )
				.append( $("<div/>").text("Total: " + data.total).addClass('payment-total') )
		} else {
			$(".story-totals")
				.empty()			
		}
	}
	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").text(data.payment)
			var confirmText = ($('button.process-payment-submit').length) ? $('button.process-payment-submit').text() : 'Confirm Credit Card';
			$(".process-payment-submit").text(confirmText)
		}
		return hasPayment;
	}
	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();
		}
		if (this.product.type === "plan" && this.product.plan === "free") {
			me.confirm(data);
			return;
		}
		$choiceForm.unbind().submit(function(event) {
			me.disable(true);
			if (me.product.gift === true) {
				me.gift_recipient = $('.gift-recipient').val()
				me.gift_message = $('.gift-message').val()
				if (!me.gift_recipient.length) {
					me.error("You must enter a recipient for your gift.")
					return false;
				}
			}
			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;
					}
				}
				if (me.product.stripe_publishable_key) Stripe.setPublishableKey(me.product.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.product.type === "story" || this.product.type === "subscription")) {
		$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)
				});
				return false;
			});			
		} else {
			$couponForm.hide();
			$couponToggle.hide()
		}
		var $giftInputs = $(".gift-inputs")
		if (this.product.gift === true) {
		    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)
			        }
			    });
		    }
		    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()
		if (data.free && data.free === true) {
			data.payment = "Free via Coupon";
			this.confirm(data)
		}
	}
	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.product_id // product for ownership stats
		}
		if (this.code) data.code = this.code;
		if (this.product.gift === true) {
			data.gift_timestamp = this.gift_timestamp
			data.gift_recipient = this.gift_recipient
			data.gift_message = this.gift_message
		}
		if (this.product.ref) {
			data.ref = this.product.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()
		};
	}
	var user = function(el, options) {
		this.$el = $(el);
		if (typeof options === 'undefined') options = {};
		this.options = options;
		if (this.$el.data('atavist-user-provides')) {
			this.provide = this.$el.data('atavist-user-provides')
		}
		this.$el.attr('data-atavist-user','') // perhaps a bad way to do this?
		return this;
	}
	user.prototype.load = function(el_this,data) {
		this.$el.addClass('loaded')
		if (typeof data === 'undefined') data = {}
		this.data = data;
		if (this.options.ready) this.options.ready.call(el_this,data);
		if (this.provide && data[this.provide]) {
			this.$el.text(data[this.provide])
		}
	}
	user.prototype.unload = function(el_this) {
		if (this.options.none) this.options.none.call(el_this);
	}
	$.fn.atavistForm = function(options) {
        return this.each(function () {
        	options = (options) ? options : {}
        	options.ready = function(user) {
				$(this)
					.addClass("loaded user")
					.html("<span class='username'>"+user.username+"</span>")
					.append("<span class='avatar' style=\"background-image:url('"+user.avatar+"');\"></span>")
					.attr("href", config.root + "/cms/account/library")
        	}
        	options.none = function() {
        		var classes = ["loaded","no-user"];
        		if ($(this).data('login-dropdown')) classes.push('atavist-login-dropdown')
        		$(this)
					.addClass(classes.join(' '))
					.append(config.loginTemplate)
        		// $(this).text("NO USER")
        	}
            if (!$.data(this, 'user')) {
                $.data(this, 'user', new user( this, options ));
            }
        });		
	}
	$.fn.atavistUser = function (options) {
        return this.each(function () {
            if (!$.data(this, 'user')) {
                $.data(this, 'user', new user( this, options ));
            }
        });
	}
	$.fn.atavistStore = function (options, arg, product_id) {
        return this.each(function () {
            if (!$.data(this, 'store')) {
            	var p = new product( $(this), options );
            	ids.push(p.product_id)
                $.data(this, 'store', p);
            } else {
            	var p =  $.data(this, 'store')
            }
	    	if (options) {
	    		if (product_id && product_id !== p.product_id) return; // filter by product_id
	    		p.loadOptions(options, arg)
	    	}
        });
	}

	$(document).ready(function() {
		var $ps = $("[data-store]").atavistStore()
		var $plans = $("[data-plan]").each(function() {
			var p = new plan($(this))
		})
		$("[data-atavist-user]").atavistUser();
		$("[data-atavist-login]").atavistForm();
		$.ajax({
			url: config.root+'/api/v2/store/store.php',
			dataType:'jsonp',
			cache:true,
			jsonpCallback : 'productsCallback',
			data:{'product_id':ids},
			success:function(result) {
				if (result.user)  {
					$('[data-atavist-user]').each(function(i,u) { $(u).data('user').load( this, result.user) });
				} else {
					$('[data-atavist-user]').each(function(i,u) { $(u).data('user').unload( this ) });					
				}
				if (!result.products) return;
				$.each(result.products,function(i,product) {
					if (product.product_id && $.inArray(product.product_id,ids) !== -1) {
						$ps.atavistStore('load', product, product.product_id)
						if (result.user) $ps.atavistStore('user', result.user.username, product.product_id)
						// $ps.atavistStore({
						// 	product:
						// })
						// list[product.product_id].load(product);
						// if (result.user) list[product.product_id].data.username = result.user.username;
					} else {
						// this story is not yet for sale
					}
				});
			}
		})
	});
})(atavistStore,jQuery);