// Deprecated but could be useful again
function storyDataForStoryID(aStoryID) {
    return storyDatas.find(function(aStoryData) {
        return aStoryData.bulletin.charAt(0) == aStoryID.charAt(0) && aStoryData.storyType.charAt(0) == aStoryID.charAt(2) && aStoryData.storyID == aStoryID.substring(4);
    });
}

function getmore(aStoryID) {
    new Effect.ScrollTo('sciBullHeader', { offset: -8, afterFinish: function(anEffect) {
        $('archiveTab').removeClassName('PurpleWindowBorderBottom').addClassName('PurpleBorderBottom').down().removeClassName('StoryTitleColor');
        $('storyInfoUnclickableTab').hide();
        $('storyInfoClickableTab').show().removeClassName('PurpleBorderBottom').addClassName('PurpleWindowBorderBottom').down().addClassName('StoryTitleColor');
        $('archive').hide();
        $('moreAbout').show();

        new Ajax.Request('content/' + aStoryID + '/storydata.json', { method: 'get', onSuccess: function(aTransport) { currentStoryData = aTransport.responseText.evalJSON(); showMore();} });
    } });
}

function showMore() {
	var resourceTitle0;   //  SG|20080812: because the first resource title isn't always 'Synopsis'
    $('resourceList').innerHTML = '';

    currentStoryData.resources.each(function(anElement, anIndex) {
		if (!resourceTitle0) resourceTitle0 = anElement.title;  // SG|20080812
		if (anElement.resourceType != '508 - text index') {
	        if (anElement.inlineP) {
	            if (['essay (inline)'].include(anElement.resourceType)) {
					print_resourceType = anElement.resourceType.replace(" (inline)","");// -m@.. Prototype's gsub() and sub() are too fancy. i couldn't escape the parenthesis 
					$('resourceList').innerHTML += '<div id="resourceItem' + anIndex + '" class="resourceItem MarginBottom PaddingTop2 PaddingBottom2 PaddingLeft4 PaddingRight"><a href="#" onclick="showResource($(this).down().innerHTML, \'resourceItem' + anIndex + '\'); return false;">' + print_resourceType.capitalize() + '<div class="LucidaGrande10 DescriptionColor">' + anElement.title + '</div></a></div>';
	            } else {
		            $('resourceList').innerHTML += '<div id="resourceItem' + anIndex + '" class="resourceItem MarginBottom PaddingTop2 PaddingBottom2 PaddingLeft4 PaddingRight"><a href="#" onclick="showResource($(this).innerHTML, \'resourceItem' + anIndex + '\'); return false;">' + anElement.title + '</a></div>';
	            }
	        } else {
	            if (['essay', 'interactive', 'video'].include(anElement.resourceType)) {
	                $('resourceList').innerHTML += '<div id="resourceItem' + anIndex + '" class="MarginBottom PaddingTop2 PaddingBottom2 PaddingLeft4 PaddingRight"><a href="#" onclick="popUpResource($(this).next().down().innerHTML); return false;">' + anElement.resourceType.capitalize() + '</a><a class="resourceTitle" href="#" onclick="popUpResource($(this).down().innerHTML); return false;"><div class="LucidaGrande10 DescriptionColor" onmouseover="$(this).up().previous().addClassName(\'UnderlineText\');" onmouseout="$(this).up().previous().removeClassName(\'UnderlineText\');">' + anElement.title + '</div></a></div>';
	            } else {
	                $('resourceList').innerHTML += '<div id="resourceItem' + anIndex + '" class="MarginBottom PaddingTop2 PaddingBottom2 PaddingLeft4 PaddingRight"><a href="#" onclick="popUpResource($(this).innerHTML); return false;">' + anElement.title + '</a></div>';
	            }
	        }
		}
    });
    
    // SG|20080812:  showResource('synopsis', 'resourceItem0');
    showResource(resourceTitle0, 'resourceItem0');
}

