 (function($) {
    $.fn.wait = function(option, options) {
        milli = 1000; 
        if (option && (typeof option == 'function' || isNaN(option)) ) { 
            options = option;
        } else if (option) { 
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
            function() {
                var self = this;
                setTimeout(function() { $.dequeue(self); },settings.msec);
            });
        }

    }
})(jQuery);

function showSearch() {
	if (typeof(Timeout) == "number") {
		clearTimeout(Timeout);
		Timeout = "";
	}
	// Show search box, hide any subcats
	$("#search").show();
	$("#blog_nav_secondary > li:visible").each(function() {
		$(this).hide();
	});
}

function toggleLoading(status) {
	if (status == "show") {
		$("#loading").show();
	} else {
		$("#loading").hide();
	}
}

function fixPostPadding(This) {
	if (This.find("p > a.more-link").size() == 1) {
		This.find("p:last").css({
			paddingRight: '140px'
		});
	} else {
		This.find("p:last").css({
			paddingBottom: '10px'
		});
	}
}

$(function() {
	$('.main_container a').mouseover(function() {
		if ($(this).find(".shadow").length < 1) {
			$(this).prepend("<div class='shadow'></div>");
		} else {
			$(this).find(".shadow").show();
		}
	}).mouseout(function() {
		$(this).find(".shadow").hide();
	});
	
	$("#blog_nav > li").mouseenter(function() {
		This = $(this);
		Class = This.attr("class");
		Secondary = $("#blog_nav_secondary > li."+Class);
		if (Secondary.size() > 0) {
			// Close other subcategories
			showSearch();
			
			$("#search").hide();
			Secondary.show();
		}
	}).mouseleave(function() {
		Secondary = $("#blog_nav_secondary > li."+Class);
		if (Secondary.size() > 0) {
			Timeout = setTimeout(showSearch, 300);
		}
	});
	
	$("#blog_nav_secondary li ul").mouseenter(function() {
		clearTimeout(Timeout);
	}).mouseleave(function() {
		Timeout = setTimeout(showSearch, 300);
	});
	
	// Load more posts
	current_page = 1;
	$("#posts_nav a").live("click", function() {
		This = $(this);
		Posts = $("#posts");
		next_url = This.attr("href");
		toggleLoading("show"); // Show loading gif
		$.get(next_url, {}, function(data) {
			Posts.append($(data).find("#posts").html());
			current_page = current_page+1;
			toggleLoading("hide"); // Hide loading gif
			This.parent().remove(); // Remove this link
			$(".post").each(function() {
				fixPostPadding($(this));
			});
		}, "html");
		
		return false;
	});
	
	// Small logo hover
	$("body.blog #small_logo").mouseenter(function() {
		$("#btt_tooltip").fadeTo(0, 0).animate({'opacity':'1', 'marginTop' : '15px'}, 400).css("display", "block");
	}).mouseleave(function() {
		$("#btt_tooltip").animate({'opacity':'0', 'marginTop' : '0px'}, 400).css("display", "block");
	});
	
	// Fix layout issues
	$(".post").each(function() {
		fixPostPadding($(this));
	});
});