jQuery.easing.def = "easeInOutSine";
var baseurl = "http:///wemeddle.com";

function isdonedigging() {
    $.getJSON("donedigging.php", function(data) {
	if (data.isdonedigging) {
	    window.location = baseurl+"/lists/";
	} else {
	    $("#loginstatus").html("Oopsy! We slipped and didn't fully process your account. Hmm. We'll work on it!"); 
	} 
    });
}

function digbabydig() {
    $("#loginlink").remove();
    $("#loginstatus").html("We're chatting with the ghost of Twitter past.<br/>It might take a minute. He's old, you know.");
    $("#dig").show();
    isdonedigging();
    diganimation();
}

function diganimation() {
    $("#bigcloud").fadeIn();
    $("#littlecloud").fadeIn();
    $("#bigcloud").animate( { left:"236px" }, 16000, "linear", function() {
	$("#bigcloud").fadeOut();
    });
    $("#littlecloud").animate( { left:"181px" }, 16000, "linear", function() {
	$("#littlecloud").fadeOut("normal", function() {
	    $("#bigcloud").css({'left': '50px'});
	    $("#littlecloud").css({'left': '-5px'});
	    setTimeout(diganimation, 1000);
	});
    });
}

function update() {
    var stat = $("#statusbox").val();
    if (!stat) return;
    $("#statusbox").css("color", "#bbb");
    $.post("updatestatus.php", { st: stat }, function() {
	setTimeout(function() {
	    $('#updatebox').slideToggle('fast');
	    $("#statusbox").val("");
	    $("#statusbox").css("color", "#fff");
	    $("#tweetcounter").html("140");
	}, 2000);
    });
}

function updatecounter(event) {
    var stat = $("#statusbox").val();
    var left = 140 - stat.length;
    $("#tweetcounter").html(left);
    if (left < 0) {
	$("#tweetbtn").attr("disabled", "true");
    } else {
	$("#tweetbtn").removeAttr('disabled'); 
    }
}

function invite() {
    var p = $("#invite").val();
    if (!p) return;
    p = $.trim(p);
    $.post("invite.php", { person: p }, function(data) {
	if (!data.success) { 
	    $("#invitemsg").html("sorry, we couldn't do it."); 
	    return;
	}
	var invitesleft = parseInt($("#invitesleft").html())-1; 
	if (invitesleft < 0) invitesleft = 0;
	$("#invitesleft").html(invitesleft);	
	$("#invitemsg").html("lovely! we @-mentioned them."); 
	$("#invitemsg").css("color", "#ed7d7a");
	$("#invite").val("");
	setTimeout(function() { 
	    $("#invitemsg").fadeOut("normal", function() {
		$("#invitemsg").html("twitter screenname, then press enter");
		if (invitesleft == 0) $("#invitemsg").html("we hope to give out more soon!");
		$("#invitemsg").css("color", "#808080");
		$("#invitemsg").fadeIn("normal");
	    });
	}, 6000);
    }, "json");
}

function override(sn, dir) {
    if (!sn || !dir) return;
    $.post("override.php", { overridesn: sn, direction: dir }, function(data) {
	if (data.success) window.location.reload();
    }, "json");
}

function togglesubtweets(subid) {
    var mt = $("#more-"+subid+" a:first").html();
    if (/\+/.test(mt)) { $("#more-"+subid+" a:first").html(mt.replace(/\+/, "&ndash;")); } 
    else { $("#more-"+subid+" a:first").html("+"+mt.substring(1)); }
    $("#subtweetblock-"+subid).slideToggle("fast");
    $("#subtweetblock-"+subid+" img").toggle(100);
}

