var message = '';

/*
function eqHeights(sel) {
	var hightest = 0;
	$(sel).each(function(i, el) {
		if ($(el).height() > hightest) hightest = $(el).height();			
	});
	$(sel).height(hightest);
}
*/

function eqHeightsFirstLastPair(selFirsts, lookFor) {
	$(selFirsts).each(function(i, element) {
		var element1, element2;
		if(lookFor) {
			element1 = $(element).find(lookFor);
			element2 = $(element).next('.rowLast').find(lookFor);
		} else {
			element1 = $(element);
			element2 = $(element).next('.rowLast');
		}
		$(element1).removeAttr('style');
		$(element2).removeAttr('style');
		if ($(element2).length > 0) {
			if ($(element1).height() > $(element2).height()) {
				$(element1).height($(element1).height());
				$(element2).height($(element1).height());
			} else {
				$(element1).height($(element2).height());
				$(element2).height($(element2).height());
			}
		}
	});
}

function matchColumnHeights(){
	// if( $('#leftColumn').length > 0 ){
		$('#secondaryNavigation').removeAttr('style');
		$('#mainColumn > .inner').removeAttr('style');
		var contentHeight = $('#content').height() - $('#breadcrumb').outerHeight(true);
		var bannerHeight = $('#banner').height();
		var headingHeight = $('#mainColumn > .mainHeading:visible').outerHeight(true)
		+ $('#mainColumn > .innerTop').outerHeight(true)
		+ $('#mainColumn > .postOfTheWeek').outerHeight(true);
		$('#secondaryNavigation').height( contentHeight - 82 );
		$('#mainColumn > .inner').height( contentHeight - bannerHeight - headingHeight - 72 );
	// }
}

function applyHeights(){
	eqHeightsFirstLastPair('.dashboardWidgets .rowFirst', '.inner');
	eqHeightsFirstLastPair('.storiesPage .paginatedList li.rowFirst');
	matchColumnHeights();
}

$(document).ready(function(){
	
	$('.homePagePods .module:nth-child(3n)').addClass('rowLast');
	
	var firstLastSelectors = [
		'.categoryPage .paginatedList > ul > li',
		'.storiesPage .paginatedList li',
		'.helpAndSupportPod',
		'.dashboardWidgets .module',
		'.dashboardPage .mainContentBlocks .module'
	];
	
	for(var i=0; i < firstLastSelectors.length; i++){
		$(firstLastSelectors[i] +':even').addClass('rowFirst');
		$(firstLastSelectors[i] +':odd').addClass('rowLast');
	}

	$('.borderradius .bodyText ol li').each(function(i){
		$(this).prepend('<span class="number">' + (i + 1).toString() + '</span>');
	});
	
	$('.postPage').has('.roundel').find('.mainHeading').last().addClass('hasRoundel');
	
	applyHeights();
	/* run again after a second to catch up with @font-face */
	setTimeout( 'applyHeights()', 1000 ); 
	
	$('#matchColumnHeights').click(function(e){
		applyHeights();
		e.preventDefault();
	});
	
	/****************  please leave this to last i.e. no more code below this line!! *******************/
	message += 'Browser: \n';
	jQuery.each(jQuery.browser, function(i, val) {
		message += i + ': ' + val + '\n';
    });
	// alert(message);
	
});

