$(document).ready(function(){
//#-------------------------------------------------------------------

	$('table').attr('border','0').attr('cellspacing','0').attr('cellpadding','0');
	$('#promo h2 a').vAlign();
	$('#promo').InitPromo();
	$('#index-boxes,div.columnbox').InitIndexBoxes();
	
	$('div.dualindexbox div:nth-child(2n)').css('margin-right','0');
	$('div.columnbox div:nth-child(2n)').css('margin-right','0');

	$('div.dualindexbox').equalHeights();

	findTablesToStyle();

//#-------------------------------------------------------------------
});


/* INIT INDEX BOXES
----------------------------------------------------*/
(function ($) {
	$.fn.InitIndexBoxes = function() {
		var indexBox = $(this);
		indexBox.find('div').each(function(){
			var indexBoxDiv = $(this);
			var list = indexBoxDiv.find('h2');
			if(list.length > 1){
				var first = indexBoxDiv.find('h2:first');
				list.not(':first').each(function(){
					first.html(first.html() +'&nbsp; | &nbsp;'+ $(this).html());
					$(this).hide().next().hide();
				});
				first.find('a:first').addClass('current');
				first.find('a').bind('click', function(){
					var index = first.find('a').index($(this));
					if(indexBoxDiv.find('dl').eq(index).length){
						first.find('a').removeClass('current');
						$(this).addClass('current');
						indexBoxDiv.find('dl').hide(200, function(){
							indexBoxDiv.find('dl').eq(index).show(200);
						});
					}
					return false;
				});
			}	
		});
	};
})(jQuery);

/* INIT PROMO
----------------------------------------------------*/
(function ($) {
	$.fn.InitPromo = function() {
		var promo = $(this);
		promo.find('h2:first').addClass('active');
		promo.find('li:not(:first) div,li:not(:first) img').hide();
		promo.find('a').each(function(){
			var link = $(this).attr('href'),
				parent = $(this).parent().parent();
			parent.find('div,img').css('cursor','pointer').bind('click', function(){
				location.href = link;
			});
			$(this).parent().bind('click', function(){
				promo.find('h2.active').removeClass('active');
				promo.find('img:visible').fadeOut(500);
				promo.find('div:visible').fadeOut(500);
				parent.find('img').fadeIn(500);
				parent.find('div').fadeIn(500);
				$(this).addClass('active');
				$(this).find('a').blur();
				return false;
			});
		
		})
	};
})(jQuery);

/* VERTICAL ALIGN
----------------------------------------------------*/
(function ($) {
	$.fn.vAlign = function() {
		return this.each(function(i){
			var elm = $(this),
				height = elm.height(),
				parentHeight = elm.parent().height(),
				margin = Math.ceil((parentHeight-height) / 2);
			$(this).css('margin-top', margin);
		});
	};
})(jQuery);

/* SET EQUAL HEIGHTS ON COLUMNS
----------------------------------------------------*/
(function ($) {
	$.fn.equalHeights = function () {
		var tallest = 0;
		$(this).children().each(function () {
			var thisHeight = parseInt($(this).css('height'),10);
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		if(tallest !== 0){
			$(this).children().css('height',tallest);
		}
	};
})(jQuery);

// Special method for styling of the share data tables from Cision
function styleTable(table){
	var tableRows = table.getElementsByTagName("tr");
	for (var i = 0, n = tableRows.length; i < n; i++){
		if (i > 0){
			if (i % 2 > 0){
				tableRows[i].style.backgroundColor = "#EEEBE6";
			}
		}
	}
}

// Looks for tables to style
function findTablesToStyle(){
	var tables = document.getElementsByTagName("table");
	for (var tableIndex = 0, n = tables.length; tableIndex < n; tableIndex++){
		if (tables[tableIndex].className === "infoTable"){
			styleInfoTable(tables[tableIndex]);
        }
		if (tables[tableIndex].className === "financialTable"){
			styleFinancialTable(tables[tableIndex]);
		}
	}
}

// Loops the rows of an infotable and set CSS-classes.
function styleInfoTable(table){
	var tableRows = table.getElementsByTagName("tr"),
		firstRow = 1,
		rowIndex = 0;
	if (tableRows.length === 0){
		return;
    }  
	if (tableRows[0].getElementsByTagName("th").length === 0){
		firstRow = 0;
	}
	for (var i = firstRow, n = tableRows.length; i < n; i++){
		if (rowIndex % 2 === 0){
			tableRows[i].className = tableRows[i].className + " evenRow";
		}
		rowIndex ++;
	}        
}

// Loops the rows and cells of a financial table and set CSS-classes.
function styleFinancialTable(table){
	var tableRows = table.getElementsByTagName("tr");
	for (var i = 0, n = tableRows.length; i < n; i++){
		var tableCells = tableRows[i].getElementsByTagName("td");
		if (tableCells.length === 0){
			tableCells = tableRows[i].getElementsByTagName("th");
        }        
		for (var j = 0, z = tableCells.length; j < z; j++){
			if (j === 0){
			}else{
				if (j % 2 > 0){
					tableCells[j].className = tableCells[j].className + " highlight";    
				}else{
					tableCells[j].className = tableCells[j].className + " normal";
				}
			}
		}       
	}
}
