function resetCloud(which) {
	$('ul#'+which).css("display","block");
	$(('ul#'+which+' a')).attr('target','_blank');
	if (which == "rtterms") {
		$('ul#'+which+' a').attr('target','_blank');
		$('ul#'+which+' a').each(function (i) {if (this.id.match('http')) {expandURL(this)}});
	} else {
		$('ul#'+which).tagcloud({type:"list",sizemin:11,sizemax:32,height:300,colormin:"6F6F6F",colormax:"000"});
	}
	switchLinks("twitter", 0);
	var c = readCookie('outlink');
	if (c) switchLinks(c);
	pageTracker._trackEvent("rt", which, "refresh");
}
function truncURL(text, cut) {
	out = text.replace(/^http:\/\//i, '').replace(/^www./i, '');
	if (out.length > cut) {
		out = out.substring(0,cut) + "...";
	}
	return out	
}
function switchLinks(searchName, cookie) {
	if (searchName == "twitter") {
		$("ul.links a").each(function (i) {if (!this.id.match('http')) {this.href='http://search.twitter.com/search?q='+escape(this.id);}});
	} else if (searchName == "spy") {
		$("ul.links a").each(function (i) {if (!this.id.match('http')) {this.href='http://spy.appspot.com/find/'+escape(this.id)+'?latest=25';}});
	}
	$('a.linkswitch').each(function(i) {
		if (this.getAttribute('id') == "link_"+searchName) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');		
		}
	});
	if (cookie != 0) {
		createCookie('outlink', searchName, 365);
	}
}
function processtextitem(json) {
	var text = "";
	if (!json.empty) {
		text = '<li value="'+json.count+'">';
		if (json.yterm.match('http')) {
			text = text + '<a href="'+json.yterm+'" id="'+json.yterm+'">'+json.yterm+'</a>';
		} else {
			text = text + '<a href="http://spy.appspot.com/find/'+json.yterm+'?latest=25" id="'+json.yterm+'">'+json.yterm+'</a>';
		}
		text = text + '</li>\n\n';
	} else {
		text="Error: Please Refresh";			
	}
	return text;	
}
function processtoplistitem(json, count) {
	var dispcount = '<span class="rank">'+ (count + 1) + '</span>';
	var text = "";
	if (!json.empty) {
		if (count < 15) {
			text = '<li value="'+json.count+'">';
			if (json.yterm.match('http')) {
				text = text + '<a href="'+json.yterm+'" id="'+json.yterm+'">'+truncURL(json.yterm, 25)+'</a>';
			} else {
				text = text + '<a href="http://spy.appspot.com/find/'+json.yterm+'?latest=25" id="'+json.yterm+'">'+json.yterm+'</a>';
			}
			text = text + '</li>\n\n';
		}
	} else {
		text="Error: Please Refresh";			
	}
	return text;	
}
function modeRefresh(which, num) {
	$('span#loader').html('<img src="/images/load.gif" alt="loading - " />');
	$.get("/getdata?type="+which+"&num="+num,
		function(data){
			var jsons = eval('(' + data + ')');
			$('ul#'+which).empty();
			$('ul#'+which).css("display","none");
			$.each(jsons.reverse(), function(i, n){$('ul#'+which).prepend(processtextitem(n));});
			resetCloud(which);
			$('span#loader').html('');
		});
}
function modeRealtime() {
	$('span#loader').html('<img src="/images/load.gif" alt="loading - " />');
    $('li#today').removeClass('curmode');
    $('li#today').addClass('othermode');
    $('li#realtime').removeClass('othermode');
    $('li#realtime').addClass('curmode');
    $(this).removeClass('active');
	$.get("/json?type=terms",
		function(data){
			var jsons = eval('(' + data + ')');
			$('ul#terms').empty();
			$('ul#terms').css("display","none");
			$.each(jsons.reverse(), function(i, n){$('ul#terms').prepend(processtextitem(n));});
			resetCloud("terms");
			$('span#loader').html('');
		});
}
function loadTopList(which) {
	$.get("/json?type="+which,
		function(data){
			var jsons = eval('(' + data + ')');
			$('ul#'+which).empty();
			$.each(jsons, function(i, n){$('ul#'+which).append(processtoplistitem(n, i));});
			resetTopTen(which);
		});
}
var refreshAuto = "";
function startAutoRefresh() {
	refreshAuto = setInterval("modeRefresh()", 180000);	
	$("a#refresh_auto").addClass('active');
	$("a#refresh_stop").removeClass('active');
	pageTracker._trackEvent("rt", "control", "startAuto");		
}
function stopAutoRefresh() {
	clearInterval(refreshAuto);
	$("a#refresh_auto").removeClass('active');
	$("a#refresh_stop").addClass('active');	
}
function resetTopTen(which) {
	$('ul#'+which+' a').attr('target','_blank');
        $('ul#'+which+' li:first').addClass('first');
	$('ul#'+which+' li:eq(1)').addClass('second');
	$('ul#'+which+' li:eq(2)').addClass('third');
	if (which == "urls"){
		expandURLs($('ul#urls a'));
	}
	$('ul#'+which+' li:gt(1)').each(function (i) {lum = 1 - (i * (.8/$('ul#'+which+' li:gt(1)').size()));$(this).css('opacity', lum)});
}
function expandURL(input) {
	var link = $(input);
	$.ajax({
		type: 'GET',
		url: 'http://api.longurl.org/v1/expand',
		data: {format: 'json', url: link.attr('href')},
		dataType: 'jsonp',
		success: function(data, responseText) {
			if (typeof(data.messages) !== 'undefined') { 
				// There was an error -- do nothing.
				return;
			} else {
				var response = data.long_url;
				link.attr('href', response);
				link.html(truncURL(response, 25));
			}
		}
	});
}
function expandURLs(links) {
	links.filter('a[href^="http://"]').each(function() {
		expandURL($(this));
	});
}
function rollOff() {
    greatest = 0;
    mean = 0;
    count = 0;
    $('ul#terms li').each(function (i,v) {
        current = $(v).attr("value");
        if (current > greatest) { greatest = current; }
            mean = mean + current;
            count = count +1;
        });
        cutoff = greatest - (greatest * .12);
        $('ul#terms li').each(function (i,v) {
            current = $(v).attr("value");
            timesmean = current/(mean/count);
            if (timesmean >= 4) {
                awaytop = 1-((greatest-current)/greatest)
                mult = 1-(.35*awaytop)
                newvalue = current*mult; 
                $(v).attr("value", newvalue)
                $(v).attr("orig", current)
                //alert(current+' roll off '+newvalue);
            }
        });
}