var count = 0;
var latest_time = "now";


$(document).ready(function()
{	
	init();
});

function page_init()
{
	$("div.hiphap a.register").unbind().fadeOut("slow", function()
	{
		$("div.hiphap a.exclusive").css("display", "block").hide().fadeIn("slow");
		
		
		$(document).everyTime(10000, function(){ hiphap("/hiphap/when") }, 5);
		$(document).oneTime(5000, function(){ hiphap("/hiphap/when/true") });

		
	});
}

function page_init_unregistered()
{
	$("div.hiphap a.register").css("display", "block").hide().fadeIn("slow").bind("bind", function()
	{
		do_register();
		
		return false;
	});
}

function init()
{
	$("div.hiphap ul li span").each(function()
	{
		$(this).removeAttr("style").attr("title", $(this).width()).css({width:"0px"});
	});
	
	
	$("div.hiphap li").unbind().bind("mouseover", function()
	{
		$(this).children("span").stop().animate({width:parseInt(parseInt($(this).children("span").attr("title")) + 5) + "px"}, 100).dequeue();
				
		return false;
	}).bind("mouseout", function()
	{
		$(this).children("span").stop().animate({width:"0px"}, 100).dequeue();
		return false;
	});
}

function set_latest_time(new_time)
{
	latest_time = new_time;
}

function hiphap(hhurl)
{
	$.ajax(
	{
		url : hhurl,
		data : "time=" + latest_time,
		dataType : "json",
		type : "post",
		timeout : 3000,
		success : function(e)
		{
			if(e.length > 0)
			{
				$("div.hiphap").prepend("<ul class='incoming'></ul>");

				for(var i = 0; i < e.length; i++)
				{
					if(e[i].link == null)
					{
						$("ul.incoming").append("<li style='display:none;'><span>" + e[i].human_date + "</span><a href='/profile/user/" + e[i].user + "'>" + e[i].user + "</a> " + e[i].description + "</li>");
					}
					else
					{	
						$("ul.incoming").append("<li style='display:none;'><span>" + e[i].human_date + "</span><a href='/profile/user/" + e[i].user + "'>" + e[i].user + "</a> <a href='" + e[i].link + "'>" + e[i].description + "</a></li>");
					}
				}

				for(var i=0; i < e.length; i++)
				{
					$("div.hiphap li:last").remove();
				}
				if(typeof e[0].unix_date != undefined)
				{
					latest_time = e[0].unix_date;
				}
				
				$("ul.incoming li").slideDown("fast");
				$("ul.incoming").removeClass("incoming");
				
				init();
			}
		}
	});
}