var ytvbp = {};

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

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

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

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

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

/**
 * container div id used to hold the search box which displays when the page
 * first loads
 * @type String
 */
ytvbp.MAIN_SEARCH_CONTAINER_DIV = 'mainSearchBox';

/** 
 * 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
 */
ytvbp.TOP_SEARCH_CONTAINER_DIV = 'searchBox';

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

/**
 * the page number to use for the previous page navigation button
 * @type Number
 */
ytvbp.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
 */
ytvbp.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
 */
ytvbp.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} queryType The type of query to be done - either 'all'
 *     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.
 */
ytvbp.listVideos = function(queryType, searchTerm, page) {
	//alert("ytvbp.listVideos");
  ytvbp.previousSearchTerm = searchTerm; 
  ytvbp.previousQueryType = queryType; 
  var maxResults = ytvbp.MAX_RESULTS_LIST;
  var startIndex =  (((page - 1) * ytvbp.MAX_RESULTS_LIST) + 1);
  ytvbp.presentFeed(queryType, maxResults, startIndex, searchTerm);
  ytvbp.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
 */
ytvbp.sendRequest = function(filePath, params, resultDivName) {
	var debug=0;
	
	if(debug){
		document.getElementById('debug_layer').style.visibility="visible";
		var debug_content=document.getElementById('debug_content');
	}
	
	if(debug){
		debug_content.innerHTML+="params: "+params+"<br>";
	}

	if (window.XMLHttpRequest)
	{
		if(debug){ debug_content.innerHTML+="using XMLHttpRequest<br>"; }
		var xmlhr = new XMLHttpRequest();
		
		// SET TIMEOUT (START)
		var timeoutId = window.setTimeout( function() {
			// NOTE: if readyState is not "4" you can't get a status code yet
			if(debug){
				debug_content.innerHTML +='Time!, readyState: '+xmlhr.readyState;
				debug_content.innerHTML += 'clearing timeout<br>'; 
			}
			window.clearTimeout(timeoutId);	// this is so you can re-use the request object.
	
			if ( xmlhr.readyState != 4 || xmlhr.status != 200 ) {
				if(debug){
					debug_content.innerHTML += ' Abort!<br>'; 
					debug_content.innerHTML += 'Server Timeout...too many users.<br />';	// friendly response to the user
				}

				xmlhr.abort();
				return false;
			} else {
			if(debug){	debug_content.innerHTML += ' loaded :)<br>'; }
			}
		}, 15000
		);
		// SET TIMEOUT (END)
		
	}
	else
	{
		if(debug){ document.getElementById('debug_layer').innerHTML+="using ActiveObject<br>"; }
		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 = '<b>Loading...</b>'; 
		}
		else if(xmlhr.readyState == 4 && xmlhr.status == 200)
		{
			if(debug){
				debug_content.innerHTML += 'readyState and Status Code ok.<br>'; 
				debug_content.innerHTML += 'clearing timeout<br>'; 
			}
			
			window.clearTimeout(timeoutId);

			if(xmlhr.responseText)
			{
				resultDiv.innerHTML = xmlhr.responseText;
				if(document.getElementById('tv_video_list_n_navigation'))
				{
					document.getElementById('tv_video_list_n_navigation').style.display="block";
				}

				if(		!params.match("queryType=show_video")  &&	params.match("queryType=")	&&	!resultDiv.innerHTML.match("ytvbp.presentVideo")	)
				{
					resultDiv.innerHTML += "No more results.";
					document.getElementById(ytvbp.NEXT_PAGE_BUTTON).disabled = true;
				}
			}
		}
		else if(xmlhr.readyState == 4)
		{
			// got full reponse but invalid status code...
			if(debug){ debug_content.innerHTML += 'clearing timeout<br>'; }
			window.clearTimeout(timeoutId);

			resultDiv.innerHTML = "Server Timeout...too many users.<br />Please click 'Next' or 'Back' again.  ";	// friendly response to the user
			//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 YT video to show
 */
ytvbp.presentVideo = function(videoId) {
	//alert("ytvbp.presentVideo");
  var params = 'queryType=show_video&videoId=' + videoId;
  var filePath = 'video.php';
  ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_PLAYER_DIV);

	setTimeout("kisooy()", 1000);
}


function kisooy(){
	////alert(kisooy);

	if(	document.getElementById('kisooy')	){
		document.getElementById('kisooy').style.display="block";
	}	
	
	/*
	if(	document.getElementById('kisooy')	&& document.getElementById('bazz')	){
		////alert(document.getElementById('videoPlayer').offsetTop);	

		document.getElementById('kisooy').style.display="block";
		document.getElementById('kisooy').style.left=document.getElementById('bazz').offsetLeft;
		document.getElementById('kisooy').style.top=document.getElementById('bazz').offsetTop;
	}
	*/
}



/**
 * Uses ytvbp.sendRequest to display a list of of YT videos.
 * @param {String} queryType The name of a standard video feed or 'all'
 * @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
 */
ytvbp.presentFeed = function(queryType, maxResults, startIndex, searchTerm){
	//alert("ytvbp.presentFeed");
  var params = 'queryType=' + queryType + 
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex + 
               '&searchTerm=' + searchTerm;
  var filePath = 'video.php';
  ytvbp.sendRequest(filePath, params, ytvbp.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
 */
ytvbp.updateNavigation = function(page) {
  ytvbp.nextPage = page + 1;
  ytvbp.previousPage = page - 1;
  document.getElementById(ytvbp.NEXT_PAGE_BUTTON).style.display = 'inline';
  document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).style.display = 'inline';
  if (ytvbp.previousPage < 1) {
    document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = true;
  } else {
    document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = false;
  }
  document.getElementById(ytvbp.NEXT_PAGE_BUTTON).disabled = false;
};

