var search_experts;


var team = {
	search : {
		init : function() {
			jQuery('#search-experts').focus();
	
			jQuery('#search-experts').keydown(function (e) {
				if (e.keyCode == 13) {
					team.search.exec();
				}
			});
		},
		
		/**
		 * function execs async search and updates the "found-searches" container-div
		 * with the generated html.
		 */
		exec : function() {
			if (jQuery('#search-experts').val().length > 0) {
				// loading animation
				jQuery('#found-experts').html('<img src="' + doc_root + 'misc/icons/loader.gif" style="padding-top:10%;padding-left:48%;padding-right:48%;"/>' +
						'<div style="padding:37%;padding-top:0;" >' + gt.gettext("Suche nach Experten ...") + '</div>');
				var search_cloud = false;
				
				if (jQuery('#search-cloud:checked').val()) {
					search_cloud = true;
				}
				
				jQuery.post(doc_root + "includes/ajax/experts.search.exec.php", {
					searchvalue: jQuery('#search-experts').val(),
					search_cloud: search_cloud,
					project_id: jQuery('#saveProjectId').val()
				}, function(data, status) {
					if ('success' == status) {
						if (typeof data == 'object') {
							
							var html = '<ul id="found-users-tree">';
							var oCount = 0;
							for(strValue in data)	{
								if (typeof data[strValue].strName != 'undefined') {
									var tmpUsers = data[strValue]['temp_users'];
//									alert('strright: ' + data[strValue].strRight);
									html += '<li id="listelement_' + data[strValue].intUserId + '">';
									html += '<a href="javascript:;" id="' + data[strValue].intUserId + '" class="expert" ' +
											'onclick="team.show_user_details(' + data[strValue].intUserId + ');" ' +
											'alt="' + gt.gettext("Details anzeigen") + '" ' +
											'title="' + gt.gettext("Details anzeigen") + '">';
									html += '<img border="0" src="' + doc_root + 'misc/icons/' + core.arrConfig.user_rights[data[strValue].strRight].key + '.gif" />&nbsp;';
									html += data[strValue].strPrename + ' ' + data[strValue].strLastname;
									html += '</a>';
									
									// add to team link
									html += '&nbsp;<a href="javascript:;" id="' + data[strValue].intUserId + '" onclick="team.add_user(' + data[strValue].intUserId + ');" ' +
											'alt="' + gt.gettext("Experte hinzufügen") + '" ' +
													'title="' + gt.gettext("Experte hinzufügen") + '">' +
															'<img src="' + doc_root + 'misc/icons/add.png" border="0"/></a>';
									
									// set as projectleader link
									html += '&nbsp;<a class="tooltip" href="javascript:;" id="" onclick="projectroom.set_leader(' + data[strValue].intUserId + ', ' + projectroom_id + ');" ' +
											' alt="' + gt.gettext("Als Projektleiter auswählen") + '"' +
											' title="' + gt.gettext("Als Projektleiter auswählen") + '"><img src="' + doc_root + 'misc/icons/user_leader.png" border="0"/></a>';															
									
									if (core.isAdministrator(data[strValue].strRight) || core.isModerator(data[strValue].strRight)) {
										html += '&nbsp;<a class="tooltip" href="javascript:;" id="" onclick="projectroom.set_moderator(' + data[strValue].intUserId + ', ' + projectroom_id + ');" ' +
												' alt="' + gt.gettext("Als Moderator auswählen") + '"' +
												' title="' + gt.gettext("Als Moderator auswählen") + '"><img src="' + doc_root + 'misc/icons/user_moderator.png" border="0"/></a>';															
									}
											
									if (tmpUsers.length > 0) {
										html += '<ul>';
										for (strIndex in tmpUsers) {
											html += '<li id="listelement_' + tmpUsers[strIndex].intUserId + '">' +
													'<a href="javascript:;" id="' + tmpUsers[strIndex].intUserId + '" class="expert" onclick="team.show_user_details(' + tmpUsers[strIndex].intUserId + ');" ' +
															'alt="' + gt.gettext("Details anzeigen") + '" ' +
																	'title="' + gt.gettext("Details anzeigen") + '">' +
														'<img border="0" src="' + doc_root + 'misc/icons/' + core.arrConfig.user_rights[tmpUsers[strIndex].strRight].key + '.gif" />&nbsp;' +
														tmpUsers[strIndex].strPrename + ' ' + tmpUsers[strIndex].strLastname + 
													'</a>' +
													'&nbsp;<a href="javascript:;" id="' + tmpUsers[strIndex].intUserId + '" onclick="team.add_user(' + tmpUsers[strIndex].intUserId + ');" ' +
															'alt="' + gt.gettext("Temporären Benutzer hinzufügen") + '" ' +
															'title="' + gt.gettext("Temporären Benutzer hinzufügen") + '"> ' +
																	'<img src="' + doc_root + 'misc/icons/add.png" border="0"/></a>';
																	
											// set as projectleader link
											html += '&nbsp;<a class="tooltip" href="javascript:;" id="" onclick="projectroom.set_leader(' + tmpUsers[strIndex].intUserId + ', ' + projectroom_id + ');" ' +
															' alt="' + gt.gettext("Als Projektleiter auswählen") + '"' +
															' title="' + gt.gettext("Als Projektleiter auswählen") + '"><img src="' + doc_root + 'misc/icons/user_leader.png" border="0"/></a>' +																	
													'</li>';
										}
										html += '</ul>';
									}
									
									html += '</li>';
		 							
									oCount ++;
		 						}
							}
							html += '</ul>';
							
							
							if (oCount <= 0) {
								var html = gt.gettext("Die Suchanfrage lieferte keine Ergebnisse.");
							}
						}
						else {
							var html = gt.gettext("Die Suchanfrage lieferte keine Ergebnisse.");
						}
						// data contains the search result 
						// update DIV found-experts with the result
						jQuery('#found-experts').html(html);
						jQuery('#search-experts').val(''); // reset search field
						jQuery('#found-users-tree').treeview({persist: 'location', collapsed: false, unique: false});
					}
					else {
						var html = gt.gettext("Die Suchanfrage lieferte keine Ergebnisse.");
					}
				}, "json"); 
			}
			else {
				alert(gt.gettext("Bitte Suchbegriff eingeben"));
			}		
		}
	},
	
	/**
	 * adds a found expert from the "found-experts" div to the
	 * current project. Visible in the "added-experts" div
	 */
	add_user : function (intUserId) {
		// hide element to prevent doubleclicking
//		var clonedLi = jQuery("#listelement_" + intUserId).clone();
//		clonedLi.prependTo(jQuery("#listelement_" + intUserId));
//		clonedLi.animate({
//	        position: "relative",
//	        marginLeft: 200,
//	        fontSize: "62.5%",
//	        fontFamily: "Arial, sans-serif",
//	        color: "#666",
//	        opacity: 0.3 
// 		}, 300, "swing", function() {clonedLi.hide()});		

 		
		// call experts searc exec to add user to the project in a session
		jQuery.post(doc_root + "includes/ajax/experts.search.exec.php", {
			type: 'add_user',
			user_id: intUserId,
			project_id: jQuery('#intProjectroomId').val()
		}, function (data, status) { // async call returns the new content for added-experts
			team.render();
		}, "json");
	},
	
	/**
	 * deletes an user from a project
	 */
	remove_user : function (intUserId) {
		// hide element to prevent doubleclicking
//		var clonedLi = jQuery("#listelement_team_" + intUserId).clone();
//		clonedLi.prependTo(jQuery("#listelement_team_" + intUserId));
//		clonedLi.animate({		
//	        position: "relative",
//	        marginLeft: -300,
//	        fontSize: "62.5%",
//	        fontFamily: "Arial, sans-serif",
//	        color: "#666",
//	        opacity: 0.3 
// 		}, 300, "swing");		
 		
		jQuery.post(doc_root + "includes/ajax/experts.search.exec.php", {
			type: 'remove_expert',
			user_id: intUserId,
			project_id: projectroom_id
			
		}, function (data, status) { // async call returns the new content for added-experts
			team.render();
			
		}, "json");		
	},
	
	/**
	 * Shows details of the selected user in a hover-div
	 */
	show_user_details : function (intUserId) {
		util.hide_workingspace();
				
		jQuery('#expert-details').css({
			'position' : 'absolute',
			'z-index' : '99999'
		});
		
//		jQuery('#mainNavWrapper').css('z-index', -1);
		
		jQuery('#expert-details').appendTo('body');
		
		jQuery('#expert-details-content').html('<img src="' + doc_root + 'misc/icons/loader.gif" style="padding-top:10%;padding-left:47%;padding-right:47%;"/>' +
				'<div style="padding:42%;padding-top:0;" >' + gt.gettext("Lade Details...") + '</div>');
				
			jQuery('#expert-details').css({
				'position' : 'absolute',
				'width' : '600px',
				'height' : '400px',
//				'overflow' : 'auto',
				'z-index' : '99999'
  			});
			jQuery('#expert-details div.boxContent').css({
//					'position' : 'absolute',
				'height' : '360px',
				'overflow' : 'auto'					
//					'z-index' : '99999'					
			});  
		
		$('#expert-details').fadeIn('normal');
		
		$.post(doc_root + "includes/ajax/expert.get.php", {
			user_id: intUserId		
		}, function (data, status) { // async call returns the new content for added-experts
			html = data;
			$('#expert-details-content').html(html);
			//user.log('Show_Userprofile', 'intUserId: ' + intUserId + ', remote: includes/ajax/expert.get.php');
		}, "json");		

		jQuery('#expert-details').center();
		center_div = 'expert-details'; // defined global
		jQuery(window).bind('scroll', util.position);
		util.position();		
		
	},
	
	/**
	 * updates different divs in search
	 */
	render : function() {
		// update added-experts div
		projectroom.box.get('added-experts', gt.gettext("Lade Team"), projectroom_id);		
	}
};



