function initQuickFinder()
{
	$('#finderForm select.qf').change(getOptions);
	$('#finderForm select.qf').hide();
	$('#searchable-fields').hide();
	//
	var first=$('#finderForm select.qf:first')[0];
	$('#'+first.id+'-loader').show();
	$.getJSON(url_path+'/ajax/'+first.id,function (j) {
			var options = '<option value="">Select your '+first.id+'...</option>';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].value + '" url="'+j[i].url+'">' + j[i].name + '</option>';
			}
			$(first).html(options);
			$(first).find('option:first').attr('selected', 'selected');
			$('#'+first.id+'-loader').hide();
			$(first).show();
		});
	$('#quick-finder-btn').click(submitSearch);
}
function getOptions(e)
{
	$('#quick-finder-btn').hide();
	var obj=e.target;
	var url=new Array();
	var prev=$(obj).prevAll('select.qf').each(function(){
		url.push(this.id);
		url.push(this.value);
	}	
	);
	$(obj).nextAll('select.qf').empty().hide();
	var next=$(obj).nextAll('select.qf')[0];

	if(next)
	{
		$.getJSON(url_path+'/ajax/'+(url.length>0?url.join('/'):'')+'/'+obj.id+'/'+obj.value+'/'+next.id,function (j) {
				var options = '<option value="">Select your '+next.id+'...</option>';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].value + '" url="'+j[i].url+'">' + j[i].name + '</option>';
				}
				if (i == 0) {
					$('#'+next.id+'-loader').hide();
					$('#quick-finder-btn').show();
				}
				else {
					// original
					$(next).html(options);
					$(next).find('option:first').attr('selected', 'selected');
					$('#'+next.id+'-loader').hide();
					$(next).show();
					// original
				}
			});
	}
	else if($('#searchable-fields').length>0&&$('#searchable-fields')[0].style.display=='none')
	{
		$('#searchable-fields').show();
		$('#quick-finder-btn').show();
	}
	else
	{
		$('#quick-finder-btn').show();
	}
}
function submitSearch(e)
{
	e.preventDefault();
	var url=[];
	var error =[];
	$('#finderForm select.qf').each(function(){
		if(this.value=='' && this.style.display == 'none')
		{
		}
		else if(this.value=='')
		{
			error.push('You must select a '+this.id); 
		}
		else
		{
			url.push(this.options[this.selectedIndex].getAttribute('url'));
		}
	}	
	);
	if(error.length>0)
	{
		alert(error.join("\n"));
	}
	else
	{
		$('#finderForm')[0].action=url_path+'/'+url.join('/');
		$('#finderForm')[0].submit();
	}
	return false;
}
jQuery.fn.extend({
  emptyOptions: function() {
    this.each(function(i,obj)
	  {
		  for(var j=obj.options.length-1;j>0;j--)
		  {
			obj.options[j]=null;
		  }
	  });
  }
});
function getWebsite(e)
{
	e.preventDefault();
	var obj=e.target;
	$.get(url_path+'/ajax/log/website/'+$(obj).attr('lid'));
	document.location.href=$(obj).attr('href');	
}
function getPhone(e)
{
	e.preventDefault();
	var obj=e.target;
	$(obj).parent().load(url_path+'/ajax/log/phone/'+$(obj).attr('lid'));
}
