
var texttube = {};

texttube.MAX_RESULTS_LIST = 10;

texttube.PREVIOUS_PAGE_BUTTON = 'previousPageButton';

texttube.NEXT_PAGE_BUTTON = 'nextPageButton';

texttube.VIDEO_LIST_CONTAINER_DIV = 'searchResultsVideoList';

texttube.nextPage = 2;

texttube.previousPage = 0;

texttube.previousSearchTerm = '';

texttube.previousQueryType = 'all';

texttube.listVideos = function(queryType, searchTerm, page) {
  texttube.previousSearchTerm = searchTerm; 
  texttube.previousQueryType = queryType; 
  var maxResults = texttube.MAX_RESULTS_LIST;
  var startIndex =  (((page - 1) * texttube.MAX_RESULTS_LIST) + 1);
  texttube.presentFeed(queryType, maxResults, startIndex, searchTerm);
  texttube.updateNavigation(page);
};
texttube.presentFeed = function(queryType, maxResults, startIndex, searchTerm){
  var params = 'queryType=' + queryType + 
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex + 
               '&searchTerm=' + searchTerm;
  var filePath = 'index.php';
  texttube.sendRequest(filePath, params, texttube.VIDEO_LIST_CONTAINER_DIV);
}
texttube.sendRequest = function(filePath, params, resultDivName) {
    var resultDiv = document.getElementById(resultDivName);
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
  xmlhr.open('POST', filePath, true);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
  xmlhr.onreadystatechange = function() {
    
    if (xmlhr.readyState == 1) {
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
      }
    } else if (xmlhr.readyState == 4) {
      alert('Invalid response received - Status: ' + xmlhr.status);
    }
  }
  xmlhr.send(params);
}
texttube.updateNavigation = function(page) {
  texttube.nextPage = page + 1;
  texttube.previousPage = page - 1;
  document.getElementById(texttube.NEXT_PAGE_BUTTON).style.display = 'inline';
  document.getElementById(texttube.PREVIOUS_PAGE_BUTTON).style.display = 'inline';
  if (texttube.previousPage < 1) {
    document.getElementById(texttube.PREVIOUS_PAGE_BUTTON).disabled = true;
  } else {
    document.getElementById(texttube.PREVIOUS_PAGE_BUTTON).disabled = false;
  }
  document.getElementById(texttube.NEXT_PAGE_BUTTON).disabled = false;
};
