/****************************************
 * Call Rebuild Location
 ****************************************
 * holt die M�glichen St�dte zur eingabe
 ****************************************/
function callRebuildLocationMiniSearch(location) {

	$('#location_list_mini').html('<div class="center_loader"><img src="typo3conf/ext/locator/pi2/res/ajax-loader.gif" border="0"></div>');
	$.ajax({
	    url: 'index.php?eID=searchData&action=location&location='+location,
	    type: 'post',
	    dataType: 'html',
	    error: function(){
	        alert('Error loading document');
	    },
	    success: function(html){
	    	if(html != null && html != 'null')
	    		parseJsonToLocationListMiniSearch(html)
	    	else
	    		$('#location_list_mini').hide();

	    }
	});
}
/****************************************
 * Call Rebuild Location DAA
 ****************************************
 * holt die M�glichen standorte zur eingabe
 ****************************************/
function callRebuildLocationMiniSearchDAA(location) {

	$('#location_list_mini2').html('<div class="center_loader"><img src="typo3conf/ext/locator/pi2/res/ajax-loader.gif" border="0"></div>');
	$.ajax({
	    url: 'index.php?eID=searchData&action=location_daa&location='+location,
	    type: 'post',
	    dataType: 'html',
	    error: function(){
	        alert('Error loading document');
	    },
	    success: function(html){
	    	if(html != null && html != 'null')
	    		parseJsonToLocationListMiniSearchDAA(html)
	    	else
	    		$('#location_list_mini2').hide();

	    }
	});
}

/****************************************
 * Parse Json To Loaction
 ****************************************
 * Baut die vorschlagfunktion f�r die St�dte auf
 ****************************************/
function parseJsonToLocationListMiniSearch(json)
{
	if(json)
	{
		
			var html = json;
			
			html += "<script type=\"text/javascript\">";
			html += "	/* <![CDATA[ */";
			html += "	$('#location_list_mini a').click(function(){";
			html += "					tmploc = this.rel.split('|');";
			html += "					tmploc2 = tmploc[0].split(',');";
			html += "					if($('#startZipcode')){";
			html += "						$('#startZipcode').val((tmploc2[0]));";
			html += "						$('#startCity').val((tmploc2[1]));";
			//html += "						$('#startLon').val((tmploc[1]));";
			//html += "						$('#startLat').val((tmploc[2]));";
			html += "					}";
			html += "					$('#locMiniSearchInput').val((tmploc2[0]+' '+tmploc2[1]));";
			html += "					$('#tx_locator_pi1_city').val((tmploc2[1]));";
			html += "					$('#tx_locator_pi1_zipcode').val((tmploc2[0]));";
			html += "					$('#location_list_mini').hide();";
			html += "					if($('#LocationSearchBoxForm'))";
			html += "						$('#LocationSearchBoxForm').submit();";
			html += "				});";
			html += "	/* ]]> */";
			html += "</script>";
			
			
			$('#location_list_mini').show();
			$('#location_list_mini').html(html);
	}
}

/****************************************
 * Parse Json To Loaction
 ****************************************
 * Baut die vorschlagfunktion f�r die Standorte auf
 ****************************************/
function parseJsonToLocationListMiniSearchDAA(json)
{
	var jsonarray = eval('('+json+')');
	if(jsonarray)
	{
		if (jsonarray.length >= 1)
		{
			var html = '<table>';
			for(var i=0; i < jsonarray.length; i++)
			{
				html += '<tr><td><a href="javascript:void(0);" rel="'+jsonarray[i]['plz']+','+jsonarray[i]['ort']+'|'+jsonarray[i]['lon']+'|'+jsonarray[i]['lat']+'|'+jsonarray[i]['strasse']+'|'+jsonarray[i]['name']+'" >'+jsonarray[i]['plz']+', '+jsonarray[i]['ort']+', '+jsonarray[i]['strasse']+', '+jsonarray[i]['name']+'<a></td></tr>';
			}
			html +='</table>';

			html += "<script type=\"text/javascript\">";
			html += "	/* <![CDATA[ */";
			html += "	$('#location_list_mini2 a').click(function(){";
			html += "					tmploc = this.rel.split('|');";
			html += "					tmploc2 = tmploc[0].split(',');";
			html += "					$('#locMiniSearchInput3').val((tmploc[0]+', '+tmploc[3]+', '+tmploc[4]));";
			html += "					$('#daaCity').val((tmploc2[1]));";
			html += "					$('#daaZipcode').val((tmploc2[0]));";
			html += "					$('#daaLat').val((tmploc[2]));";
			html += "					$('#daaLon').val((tmploc[1]));";
			html += "					$('#daaAddress').val((tmploc[3]));";
			html += "					$('#daaStorename').val((tmploc[4]));";
			html += "					$('#location_list_mini2').hide();";
			html += "				});";
			html += "	/* ]]> */";
			html += "</script>";
			
			$('#location_list_mini2').show();
			$('#location_list_mini2').html(html);
		}
	}
}

/****************************************
* Clickhandler
*
* Wird ausgefuehrt sobald ein Ort
* eingetragen wurd.
****************************************/
$(document).ready(function() {
	
	$("#locMiniSearchInput").live('keyup',function(){
		active_location = $("#locMiniSearchInput").val();
		if(active_location.length >= 3)
			callRebuildLocationMiniSearch(active_location);
		else
	    	$('#location_list_mini').hide();
	});
	$("#locMiniSearchInput3").keyup(function(){
		active_location_daa = $("#locMiniSearchInput3").val();
		if(active_location_daa.length >= 3)
			callRebuildLocationMiniSearchDAA(active_location_daa);
		else
	    	$('#location_list_mini2').hide();
	});
	
});
