var overlay_to_use;var twitter_overlay;var ajax_overlay;
function render_graphs(){var $inline_pie = $("#section_associates span.inline_pie");$inline_pie.sparkline('html', {type: 'pie', sliceColors: ['#0c0cf0','#0cf057'], height: '20px'} );$inline_pie.toggleClass("inline_pie completed_pie");}
function do_highlights(){
render_graphs();
$("img.triggerable, span.triggerable").attr("title", "delete me");
$("img.remove_all").attr("title", "delete all");
$("img.hide_button").attr("title", "hide this section");
$("img.get_button").attr("title", "show this section");
$("img.subscriber_only").attr("title", "This advanced feature is only available to subscribers.  Click now to learn more (and get a free trial of all our features)!");
$("a.tooltip_recip_preview").attr("title", "<i>Reciprocity scores</i> are a powerful way to monitor how many people are following you back, per keyword. We show you precise statistics, including a small pie chart.<p/>Click on this link to upgrade now to activate this feature (as well as many others).");
$("#remove_keyword img.tooltip_keyword_follow").attr("title", "<i>Keyword follow</i> searches Twitter for the specified keyword.  We will then carefully examine each candidate user based on filters you have specified.  If they meet your criteria, we will follow them for you, up to 40 users per go.<p><b>Before you use this feature, it is very important that you click <i>show/edit filters</i> above.</b>");
$("#remove_mimic img.tooltip_mimic_follow").attr("title", "<i>Mimic follow</i> compares the people you follow to the people they follow. We will then carefully examine each candidate user who you do not follow and based on filters you have specified.  If they meet your criteria, we will follow them for you, up to 40 users per go.<p><b>Before you use this feature, it is very important that you click <i>show/edit filters</i> above.</b>");
$("img.tooltip_upload").attr("title", "upload now to your Twitter account");
$("#the_lists img.tooltip_edit_list").attr("title", "edit this list");
$("a.tooltip_followed").attr("title", "Click to review a list of all users you have followed for this item.  We'll also show you whether they followed you back, or whether you (eventually) purged them.");
$("div.tooltip_recip").attr("title", "We track every person you follow through Tweet Spinner.  We show you 'reciprocity scores' which tell you the percentage of people who you follow who follow you back.");
$("span.tooltip_action_queue").attr("title", "Click to review the queue of users that we will follow for you for this item (provided that the schedule is on).");
$("span.tooltip_action_queue_un").attr("title", "Click to review the queue of users that we will unfollow for you for this item (provided that the schedule is on).");
$("span.tooltip_approve_queue, img.tooltip_approve_queue").attr("title", "Click to decide which of the users we've found you'd like to follow (or not follow!)  We'll automatically follow them for you once approved.");
$("span.tooltip_approve_queue_un, img.tooltip_approve_queue_un").attr("title", "Click to decide which of the users we've found you'd like to unfollow (or not!)  We'll automatically unfollow them for you once approved.");
$("img.tooltip_findnow").attr("title", "This action will search Twitter using your filters and assemble a list of candidates to follow!");
$("img.tooltip_findnow_un").attr("title", "This action will search Twitter using your filters and assemble a list of candidates to unfollow!");
$("[title]").qtip({style: {tip: {corner: true}}, position: {my: 'top left', at: 'bottom center'}});
}
function url_magic(url) {url = url + twitid_qs + "&amp;uncacheme=" + Date();return url;}
function display_activity_log(data){
$("#invisible_activity_list").empty();
var first_time = $("#time_compare").html();
$("#activity_list").html(data);
var second_time = $("#time_compare").html();
if (first_time !== second_time) {
	$("#invisible_activity_list").html(data);
	$("#invisible_activity_floater").slideDown("normal").delay(5000).slideUp("normal");
}
}
function get_activity_log() {
var url = "home?op=get_short_activity_log";
$.get(url_magic(url), function(data){if (overlay_to_use.isOpened()) {overlay_to_use.close();}display_activity_log(data);});
}
function delete_via_AJAX(to_delete, url){
overlay_to_use = ajax_overlay;
$.ajax({
	url: url_magic(url),
	beforeSend: function(msg){overlay_to_use.load();},
	success: function(data){$(to_delete).fadeOut("fast");display_activity_log(data);},
	complete: function(){overlay_to_use.close();}
});
}
function do_AJAX_delete(event){
var action = $(event.target).parents("div.deletable_list").attr("id");
var to_delete = $(event.target).parents("div.deletable, span.deletable");
var id = to_delete.find(".id");
var id_qs = $(id).length ? "&id=" + jQuery.trim(id.html()) : "";
var nm = to_delete.find(".nm");
var nm_qs = $(nm).length ? "&nm=" + jQuery.trim(nm.html()) : "";
var url = "home?op=" + action + id_qs + nm_qs;
delete_via_AJAX(to_delete, url);
}
function get_via_AJAX(url, where_to_put_data, where_to_put_data_wrapper, disable_activity_get){
var original_title = $(document).attr("title");
$.ajax({
	url: url_magic(url),
	beforeSend: function(msg){overlay_to_use.load();$(document).attr("title", "[working]");},
	success: function(data){
		$(where_to_put_data).html(data);
		if (typeof disable_activity_get === "undefined") {
			get_activity_log();
		}			
	},
	error: function(data, txt){
		alert("Hm, we got an error back from the server.  Possibly Twitter is very slow right now?  Try again, and let us know if this problem keeps happening. " + txt);
	},
	complete: function(msg){
		do_highlights();
		$(document).attr("title", original_title);
		if (typeof where_to_put_data_wrapper !== "undefined") {
			if ($(where_to_put_data_wrapper).is(':hidden')) {$(where_to_put_data_wrapper).slideDown("normal");}
		}
		if (typeof disable_activity_get !== "undefined") {
			overlay_to_use.close();
		}
	}
});		
}
function boolean_schedule(caller, state){
var parent_id = $(caller).parents(".button_holder").attr("id");
var answer;
var when;
if (state == "off") {
	if (parent_id.match("(arch|spam)")) {
		when = "We will automatically perform this action approximately EVERY FOUR HOURS.";
	} else {
		when = "We will automatically perform this action approximately EVERY THREE HOURS.";
	}
	if (parent_id.match("(keyword|mimic)")) {
		when = when + " We will start with the first item below, and slowly walk down your list (re-starting when the cycle has completed).";
	}
	if (typeof when !== "undefined") {when = when + "\n\n";}
	answer = confirm("This subscriber-only feature will turn on the schedule for this feature.\n\n" + when + "Are you sure you wish to turn this on?");
}
if (answer || state == "on") {
	var url = "home?op=save_boolean_schedule&amp;type=" + parent_id + "&amp;current=" + state;
	overlay_to_use = ajax_overlay;
	get_via_AJAX(url, "#" + parent_id);
}
}
function ajax_twitter_get(url, caller){
var section_body = $(caller).parents(".section").find(".section_body");
overlay_to_use = twitter_overlay;
get_via_AJAX("home?" + url + "&amp;via_ajax=1", section_body);
}
function ajax_section_get(url, caller){
var section_body = $(caller).parents(".section").find(".section_body");
overlay_to_use = ajax_overlay;
get_via_AJAX("home?" + url + "&amp;via_ajax=1", section_body);
}
function ajax_general_toggle(url, place, wrapper){
if ($(wrapper).is(":visible")) {
	$(wrapper).slideUp("normal");
} else {
	if ($(wrapper).find(".iexist").length){
		$(wrapper).slideDown("normal");
	} else {
		overlay_to_use = ajax_overlay;
		get_via_AJAX("home?" + url + "&amp;via_ajax=1", place, wrapper, 1);
	}
}
}
function ajax_list_toggle(url, caller){
var parent = $(caller).parents(".preview_function");
ajax_general_toggle(url, parent.find(".placeable"), parent.children(".hidden"));
}
function close_only(who){if ($(who)) {if ($(who).is(':visible')) {$(who).slideUp("normal");}}return;}
function send_post(the_form){
overlay_to_use = ajax_overlay;
var original_title = $(document).attr("title");
var where_to_place = $(the_form).children("input[name='where_to_place_results']").val() || $(the_form).parents(".section").find(".section_body");
var data = twitid ? {twitid: twitid, via_ajax: 1} : {via_ajax: 1};
var options = {
	url: 'home', type: "POST", data: data,
	beforeSend: function(msg){overlay_to_use.load();$(document).attr("title", "[working]");},
	success: function(results) {
		$(where_to_place).html(results);
		close_only($(the_form).parents(".hidden")); //close the area if we were originally class=hidden
	},
	complete: function(msg){$(document).attr("title", original_title); get_activity_log(); do_highlights();}
};
$(the_form).ajaxSubmit(options);
}
function save_section_state(caller){
var section = $(caller).parents(".section");
var new_url = "home?op=toggle_" + section.attr("id");
new_url += section.hasClass("collapsed") ? "&amp;state=1" : "&amp;state=0";
$.ajax({url: url_magic(new_url), global: false});
}
function do_section_visibility(caller){
var section = $(caller).parents(".section");
if (section.hasClass("collapsed")) {
	section.find(".section_body").slideDown("normal", function(){section.toggleClass("collapsed expanded");do_highlights();});
} else {
	section.find(".section_body").slideUp("normal", function(){section.toggleClass("collapsed expanded");});
}
}
function toggle_extant_section(caller){save_section_state(caller);do_section_visibility(caller);}
function toggle_ajaxable_section(caller){
overlay_to_use = ajax_overlay;
save_section_state(caller);
var section = $(caller).parents(".section");
var section_body = section.find(".section_body");	
if (section.find(".iexist").length) {
	do_section_visibility(caller);
} else {
	var original_title = $(document).attr("title");
	var new_url = "home?op=" + section.attr("id") + "_internal";
	$.ajax({
		type: "GET",url: url_magic(new_url),global: false,
		beforeSend: function(){
			$(document).attr("title", "[working]");
			overlay_to_use.load();
		},
		success: function(msg) {section_body.html(msg);},
		complete: function(){
			do_section_visibility(caller);
			$(document).attr("title", original_title);
			overlay_to_use.close();
		}
	});
}
}
function toggle(who){if ($(who)) {if ($(who).is(':hidden')) {$(who).slideDown("normal");} else {$(who).slideUp("normal");}}return;}
function toggle_siblings(who){$(who).siblings().slideUp();toggle(who);}
function div_sort(what_to_sort, how_to_sort){$(what_to_sort).tsort(how_to_sort);}
$(function() {
var countdown = {
	init: function(){
		countdown.remaining = countdown.max - $(countdown.obj).val().length;
		if (countdown.remaining > countdown.max) {$(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));}
		$(countdown.obj).siblings(".remaining").html(countdown.remaining + " characters remaining.");
	},
	max: null,remaining: null,obj: null
};
$(".countdown").each(function(){
	$(this).focus(function(){
		var c = $(this).attr("class");
		countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
		countdown.obj = this;
		iCount = setInterval(countdown.init,1000);
	}).keypress(function(){
		countdown.init();
		clearInterval(iCount);
	});
});
});
$(document).ready(function(){
window.setTimeout('location.reload()', 5000000);
twitter_overlay = $("#twitter_overlay").overlay({api: true, close: 'null', closeOnClick: false, speed: 'fast', fadeInSpeed: 'fast', effect: 'apple',mask: {color: '#000000', opacity: 0.5, closeSpeed: 'fast', loadSpeed: 'fast', closeOnClick: false}});
ajax_overlay = $("#ajax_overlay").overlay({api: true, close: 'null', closeOnClick: false, speed: 'fast', fadeInSpeed: 'fast', effect: 'apple',mask: {color: '#000000', opacity: 0.5, closeSpeed: 'fast', loadSpeed: 'fast', closeOnClick: false}});
$("div.deletable, span.deletable, .stripable").live("mouseenter", function(){$(this).css("background-color", "#E1F2F9");});
$("div.deletable, span.deletable, .stripable").live("mouseleave", function(){$(this).css("background-color", "");});	
$("#promotion").live("mouseenter", function(){$(this).css("background-color", "#fadcb0");});
$("#promotion").live("mouseleave", function(){$(this).css("background-color", "");});
$("img.triggerable, span.triggerable").live("click", function(event){do_AJAX_delete(event);});	
//as of july 27, 2010, live does not work for IE for submit/click events, so have to use livequery
$("form.ajax_form").livequery("submit", function(event){send_post(event.target);return false;});
$("img.subscriber_only").livequery("click", function(){$(location).attr("href", "http://tweetspinner.com/subscribe");});
do_highlights();
$("#sec1m").slideDown("slow", function(){$("#sec2m").slideDown("slow", function(){$("#sec3m").slideDown("slow", function() {$("#sec4m").slideDown("slow");});});});
});
