function archivedStoriesThatStartWith(aSubString) {
    return archivedStories.stories.findAll(function(anElement) {
        return anElement.startsWith(aSubString);
    });
}

function countArchivedStoriesThatStartWith(aSubString) {
    return archivedStoriesThatStartWith(aSubString).size();
}

function processArchivedStories() {
    ['af', 'av', 'as', 'ef', 'ev', 'es', 'bf', 'bv', 'bs', 'hf', 'hs'].each(function(anElement) {
        $(anElement + 'Count').innerHTML = '(' + countArchivedStoriesThatStartWith(anElement.charAt(0) + '.' + anElement.charAt(1) + '.') + ')';
    });

    showArchivedStoriesThatStartWith('a.f.');
}

function matchArchivedStoriesCountWithStoryDatasCount() {
    if (storyDatas.size() == archivedStoriesToShow) {
        sortStoryDatas();
        formatStoryDatasAndGetStoryTemplate();
    }
}

/* SG|20080421: added onFailure, which happens when the archived_stories.json includes a story that isn't yet on this server.
	before this was added, the slice of archive stories was not shown; this leaves the count in the menu incorrect, however.
*/
function showArchivedStory(aStoryID) {
    new Ajax.Request('content/' + aStoryID + '/storydata.json', { method: 'get', onSuccess: function(aTransport) { storyDatas.push(aTransport.responseText.evalJSON()); matchArchivedStoriesCountWithStoryDatasCount(); }, onFailure: function() {archivedStoriesToShow--;}});
}

function showArchivedStoriesThatStartWith(aSubString) {
    storyDatas = [];
    archivedStoriesToShow = countArchivedStoriesThatStartWith(aSubString);
    
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('archivePlaylists').getElementsByClassName('archivePlaylist').invoke('removeClassName', 'DarkPurpleBackgroundColor');
	$('archivePlaylists').select('div.archivePlaylist').invoke('removeClassName', 'DarkPurpleBackgroundColor')
    $(aSubString.charAt(0) + aSubString.charAt(2) + 'Playlist').addClassName('DarkPurpleBackgroundColor');

    archivedStoriesThatStartWith(aSubString).each(showArchivedStory);
}

function sortStoryDatas() {
    storyDatas = storyDatas.sortBy(function(aStoryData) { return aStoryData.storyID.split('.')[1]; }).reverse();
}

function abbreviatedMonth(aMonth) {
    abbreviatedMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
    return abbreviatedMonths[parseInt(aMonth, 10) - 1];
}

function formatStoryDatas() {
    formattedStoryDatas = [];
    storyDatas.each(function(aStoryData) {
		//SG|20080617: change in Prototype 1.6; can no longer get or set hash properties directly, have to use .get and .set
		aFormattedStoryData = $H();
        aFormattedStoryData.set ('storyIcon','content/' + aStoryData.bulletin.charAt(0) + '.' + aStoryData.storyType.charAt(0) + '.' + aStoryData.storyID + '/' + aStoryData.icon.src);
        aFormattedStoryData.set('storyBulletinType',aStoryData.bulletin.capitalize());
        aFormattedStoryData.set('storyType',aStoryData.storyType.capitalize());
        year = aStoryData.storyID.split('.')[1].substr(0, 4);
        month = aStoryData.storyID.split('.')[1].substr(4, 2);
        day = aStoryData.storyID.split('.')[1].substr(6, 2);
        switch (aStoryData.storyType) {
            case 'feature':
            case 'viz':
                duration = parseInt(aStoryData.content.duration, 10);
                minutes = Math.floor(duration / 60);
                seconds = duration % 60;
                if (seconds < 10) {
                    seconds = '0' + seconds;
                }
                aFormattedStoryData.set('storyDuration','Video (' + minutes + ':' + seconds + ') | ' + abbreviatedMonth(month).capitalize() + ' ' + year);
                break;
            case 'snapshot':
                aStoryData.content.template == 'poi.swf' ? storyMedium = 'Video' : storyMedium = 'Interactive';
                aFormattedStoryData.set('storyDuration',storyMedium + ' | ' + month + '.' + day + '.' + year);
                break;
        }
        aFormattedStoryData.set('storyID',aStoryData.bulletin.charAt(0) + '.' + aStoryData.storyType.charAt(0) + '.' + aStoryData.storyID);
        aFormattedStoryData.set('storyTitle',aStoryData.title);
        aFormattedStoryData.set('storyDescription',aStoryData.description);
        formattedStoryDatas.push(aFormattedStoryData);
    });
    formattedStoryDatas = formattedStoryDatas.eachSlice(6);
}

function evaluateStoryTemplates() {
    $('slices').innerHTML = '';

    $('slices').innerHTML += '<div class="slice currentSlice" style="height: 343px;"></div>';
    formattedStoryDatas.shift().each(function(aFormattedStoryData) {
        // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
	    //$('slices').getElementsByClassName('currentSlice').first().innerHTML += archivedStoryTemplate.evaluate(aFormattedStoryData);
		$('slices').select('div.currentSlice').first().innerHTML += archivedStoryTemplate.evaluate(aFormattedStoryData);
    });
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('slices').getElementsByClassName('currentSlice').first().innerHTML += '<div class="ClearBoth"><!-- --></div>';
	$('slices').select('div.currentSlice').first().innerHTML += '<div class="ClearBoth"><!-- --></div>';

    formattedStoryDatas.each(function(aFormattedStoryDatasSlice) {
        $('slices').innerHTML += '<div class="slice" style="height: 343px; display: none;"></div>';
        aFormattedStoryDatasSlice.each(function(aFormattedStoryData) {
            $('slices').immediateDescendants().last().innerHTML += archivedStoryTemplate.evaluate(aFormattedStoryData);
        });
        $('slices').immediateDescendants().last().innerHTML += '<div class="ClearBoth"><!-- --></div>';
    });
    
    paginateArchivedStoriesSlices();
}

