function embedlj()
{
    var ul0 = $('<ul/>').attr('class', 'lj_list');
    var ul = $('<ul/>').attr({'class': 'lj_list', 'id': 'lj_list'});
    
    for (var i=0, post; i < 10 && (post = LJ.entries[i]); i++) {

        var subject = post.subject;
        subject = subject.replace(/&hellip;/, '…');

        var li = $('<li/>').append(
            $('<a/>').attr({'href': post.url, 'rel': 'external'})
                     .html(subject)
        );
        
        if (false && post.tags.length) {
            var small = $('<span/>').attr('class', 'small').append(' [');
            
            for (var j=0, tag; tag = post.tags[j]; j++) {
                small.append($('<a/>').attr('href', tag.url)
                     .html(tag.name));
                if (post.tags[j+1])
                    small.append('|');
            }
            
            li.append(small.append(']'));
        }
        if (i == 0) {
            ul0.append(li);
        }
        else {
            ul.append(li);
        }
    }
    var hide = $('#hide_lj_list');
    $(hide).click(function(){
        $('#show_lj_list').attr('style', 'display: inline');
        $('#hide_lj_list').hide();
        $('#lj_list').slideToggle('slow');
        toggleBlogCookie();
    });

    var show = $('#show_lj_list');
    $(show).click(function(){
        $('#show_lj_list').hide();
        $('#hide_lj_list').attr('style', 'display: inline');
        $('#lj_list').slideToggle('slow');
        toggleBlogCookie();
    });
    
    // Set via a cookie
    if (isBlogCookieSet()) {
        show.hide();
        hide.show();
		ul.show();
    }
    else {
        show.show();
        hide.hide();
        ul.hide();
    }

 	$('#livejournal_contents').append(ul0).append(ul);  
	$('#livejournal_contents').show();
}

$(document).ready(function(){
    try { 
		$.ajax({
			dataType: 'script',
			url: 'http://petter-haggholm.livejournal.com/?s2id=24789471',
	        success: embedlj
		});
    }
    catch (e) { }
});


function toggleBlogCookie()
{
    var exp = new Date();
    exp.setDate(exp.getDate() + 365);
    var val = isBlogCookieSet() ? 0 : 1;
    document.cookie = 'show_blog='+val+'; expires='+exp.toGMTString();
}

function isBlogCookieSet()
{
    return getCookie('show_blog') == 1;
}


/**
 * From http://www.webreference.com/js/column8/functions.html
 *
 * name - name of the desired cookie
 * return string containing value of specified cookie or null
 * if cookie does not exist
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
    if (begin != 0)
    	return null;
    }
    else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

