/**
 * CookieRedirects
 */

var CookieRedirects = new Class({

    options: {
        cookie_name: 'autoredirect'
    },

    initialize: function() {
        
    },

    attach_events: function (elements) {
        this.elements = elements;
        this.elements.forEach(function(element) {
            if (element.get('href')) {
                element.addEvent('click', this.set_cookie.bind(this));
            }
        }, this);
    },

    check_cookie: function () {
        this.cookie = Cookie.read(this.options.cookie_name);
        if (this.cookie) {
            window.location.href = this.cookie;
        }
    },

    set_cookie: function (event) {
        var href = event.target.get('href');
        if (href) {
            this.cookie = Cookie.write(this.options.cookie_name, href, {duration: 365});
        }
    }

});
