// JavaScript Document

var finderForm;
var currentQuery;

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}






function initFinder($finderForm)
{
	finderForm = $finderForm;
	//$finderForm.addEventListener('onChange', onFinderTextChange, false);
}

function onFinderTextChange()
{
	submitToFinder(document.forms[0].finderInput.value)
}

function onInteractPause()
{
	
}

function submitToFinder(submission)
{
	 xmlhttp.open("POST", "http://randomhours.com/services/finder.php?type=JSON&fd="+submission,true);
 	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) {
   		onFinderResponse(xmlhttp.responseText)
		//onFinderResponse(newData);
 	 }
 	}
	xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')
	xmlhttp.send();
	//alert(submission + " changed");
}

function onFinderResponse(newData)
{
	
	var destination = document.getElementById('livedata');
	destination.innerHTML = "";
	var data = eval('('+newData+')');
	if(data.response.bands)
	{
		for (var i=0;i<data.response.bands.length;i++)
		{
			try
			{
				var x = document.createElement('div');
				x.className = 'responseItem';
				var y = document.createElement('h3');
				var z = document.createElement('a');
				z.href = "http://randomhours.com/"+data.response.bands[i].id+"/";
				z.appendChild(document.createTextNode(data.response.bands[i].name))
				y.appendChild(z);
				x.appendChild(y);
				destination.appendChild(x);
			}
			catch(exception)
			{
			}
		}
	}
	if(data.response.artists)
	{
		for (var i=0;i<data.response.artists.length;i++)
		{
			try
			{
				var x = document.createElement('div');
				x.className = 'responseItem';
				var y = document.createElement('h3');
				var z = document.createElement('a');
				z.href = "http://randomhours.com/"+data.response.artists[i].id+"/";
				z.appendChild(document.createTextNode(data.response.artists[i].name))
				y.appendChild(z);
				x.appendChild(y);
				destination.appendChild(x);
			}
			catch(exception)
			{
			}
		}
	}
	if(data.response.albums)
	{
		for (var i=0;i<data.response.albums.length;i++)
		{
			try
			{
				var x = document.createElement('div');
				x.className = 'responseItem';
				var y = document.createElement('h3');
				var z = document.createElement('a');
				z.href = "http://randomhours.com/"+data.response.albums[i].id+"/";
				z.appendChild(document.createTextNode(data.response.albums[i].name))
				y.appendChild(z);
				x.appendChild(y);
				destination.appendChild(x);
			}
			catch(exception)
			{
			}
		}
	}
	if(data.response.venues)
	{
	for (var i=0;i<data.response.venues.length;i++)
	{
		try
		{
		var x = document.createElement('div');
		x.className = 'responseItem';
		var y = document.createElement('h3');
		var z = document.createElement('a');
		z.href = "http://randomhours.com/"+data.response.venues[i].id+"/";
		z.appendChild(document.createTextNode(data.response.venues[i].name));
		y.appendChild(z);
		x.appendChild(y);
		destination.appendChild(x);
		}
		catch(exception)
		{
		}
	}
	}
	if(data.response.cities)
	{
	for (var i=0;i<data.response.cities.length;i++)
	{
		try
		{
		var x = document.createElement('div');
		x.className = 'responseItem';
		var y = document.createElement('h3');
		var z = document.createElement('a');
		z.href = "http://randomhours.com/"+data.response.cities[i].id+"/";
		z.appendChild(document.createTextNode(data.response.cities[i].name));
		y.appendChild(z);
		x.appendChild(y);
		destination.appendChild(x);
		}
		catch(exception)
		{
		}
	}
	}
	
	
	
	
	//destination.innerHTML = newData;
}

function writeToViewport(content)
{
}
