/* By Dara Keo 
// This relabels and reorders all disclaimers and footnotes //
*/

$(document).ready(function(){
	var fnCount = 0;
	var fnHold = "*";
	var footnoteData = new Array();
	var isSingle;
	var isType = '';
	
	function initFootnotes(){
		isType = checkType();
		
		if(isType == 'multi'){
			relabelFootnoteTags();
			relabelFootnotes();
			reorderFootnotes();
			removeExtraDisclosures();
			removeTitleAttr();
		}else{
			removeExtraDisclosures();
			removeTitleAttr();
		}
	}
	
	function checkType(){
		var objLength = $('span.fn-tag').length;
		var hold = $('span.fn-tag:first').attr('title');
		var isMultiSingle = true;
		var theState = 'multi';
		
		$('span.fn-tag').each(function(){
			var currentTag =  $(this).attr('title');
			if(currentTag != hold){
				isMultiSingle = false;	
			}
		});
				
		if(objLength == 1){
			theState = 'single';
		}

		if(isMultiSingle){
			theState = 'multi-single';
		}
		
		return theState;
	}
	
	function relabelFootnoteTags(){
		fnCount = 0;
		
		$('span.fn-tag').each(function(){						   
									   
			if(fnCount === 0){
				$(this).html('<sup>' + (fnCount + 1) + '</sup>');
				footnoteData[fnCount] = new Array();
				footnoteData[fnCount][0] = $(this).attr('title');
				footnoteData[fnCount][1] = $(this).html();
				fnCount++;
			}else{
				var isDupe = checkForDupes($(this));
				
				if(isDupe){
					$(this).html(fnHold);
				}else{	
					$(this).html('<sup>' + (fnCount + 1) + '</sup>');
					footnoteData[fnCount] = new Array();
					footnoteData[fnCount][0] = $(this).attr('title');
					footnoteData[fnCount][1] = $(this).html();
					fnCount++;
				}
			}		
		});
	}
	
	function relabelFootnotes(){
		fnCount = 0;
		$('span.fn-dis').each(function(){
			var key = $(this).attr('title');
			for(var i=0; i<footnoteData.length; i++){
				if(footnoteData[i][0] == key){
					var note = footnoteData[i][1];
					$(this).html(note);
					return;
				}
			}
		});
	}
	
	function reorderFootnotes(){
		var count = $('div.footnote_content_wrapper').length;
		$('div.footnote_content_wrapper').each(function(){
			var key = 1 * $(this).find('span.fn-dis sup').html();
			
			for(var i=0; i<count; i++){
				var nextKey = 1 * $('div.footnote_content_wrapper:nth-child(' + i + ')').find('span.fn-dis sup').html();
				if(key !== 0 && key > nextKey){
					$(this).insertAfter($('div.footnote_content_wrapper:nth-child(' + i + ')'));
				}
			}
		});
	}
	
	function removeTitleAttr(){
		$('span.fn-tag').removeAttr('title');
		$('span.fn-dis').removeAttr('title');
	}
	
	function removeExtraDisclosures(){
		if(isType == 'multi'){
			$('div.footnote_content_wrapper').each(function(){
				var theWrapper = $(this);
				var footnoteLink = $(this).find('span.fn-dis').attr('title');
				var isHere = false;
				
				for(var i=0; i<footnoteData.length; i++){
					if(footnoteLink == footnoteData[i][0]){
						isHere = true;
						break;
					}
				}
				
				if(isHere === false){
					$(this).css('display','none');	
				}
				
			});
		}else{
			if(tagLink == null || tagLink == '' || tagLink == undefined){
				return;
			}else{
				var tagLink = $('span.fn-tag:first').attr('title');
				$('div.footnote_content_wrapper').each(function(){
					var footnoteLink = $(this).find('span.fn-dis').attr('title');
					if(tagLink != footnoteLink){
						$(this).css('display','none');	
					}
				});
			}
		}
	}
	
	function checkForDupes(obj){
		var key = $(obj).attr('title');
		for(var i=0; i<footnoteData.length; i++){
			if(footnoteData[i][0] == key){
				fnHold = footnoteData[i][1];
				return true;
			}
		}
		return false;
	}
	
	initFootnotes();
		
});