/**
 * 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
 * @subpackage Demos
 * @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 using a PHP backend powered by the Zend_Gdata component
 * of the Zend Framework.
 */

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

ytvbp.PATH= 'apps/vodcast/loader.php';

ytvbp.MAX_RESULTS_LIST = 5;

ytvbp.PREVIOUS= 'vodcast_previous_span';

ytvbp.NEXT= 'vodcast_next';

ytvbp.VIDEO_LIST_CONTAINER_DIV = 'vodcast_list';

ytvbp.VIDEO_PLAYER_DIV = 'vodcast_player';

/*Initialize*/
ytvbp.next_page = 2;
ytvbp.previous_page = 0;
ytvbp.previousSearchTerm = '';
ytvbp.previousQueryType = 'all';


/*FUNCTIONS*/
ytvbp.list = function(page)
{
	var start_index =  (((page - 1) * ytvbp.MAX_RESULTS_LIST) + 1);
	var params = 'query_type=list' + 
               '&max_results=' + ytvbp.MAX_RESULTS_LIST +
               '&start_index=' + start_index;
	
	ytvbp.sendRequest(ytvbp.PATH, params, ytvbp.VIDEO_LIST_CONTAINER_DIV);
	ytvbp.update_navigation(page) 
	
	return false;
};

/**
 * 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
 */
ytvbp.update_navigation = function(page)
{
  ytvbp.next_page = page + 1;
  ytvbp.previous_page = page - 1;

  if (ytvbp.previous_page < 1) {
	hide_div(ytvbp.PREVIOUS);
  } else {
	show_div(ytvbp.PREVIOUS);
  }
};




/**
 * 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
 */
ytvbp.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, true);
  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="images/core/ajax-loader.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 ytvbp.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
 */
ytvbp.show_video = function(vodcast_id) {
  var params = 'query_type=show_video&vodcast_id=' + vodcast_id;
  ytvbp.sendRequest(ytvbp.PATH, params, ytvbp.VIDEO_PLAYER_DIV);
  setTimeout('sIFR_reload();',500);
}

ytvbp.autoload_newest = function(query) {
  var params = 'query_type=' + query;
	
  ytvbp.sendRequest(ytvbp.PATH, params, ytvbp.VIDEO_PLAYER_DIV);
}






