var timeplot;

var linecolors = ['#ff0000','#0000ff','#00FF00','#00CCCC','#FF9900'];
var fillcolors = ['#cc9999','#9999cc','#99cc99','#99cccc','#cc9966'];

/**
 * <p>create the timeplot</p>
 * 
 * @param string word
 * @param boolean fuzzy
 */
function onLoad(words,fuzzy) {
  var fuzzyText = "";
  if(document.getElementById('fuzzy').checked) {
  	var fuzzyText = "(fuzzy search on - including anything that starts with this word)";
  }
//  document.getElementById('word').value = "";
//  document.getElementById('fuzzy').checked = false;
  if(!words) {
  	words = new Array();
  	words[0] = 'obama';
  	words[1] = 'mccain';	
  	document.forms[0].word.value = words.join(" ");
  }	
  
  var resultline = document.getElementById('resultline');
  if(words.length == 1) {
  	resultline.innerHTML = 'Results for the word &quot;<span style="color:' + linecolors[0] + '">' + words[0] +'</span>&quot; ' + fuzzyText;	
  }
  else {
  	var resultlineHTML = 'Results for: &quot;<span style="color:' + linecolors[0] + '">' + words[0] +'</span>&quot;';
  	for(i=1; i<words.length; i++) {
  		resultlineHTML += ', &quot;<span style="color:' + linecolors[i] + '">' + words[i] + '</span>&quot;';
  	}
  	resultline.innerHTML = resultlineHTML;
  }
  
  var timeGeometry = new Timeplot.DefaultTimeGeometry({
    gridColor: new Timeplot.Color("#000000"),
    axisLabelsPlacement: "top"
  });

  var valueGeometry = new Timeplot.DefaultValueGeometry({
      	gridColor: "#000000",
        axisLabelsPlacement: "left"
  });
  
  var black = new Timeplot.Color('#000000');
  
  var plotInfo = Array();
  
  //Add the fillcolor if we're in Safari
  var appVersion = navigator.appVersion.toString();
  if(appVersion.search('Safari') != -1) {
  	for(i=0; i<words.length; i++) {
	  	eval('var eventSource' + i + ' = new Timeplot.DefaultEventSource()');
	  	
	  	plotInfo.push(
	  	  Timeplot.createPlotInfo({
	      id: "plot" + i,
	      dataSource: new Timeplot.ColumnSource(eval('eventSource' + i),1),
		  valueGeometry: valueGeometry,
	      timeGeometry: timeGeometry,
	      lineColor: new Timeplot.Color(linecolors[i]),
	      fillColor: new Timeplot.Color(fillcolors[i]),
	      dotColor: black, 
	      showValues: true
	    })
	    );
  	}   
  }
  else {
  	for(i=0; i<words.length; i++) {
	  	eval('var eventSource' + i + ' = new Timeplot.DefaultEventSource()');
	  	plotInfo.push(
	  	  Timeplot.createPlotInfo({
	      id: "plot" + i,
	      dataSource: new Timeplot.ColumnSource(eval('eventSource' + i),1),
		  valueGeometry: valueGeometry,
	      timeGeometry: timeGeometry,
	      lineColor: new Timeplot.Color(linecolors[i]),
	      //fillColor: new Timeplot.Color(fillcolors[i]),
	      dotColor: black, 
	      showValues: true
	    })
	    );
	}	
  }
  
  

            
  timeplot = Timeplot.create(document.getElementById("my-timeplot"), plotInfo);
  for(i=0; i<words.length; i++) {
  	//timeplot.loadText("test.txt", "|",eval('eventSource'+i));
  	timeplot.loadText("getword.php?word=" + words[i] + "&fuzzy=" + fuzzy, "|", eval('eventSource'+i));
  }
  //Part of this needs to be adding the word to the form as a hidden element or something
  //so that we can keep the old word and the new word at the same time.
 
}

var resizeTimerID = null;
function onResize() {
    if (resizeTimerID == null) {
        resizeTimerID = window.setTimeout(function() {
            resizeTimerID = null;
            timeplot.repaint();
        }, 100);
    }
}

function doSearch() {
	var word = document.getElementById('word').value;
	var fuzzy = document.getElementById('fuzzy').checked;
	var words = word.split(" "); 

	onLoad(words,fuzzy);
}