var activefilter = null;
function filterby(filter, classname) {
    if (activefilter) {
	if (activefilter == $(filter).attr("id")) return false;
	wasactive = activefilter;
	activefilter = $(filter).attr("id");
	$("#"+wasactive).mouseout();
	// only need to do this if something was just active
	$(".filterout").addClass("maybringback").removeClass("filterout");
	$(".filterin").removeClass("filterin");
	$(filter).css("background", "#4A929E");
    } else {
	activefilter = $(filter).attr("id");
	if (/comm/.test(classname)) {
	    $("#comm-showalldiv").show();
	} else {
	    $("#more-showalldiv").show();
	}
    }
    $(".strongslidercontainer").hide();
    $(".weakslidercontainer").hide();
    $(".tweet").each(function() {
	var x = $(this);
	if (!x.hasClass(classname)) x.parent().addClass("filterout");
	else x.parent().addClass("filterin");
    });

    // $(".filterin:hidden").slideDown("fast").animate({ "opacity" : 1 }, "fast");
    // $(".filterout").animate({ "opacity" : 0.01 }, "fast", function() {
    $(".filterin.maybringback").slideDown("fast", function() {
	$(".filterin.maybringback").css("opacity", 1).removeClass("maybringback");
    });
    $(".filterout").css("opacity", 0.01).slideUp(500);
    // });

    $(".liextrapad").each(function() {
	var thee = $(this);
	var prev = thee.prev();
	while (prev != null && (prev.hasClass("liextrapad") || prev.hasClass("filterout"))) prev = prev.prev();
	var next = thee.next();
	while (next != null && (next.hasClass("liextrapad") || next.hasClass("filterout"))) next = next.next();
	if (prev.hasClass("minuspacked") == next.hasClass("minuspacked") || prev.attr('id') == 'updatebox') thee.hide();	
    });

    $(".liextrapad").each(function() {
	var thee = $(this);
	var next = thee.next();
	while (next != null && next.hasClass("filterout")) next = next.next();
	if (next.hasClass("liextrapad")) thee.hide();
    });

    // done filtering tweets; handle state of filter links
    $(".filterlink a").each(function() {
	if ($(this).attr("id") != $(filter).attr("id")) {
	    $(this).css("color", "#aaa");
	    $(this).find('.bykeyword').css("color", "#aaa");
	}
    });
    return false;
}

var strongsliderhidden = false;
var weaksliderhidden = false;
function resetfilter() {
    // undo actions in fiterby
    $(".liextrapad").show();
    $(".filterlink a").each(function() {
	$(this).css("color", "#4A929E");
	$(this).css("background", "transparent");
	$(this).find('.bykeyword').css('color', '#222'); 
	$(this).find(".filternum").css('color', '#bbb'); 
    });

    $(".filterout").slideDown("normal", function() {
	$(".filterout").animate({ "opacity": 1 }, "fast");
	$(".filterout").each(function() { $(this).removeClass("filterout"); });
    });
    // $(".filterin.notthere").fadeOut("normal").slideUp("slow", function() {
	// $(".filterin").each(function() { $(this).removeClass("filterin"); });
    // });

    if (!strongsliderhidden) $(".strongslidercontainer").fadeIn("normal");
    if (!weaksliderhidden) $(".weakslidercontainer").fadeIn("normal");
    activefilter = null;
    $("#more-showalldiv").hide();
    $("#comm-showalldiv").hide();
}

function filteronmouseover(el) { 
    var bg = "#4A929E";
    if ($(el).attr("id") == activefilter) return;
    else if (activefilter) bg = "#93BEC5";
    $(el).css("color", "#fff");
    $(el).css("background", bg);
    $(el).find('.bykeyword').css('color', '#fff'); 
    $(el).find(".filternum").css('color', '#fff'); 
}
function filteronmouseout(el) { 
    if ($(el).attr("id") == activefilter) return;
    var gray = (activefilter != null);
    $(el).css("color", gray ? "#aaa" : "#4A929E");
    $(el).css("background", "transparent");
    $(el).find('.bykeyword').css('color', gray ? "#aaa" : '#222'); 
    $(el).find(".filternum").css('color', gray ? "#aaa" : '#bbb'); 
} 

