var ytVideoApp = {};
ytVideoApp.MAX_RESULTS_LIST = 10;
ytVideoApp.PREVIOUS_PAGE_BUTTON = 'previousPageButton';
ytVideoApp.NEXT_PAGE_BUTTON = 'nextPageButton';
ytVideoApp.NAVIGATION_DIV = 'navigationForm';
ytVideoApp.VIDEO_LIST_CONTAINER_DIV = 'searchResultsVideoList';
ytVideoApp.VIDEO_SEARCH_RESULTS_DIV = 'searchResultsVideoColumn';
ytVideoApp.VIDEO_PLAYER_DIV = 'videoPlayer';
ytVideoApp.TOP_SEARCH_CONTAINER_DIV = 'searchResultsVideoList';
ytVideoApp.MAIN_AUTH_CONTAINER_DIV = 'searchResultsVideoList';
ytVideoApp.VIDEO_UPLOAD_STATUS = 'searchResultsVideoList';
ytVideoApp.SYNDICATED_UPLOAD_DIV = 'searchResultsVideoList';
ytVideoApp.VIDEO_DATA_EDIT_DIV = 'searchResultsVideoList';
ytVideoApp.VIDEO_META_DATA_EDIT_DIV = 'searchResultsVideoList';
ytVideoApp.DEVELOPERKEY_FORM_DIV = 'searchResultsVideoList';
ytVideoApp.PLAYLIST_ADD_DIV = 'searchResultsVideoList';
ytVideoApp.nextPage = 2;
ytVideoApp.previousPage = 0;
ytVideoApp.previousSearchTerm = '';
ytVideoApp.previousSearchUser = '';
ytVideoApp.previousQueryType = 'all';

/**
 * Retrieves a list of videos matching the provided criteria.  The list of
 * videos can be restricted to a particular standard feed or search criteria.
 * @param {String} op The type of action to be done.
 *     for querying all videos, or the name of a standard feed.
 * @param {String} searchTerm The search term(s) to use for querying as the
 *     'vq' query parameter value
 * @param {Number} page The 1-based page of results to return.
 */
ytVideoApp.listVideos = function(op, searchTerm, page) {
  ytVideoApp.previousSearchTerm = searchTerm;
	if (op == 'search_username') {
	  ytVideoApp.previousSearchUser = searchTerm;
	}
  ytVideoApp.previousQueryType = op;
  var maxResults = ytVideoApp.MAX_RESULTS_LIST;
  var startIndex =  (((page - 1) * ytVideoApp.MAX_RESULTS_LIST) + 1);
  ytVideoApp.presentFeed(op, maxResults, startIndex, searchTerm);
  ytVideoApp.updateNavigation(page);
};

ytVideoApp.listUserVideosKey = function(user, searchTerm, page) {
  ytVideoApp.previousSearchTerm = searchTerm;
  ytVideoApp.previousSearchUser = user;
  ytVideoApp.previousQueryType = 'specific';
  var maxResults = ytVideoApp.MAX_RESULTS_LIST;
  var startIndex =  (((page - 1) * ytVideoApp.MAX_RESULTS_LIST) + 1);
  ytVideoApp.presentFeedSpec(maxResults, startIndex, searchTerm, user);
  ytVideoApp.updateNavigation(page);
};

/**
 * Sends an AJAX request to the server to retrieve a list of videos or
 * the video player/metadata.  Sends the request to the specified filePath
 * on the same host, passing the specified params, and filling the specified
 * resultDivName with the resutls upon success.
 * @param {String} filePath The path to which the request should be sent
 * @param {String} params The URL encoded POST params
 * @param {String} resultDivName The name of the DIV used to hold the results
 */
ytVideoApp.sendRequest = function(filePath, params, resultDivName) {
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }

  xmlhr.open('POST', filePath);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhr.onreadystatechange = function() {
    var resultDiv = $(resultDivName);
    if (xmlhr.readyState == 1) {
      //resultDiv.innerHTML = '<b>Loading...</b>';
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
       	if (resultDivName == ytVideoApp.VIDEO_PLAYER_DIV) {
			//tb_show(null, '#TB_inline?height=420&width=750&inlineId=videoPlayer', false);
			hs.htmlExpand($('videoPlayer'), { contentId: 'videoPlayer', height: 420, width: 750 } );
       	}
      }
    } else if (xmlhr.readyState == 4) {
      resultDiv.innerHTML = '<b>Invalid response received - Status: ' + xmlhr.status;
    }
  }
  xmlhr.send(params);
}

