
var _search = {

	get_result: function()	{
		var form_values = jQuery('#userSearchForm').serializeArray();
		
//		var form_json = jQuery.toJSON(form_values);
		// json form data
		var formdata_json = '{';
		jQuery.each(form_values, function(i, field){
			formdata_json += '"' + field.name + '": "' + field.value + '"';
			if (i < form_values.length - 1)  {
				formdata_json += ', ';
			}
		});			
		formdata_json += '}';
		
		jQuery.post(doc_root + 'includes/ajax/search.get.php', {
			strJson : formdata_json
		}, function(data, status) {
			var strOutput = '';
			var intCount = 0;
			var intUserCount = 0;
			var intCompanyCount = 0;

			objResponse = data;
			strOutput += '<table>';
			
			strOutput += '<tr bgcolor="#dddddd">';
				strOutput += '<td width="25">&nbsp;</td>';
				strOutput += '<td width="25"><b>%</b></td>';
				strOutput += '<td width="200"><b>' + gt.gettext("Name") + '</b></td>';
				strOutput += '<td width="200"><b>' + gt.gettext("Firma") + '</b></td>';
				strOutput += '<td width="50"><b>' + gt.gettext("A-Baum") + '</b></td>';
				strOutput += '<td width="50"><b>' + gt.gettext("T-Baum") + '</b></td>';
				strOutput += '<td width="100"><b>' + gt.gettext("Sprachen") + '</b></td>';
				strOutput += '<td width="100"><b>' + gt.gettext("Position") + '</b></td>';
			strOutput += '</tr>';
				
			for(strValue in objResponse)	{
				if(objResponse[strValue]['intUserId'])	{
					strOutput += ( intCount%2 == 0 ) ? '<tr bgcolor="#eeeeee">' : '<tr bgcolor="#dddddd">';
					strOutput += '<td><img border="0" src="' + core.config('root') + 'misc/icons/' + core.arrConfig.user_rights[objResponse[strValue].strRight].key + '.gif" ' +
							'title="' + gt.gettext(core.arrConfig.user_rights[objResponse[strValue].strRight].name) + '" ' +
							'alt="' + gt.gettext(core.arrConfig.user_rights[objResponse[strValue].strRight].name) + '" /></td>';	
					strOutput += '<td><b>0</b></td>';
					strOutput += '<td><a href="javascript:;" onclick="user.get(' + objResponse[strValue]['intUserId'] + ')">' + objResponse[strValue]['strPrename'] + ' ' + objResponse[strValue]['strLastname'] + '</a></td>';
					strOutput += '<td><a href="javascript:;" onclick="company.get(' + objResponse[strValue]['intCompanyId'] + ')">' + objResponse[strValue]['strCompanyName'] + '</a></td>';
					strOutput += '<td><a href="javascript:;" onclick="box.application.get(\'application\', ' + objResponse[strValue]['intUserId'] + ');" ' +
							'title="' + gt.gettext("Anwendungsorientiertes Wissen des Benutzers anzeigen") + '" ' +
							'alt="' + gt.gettext("Anwendungsorientiertes Wissen des Benutzers anzeigen") + '" ' +
									'<img border="0" src="' + core.config('root') + 'misc/icons/application_side_tree.png" /></a></td>';
					strOutput += '<td><a href="javascript:;" onclick="box.application.get(\'technology\', ' + objResponse[strValue]['intUserId'] + ');" ' +
							'title="' + gt.gettext("Technologieorientiertes Wissen des Benutzers anzeigen") + '" ' +
							'alt="' + gt.gettext("Technologieorientiertes Wissen des Benutzers anzeigen") + '" ' +
									'<img border="0" src="' + core.config('root') + 'misc/icons/application_side_tree.png" /></a></td>';
					strOutput += '<td>' + objResponse[strValue]['strLanguages'] + '</td>';
					strOutput += '<td>' + objResponse[strValue]['strFunction'] + '</td>';
					strOutput += '</tr>';
					
					intUserCount++;
					
				}
				else if(objResponse[strValue]['intCompanyId'])	{
					strOutput += ( intCount%2 == 0 ) ? '<tr bgcolor="#eeeeee">' : '<tr bgcolor="#dddddd">';
					strOutput += '<td><img border="0" src="' + core.config('root') + 'misc/icons/company.gif" /></td>';
					strOutput += '<td><b>0</b></td>';
					strOutput += '<td><a href="javascript:;" onclick="company.get(' + objResponse[strValue]['intCompanyId'] + ')">' + objResponse[strValue]['strName'] + '</a></td>';
					strOutput += '<td></td>';
					strOutput += '<td></td>';
					strOutput += '<td></td>';
					strOutput += '<td></td>';
					strOutput += '<td></td>';
					strOutput += '</tr>';
					intCompanyCount++;
				}
				intCount++;
			}
			strOutput += '</table>';
			
			var prefixText = gt.gettext("Es wurden gefunden") + ' ' + intCount + ' ' + ( intCount == 1 ? gt.gettext("Ergebnis") : gt.gettext("Ergebnisse"));
			
			if(intCount > 0)	{
				prefixText += ', ' + gt.gettext("davon") +  ' ' + intCompanyCount + ' ' + ( intCompanyCount == 1 ? gt.gettext("Firma") : gt.gettext("Firmen")) + ' ' + gt.gettext("und") + ' ' + intUserCount + ' ' + gt.gettext("Benutzer") + '\n<br />'
			}
			strOutput = prefixText + strOutput;
			
			jQuery('#searchResult').html(strOutput);

		}, 'json');
	},

	/**
	 * 
	 * @param {} tree_type
	 */
	search_criteria_load: function(tree_type) {
		jQuery.post(doc_root + "includes/ajax/search.criteria.get.php", {
			tree_type : tree_type
		}, function(data, status) {
			var content_div = "#search_criteria_" + tree_type;
			var del_icon_alttext = gt.gettext("Suchkriterium entfernen");
			var html = '<ul>';
			for (i in data) {
				html += '<li>' +
						'	<img onclick="_search.search_criteria_delete(\'' + tree_type + '\', '+ data[i].intId +');" ' +
								'src="'+ doc_root +'misc/icons/bullet_delete.png" ' +
								'alt="' + del_icon_alttext + '" ' +
								'title="' + del_icon_alttext + '" ' +
								'style="cursor:pointer;"/>' +
						'	<span>' + data[i].strHeadline + '</span></li>';
			}
			html += '</ul>';
			
			jQuery(content_div).html(html);
		}, 'json');
	},
	
	/**
	 * 
	 * @param {} event
	 */
	user_add_knowledge : function(event) {
		var id = event.data.intId;
		var intUserId = event.data.intUserId;
		var tree_type = event.data.tree_type;

		// do ajax call for add knowledge to search-criteria (session)
		jQuery.post(doc_root + "includes/ajax/user.box." + tree_type + ".update.php", {
			intApplicationId: id,
			intTechnologyId: id,
			intUserId: intUserId
		}, function(data, status) {
			
			var refresh_box_caption = 'application' == tree_type 
						? gt.gettext("Anwendungsorientiertes Wissen") 
						: gt.gettext("Technologieorientiertes Wissen");
			
			var refresh_box_domid = 'application' == tree_type 
						? 'anwendungsorientiertes-wissen' 
						: 'technologieorientiertes-wissen';
			
						
			// refresh user knowledge 
			user.profile.knowledgeBox.update(tree_type, 
						intUserId, 
						refresh_box_caption, 
						refresh_box_domid);			
		}, 'json');
		event.preventDefault();
	},
	/**
	 * 
	 * @param {} event
	 */
	search_criteria_add : function(event) {
		id = event.data.intId;
		tree_type = event.data.tree_type;

		// do ajax call for add knowledge to search-criteria (session)
		jQuery.post(doc_root + "includes/ajax/search.criteria.add.php", {
			intId: id,
			tree_type: tree_type
		}, function(data, status) {
			// refresh search crit-field (wich reads session)
			_search.search_criteria_load(tree_type);			
		});
		event.preventDefault();
	},
	
	/**
	 * 
	 * @param {} tree_type
	 * @param {} intId
	 */
	search_criteria_delete : function(tree_type, intId) {
		// do ajax call for remove knowledge from search-criteria (session)
		jQuery.post(doc_root + "includes/ajax/search.criteria.delete.php", {
			intId: intId,
			tree_type: tree_type
		}, function(data, status) {
			// refresh search crit-field (wich reads session)
			_search.search_criteria_load(tree_type);			
		});
	},
	
	/**
	 * 
	 */
	search_criteria_tree_get: function(tree_type)	{
		util.hide_workingspace();
		var title = '';
		
		if ('application' == tree_type) {
			title = gt.gettext("Anwendungsorientiertes Wissen");
		} 
		else if ('technology' == tree_type) {
			title = gt.gettext("Technologieorientiertes Wissen");
		} 
		
		
		jQuery('#objBox h1#objBoxTitle').html(title);
		
		jQuery('#objBoxContent').html(
				'<div id="explorerContainer" style="padding-top:30px; height:340px; overflow: auto;">' +
				'	<ul id="' + tree_type + '-tree" class="filetree">' +
				'		<li id="draggable_1" node_id="1">' +
				'			<span id="draggable_1_view" class="folder knowledge-tree">' + title + '</span>' +
				'				<ul id="child-of-0"></ul>' +
				'		</li>' +
				'	</ul>' +
				'</div>');			

		
		center_div = 'objBox'; // defined global
		jQuery('#' + center_div).css({
			'position' : 'absolute',
			'height' : '400px',
			'z-index' : '99999'					
		});		
		
		jQuery('#' + center_div).appendTo('body');
		jQuery('#' + center_div).fadeIn(function() {

			// use tree plugin
			jQuery('#child-of-0').treeview({
				url: doc_root + "includes/ajax/" + tree_type + ".tree.level.get.php",
				tree_location: 'user_search'
			});			
		});
		
		jQuery(window).bind('scroll', util.position);
		util.position();
		
	},
	
	/**
	 * 
	 */
	user_add_tree_get: function(tree_type, intUserId)	{
		util.hide_workingspace();
		var title = '';
		
		if ('application' == tree_type) {
			title = gt.gettext("Anwendungsorientiertes Wissen");
		} 
		else if ('technology' == tree_type) {
			title = gt.gettext("Technologieorientiertes Wissen");
		} 
		
		
		jQuery('#objBox h1#objBoxTitle').html(title);
		
		jQuery('#objBoxContent').html(
				'<div id="explorerContainer" style="padding-top:30px; height:340px; overflow: auto;">' +
				'	<ul id="' + tree_type + '-tree" class="filetree">' +
				'		<li id="draggable_1" node_id="1">' +
				'			<span id="draggable_1_view" class="folder knowledge-tree">' + title + '</span>' +
				'				<ul id="child-of-0"></ul>' +
				'		</li>' +
				'	</ul>' +
				'</div>');			

		
		center_div = 'objBox'; // defined global
		jQuery('#' + center_div).css({
			'position' : 'absolute',
			'height' : '400px',
			'z-index' : '99999'					
		});		
		
		jQuery('#' + center_div).appendTo('body');
		jQuery('#' + center_div).fadeIn(function() {

			// use tree plugin
			jQuery('#child-of-0').treeview({
				url: doc_root + "includes/ajax/" + tree_type + ".tree.level.get.php",
				tree_location: 'user_add',
				intUserId: intUserId
			});			
		});
		
		jQuery(window).bind('scroll', util.position);
		util.position();
		
	},
	

	technology: function()	{
		util.hide_workingspace();

		jQuery('#objBox h1#objBoxTitle').html(gt.gettext("Technologieorientiertes Wissen"));
		
		jQuery('#objBoxContent').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...") + '</div>');
		
		center_div = 'objBox'; // defined global
		
		jQuery('#' + center_div).css({
			'position' : 'absolute',
			'height' : '400px',
			'z-index' : '99999'					
		});
		
		jQuery('#' + center_div).appendTo('body');
		jQuery('#' + center_div).fadeIn(function() {
//			jQuery(this).dropShadow();
			
			jQuery.post(doc_root + "includes/ajax/search.technology.get.php", {
			
			}, function(data, status) {
				// set title of the box
				jQuery('#objBoxContent').html(data);
	
				jQuery('#technology-source-tree').treeview({
					collapsed: false
				});
				jQuery('#technology-target-tree').treeview({
					collapsed: false
				});
				
				var searchvalue = jQuery.trim(jQuery('#search-technology').val());
				if (searchvalue.length > 0) {
					var reg = new RegExp(',');
					var searchelements = new Array();
					if (reg.test(searchvalue)) {
						searchelements = searchvalue.split(',');
					}
					else {
						searchelements.push(searchvalue);
					}
					_search.show_added_elements(searchelements, 'technology');
				}
	
				setTimeout(function() {
						jQuery('#click-tip').slideDown('fast');
				}, 500);
				
				setTimeout(function() {
						jQuery('#click-tip').slideUp('fast');
				}, 2000);
				
			});					
			
		});
		
		jQuery(window).bind('scroll', util.position);
		util.position();	
	},

	/**
	 * 
	 * @param {} elements
	 * @param {} type
	 */
	show_added_elements : function(elements, type) {
		//alert('exec show added elements');
		var all_elements = jQuery('#' + type + '-target-tree').children('li span');
		
		for (e in elements) {
			elements[e] = jQuery.trim(elements[e]);
		}
		
		jQuery('#' + type + '-target-tree li span').each(function(i) {
			//alert(i  + ' ' + jQuery(this).text());
			var check_text = jQuery.trim(jQuery(this).text());
			//alert(check_text + ' at:' + jQuery.inArray(check_text, elements));
			if (jQuery.inArray(check_text, elements) >= 0) {
				//alert('ch: ' + jQuery.inArray(check_text, elements));
				jQuery(this).parents().show();
			}
		});
		
	},
	
	remove_value : function(remove_id, filter) {
		var arrPartList = remove_id.split('_');
		var intId = arrPartList[arrPartList.length-1];
		var targetDomId = '#droppable_' + intId;

		jQuery(targetDomId).hide();
		
		var curr_searchvalue = jQuery.trim(jQuery('#search-' + filter).val());
		var remove_searchvalue = jQuery.trim(jQuery(targetDomId + ' span').text());
		
		if (curr_searchvalue.length > 0) {
			var reg = new RegExp(',');
			var searchelements = new Array();
			if (reg.test(curr_searchvalue)) {
				searchelements = curr_searchvalue.split(',');
			}
			else {
				searchelements.push(curr_searchvalue);
			}
			
			for (i in searchelements) {
				if (jQuery.trim(searchelements[i]) == remove_searchvalue) {
					delete searchelements[i];
				}
			}
			

			var join_str = ',';
			if (searchelements.length == 1) {
				join_str = '';
			}
			
			curr_searchvalue = jQuery.trim(searchelements.join(join_str));
			
			if (curr_searchvalue.match(/,$/)) {
				curr_searchvalue = curr_searchvalue.substr(0, curr_searchvalue.length-1);
			}
			if (curr_searchvalue.match(/^,/)) {
				curr_searchvalue = curr_searchvalue.substr(1, curr_searchvalue.length);
			}
			
			jQuery('#search-' + filter).val(jQuery.trim(curr_searchvalue));
			
		} 
		
		
		
	},
	
	add_value : function(add_id, filter) {
		var arrPartList = add_id.split('_');
		var intId = arrPartList[arrPartList.length-1];
		
		var targetDomId = '#droppable_' + intId;
		
		// show all parents so the tree to the path is visible in the right-side tree.
		jQuery(targetDomId).parents().show();
		jQuery(targetDomId).show(); // show the added listitem
		
		var curr_searchvalue = jQuery('#search-' + filter).val();
		var add_searchvalue = jQuery.trim(jQuery(targetDomId + ' span').text());

		// add the value of the listitem to the searchfield
		if (typeof curr_searchvalue != 'undefined') {
			var reg = new RegExp(add_searchvalue);
			if (!reg.test(curr_searchvalue)) { // add if not already added
				if (curr_searchvalue.length > 0) {
					curr_searchvalue += ', ';
				}
				curr_searchvalue += add_searchvalue;
				

				
				jQuery('#search-' + filter).val(curr_searchvalue);
			}
		}

	}
};


