var master_list;

$(function(){
	init();
});

function init(){
	$.getScript('/ctsc/about_us/staff_list_json.js');
}

function callBack(data){
	inject_sortable_list(data);
}

function inject_sortable_list(sortable_lists){	
	
	master_list = sortable_lists;

	for (var i in master_list.sortable_lists) {

		var sortable_list = master_list.sortable_lists[i];
		
		if(sortable_list.sort_by == 'last_name')
			sortable_list.list.sort(sort_by_last_name);
		else
			sortable_list.list.sort(sort_by_name);
			
		// Create Staff List Container
		
		var containerDiv = '<div class="directory_list" id="directory'+i+'">\
								<div class="directory_header">\
									<h2>'+ sortable_list.list_title +'</h2>\
								</div>\
								<div class="cell_holder">\
								</div>\
							</div>';
							
		$('div#sortable_list').append(containerDiv);	
	}
	
	
	
	for (var j in master_list.sortable_lists){
		display_list(master_list.sortable_lists[j].list, j);
	}
	
	$('input#sortable_list_filter').keyup(function(){
		filter_list($(this).val());
	});	
	
}

function filter_list(string){
	
	for( var i in master_list.sortable_lists){

		var temp_list = [];
	
		var list = master_list.sortable_lists[i].list;
		for (var j in list){
			if(check_attribute(list[j], 'first_name', string) ||
				check_attribute(list[j], 'last_name', string) ||
				check_attribute(list[j], 'name', string))
					temp_list.push(list[j]);
		}		
		display_list(temp_list, i);
	}	
}

function check_attribute(cell, attribute, string){
	if(cell[attribute])
		if(cell[attribute].toLowerCase().indexOf(string.toLowerCase())!=-1)
			return true
		else
			return false
	else
		return false
}

function display_list(sortable_list, index){	
		var div = 'div#directory'+index+' div.cell_holder';
		$(div).html("");
		
		if(sortable_list.length){
		
			for (var j in sortable_list){
				
				var list = sortable_list[j];
			
				var cell = '<div class="directory_cell">';
				
				if(j==0)
					cell = '<div class="directory_cell first">';
					
				if(j%2==1)
					cell = '<div class="directory_cell odd">';
				// Create individual staff listing
				if(list.name){
					//	Department List
					cell+= span_wrap('col pc100 name', list.name);
					cell+= span_wrap('col px250', (span_wrap('title', 'Room: ') + list.room));
					cell+= span_wrap('col', (span_wrap('title', 'Phone: ') + list.phone));
					cell+= span_wrap('col', (span_wrap('title', 'Fax: ') + list.fax));
				}
				else{
					//	Staff List
					cell+= span_wrap('col name pc100', (list.last_name+', '+list.first_name));
					cell+= span_wrap('col pc100', list.position);
					cell+= span_wrap('col', (span_wrap('title', 'Room: ')+ list.room));
					cell+= span_wrap('col', (span_wrap('title', 'Phone: ')+ list.phone));
					cell+= span_wrap('col', (span_wrap('title', 'Fax: ')+ list.fax));
					cell+= span_wrap('col px250', (span_wrap('title', 'Email: '))+ ('<a href="mailTo:'+list.email+'">'+list.email+'</a>'));

				}
				cell+='</div>';
				$(div).append(cell);
			}
		}
		else
			$(div).html('<div class="directory_cell first"><span class="col pc100 title no_matches">No matches found.</span></div>');
}

function span_wrap(span_class, string){
	if(!string)
		string = 'n/a';
	return('<span class="'+span_class+'">'+string+'</span>');
}

function sort_by_last_name(a,b){
	if(a.last_name == b.last_name){
		if(a.first_name == b.first_name){
			return 0;
		}
		return (a.first_name < b.first_name) ? -1 : 1;
	}
	return (a.last_name < b.last_name) ? -1 : 1;
}

function sort_by_name(a,b){
	if(a.name == b.name){
		if(a.name == b.name){
			return 0;
		}
		return (a.name < b.name) ? -1 : 1;
	}
	return (a.name < b.name) ? -1 : 1;
}

