
$(document).ready(function(){
    $("a#perspectives").css("background-position","0 -18px");
    $('#artdivider').hide();
    catClick('introduction');
});

var _data = null;

function loadData() {
	return {
	'introduction': [
        { 'author': '', 'title': 'Introduction', 'id': 'intro' }
    ],
    'essays': [
        { 'author': 'John H. Dietrich', 'title': 'What Does It Mean to<br>Be Spiritual?', 'id': 'dietrich'},
        { 'author': 'Ralph Waldo Emerson', 'title': 'Gifts', 'id': 'emerson' },
        { 'author': 'Kenneth W. Phifer', 'title': 'The Practice of Spirituality', 'id': 'phifer'},
        { 'author': 'Bill Savoie', 'title': 'Introduction to "The Dyad"', 'id': 'savoie' },
        { 'author': 'Paul Weiss', 'title': 'Three Key Elements', 'id': 'weiss-three' },
        { 'author': 'Marianne Williamson', 'title': 'A Return To Love', 'id': 'williamson' }
    ],
    'humanrights': [
        {'author': 'The United Nations General Assembly', 'title': 'Universal Declaration of Human Rights', 'id': 'declaration'}
    ],
    'poetry': [
        { 'author': 'Paul Weiss', 'title': 'True Heart/True Mind', 'id': 'weiss-true' },
        { 'author': 'W.B. Yeats', 'title': 'The Secret Rose', 'id': 'yeats-rose'},
        { 'author': 'W.B. Yeats', 'title': 'He Wishes for the<br>Cloths of Heaven', 'id': 'yeats-wishes'}
    ],
    'quotations': [
        { 'author': 'E.E. Cummings', 'title': '', 'id': 'cummings' },
        { 'author': 'de Montaigne', 'title': '', 'id': 'montaigne' },
//        { 'author': 'Albert Einstein', 'title': 'The Merging of<br>Spirit and Science', 'id': 'einstein'},
//        { 'author': 'Ralph Ellison', 'title': 'Battle Royal', 'id': 'ellison' },
        { 'author': 'Albert Einstein', 'title': '', 'id': 'einstein'},
        { 'author': 'Ralph Ellison', 'title': '', 'id': 'ellison' },
        { 'author': 'Soren Kierkegaard', 'title': '', 'id': 'kierkegaard' },
		{ 'author': "Louis L'Amour", 'title': '', 'id': 'lamour' },
        { 'author': 'Henry Miller', 'title': '', 'id': 'miller' },
        { 'author': 'Friedrich Nietzsche', 'title': '', 'id': 'nietzsche' },
        { 'author': 'Anais Nin', 'title': '', 'id': 'nin' },
        { 'author': 'Plato', 'title': '', 'id': 'plato' },
        { 'author': 'Bertrand Russell', 'title': '', 'id': 'russell' },
        { 'author': 'Erwin Schrodinger', 'title': '', 'id': 'schrodinger' },
        { 'author': 'William Shakespeare', 'title': '', 'id': 'shakespeare' },
        { 'author': 'Unknown', 'title': '', 'id': 'unknown' },
        { 'author': 'Laurence van der Post', 'title': '', 'id': 'post' },
        { 'author': 'Oscar Wilde', 'title': '', 'id': 'wilde' }
    ],
    'socialartistry': [
        {'author': 'Flemming Funch', 'title': 'Social Artistry', 'id': 'funch'}
    ]
	};
}

var _currCat = '';
var _currArticle = -1;

function catClick(cat) {
	if ( _data == null ) {
		_data = loadData();
	}

	if (_currArticle != -1) {
		//$('#pcontent').fadeOut('normal');
	    //$('#particles').fadeOut('normal', function() { listArticles(cat); });
	}
    listArticles(cat);
}

function listArticles(cat) {
    var category = _data[cat];

    var art1 = $('#articles1');
    var art2 = $('#articles2');

    art1.removeClass('half');
    art2.removeClass('half');

    var arts = {'0': [], '1':[]};
    var html = '';
    for (i=0; i<category.length; i++) {
        article = category[i];
		html = '<div class="article"><a id="article_' + i + '" href="javascript: authorClick(\'' + cat + '\',' + i + ');">';
		if ( article.title != '' ) {
			html += '<strong>' + article.title + '</strong><br>';
		}
		html += '<span class="author">' + article.author + '</span></div></a>';
        arts[i%2].push(html);
	}

	if ( category.length > 1 ) {
		art1.addClass('half').html(arts[0].join(''));
		art2.addClass('half').html(arts[1].join(''));
		$('#artdivider').show();
	} else {
    	art1.html(html);
    	art2.html('');
		$('#artdivider').hide();
	}

    if ( _currCat != '' ) {
        $('#' + _currCat).removeClass('selected');
	}
    $('#' + cat).addClass('selected');
    _currCat = cat;

    $('#pcontent').html('');
	//$('#particles').fadeIn('normal');

	authorClick(cat, 0);
}

function authorClick(cat, item) {
   	//$('#pcontent').fadeOut('normal', function() { showArticle(cat,item) });
   	showArticle(cat,item);
}

function showArticle(cat, item) {
    var category = _data[cat];
    var article = category[item];

    if ( _currArticle > -1 ) {
        $('#article_' + _currArticle).removeClass('selected');
	}
    $('#article_' + item).addClass('selected');
    _currArticle = item;

    var html = '<div class="article selected">';
    if ( article.title != '' ) {
		html += '<strong>' + article.title.replace(/<br>/g," ") + '</strong><br>';
	}
	html += '<span class="author">' + article.author + '</span></div>';
	html += $('#'+article.id).html();

    $('#pcontent').html(html);
	//$('#pcontent').fadeIn('normal');
}