update_technology = function(intId)	{
										
//	var arrPartList = objEvent.target.id.split('_');
//	var intId = arrPartList[arrPartList.length-1];
	
//	var objDroppable = document.getElementById('droppable_' + intId);
//	objDroppable.style.display = 'block';
//	
//	var objDroppable = objDroppable.parentNode.parentNode;
//	objDroppable.style.display = 'block';
//	
//	var objDroppable = objDroppable.parentNode.parentNode;
//	objDroppable.style.display = 'block';
//	
//	var objDroppable = objDroppable.parentNode.parentNode;
//	objDroppable.style.display = 'block';
	
	var strUrl = core.config('root') + 'includes/ajax/user.box.technology.update.php?intId=' + intId;
	if(core.config('strType') == 'user')	{
		strUrl += '&intUserId=' + core.config('intUserId');
	}
	else if(core.config('strType') == 'company')	{
		strUrl += '&intCompanyId=' + core.config('intCompanyId');
	}
	YAHOO.util.Connect.asyncRequest('GET', strUrl, {
		success: function(objData)	{
			if(core.config('strType') == 'user')	{
				user.get(core.config('intUserId'), 'edit', 3);
			}
			else if(core.config('strType') == 'company')	{
				company.get(core.config('intCompanyId'), 'edit', 3);
			}
		}
	});
}
