var fatFootHeaders = null,
fatFootTotColArr = [],
fatFootTotColArrFiltered = [],
fatFootColLayout = null;

$(document).ready(function(){
	
	if (fatFootJSON != null || fatFootJSON != undefined){

	fatFootHeaders = fatFootJSON.fatFootItems.length;
	
	// Sorts JSON items by Column #
	fatFootJSON.fatFootItems.sort(function(a,b){
		var x = a.column < b.column? -1:1; 
		return x; 
	});

	// inserts fatFoot div to append items to 
	$('<div class="fatFoot">').insertBefore('#mmmfooters');

	// create headers
	for(var i=0; i<fatFootHeaders; i++){
		if(fatFootJSON.fatFootItems[i].headerUrl == ''){
			$('<div>',{
				'class' : 'fatFootItem' + ' ' + 'fatFootCol'+fatFootJSON.fatFootItems[i].column.substring(0,1),
				'rel' : fatFootJSON.fatFootItems[i].column.substring(0,1),
				html : function(){
					if(fatFootJSON.fatFootItems[i].icon[0].src == ""){
						return '<h6 class="fatFootHeader">'+fatFootJSON.fatFootItems[i].headerName+'</h6>';
					}else{
						return '<img class="fatFootIcon '+fatFootJSON.fatFootItems[i].icon[0].iconClass+'" src="'+fatFootJSON.fatFootItems[i].icon[0].src+'" />'+'<h6 class="fatFootHeader">'+fatFootJSON.fatFootItems[i].headerName+'</h6>';
					}
				}
				
			}).appendTo($('.fatFoot'));		
		}else{
			$('<div>',{
				'class' : 'fatFootItem' + ' ' + 'fatFootCol'+fatFootJSON.fatFootItems[i].column.substring(0,1),
				'rel' : fatFootJSON.fatFootItems[i].column.substring(0,1),
				html : function(){
					if(fatFootJSON.fatFootItems[i].icon[0].src == ""){
						return '<h6 class="fatFootHeader"><a href="'+fatFootJSON.fatFootItems[i].headerUrl+'">' + fatFootJSON.fatFootItems[i].headerName + '</a></h6>';
					}else{
						return '<img class="fatFootIcon '+fatFootJSON.fatFootItems[i].icon[0].iconClass+'" src="'+fatFootJSON.fatFootItems[i].icon[0].src+'" />'+'<h6 class="fatFootHeader"><a href="'+fatFootJSON.fatFootItems[i].headerUrl+'">' + fatFootJSON.fatFootItems[i].headerName + '</a></h6>';
					}
				}
		}).appendTo($('.fatFoot'));
		}

	// populate children links
		for(var l=0; l<fatFootJSON.fatFootItems[i].links.length; l++){
			if(fatFootJSON.fatFootItems[i].links[l].linkUrl == ''){
				$('<li>',{
				'class' : 'fatFootLink',
				html : function(){
					if(fatFootJSON.fatFootItems[i].links[l].icon[0].src == ""){
						return fatFootJSON.fatFootItems[i].links[l].linkName;
					}else{
						return '<img class="fatFootIcon '+fatFootJSON.fatFootItems[i].links[l].icon[0].iconClass+'" src="'+fatFootJSON.fatFootItems[i].links[l].icon[0].src+'" />'+fatFootJSON.fatFootItems[i].links[l].linkName;
					}
				}
				}).appendTo($('.fatFootItem').eq(i));
			}else{
				$('<li>',{
				'class' : 'fatFootLink',
				html : function(){
					if(fatFootJSON.fatFootItems[i].links[l].icon[0].src == ""){
						return '<a href="'+fatFootJSON.fatFootItems[i].links[l].linkUrl+'" target="'+fatFootJSON.fatFootItems[i].links[l].popUp+'">' + fatFootJSON.fatFootItems[i].links[l].linkName + '</a>';
					}else{
						return '<img class="fatFootIcon '+fatFootJSON.fatFootItems[i].links[l].icon[0].iconClass+'" src="'+fatFootJSON.fatFootItems[i].links[l].icon[0].src+'" />'+'<a href="'+fatFootJSON.fatFootItems[i].links[l].linkUrl+'" target="'+fatFootJSON.fatFootItems[i].links[l].popUp+'">' + fatFootJSON.fatFootItems[i].links[l].linkName + '</a>';
					}
				}
				}).appendTo($('.fatFootItem').eq(i));
		}
	
	}

	//Gather column id's to figure out how many columns are being used, and push to the 'fatFootTotColArr' array
	fatFootTotColArr.push(fatFootJSON.fatFootItems[i].column.substring(0,1));
	
}// end for loop sequence to populate from JSON	

		
	//Sort through column id's to filter out duplicates, in the 'fatFootTotColArr' array, after the 'for loop', above(must be after the 'for loop')
	fatFootTotColArrFiltered = $.grep(fatFootTotColArr, function(element, index){
		return $.inArray(element ,fatFootTotColArr) === index;		
	});
	
	//Populate the new 'fatFootTotColArrFiltered' array with the filtered contents
	fatFootTotColArrFiltered.join();
	
	//Count out the length, to determine which column layout to use
	fatFootColLayout = fatFootTotColArrFiltered.length;
	
	//Apply layout classes based on 'fatFootColLayout'
	if(fatFootColLayout === 3){
		$('.fatFoot').addClass('fatFoot3Col');
	}else if(fatFootColLayout === 4){
		$('.fatFoot').addClass('fatFoot4Col');
	}
	

	// wrap each column in their own div wrapper
	$('.fatFootCol1').wrapAll('<div id="fatFootMet" class="fatFootWrap"></div>');
	$('.fatFootCol2').wrapAll('<div id="fatFootMet" class="fatFootWrap"></div>');
	$('.fatFootCol3').wrapAll('<div id="fatFootMet" class="fatFootWrap"></div>');
	$('.fatFootCol4').wrapAll('<div id="fatFootMet" class="fatFootWrap"></div>');
	
	// wrap each group of <li>'s in a <ul>
	$('.fatFootCol1, .fatFootCol2, .fatFootCol3, .fatFootCol4').each(function(){
		$(this).find('li').wrapAll('<ul></ul>');
	});
	
	}
	
	/* Metrics Tagging */
	$("#fatFootMet a").click(function(){dcsMultiTrack('WT.ac','Fat Footer Click: '+this.innerHTML,'DCS.dcsuri',window.location.pathname+'FatFooterClick','WT.dl','99')});
	
});