// this is the base class for all custom javascript




// detect keystroke activity and send user input to fuzzy search engine
// to do: apply a switch method to all this. but leave it spread out for now as it evolves and develops. requirements are constantly changing.

var formObj = null;
var formObjTyp = "";
var request=null;

// handle input field events
window.onload=function()
{
	// this is the input handler for reactor ask
	var input_event = document.getElementById("reactor_ask");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_solutions_for(this);
    		}
    	}
    }

	// this is the input handler for citizen search: members
	// it is based on reactor_ask
	// search_people = reactor_ask
	// find_people_like = find_solutions_for
	
	var input_event = document.getElementById("search_people");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_people_like(this);
    		}
    	}
    }

	// this is the input handler for citizen search: sponsors
	// it is based on reactor_ask
	// search_sponsors = reactor_ask
	// find_sponsors_like = find_solutions_for
	
	var input_event = document.getElementById("search_sponsors");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_sponsors_like(this);
    		}
    	}
    }

    
    // this is the input handler for reactor ask
	var input_event = document.getElementById("reactor_offer");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_problems_for(this);
    		}
    	}
    }
    
    // this is the input handler engine search
	var input_event = document.getElementById("engine_search_projects_static");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_projects_static(this);
    		}
    	}
    }
    
    // this is the input handler engine search
	var input_event = document.getElementById("engine_search_projects_inmotion");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_projects_inmotion(this);
    		}
    	}
    }
    // this is the input handler engine search
	var input_event = document.getElementById("engine_search_resources_static");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_resources_static(this);
    		}
    	}
    }
    // this is the input handler engine search
	var input_event = document.getElementById("engine_search_resources_inmotion");
    if(input_event != null)
    {
    	
		input_event.onkeyup=function()
		{
    		if (this.value)
    		{
    			find_resources_inmotion(this);
    		}
    	}
    }
    
}
			
// get the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest(); //Not IE
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	}
	else
	{
		alert("Your browser doesn't support our fancy search method. Please upgrade your browser to Firefox or Safari, or use our classic search page.");
	}
}			
			
// get our browser-specific XmlHttpRequest object.
var receiveSolutions = getXmlHttpRequestObject();	
var receiveProblems = getXmlHttpRequestObject();	
var receiveProjectsS = getXmlHttpRequestObject();	
var receiveProjectsIM = getXmlHttpRequestObject();	
var receiveResourcesS = getXmlHttpRequestObject();		
var receiveResourcesIM = getXmlHttpRequestObject();		
var receivePeople = getXmlHttpRequestObject();
var receiveSponsors = getXmlHttpRequestObject();		
		
// initiate the asyncronous request.
function find_solutions_for(this_thing)
{
	if (receiveSolutions.readyState == 4 || receiveSolutions.readyState == 0)
	{
		//receiveSolutions.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=reactorsearch&fuzzball=resource&var='+encodeURIComponent(this_thing.value), true);
		receiveSolutions.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=reactorsearch&fuzzball=Resource&var='+encodeURIComponent(this_thing.value), true);
		receiveSolutions.onreadystatechange = handleSolutions; 
		receiveSolutions.send(null);
	}			
}

// initiate the asyncronous request.
function find_people_like(this_thing)
{
	if (receivePeople.readyState == 4 || receivePeople.readyState == 0)
	{
		//receivePeople.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=citizensearch&fuzzball=citizeb&var='+encodeURIComponent(this_thing.value), true);
		receivePeople.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=citizensearch&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receivePeople.onreadystatechange = handlePeople; 
		receivePeople.send(null);
	}			
}

// initiate the asyncronous request.
function find_sponsors_like(this_thing)
{
	if (receiveSponsors.readyState == 4 || receiveSponsors.readyState == 0)
	{
		//receiveSponsors.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=citizensearch&fuzzball=sponsors&var='+encodeURIComponent(this_thing.value), true);
		receiveSponsors.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=citizensearch&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receiveSponsors.onreadystatechange = handleSponsors; 
		receiveSponsors.send(null);
	}			
}


// initiate the asyncronous request.
function find_problems_for(this_thing)
{
	if (receiveProblems.readyState == 4 || receiveProblems.readyState == 0)
	{
		//receiveProblems.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=reactorsearch&fuzzball=project&var='+encodeURIComponent(this_thing.value), true);
		receiveProblems.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=reactorsearch&fuzzball=Project&var='+encodeURIComponent(this_thing.value), true);
		receiveProblems.onreadystatechange = handleProblems; 
		receiveProblems.send(null);
	}			
}


// initiate the asyncronous request.
function find_projects_static(this_thing)
{
	if (receiveProjectsS.readyState == 4 || receiveProjectsS.readyState == 0)
	{
		//receiveProjectsS.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=project&set_type=static&fuzzball=problem&var='+encodeURIComponent(this_thing.value), true);
		receiveProjectsS.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=project&set_type=static&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receiveProjectsS.onreadystatechange = handleProjectsStatic; 
		receiveProjectsS.send(null);
	}			
}

