var ES = new Object()

ES.id_counter = 0;

ES.TopicRetriever = function(url, onload, maxheadlines, maxdays, startdate) {
	this.url = url;
	this.onload = onload;
	this.maxheadlines = (maxheadlines) ? maxheadlines : 25;
	this.currentdate = (startdate) ? new Date(startdate) : new Date();
	this.maxdays = (maxdays) ? maxdays : 30;
	this.lastdate = new Date(this.currentdate);
	this.lastdate.setDate( this.lastdate.getDate() - this.maxdays );
	this.headlines = new Array();
	ES.id_counter ++;
	this.ES_id = "ES_JS_" + this.currentdate.getTime() + "_" + ES.id_counter;
	this.getDataFromServer(url);
}

ES.TopicRetriever.prototype.processDay = function()	{
	var thisObj  = ( this.retriever ) ? this.retriever : this;
	thisObj.aggregateStories();
	thisObj.getBackInTime();
	if ( thisObj.checkForContinue() ) {
		stories = null;
		thisObj.getDataFromServer(thisObj.getUrl());
	}
	else {
		stories = thisObj.headlines;
		thisObj.onload();
	}
}

ES.TopicRetriever.prototype.getDataFromServer = function(url) {
    // Fetch the element pointed to by the id. If it exists, we destroy it so we can create a new one.
    this.oScript = document.getElementById(this.ES_id);
	// Point at the script tag, if it exists
	this.head = document.getElementsByTagName("head").item(0);
	// Destroy the tag, if it exists
	if (this.oScript) { this.head.removeChild(this.oScript); }
	// Create the new script tag
	this.oScript = document.createElement("script");
	// Setup the src attribute of the script tag
	this.oScript.setAttribute("src", url);
	// Set the id attribute of the script tag
	this.oScript.setAttribute("id", this.ES_id);
	this.oScript.retriever = this;
	// Create the new script tag which causes the proxy to be called
	this.head.appendChild(this.oScript);

	if ( this.oScript.addEventListener ) {
		this.oScript.addEventListener ( "load", this.processDay, false );
		// Asynchronous script tag properties -- a proprietary IE "feature"
	} else if ( document.all ) {
		if ( this.oScript.readyState == "complete" || this.oScript.readyState == "loaded" ) {
			this.processDay();
		} else {
			this.oScript.onreadystatechange = this.checkAgain;
		}
	// All other web browsers just do the callback function
	} else if ( this.oScript.attachEvent ) {
		this.oScript.attachEvent( "onload", this.processDay );
	}
}

	// Used by IE to handle state changes
ES.TopicRetriever.prototype.checkAgain = function () {
	var thisObj  = ( this.retriever ) ? this.retriever : this;
	if ( thisObj.oScript.readyState == "complete" || thisObj.oScript.readyState == "loaded" ) {
		thisObj.oScript.onreadystatechange = null;
		thisObj.processDay();
	}
}

ES.TopicRetriever.prototype.checkForContinue = function () {
	return ( this.headlines.length < this.maxheadlines && this.currentdate >= this.lastdate);
}

ES.TopicRetriever.prototype.getBackInTime = function () {
	this.currentdate.setDate(this.currentdate.getDate() - 1);
}

ES.TopicRetriever.prototype.aggregateStories = function() {
	var s;
	var unique = true
	// check for stories
	if ( stories && stories.length ) {
		for ( var s in stories ) {
			if ( this.isUniqueinArray ( stories[s].storyid, "storyid") ) {
				if ( this.headlines.length < this.maxheadlines ) {
					this.headlines[this.headlines.length] = stories[s];
				} else {
					break;
				}
			}
		}
	}
}

ES.TopicRetriever.prototype.isUniqueinArray = function (value, key) {
	var arr = this.headlines;
	for ( var i in arr ) {
		if ( value == arr[i][key] ) {
			return false;
		}
	}
	return true;
}

ES.TopicRetriever.prototype.getUrl = function () {
	return this.url + "&datetime=[day=" + this.getDateName() + "]";
}

ES.TopicRetriever.prototype.getDateName = function () {
	return (this.currentdate.getMonth() + 1 ) + "/" + this.currentdate.getDate() + "/" + this.currentdate.getFullYear();
}

