jQuery(document).ready(function($){
    // Remove unwanted empty paragraphs
    $('p:empty').remove();
	
	// Remove unwatend <br /> inside link
	$('a > br:first-child').remove();
	
	// Define textoverlay element
	$('#hero > div:nth-child(2)').addClass('textoverlay');
	
	// Add "first" and "last" classes to appropriate elements
	$('.navigation li:first-child').addClass('first');
	$('.navigation li:last-child').addClass('last');
	
	// Add unique classes to all children elements
	$('#header').nameItems({ htmlEl : 'div' });
	$('#hero').nameItems({ htmlEl : 'div' })
	$('#feature').nameItems({ htmlEl : 'div' })
	$('#teaserTop').nameItems({ htmlEl : 'div' });
	$('#sidebar_1').nameItems();
	$('#sidebar_2').nameItems();
	$('#teaserBottom').nameItems({ htmlEl : 'div' });
	$('#footerTop').nameItems({ htmlEl : 'div' });
	$('#footerBottom').nameItems({ htmlEl : 'div' });
	
	// Equalize height of products
	var maxHeight = 0;
	$('.productBlock').each(function() {
		if ( $(this).height() > maxHeight ) { 
			maxHeight = $(this).height();
		}
	});
	$('.productBlock').height(maxHeight);
	
	// First and last children
	
	$('#pCatalogue').nameChildren();
	/* $('#feature').nameChildren();
	$('#teaserTop').nameChildren();
	$('#teaserBottom').nameChildren();
	$('#footerTop').nameChildren();
	$('#footerBottom').nameChildren(); */
});

(function($){
	
	$.fn.nameItems = function( options ){
	
		var conf = { htmlEl : 'ul > li' }
		if ( options ) $.extend( conf, options );
		
		var el = this;
		var getID = el.attr('id');
		var idCount = 0;
		var htmlEl = conf.htmlEl;
		
		el.find('> ' + htmlEl).each(function() {
			idCount++
			$(this).addClass(getID + '_item_' + idCount);
		});
		
		return this;
	};
})( jQuery );

(function($){
	
	$.fn.nameChildren = function( options ){
		
		var conf = { htmlEl : 'div' }
		if ( options ) $.extend( conf, options );
		
		var el = this;
		
		if ( el.children().length > 1 ) {
		
			var count = Math.round(( el.width() / el.find(' > ' + conf.htmlEl).outerWidth(true) ));
			console.log('Count: ' + count);
			console.log('el.width(): ' + el.width());
			console.log('el child: ' + el.find(' > ' + conf.htmlEl).width());
			el.find(' > '+ conf.htmlEl +':nth-child('+ count +'n)').addClass('lastChild');
			el.find(' > '+ conf.htmlEl +':nth-child('+ count +'n)').next().addClass('firstChild');
		}
		
		el.find(' > '+ conf.htmlEl +':first-child').addClass('firstChild');
		
		return this;
	};
})( jQuery );