var slidercurrent;
var midpoint = 3;
var zooms = 0;
function zoom(where) {
    slidercurrent = 4+13*where;
    $.post("recordpref.php", { innieoutiewhere: slidercurrent });

    var offset = midpoint-where;
    var absoffset = Math.abs(offset);
    var strongpre = "plus";
    var weakpre = "minus";
    if (offset < 0) { strongpre = "minus"; weakpre = "plus"; } 
    else if (offset == 0) { strongpre = "even"; weakpre = "even"; }
    var strongclass = strongpre;
    var weakclass = weakpre;
    for (var i=1; i <= absoffset; i++) {
	strongclass += " " + strongpre+i;
	weakclass += " " + weakpre+i;
    }
    $(".tweetblock:not(.notthere)").each(function() {
	// remove old skew classes 
	var strongset = $(this).find(".tweet.strongtie");
	var weakset = $(this).find(".tweet.weaktie");
	if (offset > 0) {
	    strongset.removeClass("even minus minus1 minus2 minus3");
	    weakset.removeClass("even plus plus1 plus2 plus3");
	    reinstateminus3();
	    switch (offset) {
		case 1: 
		    strongset.removeClass("plus2 plus3");
		    reinstateminus2();
		    break;
		case 2:
		    strongset.removeClass("plus3");
		    break;
		default: break;
	    }
	} else if (offset == 0) {
	    reinstateminus3();
	    reinstateminus2();
	    strongset.removeClass("minus minus1 minus2 minus3 plus plus1 plus2 plus3");
	    weakset.removeClass("minus minus1 minus2 minus3 plus plus1 plus2 plus3");
	} else {
	    weakset.removeClass("even minus minus1 minus2 minus3");
	    strongset.removeClass("even plus plus1 plus2 plus3");
	    reinstateminus3();
	    switch (offset) {
		case 1: 
		    weakset.removeClass("plus2 plus3");
		    reinstateminus2();
		    break;
		case 2:
		    weakset.removeClass("plus3");
		    break;
		default: break;
	    }
	}
	strongset.addClass(strongpre).addClass(strongclass);
	weakset.addClass(weakpre).addClass(weakclass);
	if (strongpre == "even") { $(".sn-controls-minus").removeClass("sn-controls-minus"); }
	else if (strongpre == "minus") { 
	    $(".tweet.strongtie").parent().find(".sn-controls").addClass("sn-controls-minus"); 
	    $(".tweet.weaktie").parent().find(".sn-controls").removeClass("sn-controls-minus"); 
	}
	else { 
	    $(".tweet.weaktie").parent().find(".sn-controls").addClass("sn-controls-minus"); 
	    $(".tweet.strongtie").parent().find(".sn-controls").removeClass("sn-controls-minus"); 
	}

    });
    $(".minus2 img.profile").pixastic("desaturate");
    $(".minus3").addClass("reinstate").css("opacity",0.01).slideUp("fast", function() {
	$(".liextrapad").hide();
    });
    $(".minus").parent().addClass("minuspacked");
    $(".plus").parent().removeClass("minuspacked");

    if (zooms++ == 0) $(".tweetblock").css("opacity", 1);
    $(".plus").each(function() {
	var li = $(this).parent();
	if (li.height() > 50) {
	    if (li.next().hasClass("liextrapad")) {
		li.next().css("padding-top", "8px");	
	    } else {
		li.next().css("margin-top", "8px");
	    }
	}
    });
}

function reinstateminus2() {
    $(".minus2 canvas.profile").each(function() {
	var a = $(this).parent();
	var s = a.attr('imgsrc');
	a.append('<img valign="top" class="profile" src="'+s+'" border="0" />');
	$(this).remove();
    });
    $(".minus2").removeClass("minus2");
}

function reinstateminus3() {
    $(".liextrapad").css("padding-top", "20px");
    $(".minus3.reinstate").show().css("opacity", 1).removeClass("minus3 reinstate");
}

function showsncontrols(sn) { $("#sn-controls-"+sn).show(); }
function hidesncontrols(sn) { $("#sn-controls-"+sn).hide(); }

function filternumbers() {
    $(".filterlink a").each(function() {
	var n = 0;
	var f = $(this).attr("id");
	n = $(".tweet."+f+":visible").size();
	$(this).find(".filternum").html(n);
    });
}

function notinalpha() {
    $("#loginstatus").css("color", "#0b5166");
    $("#loginstatus").css("font-weight", "bold");
    $("#loginstatus").html("sorry, you need an invite.<br/>@-mention us, we might have one.");
}

jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) { jQuery("<img>").attr("src", arguments[i]); }
}

function removefromlist(sn, par) {
    $("#"+par+" div[sn='"+sn+"']")
	.animate({ "opacity" : 0.25 }, "fast");
    $.post("slash.php", {listkey: par.replace("div",""), sn: sn});
}

function onlyfollowhere(sn, par) {
    $.post("onlyfollowhere.php", {listkey: par.replace("div",""), sn: sn});
}

function showsomemore(el) {
    $(el).parent().parent().parent().find(".toomany")
	.css({ "display": "inline-block", "opacity" : "0.001"})
	.animate({"opacity": "1"});
    $(el).hide();
}

