/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * @fileoverview Provides functions for browsing and searching YouTube 
 * data API feeds, as well as performing authentication, syndicated uploads
 * and playlist management using a PHP backend powered by the Zend_Gdata component
 * of Zend Framework.
 */

/**
 * provides namespacing for the YouTube Video Application PHP version (ytVideoApp)
 */
var ytVideoApp = {};

/**
 * maximum number of results to return for list of videos
 * @type Number
 */
ytVideoApp.MAX_RESULTS_LIST = 5;

/**
 * navigation button id used to page to the previous page of
 * results in the list of videos
 * @type String
 */
ytVideoApp.PREVIOUS_PAGE_BUTTON = 'previousPageButton';

/**
 * navigation button id used to page to the next page of
 * results in the list of videos
 * @type String
 */
ytVideoApp.NEXT_PAGE_BUTTON = 'nextPageButton';

/**
 * container div for navigation elements
 * @type String
 */
ytVideoApp.NAVIGATION_DIV = 'navigationForm';

/**
 * container div id used to hold list of videos
 * @type String
 */
ytVideoApp.VIDEO_LIST_CONTAINER_DIV = 'searchResultsVideoList';

/**
 * container div id used to hold video search results
 * @type String
 */
ytVideoApp.VIDEO_SEARCH_RESULTS_DIV = 'searchResultsVideoColumn';

/**
 * container div id used to hold the video player
 * @type String
 */
ytVideoApp.VIDEO_PLAYER_DIV = 'videoPlayer';

/** 
 * container div id used to hold the search box displayed at the top of
 * the browser after one search has already been performed
 * @type String
 */
ytVideoApp.TOP_SEARCH_CONTAINER_DIV = 'searchBox';

/** container div to show detailed upload status
 * @type String
 */
ytVideoApp.VIDEO_UPLOAD_STATUS = 'detailedUploadStatus';

/** 
 * container div to hold the form for syndicated upload
 * @type String
 */
ytVideoApp.SYNDICATED_UPLOAD_DIV = 'syndicatedUploadDiv';

/**
 * the page number to use for the next page navigation button
 * @type Number
 */
ytVideoApp.nextPage = 2;

/**
 * the page number to use for the previous page navigation button
 * @type Number
 */
ytVideoApp.previousPage = 0;

/** 
 * the last search term used to query - allows for the navigation
 * buttons to know what string query to perform when clicked
 * @type String
 */
ytVideoApp.previousSearchTerm = '';

/**
 * the last query type used for querying - allows for the navigation
 * buttons to know what type of query to perform when clicked
 * @type String
 */
ytVideoApp.previousQueryType = 'all';


ytVideoApp.operationfilepath = 'functions/youtube/operations.php';
/**
 * 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; 
  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);
};

/**
 * 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 = document.getElementById(resultDivName);
    if (xmlhr.readyState == 1) {
      resultDiv.innerHTML = '<img src="functions/lightbox/images/loading.gif">'; 
    } 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);
}

/**
 * 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;
  ytVideoApp.sendRequest(ytVideoApp.operationfilepath, params, ytVideoApp.VIDEO_PLAYER_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;
  ytVideoApp.sendRequest(ytVideoApp.operationfilepath, 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() {
  document.getElementById(ytVideoApp.VIDEO_SEARCH_RESULTS_DIV).innerHTML = '';
}

