jQuery.fn.timbarber = function(after){
	if( !after ) { after = function(){} }
	
	this.each(function(){
		if( $(this).hasClass('timbarber-ed') ) { after(); return false; }
	
		var src = $(this).attr('src');
		var style = $(this).attr('style');
		var alt = $(this).attr('alt');
		var height = $(this).attr('height');
		var width = $(this).attr('width');

		$(this).addClass('timbarber-ed');
		$(this).html( '<img src="' + src + '" alt="' + alt + '" height="' + height + '" width="' + width + '" />' );
		$(this).find('img').show().loading('start').css('opacity',0).load(function(){
			$(this).animate({'opacity':1}).loading('end');
			after();
		});
	
		return this;
	});
};

jQuery.fn.loading = function(mode){
	this.each(function() {

		if( mode == 'start' ) {
			jQuery.fn.loading.current++;
		} else {
			jQuery.fn.loading.current--;
		}

		var selector = '.loading';

		$(selector)[ jQuery.fn.loading.current == 0 ? 'fadeOut' : 'fadeIn' ]();	
		
	});

	return this;
}
jQuery.fn.loading.current = 0;

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

jQuery.fn.fancyHover = function() {
	$(this).hover(function() {
		$(this).animate({'opacity':1});
	}, function() {
		$(this).animate({'opacity':0.85});
	});
}

jQuery.fn.viewed = function( threshold, action ) {
	var viewed = $(this).data('viewed');
	if( typeof(viewed) == 'undefined' ) { $(this).data('viewed', 1); return false; }
	if( viewed > threshold ) { return false; }
	if( viewed == threshold && action ) { action(); }
	
	$(this).data('viewed', viewed+1);
}

jQuery.fn.real_height = function(){
	var padding = $(this).padding();
	return $(this).height() + padding.top + padding.bottom;
};

jQuery.fn.real_width = function(){
	var padding = $(this).padding();
	var margin = $(this).margin();
	
	return parseInt($(this).attr('width')) + padding.left + padding.right + margin.left + margin.right;
};

jQuery.fn.calculate_width = function(selector) {
	var width = 0;
	$(this).find(selector).each(function() {
		width += $(this).real_width();
	})
	
	return width;
}