var StopReportController = function(requestUrl, requestTimeout) {

	var E_RESULTSLOADING = "stopreports_resultsLoading";
	var E_RESULTSTIMEOUT = "stopreports_resultsTimeout";
	var E_RESULTSERROR = "stopreports_resultsError";
	var E_RESULTSBLOCK = "stopreports_resultsBlock";

	var E_RF_TITLE = "stopreports_resultsBlock_title";
	var E_RF_DATEVIEW = "stopreports_resultsBlock_date";
	var E_RF_GLPERCENT = "stopreports_resultsBlock_glPercent";
	var E_RF_GLPERCENT1 = "stopreports_resultsBlock_glPercent1";
	var E_RF_SIGNALSCOUNT = "stopreports_resultsBlock_signalsCount";

	var C_FIELD = "field";
	var C_FIELDGAIN = "field_gain";
	var C_FIELDLOSS = "field_loss";
	var C_TAB = "";
	var C_TABACTIVE = "active";

	this.pDay = new StopReportsPeriod("stopreports_tabDay", "d", "Daily");
	this.pWeek = new StopReportsPeriod("stopreports_tabWeek", "w", "Weekly");
	this.pMonth = new StopReportsPeriod("stopreports_tabMonth","m", "Monthly");
	this.pYear = new StopReportsPeriod("stopreports_tabYear", "y", "Yearly");
	this.periods = [this.pDay, this.pWeek, this.pMonth, this.pYear];

	this.lastPeriod = null;

	this.getCurrentDayReport = function() {this.load(this.pDay);}

	this.getCurrentWeekReport = function() {this.load(this.pWeek);}

	this.getCurrentMonthReport = function() {this.load(this.pMonth);}

	this.getCurrentYearReport = function() {this.load(this.pYear);}

	this.refresh = function() {
		if (this.lastPeriod != null)
			this.load(this.lastPeriod);
	}

	this.load = function(period) {
		hideElement(E_RESULTSTIMEOUT);
		hideElement(E_RESULTSERROR);
		hideElement(E_RESULTSBLOCK);
		showElement(E_RESULTSLOADING);
		this.highlightPeriodTab(null);
		this.loadReport(period);
	}

	this.loadReport = function(period) {
		var request = ((typeof XMLHttpRequest != 'undefined') ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
		var url = requestUrl + "?action=report&period=" + period.getRequestName() + "&date=current&requestId=" + (new Date().getTime());
		request.open("GET", url, true);
		var thisClosure = this;
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200)
					thisClosure.processReportResponse(period, request.responseText);
			}
		}
		request.send(null);
		setTimeout((function() {thisClosure.processReportTimeout(period, request)}), requestTimeout);
	}

	this.processReportResponse = function(period, responseText) {
		hideElement(E_RESULTSLOADING);
		if (responseText == "null") {
			showElement(E_RESULTSERROR);
			return;
		}
		try {
			var response = eval("[" + responseText + "]")[0];
			var report = new Report(response);
			this.highlightPeriodTab(period);
			this.showResults(period, report);
		} catch(e) {
			showElement(E_RESULTSERROR);
		}
	}

	this.processReportTimeout = function(period, request) {
		if (request.readyState == 4 && request.status == 200)
			return;
		request.abort();
		this.lastPeriod = period;
		hideElement(E_RESULTSLOADING);
		showElement(E_RESULTSTIMEOUT);
	}

	this.highlightPeriodTab = function(period) {
		for (var i  in this.periods)
			setElementClassName(this.periods[i].getETab(), C_TAB);
		if (period != null)
			setElementClassName(period.getETab(), C_TABACTIVE);
	}

	this.showResults = function(period, report) {
		var glPercentClass = C_FIELD;
		if (report.getGlPercent() > 0)
			glPercentClass = C_FIELDGAIN;
		else if (report.getGlPercent() < 0)
			glPercentClass = C_FIELDLOSS;
		var glPercent1Class = C_FIELD;
		if (report.getGlPercent1() > 0)
			glPercent1Class = C_FIELDGAIN;
		else if (report.getGlPercent1() < 0)
			glPercent1Class = C_FIELDLOSS;
		setElementContent(E_RF_TITLE, period.getAdjective());
		setElementContent(E_RF_DATEVIEW, report.getDateView());
		setElementContent(E_RF_GLPERCENT, report.getGlPercent() + " %");
		setElementClassName(E_RF_GLPERCENT, glPercentClass);
		setElementContent(E_RF_GLPERCENT1, report.getGlPercent1() + " %");
		setElementClassName(E_RF_GLPERCENT1, glPercent1Class);
		setElementContent(E_RF_SIGNALSCOUNT, report.getSignalsCount());
		showElement(E_RESULTSBLOCK);
	}

	var getElement = function(id) {return document.getElementById(id);}
	var setElementDisplay = function(id, display) {getElement(id).style.display = display;}
	var showElement = function(id) {setElementDisplay(id, "block");}
	var hideElement = function(id) {setElementDisplay(id,"none");}
	var setElementClassName = function(id, className) {getElement(id).className = className;}
	var setElementContent = function(id, content) {getElement(id).innerHTML = content;}
}

var StopReportsPeriod = function(eTab, requestName, adjective) {

	this.getETab = function() {return eTab;}

	this.getRequestName = function() {return requestName;}

	this.getAdjective = function() {
		return adjective;
	}
}

var Report = function(hash) {

	this.getPeriod = function() {return hash["period"];}

	this.getStartDate = function() {return hash["startDate"];}

	this.getDateView = function() {return hash["dateView"];}

	this.getGlPercent = function() {return hash["glPercent"];}

	this.getGlPercent1 = function() {return hash["glPercent1"];}

	this.getSignalsCount = function() {return hash["signalsCount"];}

	this.isCurrent = function() {return hash["isCurrent"];}

	this.getAdjective = function() {
		var result = this.getPeriod();
		result = result.charAt(0).toUpperCase() + result.substring(1, result.length) + "ly";
		return result;
	}
}