if(window['console'] === undefined) window.console = {log: function(){}};

var Cookie = {
    data: {},
    options: {expires: 1, domain: "", path: "", secure: false},

    init: function(options, data) {
        Cookie.options = Object.extend(Cookie.options, options || {});

        var payload = Cookie.retrieve();
        if(payload) {
            Cookie.data = payload.evalJSON();
        }
        else {
            Cookie.data = data || {};
        }
        Cookie.store();
    },
    getData: function(key) {
        return Cookie.data[key];
    },
    setData: function(key, value) {
        Cookie.data[key] = value;
        Cookie.store();
    },
    removeData: function(key) {
        delete Cookie.data[key];
        Cookie.store();
    },
    retrieve: function() {
        var start = document.cookie.indexOf(Cookie.options.name + "=");

        if(start == -1) {
            return null;
        }
        if(Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
            return null;
        }

        var len = start + Cookie.options.name.length + 1;
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        }
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
        var expires = '';

        if (Cookie.options.expires) {
            var today = new Date();
            expires = Cookie.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = Cookie.options.name + '=' + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires;
    },
    erase: function() {
        document.cookie = Cookie.options.name + '=' + Cookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    getOptions: function() {
        return (Cookie.options.path ? ';path=' + Cookie.options.path : '') + (Cookie.options.domain ? ';domain=' + Cookie.options.domain : '') + (Cookie.options.secure ? ';secure' : '');
    }
};


var Custom = Class.create({
    initialize: function() {

        Cookie.init({name: 'MORE', expires: 90});

        if ($('mail')) {
            $('mail').writeAttribute('href', $('mail').readAttribute('href').replace('.nospam.', '@'));
            $('mail').update($('mail').innerHTML.replace('.nospam.', '@'));
        }

        // if ($('commentfold')) new S2.UI.Accordion('commentfold', { multiple: true });

        $$('.home .post').each(this.clickablePost.bind(this));

        if ($('lightbox-opener')) {
            setTimeout(this.openLightbox, 2000);
        }
    },
    clickablePost: function(el) {
        el.observe('click', function() {
            window.location = el.select('h1 a')[0].href;
        });
    },
    openLightbox: function() {

        if (Cookie.getData('lightbox')) return false;

        Cookie.setData('lightbox', true);

        if (window.lightbox) window.lightbox.start($('lightbox-opener'));
    }
})

    document.observe('dom:loaded', function(){
        new Custom();
    });
