/* global pricely */
"use strict";
pricely.ui = jQuery(document);

//===============
// Init Functions
//===============
pricely.init = function (container) {


    pricely.init_timer(container);
};

pricely.init_timer = function (container) {

    pricely.find_element(container, '.pricely-timer').each(function () {

        var pricely_timer = jQuery(this);

        var dt = pricely_timer.attr('data-pricely_datetime');
        var dt_refresh = pricely_timer.attr('data-pricely_refresh');
        var dt_url = pricely_timer.attr('data-pricely_url');

        pricely_timer.countdown(dt).on('update.countdown', function (event) {

            pricely_timer.find('.prly-tm-days').html(event.strftime('%D'));
            pricely_timer.find('.prly-tm-hours').html(event.strftime('%H'));
            pricely_timer.find('.prly-tm-minutes').html(event.strftime('%M'));
            pricely_timer.find('.prly-tm-seconds').html(event.strftime('%S'));

        }).on('finish.countdown', function (event) {

            if ('yes' === dt_refresh) {

                if ('' !== dt_url) {

                    location.href = dt_url;
                } else {

                    location.reload();
                }
            }

        });

    });

};


//===============
// Base functions
//===============
pricely.find_element = function (container, selector) {
    if (false === container || undefined === container) {

        return jQuery(selector);
    }

    return container.find(selector);
};


pricely.ui.ready(function ($) {

    pricely.init(false);
});