function formatStoryDatasAndGetStoryTemplate() {
    formatStoryDatas();
    new Ajax.Request('json_templates/archived_story.html', { method: 'get', onSuccess: function(aTransport) { archivedStoryTemplate = new Template(aTransport.responseText); evaluateStoryTemplates(); } });
}

function paginateArchivedStoriesSlices() {
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('slices').getElementsByClassName('slicePaginator').invoke('remove');
    $('slices').select('div.slicePaginator').invoke('remove');

    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('slices').getElementsByClassName('slice').each(function(aSlice, anIndex) {
	$('slices').select('div.slice').each(function(aSlice, anIndex) {
        if (aSlice.hasClassName('currentSlice')) {
            currentSlice = anIndex;
        }
    });
    firstStoryNumberInCurrentSlice = (currentSlice * 6 + 1);
    lastStoryNumberInCurrentSlice = archivedStoriesToShow < (currentSlice * 6 + 6) ? archivedStoriesToShow : (currentSlice * 6 + 6);
    sliceInfo = firstStoryNumberInCurrentSlice + '-' + lastStoryNumberInCurrentSlice + ' of ' + archivedStoriesToShow;
    
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //if ($('slices').getElementsByClassName('slice').size() > 1) {
	if ($('slices').select('div.slice').size() > 1) {
        // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
	    //if ($('slices').getElementsByClassName('slice').first().hasClassName('currentSlice')) {
		if ($('slices').select('div.slice').first().hasClassName('currentSlice')) {
            $('slices').innerHTML = '<div class="slicePaginator MarginBottom LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span>&lt; Previous</span> <span><a href="#" onclick="showNextSlice(); return false;">Next &gt;</a></span></div><div class="ClearBoth"><!-- --></div></div>' + $('slices').innerHTML;
            $('slices').innerHTML += '<div class="slicePaginator MarginTop LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span>&lt; Previous</span> <span><a href="#" onclick="showNextSlice(); return false;">Next &gt;</a></span></div><div class="ClearBoth"><!-- --></div></div>';
        // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
		//} else if ($('slices').getElementsByClassName('currentSlice').first().next('.slice')) {
			} else if ($('slices').select('div.currentSlice').first().next('.slice')) {
            $('slices').innerHTML = '<div class="slicePaginator MarginBottom LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span><a href="#" onclick="showPreviousSlice(); return false;">&lt; Previous</a></span> <span><a href="#" onclick="showNextSlice(); return false;">Next &gt;</a></span></div><div class="ClearBoth"><!-- --></div></div>' + $('slices').innerHTML;
            $('slices').innerHTML += '<div class="slicePaginator MarginTop LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span><a href="#" onclick="showPreviousSlice(); return false;">&lt; Previous</a></span> <span><a href="#" onclick="showNextSlice(); return false;">Next &gt;</a></span></div><div class="ClearBoth"><!-- --></div></div>';
        } else {
            $('slices').innerHTML = '<div class="slicePaginator MarginBottom LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span><a href="#" onclick="showPreviousSlice(); return false;">&lt; Previous</a></span> <span>Next &gt;</span></div><div class="ClearBoth"><!-- --></div></div>' + $('slices').innerHTML;
            $('slices').innerHTML += '<div class="slicePaginator MarginTop LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span><a href="#" onclick="showPreviousSlice(); return false;">&lt; Previous</a></span> <span>Next &gt;</span></div><div class="ClearBoth"><!-- --></div></div>';
        }
    } else {
        $('slices').innerHTML = '<div class="slicePaginator MarginBottom LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span></span> <span></span></div><div class="ClearBoth"><!-- --></div></div>' + $('slices').innerHTML;
        $('slices').innerHTML += '<div class="slicePaginator MarginTop LucidaGrande12 LightGrayColor"><div class="FloatLeft" style="width: 294px;">' + sliceInfo + '</div><div class="FloatRight RightText" style="width: 294px;"><span></span> <span></span></div><div class="ClearBoth"><!-- --></div></div>';
    }
}

function hideAllSlices() {
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    // $('slices').getElementsByClassName('slice').invoke('hide');
	$('slices').select('div.slice').invoke('hide');
}

function showNextSlice() {
    hideAllSlices();
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('slices').getElementsByClassName('currentSlice').first().removeClassName('currentSlice').next().addClassName('currentSlice').show();
	$('slices').select('div.currentSlice').first().removeClassName('currentSlice').next().addClassName('currentSlice').show();    	
	paginateArchivedStoriesSlices();
}

function showPreviousSlice() {
    hideAllSlices();
    // SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    //$('slices').getElementsByClassName('currentSlice').first().removeClassName('currentSlice').previous().addClassName('currentSlice').show();
	$('slices').select('div.currentSlice').first().removeClassName('currentSlice').previous().addClassName('currentSlice').show();
    paginateArchivedStoriesSlices();
}

function clickArchiveTab() {
    $('archive').show();
    $('moreAbout').hide();
    $('archiveTab').removeClassName('PurpleBorderBottom').addClassName('PurpleWindowBorderBottom').down().addClassName('StoryTitleColor');
    $('storyInfoClickableTab').removeClassName('PurpleWindowBorderBottom').addClassName('PurpleBorderBottom').down().removeClassName('StoryTitleColor');
    new Effect.ScrollTo('tabs');
}


function show_static_content(which_url) {
	new Ajax.Updater('slices', which_url, { method: 'get' });
	$('archivePlaylists').select('div.archivePlaylist').invoke('removeClassName', 'DarkPurpleBackgroundColor')
    $('about_kml').addClassName('DarkPurpleBackgroundColor');
}