var experts_admin = {
	
	/**
	 * Show members of the project-starter first.
	 * 
	 * Then the pool defined by the TMG-Administrator
	 * @param {Integer} projectroom_id - Id of the projectroom
	 */
	load_user_pool : function(projectroom_id) {
		
		jQuery.post(doc_root + "includes/ajax/experts.project.team.php", {
			content_type: 'user_pool',
			projectroom_id: projectroom_id
		}, function (data, status) { // async call returns html content for user-pool
			jQuery('#user-pool').html(data);
			jQuery('#user_pool').treeview({persist: 'location', collapsed: false, unique: false});
			
		}, 'json');			
	},
	
	
	/**
	 * Team constelation as definded by the project-starter.
	 * 
	 * @param {Integer} projectroom_id - Id of the projectroom
	 */
	load_team_members : function(projectroom_id) {
		
		jQuery.post(doc_root + "includes/ajax/experts.project.team.php", {
			content_type: 'team_members',
			projectroom_id: projectroom_id
		}, function (data, status) { // async call returns html content for user-pool
			jQuery('#project-team').html(data);			
			jQuery('#team_members').treeview({persist: 'location', collapsed: false, unique: false});
		}, 'json');			
	},
	
	/**
	 * Adds a member to team of the project-starter.
	 * 
	 * @param {Integer} projectroom_id - Id of the projectroom
	 * @param {Integer} user_id id of the user who will be added to the team
	 * @param {String} from - Marks the session-index from which the user will retrieved
	 */
	add_member_to_team : function(projectroom_id, user_id, from) {
		// hide element to prevent doubleclicking
		jQuery("#user" + user_id).animate({
	        position: "relative",
	        marginLeft: 200,
	        fontSize: "62.5%",
	        fontFamily: "Arial, sans-serif",
	        color: "#666",
	        opacity: 0.3 
 		}, 1000, "swing");		
		
		jQuery.post(doc_root + "includes/ajax/experts.project.team.php", {
			content_type: 'add_member_to_team',
			projectroom_id: projectroom_id,
			user_id: user_id,
			from_index: from
		}, function (data, status) { 
			experts_admin.load_user_pool(projectroom_id);
			experts_admin.load_team_members(projectroom_id);
		});			
	},
	
	/**
	 * Removes a member from a team of the project-starter.
	 * @param {Integer} projectroom_id - Id of the projectroom
	 * @param {Integer} user_id - Id of the user who will be removed from the team
	 */
	remove_team_member : function(projectroom_id, user_id, from) {
		// hide element to prevent doubleclicking
		jQuery("#member" + user_id).animate({
	        position: "relative",
	        marginLeft: -300,
	        fontSize: "62.5%",
	        fontFamily: "Arial, sans-serif",
	        color: "#666",
	        opacity: 0.3 
 		}, 1000, "swing");
		
		jQuery.post(doc_root + "includes/ajax/experts.project.team.php", {
			content_type: 'remove_team_member',
			projectroom_id: projectroom_id,
			user_id: user_id,
			from_index: from
		}, function (data, status) { 
			experts_admin.load_user_pool(projectroom_id);
			experts_admin.load_team_members(projectroom_id);
		});			
		
	},
	
	/**
	 * 
	 * @param {String} element_id
	 */
	toggle_user_info : function(element_id, user_id) {
		
		// get positions of the mouse over element
		var pos = jQuery('#'+element_id).offset();
		
//		if (jQuery('#'+element_id).css('display') == 'none') {
			
			jQuery('#user-info').html('<img src="' + doc_root + 'misc/icons/loader.gif" style="padding-top:10%;padding-left:47%;padding-right:47%;"/>' +
					'<div style="padding:42%;padding-top:0;" >' + gt.gettext("Lade Details...") + '</div>');
					
			jQuery('#user-info').css({'top': pos.top-210, 'left': pos.left-jQuery('#'+element_id).width()-400});
			jQuery.post(doc_root + 'includes/ajax/experts.project.team.php', {
				content_type: 'user_info',
				user_id: user_id
			}, function(data, status) {
				jQuery('#user-info').html(data);
			});
//		}
		
		jQuery('#user-info').toggle();
	}
	
};
