$(document).ready(function(){
	
	$('.directory-nav li a').click(function(e){
		e.preventDefault();
		
		var parent = $(this).parent();
		
		swapDirectory( parent );
		
		sortDirectoryHeight();
		
	});
	

	// add the alphabet event listener for the directory results
	$('#directory .alphabet a').click(function(e)
	{
		e.preventDefault();
		var url = this.href;
		var object = $(this);
			
		$.get(url, function(response)
		{
			$('#directory-results').html(response);
			var nav_item = $('.directory-nav li.directory');
			swapDirectory( nav_item );
			addPaginationListener();
			addTooltips();
			
			$('#directory .alphabet ol li.active').removeClass('active');
			object.parent().addClass('active');
		});
	});
	
	$('#profile-details ul li a').click(function(e)
	{
		e.preventDefault();
		var url = this.href;
		
		$.get(url, function(response)
		{
			$('#directory-results').html(response);	
			var nav_item = $('.directory-nav li.directory');
			swapDirectory( nav_item );
			addPaginationListener();
			addTooltips();
		});
	});
	
	$('a.add-to-contacts').click(function(e)
	{
		e.preventDefault();
		var url = this.href;
		var clickedItem = this;
		
		$.get(url  + '&ajax=true', function(response)
		{	
			updateMyContacts();
			var profile_id = url.split('profile_id=');
			profile_id = profile_id[1];
			if( response == 'Added' )
			{
				$(clickedItem).attr({href: includes_path + '/view/removeContact.inc.php?profile_id=' + profile_id});	
				$(clickedItem).html('Remove from My Contacts');
			}
			else if( response == 'Removed' )
			{
				$(clickedItem).attr({href: includes_path + '/view/addContact.inc.php?profile_id=' + profile_id});	
				$(clickedItem).html('Add to My Contacts');
			}
		});
	});
	
	// clicking on the university name on the homepage searches for more people from the same place
	$('#search-university').click(function(e){
		e.preventDefault();
		var url = this.href;
		
		$.get(url, function(response)
		{
			$('#directory-results').html(response);	
			var nav_item = $('.directory-nav li.directory');
			swapDirectory( nav_item );
			addPaginationListener();
		});
	});
	
	// clicking on the "[x] people bookmarked bit on the events calendar
	// just needs to show your contacts on the my nido tab
	$('#search-bookmarks').click(function(e){
		e.preventDefault();
		var url = this.href;
		
		$.get(url, function(response)
		{
			$('#directory-results').html(response);	
			var nav_item = $('.directory-nav li.directory');
			swapDirectory( nav_item );
			addPaginationListener();
			addTooltips();
		});
	});

	//set up initial listener
	addPaginationListener();
	addMyContactsPaginationListener();
	removeFriendListener();
	addTooltips();
	directoryFormListener();
	directorySearchListener();
	
	// add the listener for the directory results pagination, which then 
	// calls itself on complete to re-add the listener to the new elements
	function addPaginationListener()
	{
		$('#directory-results .pagination a').click(function(e)
		{
			e.preventDefault();
			var url = this.href;
			
			$.get(url, function(response)
			{
				$('#directory-results').html(response);	
				addPaginationListener();
				addTooltips();
				sortDirectoryHeight();
			});
		});	
	}
	
	function directorySearchListener()
	{
		$('#directory form').submit(function(e)
		{								 
			e.preventDefault();
			
			var url = includes_path+'/view/directory-search.inc.php';
			var data = $('#directory form').serialize();
			var type = 'html';
			
			$.post(url, data, function(response)
			{
				$('#directory-results').html(response);
				var nav_item = $('.directory-nav li.directory');
				swapDirectory( nav_item );
				addPaginationListener();
				addTooltips();
			}, type);
			
			$('#directory .alphabet li.active').removeClass('active');
			
		 });	
	}
	
	function directoryFormListener()
	{
		$('#directory form').change(function()
		{
			var url = includes_path+'/view/directorySearchForm.inc.php'; 
			var data = $('#directory form').serialize();
			var type = 'html';
			
			$.post(url, data, function(response)
			{
				$('#directory-search').html(response);
				directoryFormListener();
				directorySearchListener();
				addTooltips();
			}, type);
		});	
	}
	
	//the tooltips for the directory results
	function addTooltips()
	{
		if( $('#directory-results a.profile-link') ) {
			$('#directory-results a.profile-link').hover (
				function() {
					$(this).parent().css( "z-index", "9999" )
				},
				function() {
					$(this).parent().css( "z-index", "1" )
				}
			);
		}
		
		$('#directory-results a.profile-link').each(function()
		{
			var url = this.href;
			url = url.split('profile_id=');
			var profile_id = url[1];
			var text = this.innerHTML;
			
			$(this).simpletip({
				
				fixed: true,
				position: [5,18],
		   		content: '<img style="border:3px solid #ffffff;" src="'+ includes_path +'/profile-image.php?profile_id='+ profile_id +'&thumb&nocache" alt="'+ text +'" />'
		   });
		});
		
		if( $('#my-contacts-results a.profile-link') ) {
			$('#my-contacts-results a.profile-link').hover (
				function() {
					$(this).parent().css( "z-index", "9999" )
				},
				function() {
					$(this).parent().css( "z-index", "1" )
				}
			);
		}
		
		$('#my-contacts-results a.profile-link').each(function()
		{
			var url = this.href;
			url = url.split('profile_id=');
			var profile_id = url[1];
			var text = this.innerHTML;
			
			$(this).simpletip({
				
				fixed: true,
				position: [5,18],
		   		content: '<img style="border:3px solid #ffffff;" src="'+ includes_path +'/profile-image.php?profile_id='+ profile_id +'&thumb&nocache" alt="'+ text +'" />'
		   });
		});
	}
	
	// the event listener for the remove friend functionality in the LHS directory
	function removeFriendListener()
	{
		$('a.remove-friend').click(function(e)
		{
			e.preventDefault();
			var findUrl = this.href;
			var url = this.href;
			var profile_id = url.split('profile_id=');
			profile_id = profile_id[1];
			var url = url + '&ajax=true';
			
			$.get(url, function(response)
			{	
				updateMyContacts();
			});
			
			$(".profile-summary-content a[href='"+findUrl+"']").text('Add to My Contacts');
			$(".profile-summary-content a[href='"+findUrl+"']").attr({href: includes_path + '/view/addContact.inc.php?profile_id='+profile_id});
		});
	}
	
	function updateMyContacts()
	{
		var url = includes_path + '/view/myContacts.inc.php';
		$.get(url, function(response)
		{
			$('#my-contacts-results').html(response);
			addMyContactsPaginationListener();
			removeFriendListener();
			sortDirectoryHeight();
			addTooltips();
		});
	}
	
	function addMyContactsPaginationListener()
	{
		$('#my-contacts-results .pagination a').click(function(e)
		{
			e.preventDefault();
			var url = this.href;
			
			$.get(url, function(response)
			{
				$('#my-contacts-results').html(response);	
				addMyContactsPaginationListener();
				sortDirectoryHeight();
				addTooltips();
				removeFriendListener();
			});
		});	
	}
	
	//object is the a-tag in the directory-nav that you want active
	function swapDirectory( object )
	{
		if(object.hasClass('directory'))
		{
			$('.directory-nav li').removeClass('active');
			object.addClass('active');
			$('.directory-nav li.search').addClass('bg-right');
			$('#directory-directory').removeClass('hide');
			$('#my-nido').addClass('hide');
			$('#directory-search').addClass('hide');
		}
		if(object.hasClass('search'))
		{
			$('.directory-nav li').removeClass('active');
			object.addClass('active');
			$('#directory-directory').addClass('hide');
			$('#my-nido').addClass('hide');
			$('#directory-search').removeClass('hide');
		}	
		if(object.hasClass('my-nido'))
		{
			$('.directory-nav li').removeClass('active');
			object.addClass('active');
			$('.directory-nav li.search').removeClass('bg-right');
			$('#directory-directory').addClass('hide');
			$('#my-nido').removeClass('hide');
			$('#directory-search').addClass('hide');
		}
		
		sortDirectoryHeight();
	}
	
	function sortDirectoryHeight()
	{
		var directory_height = $('#directory-container').height();
		$('#directory-bottom').css('top', directory_height );	
	}
	
});