﻿//Javascript code for Water Conflict Chronology Timeline - Table Only
//created by Matthew Heberger, August 2009


function loadNewData(){
  var params = {}
  params.region=document.getElementById('region').value;
  params.conftype = document.getElementById('conftype').value;
  params.epoch = document.getElementById('epoch').value;
  params.search = document.getElementById('search').value;
  
  var jstr = JSON.stringify(params);
  var myURL = '../../php/table.php'
  var myRequest = new ajaxObject(myURL, processData);

  myRequest.update('jstr=' + jstr,'GET');
  showImage(true);
}

function processData(responseText, responseStatus) {
  showImage(false);
  if (responseStatus == 200) {
    var dict = JSON.parse(responseText);
    print(dict.txt);
    fillInfo(dict);
    highlighttablerows();
    highlightSearchText();
    }
  else {
    alert(responseStatus + ' -- Error Processing Request');
    }
}

function print(str){
  document.getElementById("txt").innerHTML = str;
}

function ListProperties(){
  o = eventSource;
  for(att in o){
    print(o[att]);
  }
}


function fillInfo(dict){
  var num =dict.num;
  var begin =dict.begin;
  var end = dict.end;
  
  document.getElementById("num").innerHTML = num;
  if (num>1){
    var a = " entries from " + formatBC(begin);  
    document.getElementById("begin").innerHTML = a;
    
    var a = " to " + formatBC(end);
    document.getElementById("end").innerHTML = a;
    
  }else if (num==1){
    var a = " entry in " + formatBC(begin);  
    document.getElementById("begin").innerHTML = a;
    document.getElementById("end").innerHTML = "";

  }else {
    document.getElementById("begin").innerHTML = "";
    document.getElementById("end").innerHTML = "entries";
  }
}

function formatBC(yr){
  if (yr>=0){
    return yr;
  }else{
    return -yr + " BC";
  }
}

//This function shows or hides the AJAX load image
function showImage(bShow){
  
  if (bShow){
    document.getElementById("ajaximg").style.display = 'block';}
  else{
    document.getElementById("ajaximg").style.display = 'none';}
}

function highlighttablerows(){

  $("tr:even").addClass("striped");
  $("td").css("border", "none");
    
  $("td").hover(function() {
            $(this).parents('tr').addClass('highlight');
          }, function() {
            $(this).parents('tr').removeClass('highlight');
      });
}

function highlightSearchText(){
  //highlights the search term
	$term = $("#search").val();

	if ($term.length > 0){
	  $("#txt").highlight($term);
	} else {
	  $('#highlight-plugin').removeHighlight();
	}
}


$(document).ready(function(){
  
	//IE does not respect the "selected" attribute of select form objects, so reset them to their defaults.
	$("#epoch").val("%");
	$("#region").val("%");
	$("#conftype").val("%");
	$("#search").val("");
	
	//the little x to celar the search box.
	$("#clearer").click(function(){
	  var curval = $(this).prev("input").val(); 
    $(this).prev("input").val("");
		if (curval != ""){
		  loadNewData();
		}
  });
	
	//capture the default behavior of the Enter key on form fields
	$("input").keypress(function (event){ 
	  if (event.keyCode == 13){
		  //need to make the Autocomplete list go away
			$("#search").autocomplete("close");
		  loadNewData();
			return false;
		}
	}
	);
	
	//Set up the autocomplete function
	$("#search").autocomplete({
		source: "../../php/autocomplete.php",
		select: function( event, ui ) {
			$("#search").val(ui.item.value);
			loadNewData();
  	}
	}
	);
	
	//Finally, do an initial data load!
	loadNewData();
	
});
