/*$(document).ready(function() {
}*/

var BatteryLifePercent = 0;
var dwBattWeightPercentage = 1;
var dwKADC = 2;
var dwRARC = 3;
var BatteryVoltage = 4;
var dwBattResistor = 5;
var dwPd_M = 6;
var BatteryTemperature = 7;
var BatteryCurrent = 8;
var dwACR = 9;
var dwFL = 10;
var dwFstDischgPcent = 11;
var dwFstDischgACR = 12;
var dwCycle = 13;
var Status = 14;


/************************************************************************************************
 * debugMsg(message)																			*
 * 																								*
 * Appends a given string to div#Debug															*
 ************************************************************************************************/
function debugMsg(message)
{
	//document.getElementById("Debug").innerHTML += message + "<br />";
}


/************************************************************************************************
 * errMsg(message)																				*
 * 																								*
 * Appends a given string to div#Err															*
 ************************************************************************************************/
function errMsg(message)
{
	//document.getElementById("Err").innerHTML += message + "<br />";
}


/************************************************************************************************
 * textareaToArray(textarea)																	*
 * 																								*
 * Splits string into array after "\r\n" or "\n"												*
 ************************************************************************************************/
function textareaToArray(textarea){
	return textarea.value.split(/[\n\r]+/);
}


/************************************************************************************************
 * extractData(log)																				*
 * 																								*
 * One line of data looks like this:															*
 * 2010/03/15-23:22:31 52,52,506,519,3.821,20.2,0,34.5,14,6393,12296,498,6134,0,USB				*
 * The timestamp is seperated by a [space], the values by [,]									*
 ************************************************************************************************/
function extractData(raw) {		
	var splitted;		// Cache splitted raw data
	var timestamp;		// Cache extracted timestamp
	var valCount = 0;	// Count valid entries
	
	for(var i = 0; i < raw.length; i++) {
		splitted = raw[i].split(" ", 2);	// Split every row at the space that should divide date and values
		
		// If beginning of line resembles a valid timestamp, the timestamp and corresponding values will be saved (as Int)
		if((timestamp = getDateFromFormat(splitted[0], "yyyy/MM/dd-HH:mm:ss")) != 0) {
			this.date[valCount] = timestamp;	// Save timestamp (in ms!)
			this.values[valCount] = splitted[1].split(",");	// Split all values and save in array
			valCount++;
		} else {
			errMsg("No valid date: " + splitted[0]);
		}
	}
	debugMsg("Read values: " + valCount);
}


/************************************************************************************************
 * log-Object																					*
 ************************************************************************************************/
log = {
	date: [],
	values: [],
	max: [],
	extractData: extractData,
	getMax: function (val) {
		max = 0;
		debugMsg("Calculating Maximum for " + val);
		
		for(var i = 0; i < this.values.length; i++) {
			log.values[i][val] = parseFloat(log.values[i][val]);
			
			if(log.values[i][val] > max) {
				max = log.values[i][val];
			}
		}
		debugMsg("Done calculating maximum: " + max);
		
		return max;
	}
};

/************************************************************************************************
 * analyzetBattery()																			*
 * 																								*
 * 																								*
 * 																								*
 * 																								*
 ************************************************************************************************/
function analyzetBattery(logForm)
{	
	var i, x, y;
	var data = [];
	var toDisplay = logForm.toDisplay.value;
	
	var valueLabel = [
		"Battery Life Percentage",
		"dwBatt Weight Percentage",
		"dwKADC",
		"dwRARC",
		"Battery Voltage",
		"dwBattResistor in Ohm",
		"dwPd_M",
		"Battery Temperature",
		"Battery Current",
		"dwACR",
		"dwFL",
		"dwFstDischgPcent",
		"dwFstDischgACR",
		"dwCycle",
		"Status"
	];
	
	var valueColor = [
		"#0A0",
		"#0A0",
		"#0A0",
		"#0A0",
		"#00E",
		"#0A0",
		"#0A0",
		"#C00",
		"#CC0",
		"#0A0",
		"#0A0",
		"#0A0",
		"#0A0",
		"#0A0",
		"#0A0"
	];
	
	$('#chart').removeClass('hide');
	
	debugMsg("Start analyzing");
	debugMsg("Display: " + toDisplay);
	
	// --- Extract date and values -----------------------------------------------------------------
	log.extractData(textareaToArray(logForm.dataLog));
	
	// --- Calculate Maximum -----------------------------------------------------------------------
	log.max[toDisplay] = log.getMax(toDisplay);
		
	debugMsg("Done analyzing");	
	
	// --- Display the graph(s) ----------------------------------------

	for(var i = 0; i < log.date.length; i++) {
		x = log.date[i];
		y = log.values[i][toDisplay];	
		data[i] = [x, y];
	}
		/*
	var options =  {	
		legend: {
			show: true
		},
		seriesDefaults: {
			showMarker: false,
			trendline: { show: false },
			lineWidth: 2
		},
		axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}},
		series: [{
			label: 'Battery Life Percentage',
			color: "#0A0"
		}]
	};
	
	$('#input').fadeOut(300, function() {	
		$('#input').remove();		
		$('#output').removeClass("hide");
		$('#output').fadeIn(200, function() {
			// Darstellung
			//var plot1 = $.jqplot('chart', [data], options);
		});	
	});
	*/
	
	var options = {
		legend: {
			show: true
		},
		axes:{
			yaxis: {
				label: '%',
				autoscale: false, 
				max: 100,
				tickSize: 10,
				ticks: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
				tickDecimals: 2,
				min: 0
			},
			xaxis:{
				//renderer: $.jqplot.CategoryAxisRenderer
				renderer:$.jqplot.DateAxisRenderer,
				ticks: [],
				tickOptions: { 
					formatString:'%H:%M:%S <br /> %#d.%m.%Y'
				}
			}
		},
		seriesDefaults: {
			showMarker: false,
			trendline: { show: false },
			lineWidth: 2
		},
		series: [{
			label: valueLabel[toDisplay],
			color: valueColor[toDisplay]
		}],
        cursor:{
			show: true,
			showTooltip: true,
			followMouse: false,
			tooltipLocation: 'nw',
			tooltipOffset: 6, 
			showTooltipGridPosition: false,
			showTooltipUnitPosition: false,
			showTooltipDataPosition: true,
			tooltipFormatString: '%s: %s minutes at %s %',
			useAxesFormatters: false,
			//tooltipAxisGroups: [],
			zoom: false,
			clickReset: false,
			showVerticalLine: true,
			showHorizontalLine: true,
			constrainZoomTo: 'none',
			intersectionThreshold: 40,
			showCursorLegend: false
        }
	};
	
	var ticks = [
					'0',
					'2',
					'4',
					'6',
					'8',
					'10',
					'12',
					'14',
					'16',
					'18',
				];
	plot10 = $.jqplot('chart', [data], options);
}





