var frm; // box that contains the form
var shr; // box that contains the share options
var lbl; // label for url text box
var txt; // url text box
var btn; // submit button
var err; // hidden error box
var ldr; // loading animation
var error_visible;


var twt_this; // tweet this link
var start_again; // start again link
var visit_link; // visit the link

var chart_tv; // chart total velocity chart_total_velocity


function base_init()
{
	txt = $("#url");
	btn = $("#btn");
	err = $("#err");
	frm = $("#form");
	lbl = $("#lbl");
	shr = $("#share");
	ldr = $("#loading");
	chart_tv = $("#chart_total_velocity");
	
	twt_this = $("#a_tweet_this");
	start_again = $("#a_start_again");
	visit_link = $("#a_track_this");
}

$(document).ready(function(){

	base_init();
	
	if( txt )txt.focus();
	
	// Wire up event handler for squish button
	if( btn )btn.click(validate);
	
	// Check for quick shorten querystring
	// and if present, auto initiate the 
	// squishing.
	if( jQuery.url.param("url") ) 
	{
		//if( txt) txt.val(jQuery.url.param("url"));
		//if( btn) btn.click();
	}
	
	bind_charts();

});

function schedule(fn) {
	window.setTimeout(fn, 20000);
}

function bind_charts()
{
	
	get_velocity_chart(); 
	schedule(bind_charts);
}

function get_velocity_chart()
{
		if( !chart_tv ) return;

		$.getJSON(
		"hour.php",
		function(d) {
			
			if( d.error ) {
				throw d.error;
			}
			
			var series = "";
			$.each( d.response, function(index, item) {

				series += item.redirects + ",";
				
			
			} );
			
			if( series.length == 0 ) {
				throw "could not reach the api for velocity statistics";
			}
			
			// chop last comma
			series = series.substr(0, series.length-1);
			
			chart_tv.text(series);
			chart_tv.sparkline("html", { height: "75", width: "100%" } );
			
			
		} 
	);
}



function validate(e) {

	base_init();
	if( txt.val() == "" )
	{
		error("Enter a url to shorten first.");
		return;
	}

	btn.hide();
	lbl.hide();
	txt.hide();
	ldr.fadeIn("fast");
	
	$.getJSON(
		"api/shorten/",
		{ url : txt.val() },
		function(d) {
			if( d.error ) {
				
				ldr.fadeOut("normal", function(){ 
					error(d.error_message);
					btn.show();
					lbl.show();
					txt.show();
				});
				return;
			}
			
			display_short_url(d.response);
			wire_up_share_links(d.response);
			
		}
	);
}


function display_short_url(url)
{
	txt.val(url);
	
	ldr.fadeOut("normal", function() {
		txt.fadeIn("normal");
		shr.fadeIn("normal");
	});
}

function wire_up_share_links(url)
{
	// Wire up retweet link
	twt_this.attr("href", make_retweet(url));
	
	// Wire up start again link
	start_again.attr("href", "/" );
	
	visit_link.attr("target", "_blank");
	visit_link.attr("href", url + "/stats");
	
}

function error(msg)
{
	if( error_visible) hide_error();
	
	disable_form(); 
	err.html(make_error(msg));
	err.slideDown("slow");
	
	error_visible = true;
	window.setTimeout( hide_error, 3000 );
}

function disable_form()
{
	btn.attr("disabled", "true");
	txt.attr("disabled", "true");
	
}

function enable_form()
{
	txt.attr("disabled", "");
	btn.attr("disabled", "" );
}

function hide_error()
{
	if( error_visible )
	{
		err.slideUp("normal");
		error_visible = false;
		enable_form();
	}
}

function make_retweet(url)
{
	return 'http://twitter.com/home?status=' + url;
}

function make_error(msg)
{
	return '<div id="auto_error"><h1>Error</h1><h2>Oops, an error ocurred:</h2><p class="error">' + msg + '</p></div>';
}