function showResource_03052009(aResourceTitle, aResourceItem) {
	// SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    // $('resourceList').getElementsByClassName('resourceItem').invoke('removeClassName', 'DarkPurpleBackgroundColor');
    $$('div.resourceItem').invoke('removeClassName', 'DarkPurpleBackgroundColor');
    $(aResourceItem).addClassName('DarkPurpleBackgroundColor');

	// added by -m@.. 20080102
	// generically grab the object and pull multiple values out of it.
	current_resource = currentStoryData.resources.find(function(aResource) {
		return aResource.title == aResourceTitle;
	})
	$('resourceDetailContent').innerHTML = current_resource.description;


	// added by -m@.. 20080102
	story_path = 'http://yoda.amnh.org/sciencebulletins/content/';
	story_id = currentStoryData.bulletin.substring(0,1) + '.';
	story_id += currentStoryData.storyType.substring(0,1) + '.';
	story_id += currentStoryData.storyID;
	asset_id = current_resource.resource_id;
	asset_path = story_path+story_id+'/resources/'+asset_id+'/';
//	prompt(asset_path);
//	$$('div.assetThumb img').each(function(k,v) {k.writeAttribute({src: asset_path+k.readAttribute('src')})});

}
function showResource(aResourceTitle, aResourceItem) {
	// SG|20080617: .getElementsByClassName deprecated as of prototype 1.6
    // $('resourceList').getElementsByClassName('resourceItem').invoke('removeClassName', 'DarkPurpleBackgroundColor');
    $$('div.resourceItem').invoke('removeClassName', 'DarkPurpleBackgroundColor');
    $(aResourceItem).addClassName('DarkPurpleBackgroundColor');

	// added by -m@.. 20080102
	// generically grab the object and pull multiple values out of it.
	current_resource = currentStoryData.resources.find(function(aResource) {
		return aResource.title == aResourceTitle;
	})
	


if(current_resource.resourceType == 'essay (inline)') {
	//full_version = current_resource.description;
	
	description_parts = current_resource.description.split('<hr />');
	if(description_parts.length > 1) {
		tID = 'toggle_div_'+(Math.round(Math.random()*100));
		final_description = description_parts[0]+'<div class="readmore" onclick="Effect.SlideDown(\''+tID+'\', { offset: -8, afterFinish: function(anEffect) {Effect.ScrollTo(\'moreAbout\')}});$(this).hide();">read more</div>'+'<div id="'+tID+'" style="display:none">'+description_parts[1]+'</div>';
		$('resourceDetailContent').innerHTML = final_description;
	} else {
		$('resourceDetailContent').innerHTML = current_resource.description;
	}
} else {
	$('resourceDetailContent').innerHTML = current_resource.description;
}


	// added by -m@.. 20080102
	story_path = '/sciencebulletins/content/';
	story_id = currentStoryData.bulletin.substring(0,1) + '.';
	story_id += currentStoryData.storyType.substring(0,1) + '.';
	story_id += currentStoryData.storyID;
	asset_id = current_resource.resource_id;
	asset_path = story_path+story_id+'/resources/'+asset_id+'/';
	$$('div.assetThumb img').each(function(k,v) {
		img_name = k.readAttribute('src').split('/').pop();// IE/win was pepending the index.php path to the static path of these images.
		k.writeAttribute({src: asset_path+img_name})
	});

}

function popUpResource(aResourceTitle) {
    storyFolder = currentStoryData.bulletin.charAt(0) + '.' + currentStoryData.storyType.charAt(0) + '.' + currentStoryData.storyID;
    currentResource = currentStoryData.resources.find(function(aResource) {
        return aResource.title == aResourceTitle;
    });
    resourceLink = currentResource.link;
    resourceWindowWidth = currentResource.width;
    resourceWindowHeight = currentResource.height;
    if (typeof(resourceWindow) == 'object' && !(resourceWindow.closed)) {
        resourceWindow.close();
    }
    resourceWindow = Typical.popUp('content/' + storyFolder + '/' + resourceLink, 'resourceWindow', 'menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=1,width=' + resourceWindowWidth + ',height=' + resourceWindowHeight, 'Science Bulletins was unable to open a window. Your browser may be blocking pop-up windows.');
}

function hidemore() {
    $('archiveTab').removeClassName('PurpleBorderBottom').addClassName('PurpleWindowBorderBottom').down().addClassName('StoryTitleColor');
    $('storyInfoUnclickableTab').show();
    $('storyInfoClickableTab').hide();
    $('archive').show();
    $('moreAbout').hide();
}



// Legacy (Steve) code
function bookmarkStory(sid) {
	if( window.external ) { // IE Favorite
		var url = self.location.href + "/?sid=" + sid + "&src=b";
		var title = currentStoryData.title;
		window.external.AddFavorite( url, title);
	} else {
		alert('Click "COPY TO CLIPBOARD" and then select "Add Bookmark" from your browser\'s menu.');
	}
}
