function insertTimestamp(timestamp, id) {
  var d = new Date(timestamp*1000);
  document.getElementById(id).innerHTML = d.toLocaleString();
}

function inject_helpful_prompts(){
    $(".helpful").show();
    $(".helpful .yes").click(function(){
        var thisHelpful = $(this).closest('.helpful');
        thisHelpful.find(".prompt").fadeOut();
        thisHelpful.find(".thanks").fadeIn();
        thisHelpful.delay(1000).hide('blind');
        return false;
    });
    $(".helpful .no").click(function(){
        $(this).closest('.helpful').find(".prompt").fadeOut();
        $(this).closest('.helpful').find(".askmore").show('blind');
        return false;
    });
    $(".helpful form").submit(function(evt){
        var form = $(this);
        var finishSubmit = function(){
                    form.parent().fadeOut("normal", function(){
                        form.closest('.helpful').find('.thanks').fadeIn();
                        form.closest('.helpful').delay(1000).hide('blind');
                    });
                };
        $.ajax({
            type: "POST",
            url: "/helpful-send",
            data: form.serialize(),
            success: finishSubmit,
            error: finishSubmit
        });
        return false;
    });
}


$(function(){
	//Nicen the menu dropdowns
	$("#mainNav ul ul").css('display','none');
	$("#mainNav li").each(function(){
	   var t = $(this);
	   t.hover(function(){
	       t.find('ul').slideDown(70);
	   }, function(){
	       t.find('ul').slideUp(70);
	   });
	});
	
	// Initiate logo rotation
	$("#logos-list").hide();
	$("#logos-rotation").each(function(){
	   $(this).tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 5000); });
    
    (function(){
        var x=0;
        var rl = function(){
            $(".logos-rotation img:eq("+x+")").fadeOut(function(){
                x ++;
                x = x % $(".logos-rotation img").length;
                $(".logos-rotation img:eq("+ x+")").fadeIn();
            });
        };
        setInterval(rl, 5000);
    }());
    

	// Rounded corners
	$(".gray-box").corner("8px");
	$("#sidebar-gray").corner("8px");

	$("#account-form input[type='text']").focus(function() {
		$(this).toggleClass("focus");
	});

    inject_helpful_prompts();
    
    
    $(window).scroll(function() {
        $(".movedown").each(function(){
            var sb = $(this);
            var ht = sb.height();
            if ($(window).height() > ht) {
                var scrollTo = $(window).scrollTop()
                if (scrollTo < sb.parent().height() - ht){
                    sb.animate(
                        { top: scrollTo + 'px' },
                        { queue: false, duration: 500, easing: 'easeInOutSine' }
                    );
                }       
            }
        });
    });
    
    
    
    
});

/* BE CAREFUL: putting this inside the $.ready() block
 * seems to cause trouble, and it will break the build.
 * We need to clean this up and figure out exactly what's
 * going on at some point. */
$("form#account-form").each(function(){
    $(this).validate({rules: {username: {required: true,
                              minlength: 4,
                              maxlength: 20,
                              remote: "/ajax-validate"},
                   password: {required: true,
                              minlength: 6},
                   email: {required: true,
                           email: true}
                  }
          });
});

function record_metric(category, name) {
    $.post("/metric/" + category + "/" + name);
}

already_recorded_metrics = {};

function record_metric_once(category, name) {
    if(already_recorded_metrics[category+"/"+name] === undefined){
        /* record a new one */
        record_metric(category, name);
        already_recorded_metrics[category+"/"+name] = true;
    }
}

