jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function infiniteScroll() {
	var last = $('#posts div:last').attr("id");
	//alert(last);

	$("#posts").append("<div class='post' id='loader_" + last + "' style='text-align: center;'><img src='/images/ajax-loader.gif' alt='loading'/></div>");

	var $_GET = {};

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) {
			return decodeURIComponent(s.split("+").join(" "));
		}

		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});

	var currentNum = $('#posts div').size();
	currentNum = currentNum - 1;
	currentNum = currentNum / 3;

	$.ajax({
		type: "GET",
		url: "infinitescroll.php",
		data: "last=" + last + "&tag=" + $_GET["tag"] + "&currentNum=" + currentNum + "&search=" + $_GET["q"],
		success: function(msg){
			$("#posts").append(msg);
			$("#loader_" + last).remove();
		}
	});
}

$(document).ready(function() {
	$.preloadImages("/images/pre_post_roll.gif", "/images/next_post_roll.gif");
});



function clearSearch() {
	if($('#search_box').val() == "search") {
		$('#search_box').val("");
	}
}
function addSearch() {
	if($('#search_box').val() == "") {
		$('#search_box').val("search");
	}
}

function vote(id) {
	$.ajax({
		type: "POST",
		url: "vote.php",
		data: "id=" + id,
		success: function(msg){
			updatePage(msg);
		}
	});

	function updatePage(msg) {
		$('#votes_' + id).attr("innerHTML", msg);
	}
}

function showTags() {
	//$('#tags_bar').stop();
	if($('#tags_bar').css('display') == "none") {
		$('#tags_bar').slideDown("normal");
	} else {
		
		$('#tags_bar').slideUp("normal");
	}
}