
var t;
var timer_running=0;

function doTimer() {
	if (!timer_running) {
		timer_running=1;
		timer_callback();
	}
}

/* my formula */
/* so there are 604,800 seconds per week.  We get at least 1,000 books a week.
 * 505 topic and style points, plus around 45 keywords avg per book,
 * so we'll call it 550 data points per book(not counting sub-book stuff.
 * so that's 550,000 data points per week(~0.90939 per sec).  
 * We are starting with roughly 30,000*550=16,500,000,
 * starting somewhat arbitrarily at June 1st.  June 1st was about 1,306,905,314 secs epoch time.
 * thus, the constants going into the function below */
function timer_callback_old() {
	var d = new Date();
	var x = (d.getTime()/1000) - 1306905314;
	var b = 16500000;
	var m = 0.909391534391534;
	/* rounded m to 1 to make the animation smoother. */
	m = 1;
	var y = Math.round(((m*x) + b)) + " "; // make sure it's converted to a string
	// if there gets to be more decimal places, it'll be obvious since there will be 4 places
	// after the last comma, and hence we will know for sure to adjust
	var outStr = y.substr(0,2) + "," + y.substr(2,3) + "," + y.substr(5);	
	$("#nowUsingNumber").text(outStr);
	t=setTimeout("timer_callback()",1000);
}

/* ok, might as well do this right instead of hardcoding comma location */
function commify(num) {
	var len=num.length;
	var out;
	var processed=0;

	/* get the first chunk */
	first_bite = (len % 3) ? (len % 3) : 3;
	out = num.substr(0,first_bite);

	/* get the rest */
	processed=first_bite;
	while(processed < len) {
		out += ",";
		out += num.substr(processed, 3);
		processed += 3;
	}
	return out;
}

/* Aaron's new formula */
/*  
 * Raw writing style metrics per scene: 90
 * Scenes per book: 101 = 9090
 *
 * Themes measure per book: 2000 
 * Divided into 10 scenes per book: 20,000 
 *
 * Character tracking: Est. 10 characters per book
 * divided into 101 scenes per book= 1,010 
 *
 * Keywords: Est 60 per book
 *
 * total average number of data points per book: 30,160 data points.  
 * also, new estimate is 200 books per week */
function timer_callback() {
	var d = new Date();
	var x = (d.getTime() - Date.parse("Aug 1, 2011")) / 1000;
	var b = 20000 * 30160 ;
	/* m is data points per sec (30160 * 200 ) / (7*86400) */
	var m = 9.9735449735;
	var y = String(Math.round(  ((m*x) + b)  )); 
	var outStr = commify(y);
	$("#nowUsingNumber").text(outStr);
	t=setTimeout("timer_callback()",1000);
}

function show_genre_dialog() {
	$("#genreDialog").dialog({width: 550, title: "Select a genre:"});
}

function show_author_dialog() {
	$("#authorDialog").dialog({width: 400, title: "Browse authors by last name:"});
}

function show_topic_dialog() {
	$("#topicDialog").dialog({width: 800, title: "Browse by Story DNA:", resizable: false });
}

function hide_genre_dialog() {
	$("#genreDialog").dialog('close');
}

function hide_author_dialog() {
	$("#authorDialog").dialog('close');
}

function hide_topic_dialog() {
	$("#topicDialog").dialog('close');
}


function show_help(questDiv) {
        var offset = $(questDiv).offset();
        var helpDiv = $('#simpleHelp');
        var id = $(questDiv).attr('id');
        var helpText ="";
	var diagOffset=21;
        if(id == 'question1' || id == 'uniqueTitle') {
                helpText='Uniquely unique titles are books that are mathematically very different from other books in our system.  A book featured here may have an unusual thematic make-up or is written in an unusual way.  Aside from being a fun way to discover new titles, this sort of "negative suggestion" is a simple example of something that is difficult for social recommendations to do, but easy for DNA-style analysis.';
		diagOffset=50;
        } else if(id == 'question2' || id == 'link2') {
		helpText='StoryDNA is a breakdown of the top "Thematic Ingredients" that are present in a title.  Unlike genres, which classify what a book is about and the genre conventions used, StoryDNA attempts to represent which themes appear IN the title compared to other books in the corpus.  For example, are there more "Horses" in one book than in others in the database.';
        } else if(id == 'question3' ) {
		helpText='This is the average number of DNA elements in our database, currently expanding at a rate of about 6,000,000 a week.  Other sites always had big counters displaying how much content they had, often in the millions of book records.  So we sat down and calculated out how many data points we extracted per-book during analysis.  The answer was 32,162 points of DNA per title.  If we are successful at recruiting additional publishers to participate in the project, we expect to be approaching two BILLION elements of BookDNA in our system within a few months.';
	}
        /* need to at least offset the help div the size of the icon.  otherwise
           the div pops up in front of the icon and triggers a mouseout, which
           hides the div, until the ? is visible again, so it flipflops! */
        helpDiv.html(helpText); 
        helpDiv.css('top', offset.top + 15 + diagOffset); 
        helpDiv.css('left', offset.left + diagOffset); 
        helpDiv.show();
}               
                
function hide_help(questDiv) {
        var helpDiv = $('#simpleHelp');
        helpDiv.hide();         
}

var storydna_text="empty";
function show_storydna(data, textStatus, jqXHR) {
        var dialog_title = "StoryDNA of <i>" + data.title + "</i>, by " + data.author;
        storydna_text=data.storydna;
        $("#storydna_text").html(storydna_text);
        $("#storydna_text").dialog({width: 450, title: dialog_title });
}

$(document).ready(function() {
	search_init();
	doTimer();
});

