function sleep(naptime){
    naptime = naptime * 1000;
    var sleeping = true;
    var now = new Date();
    var alarm;
    var startingMSeconds = now.getTime();
    while(sleeping){
        alarm = new Date();
        alarmMSeconds = alarm.getTime();
        if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
    }        
}

function record_event_before_submit_j(category,action,label,form_name) {
/*	var message = "<div id='all_messages' class='prepend-4 span-16'><div class='notice_message'><div style='text-align: left; margin-left: 3em;'>You are being Signed Up. Please Wait... <img src='/site_media/image/indicator.gif'/></div></div></div><br/>" */
/*	window.parent.$('#njs-message').html(message); */
	$.ajax({
		url: "/record_event/",
		type: 'post',
        data: {category: category, action:action, label:label},
        complete: function( result ) {
        	$('#' + form_name).submit(); 
        }
    });
}

/* function clear_messages() {
	window.parent.$('#njs-message').html('');
 } */

function record_event(category,action,label) {
    $.ajax({
	url:'/record_event/',
	type: 'post',		
	data: {category: category, action:action, label:label }
    });
    if (action && $(window).data("action")) {
	$(window).data("action").push(action);
	$(window).trigger("event_triggered", [category, action, label]);
    } else {
	$(window).data("action", [action]);
    }
}

function showSurveyWidget(surveyActions) {
    $(window).bind("event_triggered", function(event, category, action, label) { 
	$(window).data("action").reverse();
	if ($.isArray(surveyActions) && $.inArray(action, surveyActions) != -1 && $.inArray(action, $(window).data("action")) == 0 && !$.cookie("survey_answered")) {
	    $("div#initial_message").show();
	    $("form#surveyForm").hide();
	    $("div#surveyContent").children().remove();
            $("div#surveyBox").dialog({modal: true, width: 500, open: function(event, ui) { 
		$.get("/fetch_survey/", {"action": action}, function(data, textStatus){ 
		    record_event("Survey", "Survey Form Shown", label);
		    $("input[name=survey_id]").val(data.id);
		    $("input[name=next]").val(window.location);
		    $(data.content).each(function(i, k) { 
			$("div#surveyContent").append("<p id=question_"+i+"><strong>"+$(k).attr("question")+"</strong></p>");
			$.each($(k).attr("options"), function(j, l) { 
			  $("p#question_"+i).append("<p><input type='checkbox' name='response' value=\""+l+"\" />"+l+"</p>");
			});
			$("p#question_"+i).append("<br />");
		    });
		    $("div#initial_message").hide();
		    $("form#surveyForm").show();
		    $(window).unbind("beforeunload");
		});
            }, close: function(event, ui) { 
	    	record_event("Survey","Survey Not Taken",label);
	    }
				      });
	}
    });
}
