// **
// scripts relevant to the AAJ Publications section
// **

var cols = 5;
var archiveSelected = "archive_selected";
var tableWidth = 450;
var tableHeight = 250;
var cellWidth = Math.floor( tableWidth/cols );

function sortArchive(){
	var archBox = $('archiveBox');
	var archTabs = $('archiveTabs');
	var arch = $('archive');
	var archList = collectionToArray( arch.getElementsByTagName( "li" ) ).reverse();
	var yearsList = getListOfYears( archList ).reverse();
	
	for( y=0; y<yearsList.length; y++ ){
		
		if( y%cols == 0 ){
			//alert( "new table: "+yearsList[y]  );
			var newtable = document.createElement( "table" );
			newtable.width = tableWidth;
			newtable.border = newtable.cellspacing = newtable.cellpadding = "0"; 
			newtable.id = "row_" + yearsList[y];
			if( y == 0 ){
				newtable.className = archiveSelected;
			}
			archBox.appendChild( newtable );

			var tb = document.createElement( "tbody" );
			newtable.appendChild( tb );
			
			var tr = document.createElement( "tr" );
			tr.vAlign = "top";
			tb.appendChild( tr );
			
		}
				
		// create a year cell (but do not append yet)
		var yearCell = document.createElement( "td" );
		yearCell.className = "yearCell";
		if( y%cols == 0 ){
			yearCell.className = "yearCell first";
		}
		yearCell.width = cellWidth;
		yearCell.height = tableHeight;
		yearCell.id = "archive_" + yearsList[y];
		
		// create a label for year cell
		var yearLabel = document.createElement( "h2" );
		yearLabel.innerHTML = yearsList[y];
		yearCell.appendChild( yearLabel );
		
		// create a list for year cell
		var yearList = document.createElement( "ul" );
		yearCell.appendChild( yearList );
		

		// assign items with corresponding year
		for( x=0; x<archList.length; x++ ){
			if( archList[x].innerHTML.indexOf( yearsList[y] ) > -1 ){
				var thisEl = archList[x];
				var txt = thisEl.getElementsByTagName("a")[0].innerHTML;
				thisEl.getElementsByTagName("a")[0].innerHTML = txt.split(" ")[0];
				yearList.appendChild( thisEl );
			}	
		}
		
		tr.appendChild( yearCell );
		
		
	}
	
	// insert an empty cell for each leftover in that row
	var leftovers = cols - (y%cols);
	if( y > 0 && y%cols == 0 ){
		leftovers = 0;
	}
	for( var m=0; m<leftovers; m++ ){
		var yearCell = document.createElement( "td" );
		yearCell.className = "yearCell";
		yearCell.width = cellWidth;
		yearCell.height = tableHeight;
		yearCell.innerHTML = "<br/>";
		tr.appendChild( yearCell );
	}
	
	// remove original
	arch.parentNode.removeChild( arch );
	
	
	// create tabs
	var tabsList = document.createElement( "ul" );
	archTabs.appendChild( tabsList );
	
	var tabsLabel = document.createElement( "li" );
	tabsLabel.className = "first";
	tabsLabel.innerHTML = "<b>MORE</b>";
	tabsList.appendChild( tabsLabel );
	
	var rows = archBox.getElementsByTagName("table");
	var numRows = rows.length;
	for( var r=0; r<numRows; r++){ 	
		var rowCells = rows[r].getElementsByTagName("h2");
		var tabLabel = rowCells[0].innerHTML;
		if(rowCells.length > 1 ){ tabLabel += " - "+ rowCells[rowCells.length-1].innerHTML; }
		var li = document.createElement( "li" );
		if( r == numRows-1 ){
			li.className = "last";
		}
		var anc = document.createElement( "a" );
		anc.innerHTML = tabLabel;
		anc.href = "javascript: selectArchiveRow( 'row_"+rowCells[0].innerHTML+"' );"
		tabsList.appendChild( li );
		li.appendChild( anc );
	}
		
}
function selectArchiveRow( id ){
	var archBox = $('archiveBox');
	var aRows = archBox.getElementsByTagName("TABLE");
	for( var r=0; r<aRows.length; r++ ){
		aRows[r].className = "";
	}
	var newSel = $( id );
	newSel.className = archiveSelected;
}

function getListOfYears( theArray ){
	var yAr = new Array();
	for( var a=0; a<theArray.length; a++ ){
		yAr.push( theArray[a].getElementsByTagName("a")[0].innerHTML.split(" ")[1] );				
	}
	return yAr.uniq();
}

  
function collectionToArray( theCol ){
	var theArray = new Array();
	for( var a=0; a<theCol.length; a++ ){ theArray.push( theCol[a] ); }
	return theArray;
}

addLoadListener( sortArchive );

