window.google_enable_async = 1;
var thread, active, post;

$(function(){

	return;

	thread = $("#thread");
	thread.addClass("thread");
	$("a[href='"+document.location+"']", thread).parent().addClass("active").next().addClass("active-thread");
	
	active = $("div.active", thread);
	if(active.length){

		$('body').addClass('post');

		var containers = $("li > div", thread).not(".sub");

		post = $("#post-wrap").hide();
		post.prepend($("<div class='navigator'><a class='up'>&uarr;</a> <a class='down'>&darr;</a></div>"));
		
		var index = containers.index(active);
		
		var href = $("a:first", containers.eq(index - 1)).attr('href');
		if(index > 0 && href){
			$(".up", post).attr({'href': href});
		} else {
			$(".up", post).addClass("disabled");
		}
		href = $("a:first", containers.eq(index + 1)).attr('href');
		if(href){
			$(".down", post).attr({'href': href});
		} else {
			$(".down", post).addClass("disabled");
		}
		
		active.css({"height": post.outerHeight()});
		setTimeout(function(){
			rr();
		}, 0); // fuck you chrome
		post.addClass("attached").show();
		$("li > div > a", thread).live('click', function(){
			$.cookie("scrollTop", $(document).scrollTop());
			return true;
		});

		var top = $.cookie("scrollTop") || active.position().top;
		top = (top > active.position().top) ? active.position().top : top;
		$(document).scrollTop(top);

		decorateTree(thread);

		$("#post-wrap img").hide().load(function(){
			$(this).show();
			rr();
		});

		$("#body").html(linkify($("#body").html()));
		$(".meta h1").html(linkify($(".meta h1").html()));

	}

	$(window).resize(function(){
		setTimeout(rr, 100);
	});
	
	var gmenu;
	$(".moar-groups").click(function(e){
		e.preventDefault();
		if(!gmenu){
			e.stopPropagation();
			$(document).click(function(e){
				if($("#groups").index($(e.target).parents()) < 0 && !$(e.target).is("#groups")){
					$(".moar-groups").trigger('click');
				}
			});
			gmenu = $("<div class='popup'></div>");
			gmenu.load("/ajax/groups", function(){ gmenu.prepend($("<div class='shadow'>")); groupAltify(); });
			$("body").append(gmenu);
		} else {
			$(document).unbind('click');
			gmenu.remove();
			gmenu = false;
		}
	});
	
	var firstDate;
	$("li > div.meta > a").each(function(i){
		var current = $(this).attr("data-date");
		if(i === 0){
			firstDate = toDate(current);
			$(this).parent(".meta").append($("<span class='date'>").text(current));
		} else {
			prevDate = $($(this).parents("li:first")).parents("li:first").find(" > div.meta > a").attr("data-date");
			var diff = 0 | (toDate(current) - toDate(prevDate)) / 1000 / 60;
			$(this).parent(".meta").append($("<span class='datediff'>").attr({title: current}).text("+" + prettyDiff(diff * 60)));
		}
	});

});

function toDate(d){
	return new Date(d.replace(/-/gi, '/'));
}

function prettyDiff(diff){

    var interval = 0 | diff / 31536000;
    if (interval){
        return interval + " m.";
    }
    interval = 0 | diff / 2592000;
    if (interval){
        return interval + " mėn.";
    }
    interval = 0 | diff / 86400;
    if (interval){
        return interval + " d.";
    }
    interval = 0 | diff / 3600;
    if (interval){
        return interval + " val." ;
    }
    interval = 0 | diff / 60;
    if (interval){
        return interval + " min.";
    }
    return diff + " sek.";
}

function markByDate(context){
	context = context || $("#threads");
	var now = new Date();
	$(".date", context).each(function(){
		el = $(this);
		temp = el.text();
		temp = temp.replace(/-/gi,'/');
		delta = (now - new Date(temp + ' +' + (now.getTimezoneOffset()/60)))/1000;
		if(delta < 3600){ // 1 hour
			el.addClass("date-new");
		} else if(delta < 21600){ // 6 hours
			el.addClass("date-newish");
		} else if(delta < 43200){ // 12 hours
			el.addClass("date-recent");
		} else if(delta < 86400){ // 1 day
			el.addClass("date-day");
		} else if(delta < 172800){ // 2 days
			el.addClass("date-dayish");
		} else if(delta < 345600){ // 4 days
			el.addClass("date-weekish");
		} else {
			el.addClass("date-old");
		}
	});
}

var rr = function(){
	post.css({"top": active.offset().top, "left": active.offset().left + 10});
	active.css({"height": post.outerHeight()});
};

var decorateTree = function(tree){
	$("ul",tree).each(function(){
		$("> li:last",this).addClass("last");
	});
	$("> .wrap > ul > li:first > div", tree).addClass("first");

	var li = $(".active", tree).parents("li");
	li.each(function(){
		temp = $(this).addClass("path-crossing").prevAll().addClass("path");
	});
	return tree;
};


