﻿var debug = false;

// Create the Show/Hide button, and assign the onClick event.
function assignEvents(ItemClass, ItemTitle, maxDisplay, displayFullTitle )
{
	var items = null;
    try {
		 items = $$("." + ItemClass + " ul li");
    }catch( ex ) {  
		if( debug )	
			alert( "line 11: " + ex.description ); 
	}
    
    var numItems = items.length;
    
    if( numItems > maxDisplay ) 
    {
		for(var i = 0; i < numItems; i++)
			if( i >= maxDisplay)
				items[i].toggleClass("extra");
	    
		var ShowHideBtn = new Element("a");
		
		if( displayFullTitle == true )
		{
			try {
				ShowHideBtn.setHTML("Show All " + $$("div.globalBreadcrumb a.active")[0].innerHTML + " " + makePlural(ItemTitle));
			}catch(ex){
				if( debug )	
					alert( "line 30: " + ex.description ); 
			}
		}
		else
			ShowHideBtn.setHTML("Show All");
		
		ShowHideBtn.href = "#";
		ShowHideBtn.addClass("ShowHide"+makePlural(ItemClass));
	    
		ShowHideBtn.addEvent("click",function(e){
			new Event(e).preventDefault()
			
			var items = null;
			
			try {
				items = $$("." + ItemClass + " ul li");
			}catch(ex){
				if( debug )
					alert( "line 48: " + ex.description );
			}
			if( items == null ) return;
			
			var size = items.length;
			for(var i = 0; i < size; i++)
	    		if( i >= maxDisplay)
					items[i].toggleClass("extra");
					
			var ShowHideBtnText = null;
			
			try { 
				ShowHideBtnText = $$(".ShowHide"+makePlural(ItemClass))[0].innerHTML;
			}catch(ex){
				if( debug )
					alert( "line 63: " + ex.description );
			}
			
			if( ShowHideBtnText.match(/^[Ss][Hh][Oo][Ww] [Aa][Ll]{2}.*/) )
			{
				try {
					$$(".ShowHide"+makePlural(ItemClass))[0].setHTML(ShowHideBtnText.replace(/^[Ss][Hh][Oo][Ww] [Aa][Ll]{2}(.*)/,"Hide $1"));
				}catch(ex){
					if( debug )
						alert( "line 75: " + ex.description );
				}
			}
			else if( ShowHideBtnText.match(/^[Hh][Ii][Dd][Ee].*/) )
			{
				try {
					$$(".ShowHide"+makePlural(ItemClass))[0].setHTML(ShowHideBtnText.replace(/^[Hh][Ii][Dd][Ee] (.*)/,"Show All $1"));
				} catch(ex){
					if( debug )
						alert( "line 81: " + ex.description );
				}
			}
		});
		
		try {
			ShowHideBtn.injectAfter(items[numItems-1]);
		}catch(ex){
			if( debug )
				alert( "line 90: " + ex.description );
		}
    }
}


function domReady() {
    assignEvents("ReportTeaser","Report",5,true);
    assignEvents("PressRelease","PressRelease",2,false);
    assignEvents("PublicOpinion","PublicOpinion",2,false);
    assignEvents("FactSheet","FactSheet",2,false);
    assignEvents("Article","Media Coverage",2,false);
    assignEvents("Speech","Speech",2,false);
    assignEvents("Research","Research",2,false);
    assignEvents("AVWebCast","Audio Video Webcast",2,false);
    assignEvents("Summary","Summary",2,false);
    assignEvents("Events","Events",2,false);

    setMainContentHeight();
}

// The main content may be shorter than the right side bar; therefore the border may not look correct.
function setMainContentHeight()
{
	var mainContent = null;
	var rightSidebar = null;
	var leftSidebar = null;
	
	try {
		mainContent = $("main_content");
		rightSidebar = $$(".rightSidebar")[0];
		leftSidebar = $$(".leftSidebar")[0];
	} catch (ex) {
		if( debug )
			alert( "line 116,117,118:" + ex.description );
	}
	
	if( rightSidebar != null )
	{
		if( mainContent.getCoordinates().height < rightSidebar.getCoordinates().height )
			mainContent.style.height = rightSidebar.getCoordinates().height - 26 + "px";
		
		if( mainContent.getCoordinates().height < leftSidebar.getCoordinates().height )
			mainContent.style.height = leftSidebar.getCoordinates().height - 26 + "px";
	}
}


try {
    window.addEvent("domready",domReady);
}catch(e) {}// Either the MooTools JS file Doesn't exist or something else happened, FAILED, this way there are no Javascript errors on the page.


function makePlural(word)
{
	if( word != null && word.length > 0)
	{
		var LastChar = word.charAt(word.length-1);
		if( LastChar != "s" )
			return word+"s";
	}
}
