// Assumes core.js is loaded and defines uShow
// should rename to home.js

$(document).ready(function(){
	// profile links if on profile page;

	uShow.loadHomeFeed = function(show_load) {
		var container = $("#home_feed");
		if (show_load) uShow.showLoading(container, 1);
		if (container.length) {
			var url = container.attr("url");
			$.get(url, null, function(data, textStatus) {
				container.html(data);
				uShow.setScroll();
				uShow.setStoryCount();
			});
		}
	}
	uShow.loadHomeFeed(1);

	$("#id_public_feed").change(function() {
		var state = $(this).val();
		var url = "/flags/public_feed/" + state;
		uShow.showLoading($("#home_feed", 1));
		$.get(url, null, function() {
			uShow.loadHomeFeed(1);
		})
	});

	$(".post-link-ie").live('click', function() {
		$(".post-area").html("").hide();
		$(".post-area-ie").show();
		uShow.addvideo.resetUpload();
		return false;
	});

	$(".post-link").live('click', function() {
		$(".post-area-ie").hide();
		var url = $(this).attr("href");
		var postload = $(this).attr("load");
		var container = $(".post-area").show();
		uShow.showLoading(container, true);
		$.get(url, null, function(data, textStatus) {
			container.html(data);
			if (postload)
			  uShow.addVideo[postload]();
		});
		return false;
	});
	$("#home a.youtube").trigger('click');

	$(".post-links-close").click(function() {
		$(".post-area").fadeOut("fast", function() { $(this).html(""); });
	});

	$("a.join_ushow").live('click', function() {
		var msg = $("#join_dialog").html();
		uShow.showDialogOK("Join the Conversation", msg);
	});

	uShow.checkNewer = function() {
		var div = $(".show-newer"),
		url = div.attr("url"),
		count = div.attr("count");
		if (url) {
			$.get(url, "count="+count, function(data, textStatus) {
				div.replaceWith(data);
				setTimeout(uShow.checkNewer, 20000);
			});
		}
	}

	$(".show-newer").live('click', function() {
		$(this).attr("count", 0).hide("normal").html(" ").show("normal");
		$(".feed").show("normal");
	});

	setTimeout(uShow.checkNewer, 20000);

});
