var configXML = "/var/www/includes/config/www_newfields_com.xml";
var criteria = new Array();
criteria['search'] = '';
criteria['locationid'] = '';
criteria['discipline'] = '';

// this extension allows us to select without case sensitivity.  I have no idea what m[3] means.
jQuery.extend(jQuery.expr[':'], {
		"contains-ic": "jQuery.fn.text.apply([a]).toLowerCase().indexOf(m[3].toLowerCase())>=0"
});

$(document).ready(function()
{
	CreateLocationMenu();
	CreateDisciplinesMenu();
	$("input#search").val('');

	$("input#search").keyup(function(){
		setTimeout(SearchTextChanged, 750);
	});

	$("select#searchdiscipline").change(function ()
	{
		DisciplineChanged($(this).val(), $(this).children("[@selected]").text());
	});

	LocationChanged();

});

function ClearCriteria()
{
	criteria['discipline'] = "";
	criteria['locationid'] = "";
	criteria['search'] = "";
}

function CreateDisciplinesMenu()
{
	//alert(sql);
	// JOIN newfieldsuser_discipline ON newfieldsuser_discipline.right_id = discipline.id',
	$.getJSON("/lib/Query.js.php",
	{
		XMLFile: configXML,
		QueryID: "DisciplinesMenu"
	},
	function(json){
		var sHtml = '';
		if(json)
		{
			//it's an array of records to loop through
			$(json).each(function(i)
			{
				sHtml += '<option value="' + json[i].id + '">' + json[i].name + '</option>';
			});
			 
			$("select#searchdiscipline").append(sHtml);
			$("select#searchdiscipline option")[0].selected = true;
		}
		else
		{
			alert('Error getting discipline category list');
		}
	});

};

function CreateLocationMenu()
{
	$.getJSON("/lib/Query.js.php",
	{
		XMLFile: configXML,
		QueryID: "LocationMenu"

	},
	function(json){
		var sHtml = '<div class="loc_person"><a href="javascript:LocationChanged()">VIEW ALL</a></div>';
		var sCountry = '';
		if(json)
		{
			//it's an array of records to loop through
			$(json).each(function(i)
			{
				if(sCountry != json[i].country)
				{
					sHtml += '<div class="loc_person_country">' + json[i].country + '</div>';
					sCountry = json[i].country;
				}
				var sTitle = json[i].state ? json[i].cs_display : json[i].country
				sHtml += '<div class="loc_person"><a href="javascript:LocationChanged(\'';
				sHtml += json[i].locationid + '\',\'' + sTitle + '\')">' + sTitle + '</a></div>';
			});
			 
			$("td#location_list").append(sHtml);
		}
		else
		{
			alert('Error getting names list');
		}
	});
}

function DisciplineChanged(disciplineID, disciplineName)
{
	ClearCriteria();
	$("input#search").val('');

	if (disciplineID == '0'){
		criteria['discipline'] = '';
		$("div#search_title").html('SHOWING ALL');
	}
	else
	{
		criteria['discipline'] = disciplineID;
		$("div#search_title").html('DISCIPLINE: ' + disciplineName);
	}
	GetEmployeeData();
}

function GetEmployeeData()
{
	var bNeedsAND = false;

	$.getJSON("/lib/SearchQuery.js.php",
	{
		XMLFile: configXML,
		QueryID: "Personnel",
		search: criteria['search'],
		locationid: criteria['locationid'],
		discipline: criteria['discipline']
	},
	function(json){
//		jsonEmployees = json;
		ShowSelection(json);
	});
};

function LocationChanged(locationid, locationName)
{
	ClearCriteria();
	$("select#searchdiscipline option")[0].selected = true;
	$("input#search").val('');

	if (locationid == null){
		criteria['locationid'] = "";
		$("div#search_title").html('SHOWING ALL');
	}
	else
	{
		criteria['locationid'] =  locationid;
		$("div#search_title").html(locationName);
	}

	GetEmployeeData();
}

function SearchTextChanged()
{
	ClearCriteria();
	$("select#searchdiscipline option")[0].selected = true;

	if ($("input#search").val().length == 0){
		criteria['search'] = '';
	}
	else
	{
		criteria['search'] = $("input#search").val();
		$("div#search_title").html('SEARCHING BY NAME');
	}
	GetEmployeeData();	
};

function ShowSelection(json)
{
	var sHtml='';
	var searchString = '';
	
	if(!json) 
		sHtml = "No results";
	else
	{
		$(json).each(function(i)
		{
			r = json[i]; 
			sHtml += '<tr><td class="cell_bot">';
			sHtml += '<table width="100%" border="0" cellspacing="0" cellpadding="8">';
			sHtml += '<tr><td width="1%" valign="top">';
			if(r.avatar_filename)
				sHtml += '<img src="http://corp.newfields.com/ElectronicDocuments/users/' + r.username + '/' + r.avatar_filename + '" width="82" height="101" alt="'+ r.fullname +'"/>';
			else
				sHtml += '<img src="/images/PhotoNotAvailable.jpg" width="82" height="101" alt="'+ r.fullname +'"/>';
	
			sHtml += '<div class="title_sm_bl">Office Location<br />'; 
			sHtml += r.city + (r.state ? ', ' + r.state : '') + '</div></td>';
			sHtml += '<td valign="top" class="expertise"><strong>';
			sHtml += r.fullname + ', ' + r.title + '</strong><br />';
			sHtml += r.phone + '&nbsp;';
			sHtml += '&nbsp;|&nbsp;&nbsp;<a style="display:inline" href="mailto:' + r.email + '" >Email</a>&nbsp;&nbsp;|&nbsp;&nbsp;';
			sHtml += '<a style="display:inline" href="lib/employee2vcard.php?id=' + r.id + '" >Import V-card</a><br /><br />';
			sHtml += r.bio ? r.bio + '<br /><br />': '';
			sHtml += r.disciplines ? r.disciplines + '<br />' : '';
			sHtml += '</td>';
			sHtml += '</tr></table></td></tr>';
		});
	}
	$("table#partner_list").html(sHtml);
}