/**
 * Uses ytVideoApp.sendRequest to display a YT video player and metadata for the
 * specified video ID.
 * @param {String} videoId The ID of the YouTube video to show
 */
ytVideoApp.presentVideo = function(videoId, updateThumbnail) {
  var params = 'operation=show_video&videoId=' + videoId;
  var filePath = 'operations.php';
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_PLAYER_DIV);
}

ytVideoApp.setDeveloperKey = function(developerKey) {
  var filePath = 'operations.php';
  var params = 'operation=set_developer_key' +
               '&developerKey=' + developerKey;
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.DEVELOPERKEY_FORM_DIV);
}

/**
 * Uses ytVideoApp.sendRequest to create the authSub link.
 */
ytVideoApp.presentAuthLink = function() {
  var filePath = 'operations.php';
  var params = 'operation=auth_sub_request';
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.MAIN_AUTH_CONTAINER_DIV);
}

/**
 * Uses ytVideoApp.sendRequest to display a list of of YT videos.
 * @param {String} op  The operation to perform to retrieve a feed
 * @param {Number} maxResults The maximum number of videos to list
 * @param {Number} startIndex The first video to include in the list
 * @param {String} searchTerm The search terms to pass to the specified feed
 */
ytVideoApp.presentFeed = function(op, maxResults, startIndex, searchTerm){
  var params = 'operation=' + op +
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex +
               '&searchTerm=' + searchTerm;
  var filePath = 'operations.php';
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_LIST_CONTAINER_DIV);
};

ytVideoApp.presentFeedSpec = function(maxResults, startIndex, searchTerm, user){
  var params = 'operation=search_specific' +
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex +
               '&searchTerm=' + searchTerm +
               '&searchUser=' + user;
  var filePath = 'operations.php';
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_LIST_CONTAINER_DIV);
};

/**
 * Updates the variables used by the navigation buttons and the 'enabled'
 * status of the buttons based upon the current page number passed in.
 * @param {Number} page The current page number
 */
ytVideoApp.updateNavigation = function(page) {
/*  ytVideoApp.nextPage = page + 1;
  ytVideoApp.previousPage = page - 1;
  document.getElementById(ytVideoApp.NEXT_PAGE_BUTTON).style.display = 'inline';
  document.getElementById(ytVideoApp.PREVIOUS_PAGE_BUTTON).style.display = 'inline';
  if (ytVideoApp.previousPage < 1) {
    document.getElementById(ytVideoApp.PREVIOUS_PAGE_BUTTON).disabled = true;
  } else {
    document.getElementById(ytVideoApp.PREVIOUS_PAGE_BUTTON).disabled = false;
  }
  document.getElementById(ytVideoApp.NEXT_PAGE_BUTTON).disabled = false;*/
};

/**
 * Hides the navigation.
 */
ytVideoApp.hideNavigation = function() {
  //document.getElementById(ytVideoApp.NAVIGATION_DIV).style.display = 'none';
};

/**
 * Update video results div
 */
ytVideoApp.refreshSearchResults = function() {
  $(ytVideoApp.VIDEO_SEARCH_RESULTS_DIV).innerHTML = '';
}

/**
 * Method called when the query type has been changed.  Clears out the
 * value of the search term input box by default if one of the standard
 * feeds is selected.  This is to improve usability, as many of the standard
 * feeds may not include results for even fairly popular search terms.
 * @param {String} op The operation to perform.
 *     for querying all videos, or the name of one of the standard feeds.
 * @param {Node} searchTermInputElement The HTML input element for the input
 *     element.
 */
ytVideoApp.queryTypeChanged = function(op, searchTermInputElement) {
  if (op == 'search_username') {
    searchTermInputElement.value = '-- enter username --';
  } else if (op != 'search_all') {
    searchTermInputElement.value = '';
  }
};