/**
 * An ajax update viewer.
 * @class
 * @scope public
 */
 
 /**
  *	@brief Constructor
  *
  *	@param in_view_type is the view we want (there are 3 views, by letter, regular search and no view)
  *	@param in_search_input is the search input the user entered
  * @param in_div the div where the ajax content will go
  *	@param in_name The name of the object
  */
function UpdateViewer(in_view_type, in_search_input, in_div, in_name, in_nats) 
{
    /**
     * The currently selected suggestions.
     * @scope private
     */   
    this.view_type = in_view_type;
	this.search_input = in_search_input;
	this.div = in_div;
	this.name = in_name;
	this.nats = in_nats || "";
	this.request = null;
	this.mutex = 0;		//semaphore
	
	//make our request object
    if (typeof XMLHttpRequest != "undefined") {
        this.request = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this.request = new ActiveXObject("MSXML2.XmlHttp");
    } else {
        alert("No XMLHttpRequest object available. This functionality will not work.");
    }
	
	if (this.view_type == "search")
		this.getPage(1);
	/*
	else if (this.view_type == "letter")
		this.getLetter(this.search_input);
	else
		this.div.innerHTML = "<h1>Click on the first letter of your favorite pornstar!</h1>";
	*/
}

 /**
  *	@brief show the requested page
  *
  *	@param in_search_input the search the user did
  * @param new_page_num the new page we want
  */
UpdateViewer.prototype.getPage = function(in_page_num, in_search_input) 
{
	this.search_input = in_search_input || this.search_input;
	
	if (in_search_input == "")
		return;
	
	var me = this;	//for some reason I can't use this on the 'onreadystatechange' function, it has to be a local variable
	
	if (me.mutex == 0)
	{
		me.mutex = 1;	//lock the control
		
		me.showAnimation();
		
		var prefix = "";
		if (document.getElementById("prefix")) {
			prefix = "prefix=1&";	
		}
		
		var sURL = "/t1/pps=bbonet/ajax/search_ajax.htm?" + prefix + "t=3&q=" + encodeURIComponent(me.search_input) + 
				   "&page=" + in_page_num; // + "&dummy=" + new Date().getTime();

		//open connection to the gallery getter
		me.request.open("get", sURL , true);

		me.request.onreadystatechange = function () {
			if (me.request.readyState == 4) 
			{
				if (me.request.status == 200)
				{
					me.div.innerHTML = me.request.responseText;
				}
				me.mutex = 0;
			}   
		};
		
		me.request.send(null);
	}
}

UpdateViewer.prototype.showAnimation = function()
{
	// document.getElementById("search_form").style.display = "none";
	this.div.innerHTML = "<div style=\"padding: 100px; text-align: center;\"><img src=\"http://images2.bangbros.com/bangbrosnetwork/t1/loading_animation.gif\" alt=\"loading\" /><h1>Searching...</h1></div>";
}