// initiate the asyncronous request.
function find_projects_inmotion(this_thing)
{
	if (receiveProjectsIM.readyState == 4 || receiveProjectsIM.readyState == 0)
	{
		//receiveProjectsIM.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=project&set_type=inmotion&fuzzball=problem&var='+encodeURIComponent(this_thing.value), true);
		receiveProjectsIM.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=project&set_type=inmotion&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receiveProjectsIM.onreadystatechange = handleProjectsInMotion; 
		receiveProjectsIM.send(null);
	}			
}


// initiate the asyncronous request.
function find_resources_static(this_thing)
{
	if (receiveResourcesS.readyState == 4 || receiveResourcesS.readyState == 0)
	{
		//receiveResourcesS.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=resource&set_type=static&fuzzball=solution&var='+encodeURIComponent(this_thing.value), true);
		receiveResourcesS.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=resource&set_type=static&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receiveResourcesS.onreadystatechange = handleResourcesStatic; 
		receiveResourcesS.send(null);
	}			
}

// initiate the asyncronous request.
function find_resources_inmotion(this_thing)
{
	if (receiveResourcesIM.readyState == 4 || receiveResourcesIM.readyState == 0)
	{
		//receiveResourcesIM.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=resource&set_type=inmotion&fuzzball=solution&var='+encodeURIComponent(this_thing.value), true);
		receiveResourcesIM.open("GET", 'meta/wigglefish/fuzzysearch.php?fuzztype=enginesearch&set=resource&set_type=inmotion&fuzzball=activity&var='+encodeURIComponent(this_thing.value), true);
		receiveResourcesIM.onreadystatechange = handleResourcesInMotion; 
		receiveResourcesIM.send(null);
	}			
}


// call each time the XmlHttpRequest object state changes.
function handleSolutions()
{
	if (receiveSolutions.readyState == 4)
	{
		new Effect.DropOut('reactor_offer_area');
		document.getElementById('reactor_solutions').innerHTML = receiveSolutions.responseText;
		new Effect.Appear('reactor_solutions', {duration: 5, scaleX: true, scaleContent: true});
		new Effect.Appear('post_problem', {duration: 10, scaleX: true, scaleContent: true});
		new Effect.Appear('search_solutions_oldschool', {duration: 20, scaleX: true, scaleContent: true});

	}
}

function handlePeople()
{
	if (receivePeople.readyState == 4)
	{
		new Effect.DropOut('featured_people');
		document.getElementById('search_people_results').innerHTML = receivePeople.responseText;
		new Effect.Appear('search_people_results', {duration: 5, scaleX: true, scaleContent: true});
	}
}

function handleSponsors()
{
	if (receiveSponsors.readyState == 4)
	{
		new Effect.DropOut('featured_sponsors');
		document.getElementById('search_sponsor_results').innerHTML = receiveSponsors.responseText;
		new Effect.Appear('search_sponsor_results', {duration: 5, scaleX: true, scaleContent: true});
	}
}


function handleProblems()
{
	if (receiveProblems.readyState == 4)
	{
		new Effect.DropOut('reactor_ask_area');
		document.getElementById('reactor_problems').innerHTML = receiveProblems.responseText;
		new Effect.Appear('reactor_problems', {duration: 5, scaleX: true, scaleContent: true});
		new Effect.Appear('post_solution', {duration: 10, scaleX: true, scaleContent: true});
		new Effect.Appear('search_problems_oldschool', {duration: 20, scaleX: true, scaleContent: true});
	}
}

function handleProjectsStatic()
{
	if (receiveProjectsS.readyState == 4)
	{
		document.getElementById('engine_results_projects_static').innerHTML = receiveProjectsS.responseText;
		new Effect.Appear('engine_results_projects_static', {duration: 5, scaleX: true, scaleContent: true});
	}
}

function handleProjectsInMotion()
{
	if (receiveProjectsIM.readyState == 4)
	{
		document.getElementById('engine_results_projects_inmotion').innerHTML = receiveProjectsIM.responseText;
		new Effect.Appear('engine_results_projects_inmotion', {duration: 5, scaleX: true, scaleContent: true});
	}
}


function handleResourcesStatic()
{
	if (receiveResourcesS.readyState == 4)
	{
		document.getElementById('engine_results_resources_static').innerHTML = receiveResourcesS.responseText;
		new Effect.Appear('engine_results_resources_static', {duration: 5, scaleX: true, scaleContent: true});
	}
}

function handleResourcesInMotion()
{
	if (receiveResourcesIM.readyState == 4)
	{
		document.getElementById('engine_results_resources_inmotion').innerHTML = receiveResourcesIM.responseText;
		new Effect.Appear('engine_results_resources_inmotion', {duration: 5, scaleX: true, scaleContent: true});
	}
}
