(function($) {
	
	/**
	 * Replaces the text with a png image in nav items
	 */
	$.fn.replacetext = function() {
	
		var href = this.attr('data-href');
		var text = this.text();
		var navclass = this.closest('.nav').hasClass('vnav') ? 'vnav' : 'hnav';
		
		var imgsrc = 'img/nav_'+href.replace('?p=/', navclass+'_').replace(/\//g, '_').replace(/-/g,'')+'.png';
		this.html(''/*<span class="hitarea"><span class="inner">'+this.text()+'</span></span>'*/);
		this.find('.hitarea').css({
			display: 'block',
			position: 'absolute',
			top: 0,
			left: 0
		});
		var self = this;
		this.css({cursor:'default'});
		this.append($('<div class="text-image">'));
		this.attr('data-text', text);
		$('<img>')
			.attr({
				'class': 'up-state replacement-image',
				alt: text, 
				src: imgsrc 
			})
			.css({
				cursor: 'pointer'
			}) 
			.load(function() {
				$(this).closest('a').css({
					width:$(this).width(),
					height: $(this).isChildOf('.l3') ? 12 : $(this).height()
				});
			})
			.error(function() {
				//$(self).html(text)
				//console.log(imgsrc);
			})
			.appendTo(this.find('.text-image'));
		
		if ($(this).isChildOf('.hnav')) {
			var active_src = imgsrc.replace('.png', '_active.png');
			$('<img>')
			.attr({
				'class': 'over-state replacement-image',
				alt: text, 
				src: active_src 
			})
			.appendTo(this.find('.text-image'));
		}
		
		return this;
		
	}
	 
}(jQuery));
