

function printContact(row){

	if(!row) return "";

	var retStr = "<div class='contact'><img class='contact_portrait' src='"
			+ row.img
			+ "' /><div class='contact_details'><div class='name'>"
			+ feedbackURL(row.name,row.email,"gx")
			+ "</div><div class='title'>"
			+ row.title
			+ "</div><div class='phone'>"
			+ row.tel
			+ "</div></div></div>";
	
	return retStr;
}


function printContacts(contacts){
	for(var i = 0; i< contacts.length; i++){
		if(people[contacts[i]])
		 	$("#people_search_results").append(printContact(people[contacts[i]]));
	}
}

function merge(a1, a2){

	var match = false;

	for(var i= 0; i<a2.length; i++){
		
		match = false;

		for(var j = 0; j < a1.length; j++){
			if(a1[j] == a2[i]){
				match = true;
				break;
			}
		}
		
		if(!match) 
			a1.push(a2[i]);
	}


	return a1;

}


function findContacts(params){
	var c = params.country || "all",
	    s = params.specialization || "all",
	    temp = [],
	    temp2 = [],
	    contacts = [];

	if (s != "all") { //if a specialization is specified, get those first
		
		if(!practiceArea.hasOwnProperty(s)){
			alert("Incorrect code: "+s+"\n No such practice area");
			return [];
		}

		temp = otherSpecialists[s] || [];
		temp2 = practiceArea[s].speacialist || [];
		temp = merge(temp, temp2);
		if (c != "all") {
			for(var i = 0; i< temp.length; i++){
				if (people[temp[i]] && people[temp[i]].territory_code && people[temp[i]].territory_code == c) 
					contacts.push(temp[i]);
			}
		}else {
			contacts = temp;
		}

	} else {  // no specialization, just get all the contacts from the country
		for(var i in people){
			if (people[i].territory_code == c) 
					contacts.push(i);
		}
	}
	// contacts.unshift(257);  // Add the global legal leader to the front of every result set.
	return contacts;

}



$(function(){
	
	// build territory list
	for(var x in legalData){
		if(legalData[x].countryName!='Hungary')
			$("#territory_search_list").append("<option value='"+x+"'>"+legalData[x].countryName+"</option>");
	}

	// build specialty list
	$("#specialty_search_list option:not(.service_area)").prepend("&nbsp;&nbsp;&nbsp;&nbsp;");
	

	// make them functional
	$("#territory_search_list, #specialty_search_list").change(function(){
		
		$("#people_search_results").fadeOut(300,function(){
			$(this).html("");
			var p = {
				"country" : $("#territory_search_list").val(),
				"specialization" : $("#specialty_search_list").val()
			};
			
			printContacts(findContacts(p));

			$("#people_search_results").fadeIn(300);				
		});	



	});


	var q = location.search;
	
	if(q != ""){
		if(q.charAt(1) == "t"){ // territory request
			q = q.slice(3);
			printContacts(findContacts({"country":q}));
			$("#territory_search_list").val(q);

		} else if(q.charAt(1) =="s") { // service area or practice area request
			q = q.slice(3);
			printContacts(findContacts({"specialization":q}));
			$("#specialty_search_list").val(q);
		}

	}



});
