// localize dates and times
window.addEvent('domready', function () {
    $$('time[datetime$=00:00]').each(function (element) {
        var d, date, datetime, offset, spans;
        spans = element.getChildren();
        d = new Date().parse(element.get('datetime'));
        offset = d.format('%T'); // e.g. +1300
        offset = offset.slice(0, 3) + ':' + offset.slice(3); // e.g. +13:00
        datetime = d.format('%Y-%m-%dT%H:%M') + offset;
        date = d.getDate() + d.format(' %B %Y');

        if (element.getParent().get('tag') == 'article') { // large post date
            element.set({
                datetime: datetime,
                html: [d.getDate(), d.getMonth() + 1, d.format('%Y')].join('/')
            });
        } else if (spans.length == 2) { // post meta / comments
            element.set('datetime', datetime);
            spans[0].set('html', date);
            spans[1].set('html', d.format('%I:%M\u2009%p').toLowerCase());
        }
    });
});

window.addEvent('domready', function () {
    var article, date, gmtoffset, img, spans, strong, time,
        gravatar = { s: 32, d: escape('http://blog.mtg-apps.com/static/images/no-gravatar.gif').replace(/\//g, '%2F') },
        comments = document.id('comments'),
        respond = document.id('respond'),
        author_name = document.id('author_name'),
        author_email = document.id('author_email'),
        author_url = document.id('author_url');

    function md5(data) {
        // http://www.onicos.com/staff/iz/amuse/javascript/expert/md5.txt
        var MD5_T,MD5_round1,MD5_round2,MD5_round3,MD5_round4,MD5_round;MD5_T=[0,3614090360,3905402710,606105819,3250441966,4118548399,1200080426,2821735955,4249261313,1770035416,2336552879,4294925233,2304563134,1804603682,4254626195,2792965006,1236535329,4129170786,3225465664,643717713,3921069994,3593408605,38016083,3634488961,3889429448,568446438,3275163606,4107603335,1163531501,2850285829,4243563512,1735328473,2368359562,4294588738,2272392833,1839030562,4259657740,2763975236,1272893353,4139469664,3200236656,681279174,3936430074,3572445317,76029189,3654602809,3873151461,530742520,3299628645,4096336452,1126891415,2878612391,4237533241,1700485571,2399980690,4293915773,2240044497,1873313359,4264355552,2734768916,1309151649,4149444226,3174756917,718787259,3951481745];MD5_round1=[[0,7,1],[1,12,2],[2,17,3],[3,22,4],[4,7,5],[5,12,6],[6,17,7],[7,22,8],[8,7,9],[9,12,10],[10,17,11],[11,22,12],[12,7,13],[13,12,14],[14,17,15],[15,22,16]];MD5_round2=[[1,5,17],[6,9,18],[11,14,19],[0,20,20],[5,5,21],[10,9,22],[15,14,23],[4,20,24],[9,5,25],[14,9,26],[3,14,27],[8,20,28],[13,5,29],[2,9,30],[7,14,31],[12,20,32]];MD5_round3=[[5,4,33],[8,11,34],[11,16,35],[14,23,36],[1,4,37],[4,11,38],[7,16,39],[10,23,40],[13,4,41],[0,11,42],[3,16,43],[6,23,44],[9,4,45],[12,11,46],[15,16,47],[2,23,48]];MD5_round4=[[0,6,49],[7,10,50],[14,15,51],[5,21,52],[12,6,53],[3,10,54],[10,15,55],[1,21,56],[8,6,57],[15,10,58],[6,15,59],[13,21,60],[4,6,61],[11,10,62],[2,15,63],[9,21,64]];function MD5_F(a,c,b){return(a&c)|(~a&b);}function MD5_G(a,c,b){return(a&b)|(c&~b);}function MD5_H(a,c,b){return a^c^b;}function MD5_I(a,c,b){return c^(a|~b);}MD5_round=[[MD5_F,MD5_round1],[MD5_G,MD5_round2],[MD5_H,MD5_round3],[MD5_I,MD5_round4]];function MD5_pack(a){return String.fromCharCode(a&255)+String.fromCharCode((a>>>8)&255)+String.fromCharCode((a>>>16)&255)+String.fromCharCode((a>>>24)&255);}function MD5_number(a){while(a<0){a+=4294967296;}while(a>4294967295){a-=4294967296;}return a;}function MD5_apply_round(n,w,i,h,e){var m,l,k,j,g,v,p,q,o;m=h[0];l=h[1];k=h[2];j=h[3];g=e[0];v=e[1];p=e[2];o=i(w[l],w[k],w[j]);q=w[m]+o+n[g]+MD5_T[p];q=MD5_number(q);q=((q<<v)|(q>>>(32-v)));q+=w[l];w[m]=MD5_number(q);}function MD5_hash(l){var c,q,b,t,p,o,m,n,a,h,e,d,g;b=[1732584193,4023233417,2562383102,271733878];p=l.length;o=p&63;m=(o<56)?(56-o):(120-o);if(m>0){l+="\x80";for(h=0;h<m-1;h++){l+="\x00";}}l+=MD5_pack(p*8);l+=MD5_pack(0);p+=m+8;c=[0,1,2,3];q=[16];t=[4];for(d=0;d<p;d+=64){for(h=0,e=d;h<16;h++,e+=4){q[h]=l.charCodeAt(e)|(l.charCodeAt(e+1)<<8)|(l.charCodeAt(e+2)<<16)|(l.charCodeAt(e+3)<<24);}for(h=0;h<4;h++){t[h]=b[h];}for(h=0;h<4;h++){n=MD5_round[h][0];a=MD5_round[h][1];for(e=0;e<16;e++){MD5_apply_round(q,t,n,c,a[e]);g=c[0];c[0]=c[3];c[3]=c[2];c[2]=c[1];c[1]=g;}}for(h=0;h<4;h++){b[h]+=t[h];b[h]=MD5_number(b[h]);}}return MD5_pack(b[0])+MD5_pack(b[1])+MD5_pack(b[2])+MD5_pack(b[3]);}function MD5_hexhash(e){var d,b,f,a;a=MD5_hash(e);b="";for(d=0;d<16;d++){f=a.charCodeAt(d);b+="0123456789abcdef".charAt((f>>4)&15);b+="0123456789abcdef".charAt(f&15);}return b;}
        return MD5_hexhash(data);
    }
    function update_time() {
        date = new Date();
        gmtoffset = date.get('gmtoffset');
        time.set('datetime', date.format('%Y-%m-%dT%H:%M') + gmtoffset.substr(0, 3) + ':' + gmtoffset.substr(3));
        spans[0].set('text', date.get('date') + date.format(' %B %Y'));
        spans[1].set('text', date.format('%I:%M\u2009%p').toLowerCase());
    }
    function update_gravatar() {
        img.set('src', 'http://www.gravatar.com/avatar/' + md5(this.value.trim().toLowerCase()) +
                '?s=' + gravatar.s + '&d=' + gravatar.d);
    }
    function update_name() {
        var url = author_url.value.trim(),
            name = this.value.trim() || 'Anonymous';
        strong.set('html', url ? '<a href="' + url + '">' + name + '</a>' : name);
    }

    // don't proceed unless the page contains the necessary elements
    if (author_name && author_email && author_url && respond && $$('textarea:not(.wmd-ignore)').length) {

        // create a new article element to contain the comment preview
        article = new Element('article', {
            html: '<div class="wmd-preview"></div><footer><img alt="" /><strong></strong><time><span></span><span></span></time></footer>'
        });

        // add the current date and time
        time = article.getElements('time')[0];
        spans = time.getChildren();
        update_time(); // init
        update_time.periodical(60000); // update time every minute

        // tie the author's avatar in the preview to the `author_email` field in the comment form
        img = article.getElements('img')[0];
        update_gravatar.bind(author_email)(); // init
        author_email.addEvent('change', update_gravatar);

        // tie the author's name in the preview to the `author_name` field in the comment form
        strong = article.getElements('strong')[0];
        update_name.bind(author_name)(); // init
        author_name.addEvents({ 'change': update_name, 'keyup': update_name });
        author_url.addEvent('change', update_name.bind(author_name));

        // inject the comment preview into the document
        if (!comments) {
            comments = new Element('h2', { id: 'comments', text: 'Comments' });
            comments.inject(respond, 'before');
        }
        article.inject(respond, 'before');
    }
});

wmd_options = {
    output: 'Markdown', // format sent to the server
    lineLength: 50,     // line wrapping length for lists, blockquotes, etc.
    buttons: 'bold italic | link blockquote image | ol ul',
    autostart: true
};
