$(function() {
	
	function round(num) {
		num = num.toString();
		var split = num.split(".");
		return split[0];
	}
	
	$.updateClicks = function(a) {
		$.getJSON("http://cuthut.com/api/clicks/clicks.json?url="+a, function(json) {
			$('#theclicks li.today').html(json.today);
			$('#theclicks li.thisweek').html(json.week);
			$('#theclicks li.thismonth').html(json.month);
			$('#theclicks li.alltime').html(json.all);
		});
		$.getJSON("http://api.tweetmeme.com/url_info.json?url=http://cuthut.com/"+a, function(json) {
			$('li.retweetnumber').html(json.url_count);
		});
	}
	$.hourGraph = function(a) {
		$.getJSON("http://cuthut.com/api/clicks.json?url="+a, function(json) {

			var plot_data = new Array();
			var plot_ticks = new Array();
			
			for (var i in json) {
				i = parseInt(i);
				plot_data.push([i, json[i].clicks]);
				plot_ticks.push([i, json[i].time]);
			}
			
			$.plot($("li#clicksgraph #innerChart"),
				[
					{data: plot_data, color: "#6fb5d2", hoverable: true}
				], {
					points: {show: true}, 
					lines: {show: true},
					yaxis: {
						tickDecimals:0
					},
					xaxis: {
						ticks: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
					},
					grid: {
						hoverable: true
					}
				}
			);
			
			function showTooltip(x, y, contents) {
			$('<div id="tooltip">' + contents + '</div>').css( {
				position: 'absolute',
				display: 'none',
				top: y + 5,
				left: x + 5,
				border: '1px solid #fdd',
				padding: '4px',
				'background-color': '#b3e0f3',
				opacity: 0.80
				}).appendTo("body").fadeIn(200);
			}
	
			var previousPoint = null;
			$("#innerChart").bind("plothover", function (event, pos, item) {
				$("#x").text(pos.x.toFixed(2));
				$("#y").text(pos.y.toFixed(2));
				
				if (item) {
					if (previousPoint != item.datapoint) {
						previousPoint = item.datapoint;
						
						$("#tooltip").remove();
						var x = item.datapoint[0].toFixed(2),
							y = item.datapoint[1].toFixed(2);
							
						showTooltip(item.pageX, item.pageY,
		                           round(y) + " Clicks for minute " + round(x));
					}
				} else {
					$("#tooltip").remove();
					previousPoint = null;          
				}
				
			});
		
	
		});
	}
	
	var numimg = Math.floor(Math.random()*4) + 1;
	$('#medium-rect-ad a').css('display', 'none');
	$('#medium-rect-ad a.' + numimg).css('display', 'block');
	
	$('a#desktopref').hover(function() {
		$('#desktophover').fadeIn(200);
	},function(){
		$('#desktophover').stop().fadeOut(200);
	});
	
	$('#bookmarklet p a').hover(function() {
		$('#bookmarklet p a img').attr('src', '/static/img/bookmarklet-hover.png');
	}, function() {
		$('#bookmarklet p a img').attr('src', '/static/img/bookmarklet.png');
	});
	
	$('#clicks h3 a').click(function() {
		$(this).addClass('on');
		$('#referrers h3 a').removeClass('on');
		$('#clicksinfo').css('display', 'block');
		$('#referrersinfo').css('display', 'none');
	});
	$('#referrers h3 a').click(function() {
		$(this).addClass('on');
		$('#clicks h3 a').removeClass('on');
		$('#clicksinfo').css('display', 'none');
		$('#referrersinfo').css('display', 'block');
	});
	
	$('#clicksinfo .times li').click(function() {
		$('#clicksinfo .times li').removeClass('on');
		$(this).addClass('on');
	});
	/* start time changes for clicks */
	$('#clicksinfo .times li a').click(function() {
		var timeper = $(this).attr('class');
		$('#theclicks li').css('display', 'none');
		$('#theclicks li.retweetnumber').css('display', 'block');
		$('#theclicks li.' + timeper).css('display', 'block');
	});
	
	$('#referrersinfo .times li').click(function() {
		$('#referrersinfo .times li').removeClass('on');
		$(this).addClass('on');
	});
	/* start time changes for clicks */
	$('#referrersinfo .times li a').click(function() {
		var timeper = $(this).attr('class');
		$('#referrers li').css('display', 'none');
		$('#referrers li.retweetnumber').css('display', 'block');
		$('#referrers li.' + timeper).css('display', 'block');
	});
	
	$('#referrers h3 a').click(function() {
		$('#clicksinfo').css('display', 'none');
		$('#referrersinfo').css('display', 'block');
	});
	
});