$(document).ready(function(){
	//messaging
	$('.message').append(' <a class="close" href="#">Dismiss</a>').hide().slideDown(400).animate({opacity:1},4000).slideUp(300);
	$('.close, .message').click(function(){
		$(this).closest('.message').remove();
		return false;
	});

	//alertboxing
	$('.alertbox').append(' <a class="close" href="#">Dismiss</a>');
	$('.close, .message').click(function(){
		$(this).closest('.alertbox').remove();
		return false;
	});

	//retrieve lists via ajax
	var screen_name = $('#screen_name').val();
  	$('#screen_name').focus(function(){
  		screen_name = $('#screen_name').val();
  		$('#list_dropdown').slideUp('fast');
  	});
  	//$('#screen_name').after('<a id="get_lists" class="half_button">Get Lists</a>');
  	var show_dropdown = true;
	$('#screen_name').blur(function(){
		//alert('click');
		//console.log('clicked');
		//if ($(this).val() !== screen_name){
			//$('label[for="listid"]').after('<div id="list_loading">Loading</div>');
			$('#listid').val('Loading User\'s Lists');
			show_dropdown = false;
			$.ajax({
			   type: "POST",
			   url: "/app/get_lists",
			   data: "screen_name="+$('#screen_name').val(),
			   success: function(msg){
			     //console.log( msg );
			     show_dropdown = true;
			     $('#list_dropdown').html(msg).slideDown(600);
			     $('#listid').val($('#list_dropdown li:first-child').text());
			    // $('#list_loading').remove();
			   }
			});
		//}
	});
	$('#listid').focus(function(){
		if (show_dropdown){
			$('#list_dropdown').slideDown('fast');
		}
		$(this).keypress(function(e){			
			list_dropdown = $('#list_dropdown');
			
			var selected_list = $('#list_dropdown li').eq(0);
			
			if ($('.hover', list_dropdown).length !== 0){
				selected_list = $('.hover', list_dropdown);
			}
			
			$('.hover', list_dropdown).removeClass();
			
			if (e.keyCode == '38') { //up
				selected_list.prev('li').addClass('hover');
				e.preventDefault();
			} else if (e.keyCode == '40') { //down
				selected_list.next('li').addClass('hover');
				e.preventDefault();
			} else if (e.keyCode == '13') { //enter
				if ($('#list_dropdown').css('display') == 'block'){
					$('#listid').val(selected_list.text());
					$('#list_dropdown').slideUp('fast');
					e.preventDefault();
				}
			}
		});
	}).blur(function(){
		//$('#list_dropdown').slideUp('fast');
	}).click(function(){
		if (show_dropdown){
			$('#list_dropdown').slideDown('fast');
		}
	});;
	
	$('#list_selector').submit(function(){
		var screen_name = $('#screen_name', this).val();
		var listid = $('#listid', this).val();
		if (screen_name == '' || listid == '' || listid == 'Loading User\'s Lists' || listid.match(/no lists/i) || listid.match(/invalid username/i)) {
			alert('Please fill in both the user name and list fields!');
			return false;
		}
	});
	
	$('#list_dropdown li').live('mouseover', function(){
		$(this).addClass('hover');
	}).live('mouseout', function(){
		$(this).removeClass('hover');
	}).live('click', function(){
		$('#listid').val($(this).text());
		$('#list_dropdown').slideUp('fast');
	});
	
	//figure out how many users we're viewing and disable animations if more than 40
	if ($('.statistic .first .bar').length < 40) {
		var animations = true;
	} else {
		var animations = false;
	}
	
	if (animations){
		//fade in graphs on viewing
		$('.bar:not(.legend) .bar_wrapper .bar_proper', $('.horizontal_bar_graph')).css('opacity',0);
		$('.horizontal_bar_graph').one('inview', function (event, visible) {
		  if (visible == true) {
		    $(this).find('.bar:not(.legend) .bar_wrapper .bar_proper').each(function(){
				var target_width = $(this).css('width');
				$(this).css('width',0).animate({'width':target_width, 'opacity':1}, 2000);
			});
		  } else {
		    // element has gone out of viewport
		  }
		});
		$(window).scroll();
	}
		
	//sort graphs
	$('.statistic').each(function(){
		var originalRows = $(this).find('.bar:not(.legend)').get();
		$('.sort_by dd', this).css('cursor','pointer').click(function(){
		
			var rows = originalRows;
			var sortby = $(this).attr('class');
			if (sortby == 'alphabetical'){
				$.each(rows, function(index, row) {
					row.sortKey = $(row).find('.bar_title a:not(.profile_image)').text().toUpperCase();
				});
			} else if (sortby == 'updates') {
				$.each(rows, function(index, row) {
					row.sortKey = parseFloat($(row).find('.bar_proper').text());
				});
			} else if (sortby == 'original') {
				$.each(rows, function(index, row) {
					row.sortKey = parseFloat($(row).attr('class').replace(/[A-Za-z$-]/g, ""));
				});
			} else if (sortby == 'friends') {
				$.each(rows, function(index, row) {
					row.sortKey = parseFloat($(row).find('.bar_proper.alpha').text());
				});
			} else if (sortby == 'followers') {
				$.each(rows, function(index, row) {
					row.sortKey = parseFloat($(row).find('.bar_proper:not(.alpha)').text());
				});
			}
			
			if (sortby == 'alphabetical'){
				rows.sort(function(a, b) {
					if (a.sortKey < b.sortKey) return -1;
					if (a.sortKey > b.sortKey) return 1;
					return 0;
				});
			} else if (sortby == 'original') {
				rows.sort(function(a, b) {
					if (a.sortKey < b.sortKey) return -1;
					if (a.sortKey > b.sortKey) return 1;
					return 0;
				});
			} else { //sort #'s desc
				rows.sort(function(a, b) {
					if (a.sortKey < b.sortKey) return 1;
					if (a.sortKey > b.sortKey) return -1;
					return 0;
				});
			}
			
			$(this).closest('.statistic').find('div.legend').hide();
			
			$.each(rows, function(index, row) {
				if (animations){
					$(row).slideUp(400, function(){
						$(this).closest('.horizontal_bar_graph').append(row);
					});
					$(row).slideDown(400, function(){
						if (index == rows.length - 1){
							$(row).siblings('div.legend').each(function(){
								$(this).appendTo($(this).parent('div.horizontal_bar_graph'));
								$(this).slideDown(400);
							});
						}
					});
					row.sortKey = null;
				} else {
					$(this).closest('.horizontal_bar_graph').append(row);
					if (index == rows.length - 1){
						$(row).siblings('div.legend').each(function(){
							$(this).appendTo($(this).parent('div.horizontal_bar_graph'));
						});
					}
				}
			});
			
			
	
		});
		
	});
	
	$('a[href^=#]').click(function(){
		$.scrollTo($(this).attr('href'), 800);
		return false;
	});
	
	$('#tour li').eq(0).addClass('current');
	$('#tour').after('<a href="#" id="previous">Previous<span></span></a><a href="#" id="next">Next<span></span></a>');
	$('#previous').addClass('fail').hide();
	
	$('#next, #previous').click(function(){
		var tour = $('#tour'); 
		if ($(this).attr('id') == 'next'){
			whereToScroll($('.current', tour).next());
			$('.current', tour).removeClass().next().addClass('current');
		} else {
			whereToScroll($('.current', tour).prev());
			$('.current', tour).removeClass().prev().addClass('current');
		}
		
		if ($('.current', tour).index() == 0){
			$('#previous').hide();
		} else if ($('.current', tour).index() == $('li', tour).length - 1){
			$('#next').hide();
		} else {
			$('#previous, #next').show();
		}
		
		function whereToScroll(target){
			$('#tour').scrollTo(target, 800, {margin:true});
		}
		
		return false;
	});
	
	$('.bar:not(.legend) .bar_title').hover(function(){
		$(this).closest('.bar').addClass('hover').siblings('.hover').removeClass('hover');
		$('.status', $(this).closest('.bar')).show();
	}, function(){
		$(this).closest('.bar').removeClass('hover');
	});
	
	$('.timeline .user').hover(function(){
		$(this).addClass('hover').siblings('.hover').removeClass('hover');
		$('.status', $(this)).show();
	}, function(){
		$(this).removeClass('hover');
	});
	
	
});// end document.ready()