var user = {
	log : function(action, info) {
		jQuery.post(doc_root + 'includes/ajax/user.log.php', {
			action : action,
			info : info
		});
	},
	
	
	/**
	 * 
	 * @type 
	 */
	history : {
		back : function() {
			jQuery.post(doc_root + '/includes/ajax/user.history.action.php', {
				action: 'back'
			}, user.history.action, 'json');
		},
		
		next : function() {
			jQuery.post(doc_root + '/includes/ajax/user.history.action.php', {
				action: 'next'
			}, user.history.action, 'json');
		},
		
		action : function(data, status) {
			var current = data.current;
			switch(current.strAction) {
				case "PageAccess":
//					alert('Call Url: ' + data.strUrl);
					document.location.href = doc_root + current.strUrl;
					break;
				case "Tab":
					// strInfo is the id of the element 
					project.open_tab(current.strInfo, true);
					
					// update history buttons
					jQuery('#history-buttons').replaceWith(data.history_buttons);
					break;
			}
		}
	},
	
	contact : function() {
		var contents = jQuery.trim(jQuery('#contact-content').val());
		if (contents.length > 0) {
			jQuery.post(doc_root + 'includes/ajax/user.contact.php',{
				message: contents
			}, function (data, status) {
				if (data) {
					alert(gt.gettext("Ihre Nachricht wurde erfolgreich gesendet."));
					jQuery('#contact-area').hide();
					jQuery('#contact-button').hide();
					jQuery('#contact-success').show();
					jQuery('#contact-content').val("");
				}
				else {
					alert(gt.gettext("Ihre Nachricht konnte nicht gesendet werden. Versuchen Sie es bitte später noch ein mal"));
				}
			}, 'json');
		}
		else {
			alert(gt.gettext("Bitte tragen Sie eine Nachricht ein"));
		}
	},
	profile : {
		knowledgeBox : {
			update : function(type, intUserId, title, dom_id) {
				jQuery.post(doc_root + 'includes/ajax/user.box.' + type + '.get.php',{
					intUserId: intUserId
				}, function(data, status) {
					// update div with id technologieorientiertes-wissen
					var html = '<div style="font-weight:bold; margin-bottom:10px;">' + title + '</div>' +
							'<input type="text" id="add_' + type + '_class" name="add_technology_class" style="float:left; width:250px; margin-left:5px;"/>' +
							'<input type="button" value="' + title + '" style="float:left" onclick="user.' + type + '.add_by_class(' + intUserId + ');"/>' +
									'<div style="clear:both; display:block; height:20px;">&nbsp;</div>' +
									'<div style="font-weight: bold; margin-bottom: 10px;">' + title + '</div>' +
							'<div>' +
							'<ul>';
					for (var index in data) {
						html += '<li>';
						
						var rating = data[index].intRating;
						
						// add rating checkboxes
						for (var i = 1; i <= 5; i ++) {
							var checkbox_img = 'checkbox.jpg';
							
							if (rating > 0 && rating >= i) {
								checkbox_img = 'checkbox_checked.jpg';
							}
							
							var onclickEvent = data[index].admin ? 
								'user.profile.knowledge_rating(\'' + type + '\', ' + data[index].intKnowledgeId + ', ' + intUserId + ', ' + i + ');' 
											: 'return false;';

							html += '<img id="ratingInProfile_' + data[index].intKnowledgeId + '_' + i + '" ' +
									'onclick="' + onclickEvent + '" border="0" style="margin-right:3px;" ' +
									'src="' + doc_root + 'misc/icons/' + checkbox_img + '" width="10" />';
							
						}
						html += '<img src="' + doc_root + 'misc/icons/chart_organisation_delete.png"' +
								'style="margin-left:10px; cursor:pointer;"' +
								'alt="' + gt.gettext('Wissen entfernen') + '"' +
								'title="' + gt.gettext('Wissen entfernen') + '"' +
								'onclick="user.' + type + '.deleteIt(' + data[index].intKnowledgeId + ', ' + intUserId + ');"/>';
						html += '<span style="margin-left:10px;color:black;">' + data[index].strKlasse + '</span>';	
						html += '<span style="margin-left:10px;">' + data[index].strHeadline + '</span></li>';	
					}
					html += '</ul>' +
							'</div>';
					
					jQuery('#' + dom_id).html(html);			
				}, 'json');
			}
		}, 
		
		
		/**
		 * Set rating for a knowledge defined in 'type'
		 * @param {String} type - of knowledge to be rated 
		 * @param {Integer} intTypeId - id of the type of the knowledge
		 * @param {Integer} intUserId - rated user
		 * @param {Integer} intRating - 
		 */
		knowledge_rating: function(type, intTypeId, intUserId, intRating)	{
			// mark checkboxes
			for (var i = 1; i <= intRating; i ++) {
				jQuery('#rating_' + intTypeId + '_' + i + ', #ratingInProfile_' + intTypeId + '_' + i)
					.attr('src', doc_root + 'misc/icons/checkbox_checked.jpg');
			}
			for (var i = intRating+1; i < 6; i ++) {
				jQuery('#rating_' + intTypeId + '_' + i + ', #ratingInProfile_' + intTypeId + '_' + i)
					.attr('src', doc_root + 'misc/icons/checkbox.jpg');
			}
			
			// set rating in db
			jQuery.post(doc_root + 'includes/ajax/user.box.' + type + '.rating.php', {
				intTypeId : intTypeId,
				intUserId : intUserId,
				intRating : intRating
			});
		} // end checkbox		
	},
	
	request_technology_form: {
		send: function() {
			// get and validate fields
			var subject = jQuery('#request-technology-subject').val();
			var message = jQuery('#request-technology-message').val();
			var technology_id = parseInt(jQuery('#request-technology-id').val());
			

			if (subject == '' || message == '' || isNaN(technology_id)) {
				alert(gt.gettext("Bitte füllen Sie die Felder richtig aus."));
				return;
			}
			
			// submit fields to save script
	        jQuery.post(doc_root + "includes/ajax/technology.request.save.php", {
	                subject: jQuery('#request-technology-subject').val(),
	                message: jQuery('#request-technology-message').val(),
	                technology_id: jQuery('#request-technology-id').val()
	        }, function (data, status) {
					// validate the return from script
	                if (!data) {
	                    alert(gt.gettext("Es tut uns Leid, aber beim Senden Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut."));
	                }
	                else {
	                	alert(gt.gettext("Vielen Dank für Ihre Anfrage! Ihre Nachricht wurde an die zuständigen TMG Mitarbeiter übermittelt."))
	                	util.close('mail-form-box');
	                }
	        }, "json"); 
		}
	},
	
	country:	{
		update: function (strContinent, strCountry, intUserId)	{
			
			strUrl = 'strCountry=' + strCountry + '&intUserId=' + intUserId;
		
			YAHOO.util.Connect.asyncRequest('POST', core.config('root') + "includes/ajax/user.country.update.php", {
				success: function(objData)	{
					var intId = objData.responseText;
					if(intId > 0)	{
						var strUlElement = document.getElementById('divAbroadView').getElementsByTagName('ul')[0];
						var strLiElement = document.createElement("li");
						strLiElement.innerHTML = strContinent + ' &gt; ' + strCountry + '&nbsp;<a href="javascript:;" ' +
								'onclick="this.parentNode.style.display = \'none\'; user.country.delete(' + intId + ');">' + gt.gettext("Löschen") + '</a>';
						strUlElement.appendChild(strLiElement);
					}
				}
			}, strUrl);
			
		}, // end update
		
		Delete: function(intId)	{
			var strUrl = core.config('root') + "includes/ajax/user.country.delete.php?intId=" + intId;
			var objRequest = YAHOO.util.Connect.asyncRequest('GET', strUrl, {
				success: function(objData)	{}
			});
		} // end delete
		
	}, // end country
	get: function (intUserId, strPageMode, tabViewActiveIndex)	{

		tabViewActiveIndex = (typeof tabViewActiveIndex == "undefined") ? 0 : tabViewActiveIndex;
		if(intUserId != undefined)	{
			core.config('strType', 'user');
			core.config('intUserId', intUserId);
			
			var requestPage = strPageMode == 'edit' ? 'user-edit.get.php' : 'user.get.php';
			
			jQuery.post(doc_root + "includes/ajax/" + requestPage, {
				intUserId : intUserId,
				strPageMode : strPageMode
			}, function(data, status) {
				jQuery('#view').html(data);
				
				util.initTabs();
				
				
				jQuery('ul.tabNavigation a').address(function() {
//					$.address.value($(this).attr('id'));					
				});
				
				
				if ('edit' == strPageMode) {
					jQuery.post(doc_root + "includes/ajax/user.info.php", {
						intUserId : intUserId	
					}, function (data, status) {
						objUser = data;
						var arrRegister = objUser.dtmRegister.toString().split("-");
						var arrValidFrom = objUser.dtmValidFrom.split("-");
						var arrValidTo = objUser.dtmValidTo.split("-");
						
						arrRegister = util.removeLeadingZeros(arrRegister);
						arrValidFrom = util.removeLeadingZeros(arrValidFrom);
						arrValidTo = util.removeLeadingZeros(arrValidTo);
		
						
						// avoid 0s in date
						var registerDate = 
							arrRegister[0] == 0 || (arrRegister[1]-1) == 0 || arrRegister[2] == 0 
							|| isNaN(arrRegister[0]) || isNaN((arrRegister[1]-1)) || isNaN(arrRegister[2]) 
									? new Date() : new Date(arrRegister[0], (arrRegister[1]-1), arrRegister[2]);
						
						$("#dtmRegister").datepicker({
							defaultDate: new Date(registerDate),
							altField: '#Register',
							altFormat: 'yy-mm-dd',
							changeMonth: true,
							changeYear: true
						});					
		
						// avoid 0s in date
						var validToDate = 
								arrValidTo[0] == 0 || (arrValidTo[1]-1) == 0 || arrValidTo[2] == 0 
									|| isNaN(arrValidTo[0]) || isNaN((arrValidTo[1]-1)) || isNaN(arrValidTo[2])
									? new Date() : new Date(arrValidTo[0], (arrValidTo[1]-1), arrValidTo[2]);
						$("#dtmValidTo").datepicker({
							defaultDate: new Date(validToDate),
							altField: '#ValidTo',
							altFormat: 'yy-mm-dd',
							changeMonth: true,
							changeYear: true
						});					
		
						// avoid 0s in date
						var validFromDate = 
							arrValidFrom[0] == 0 || (arrValidFrom[1]-1) == 0 || arrValidFrom[2] == 0 
								|| isNaN(arrValidFrom[0]) || isNaN((arrValidFrom[1]-1)) || isNaN(arrValidFrom[2])
									? new Date() : new Date(arrValidFrom[0], (arrValidFrom[1]-1), arrValidFrom[2]);
						$("#dtmValidFrom").datepicker({
							defaultDate: new Date(validFromDate),
							altField: '#ValidFrom',
							altFormat: 'yy-mm-dd',
							changeMonth: true,
							changeYear: true
						});					
					}, 'json');
				}
				
			});
		}
		
	}, // end get
	update:	{
	
		password: function(intId)	{
			YAHOO.util.Connect.setForm('formPassword', true);
			YAHOO.util.Connect.asyncRequest('POST', core.config('root') + 'includes/ajax/user.update.password.php', {
				upload: function(objData) {}
			});
			
		}, // end password
		
		image: function(){
			YAHOO.util.Connect.setForm('formImage', true);
			YAHOO.util.Connect.asyncRequest('POST', core.config('root') + 'includes/ajax/user.update.image.php', {
				upload: function(objData) {
					document.getElementById('profileImage').src = core.config('root') + 'includes/ajax/getfile.php?filename=' + objData.responseText;
			  }
			});
		} // end image
	
	}, // end update
	create:	function(role, intParentId){
		intParentId = typeof intParentId != 'undefined' ? intParentId : false;
		jQuery.post(doc_root + "includes/ajax/user.create.php", {
			strRight : core.getConfigRightBitstring(role),
			intParentId : intParentId
		}, function(intUserId, status) {
			core.menu.get();
			user.get(intUserId, 'edit');
		});
	}, // end create
	Delete: function(intUserId)	{
		if(intUserId)	{
			if(confirm("Wollen Sie wirklich?"))	{
				jQuery.post(doc_root + "includes/ajax/user.delete.php", {
					intUserId : intUserId
				}, function(data, status){
					core.menu.get(); 
				});
			}
		}
	}, // end delete
	save:	function(objElementList)	{
		var intUserId = core.config('intUserId');
		// gather user data from from
		var user_params = new Object();
		
		for(var intOuterCount = 0; intOuterCount < objElementList.length; intOuterCount++)	{
			var strId = objElementList[intOuterCount];
			var divViewName = 'div' + strId + 'View';
			var divEditName = 'div' + strId + 'Edit';
			var strPTag = document.getElementById(divViewName).getElementsByTagName('p')[0];
			
			var arrInputValues = document.getElementById('form' + strId).getElementsByTagName('input');
			var arrSelectValues = document.getElementById('form' + strId).getElementsByTagName('select');
			var arrTextareaValues = document.getElementById('form' + strId).getElementsByTagName('textarea');
					

			for(var intCount = 0; intCount < arrInputValues.length; intCount++)	{
				user_params[arrInputValues[intCount].name] = arrInputValues[intCount].value;
			}
			
			for(var intCount = 0; intCount < arrTextareaValues.length; intCount++)	{
				user_params[arrTextareaValues[intCount].name] = arrTextareaValues[intCount].value;
			}
			
			for(var intCount = 0; intCount < arrSelectValues.length; intCount++)	{
				var strValue = arrSelectValues[0].value;
				user_params[arrSelectValues[intCount].name] = strValue;
			}
		
		}
		
		
		user_params['intUserId'] = intUserId;

		jQuery.post(doc_root + "includes/ajax/user.element.save.php", user_params , function(data, status) {
			core.menu.get();
			user.get(intUserId);
		}, 'json');
		
	}, // end save
	element: {
	
		edit: function(strId)	{
		
			var divViewName = 'div' + strId + 'View';
			var divEditName = 'div' + strId + 'Edit';
			
			document.getElementById(divViewName).style.display = 'block';
			document.getElementById(divEditName).style.display = 'block';
			//alert('view:  ' + divViewName + ' edit: ' + divEditName);
		
		}, // end edit
		
		reset: function (strId, objDefaultList)	{
			for(var strKey in objDefaultList)	{
				//alert(objDefaultList[strKey]);
				//document.getElementsByName(strKey)[0].value = objDefaultList[strKey];
			}
			
			var divViewName = 'div' + strId + 'View';
			var divEditName = 'div' + strId + 'Edit';
			
			document.getElementById(divViewName).style.display = 'block';
			document.getElementById(divEditName).style.display = 'none';
			return false;
		}, // end reset
		
		save: function (strId, intUserId, objData)	{

			var objData = objData != undefined ? objData : false; 

			var strUrl = '';
			var strView = '';
			
			var divViewName = 'div' + strId + 'View';
			var divEditName = 'div' + strId + 'Edit';
			
			var strPTag = document.getElementById(divViewName).getElementsByTagName('p')[0];
			var arrInputValues = document.getElementById('form' + strId).getElementsByTagName('input');
			var arrSelectValues = document.getElementById('form' + strId).getElementsByTagName('select');
			var arrTextareaValues = document.getElementById('form' + strId).getElementsByTagName('textarea');
			
			if(objData !== false)	{
				strUrl += objData.strKey + '=' + objData.strValue + '&';
				strView += objData.strValue + '&nbsp;';
			}
							
			for(var intCount = 0; intCount < arrInputValues.length; intCount++)	{
				strUrl += arrInputValues[intCount].name + '=' + arrInputValues[intCount].value + '&';
				strView += arrInputValues[intCount].value + '&nbsp;';
			}
							
			for(var intCount = 0; intCount < arrTextareaValues.length; intCount++)	{
				strUrl += arrTextareaValues[intCount].name + '=' + arrTextareaValues[intCount].value + '&';
				strView += arrTextareaValues[intCount].value + '&nbsp;';
			}
			
			for(var intCount = 0; intCount < arrSelectValues.length; intCount++)	{
				var strValue = arrSelectValues[0].value;
				strUrl += arrSelectValues[intCount].name + '=' + strValue + '&';
				strView += arrSelectValues[0].options[arrSelectValues[0].selectedIndex].innerHTML + '&nbsp;';
			}
			
			strUrl += "intUserId=" + intUserId;
			strView += '<a href=\"javascript:;\" onclick=\"user.element.edit(\'' + strId + '\');\">' + gt.gettext("Bearbeiten") + '</a>';

			YAHOO.util.Connect.asyncRequest('POST', core.config('root') + "includes/ajax/user.element.save.php", {
				succes: function(objData)	{}
			}, strUrl);
			
			strPTag.innerHTML = strView;
			
			document.getElementById(divViewName).style.display = 'block';
			document.getElementById(divEditName).style.display = 'none';
			
			core.menu.get();

			
		} // end save
					
	}, // end element
	application: {
		add_by_class: function(intUserId) {
			// get classname from input field "add_technology_class"
			var application_class = jQuery('#add_application_class').val();
			
			
			jQuery.post(doc_root + 'includes/ajax/user.box.application.update_by_class.php', { 
				strClass: application_class, 
				intUserId: intUserId
			}, function (data, status) {
				user.profile.knowledgeBox.update('application', 
												intUserId, 
												gt.gettext("Anwendungsorientiertes Wissen"), 
												'anwendungsorientiertes-wissen');
			},'json');
		},
		
		add: function(intApplicationId, intUserId) {
		
			jQuery('#droppable_' + intApplicationId).parents().show(); 
			jQuery('#droppable_' + intApplicationId).show(); // view added application
			jQuery('#droppable').scrollTo('#droppable_' + intApplicationId); // jump to added application
			
			jQuery.post(doc_root + 'includes/ajax/user.box.application.update.php', { 
				intApplicationId: intApplicationId, 
				intUserId: intUserId
			}, function (data, status) {
				user.profile.knowledgeBox.update('application', 
												intUserId, 
												gt.gettext("Anwendungsorientiertes Wissen"), 
												'anwendungsorientiertes-wissen');
			},'json');
		},	
		deleteIt:	function (intApplicationId, intUserId){
			jQuery.post(doc_root + 'includes/ajax/user.box.application.delete.php', {
				intApplicationId : intApplicationId,
				intUserId : intUserId

			}, function (data, status) { 
				user.profile.knowledgeBox.update('application', 
												intUserId, 
												gt.gettext("Anwendungsorientiertes Wissen"), 
												'anwendungsorientiertes-wissen');

			});			
			
		}, // end delete		
		Delete:	function (intApplicationId, intUserId){
			// update the deletion in gui
			jQuery('#droppable_' + intApplicationId).hide();
			jQuery.post(doc_root + '/includes/ajax/user.box.application.delete.php', {
				intApplicationId : intApplicationId,
				intUserId : intUserId
			}, function(data, status) {
				user.profile.knowledgeBox.update('application', 
												intUserId, 
												gt.gettext("Anwendungsorientiertes Wissen"), 
												'anwendungsorientiertes-wissen');
			}, 'json');
			
		} // end delete
	
	}, // end technology
	
	
	technology: {
		add_by_class: function(intUserId) {
			// get classname from input field "add_technology_class"
			var technology_class = jQuery('#add_technology_class').val();
			
			
			jQuery.post(doc_root + 'includes/ajax/user.box.technology.update_by_class.php', { 
				strClass: technology_class, 
				intUserId: intUserId
			}, function (data, status) {
				user.profile.knowledgeBox.update('technology', 
												intUserId, 
												gt.gettext("Technologieorientiertes Wissen"), 
												'technologieorientiertes-wissen');
			},'json');			
		},
		add: function(intTechnologyId, intUserId) {
		
			jQuery('#droppable_' + intTechnologyId).parents().show(); 
			jQuery('#droppable_' + intTechnologyId).show(); // view added application
			jQuery('#droppable').scrollTo('#droppable_' + intTechnologyId); // jump to added application
			
			
			jQuery.post(doc_root + 'includes/ajax/user.box.technology.update.php', { 
				intTechnologyId: intTechnologyId, 
				intUserId: intUserId
			}, function (data, status) {
				user.profile.knowledgeBox.update('technology', 
												intUserId, 
												gt.gettext("Technologieorientiertes Wissen"), 
												'technologieorientiertes-wissen');
			},'json'); 
		},
		
		deleteIt:	function (intTechnologyId, intUserId){
			jQuery.post(doc_root + 'includes/ajax/user.box.technology.delete.php', {
				intTechnologyId : intTechnologyId,
				intUserId : intUserId

			}, function (data, status) { 
				user.profile.knowledgeBox.update('technology', 
												intUserId, 
												gt.gettext("Technologieorientiertes Wissen"), 
												'technologieorientiertes-wissen');

			});			
			
		}, // end delete
		
		get_user: function() {
			jQuery.post(doc_root + '/includes/ajax/user.get.php');
		},
		
		checkbox: function(strPrefix, intCurrentCount)	{
			var arrPrefix = strPrefix.split('_');
			
			var strUrl = core.config('root') + 'includes/ajax/user.box.technology.rating.php?';
			strUrl += 'intTechnologyId=' + arrPrefix[1] + '&intRating=' + intCurrentCount + '';
			strUrl += '&intUserId=' + core.config('intUserId');
			
			intCurrentCount++;
		
			for(var intCount = 1; intCount < 6; intCount++)	{
				document.getElementById(strPrefix + intCount).src = '/tmg/misc/icons/checkbox.jpg';
			}
		
			for(var intCount = 1; intCount < intCurrentCount; intCount++)	{
				document.getElementById(strPrefix + intCount).src = '/tmg/misc/icons/checkbox_checked.jpg';
			}
			
			var objRequest = YAHOO.util.Connect.asyncRequest('GET', strUrl, {
				success: function(objData)	{
				
				}
			});
		
		} // end checkbox
	
	}, // end technology
	box : {
		get: function(element_id, project_room_id, thema_id, article_id) {
			//if project_room_id is defined and is not a number then it's container for a filter parameter (string)
			var viewMode = ( isNaN(project_room_id) && typeof project_room_id != 'undefined' ) ? project_room_id : false;
			
			project_room_id = ( typeof project_room_id == 'undefined' || isNaN(project_room_id) ) ? false : project_room_id;
			thema_id = typeof thema_id == 'undefined' ? false : thema_id;
			article_id = typeof article_id == 'undefined' ? false : article_id;
			
			if ('event-calendar' == element_id) {
				project_room_id = jQuery('#project_room_id').val();	
			}

			// check if element exists
			if (jQuery('#' + element_id).length > 0) { 
				// load msg before update
				jQuery('#' + element_id).html('<img src="' + doc_root + 'misc/icons/loader.gif" style="padding:45%;" />'); 
				
				jQuery.post(doc_root + 'includes/ajax/user.' + element_id + '.get.php', {
					project_room_id: project_room_id,
					thema_id: thema_id,
					article_id: article_id,
					viewMode: viewMode
					
				}, function (data, status) {
					
					// check if its box search cloud before
					// adding html to the box
					if ('project-box-search-cloud' == element_id) {
						var cloud_html = '';
						jQuery('.mainsearcha', jQuery(data)).each(function(i) {
							cloud_html += '<span style="padding:4px; ">' + jQuery(this).text() + '</span>';
							if (i >= 5) {
								return false;	
							}
							
							if (i < 5) {
								cloud_html += ', ';	
							}
						});
						
						cloud_html = jQuery.trim(cloud_html);
						if (cloud_html.length > 0) {
							data = cloud_html;	
						}
						else {
							data = '<span style="padding:4px; ">' + gt.gettext("Keine Suchwolke vorhanden") + '</span>';		
						}
						
						data += '<p style="margin-top:10px;">' +
								'<a href="javascript:;" onclick="project.open_tab(\'tabCloud\');" ' +
								'	alt="' + gt.gettext("Wissensbasis anzeigen") + '" ' +
								'	title="' + gt.gettext("Wissensbasis anzeigen") + '" ' +
								'	class="link-tab">' +
								'<span><img src="' + doc_root + 'misc/icons/lightbulb.png" style="margin-right:5px;" border="0"  />' + gt.gettext("Wissensbasis") + '</span></a></p>' +
										'<div style="clear:both;"></div>';
					}
					

					
					// update element
//					alert('update: ' + element_id);
					jQuery('#' + element_id).html(data);
					
					util.check_footer_location();
					
					
					if ('project-search-cloud' == element_id) {
//						alert('hide');
						jQuery('.hovertip').remove();
						// remove onmouse events from search-cloud in any of the above case
						jQuery('a.mainsearcha').attr('onmouseover', '');
						jQuery('a.mainsearcha').attr('onmouseout', '');
						jQuery('a.mainsearcha').unbind('mouseover');
						jQuery('a.mainsearcha').unbind('mouseout');			
					}
					
					if ('event-calendar' == element_id) {
						user.init_calendar_events();	
					}
					
					if ('events-overview' == element_id) {
						// add jquery tooltips to event links
//						alert('loaded event-overview');
						jQuery('.event-short-view-link').tooltip();
					}
				},'json');
			}
		},
		
		
		/**
		 * Saves the position/rank of a box in the side-columns after sorting them.
		 * @param {} event
		 * @param {} ui
		 */
		save_position : function(event, ui) {
			// renumerate array
			jQuery(event.target).children().each(function(i) {
				jQuery(this).attr('box_position', (i+1));
			});
			
			var boxPositions = new Object();
			
			// create object with box_ids and new position
			jQuery('div[box_id]').each(function(i) {
				
				boxPositions[i] = {
					id: jQuery(this).attr('box_id'),
					position: jQuery(this).attr('box_position')
				};	
			});
			
//			console.log(jQuery.toJSON(boxPositions));
			
			//var content_id = jQuery('.boxContent', event.target).attr('id');
			
			jQuery.post(doc_root + 'includes/ajax/user.box.save-position.php', {
				box_positions: jQuery.toJSON(boxPositions)	
			});
			
		}
	}, // end box
	
	calendar : {
		get_month : function(month_no) {
			jQuery.post(doc_root + 'includes/ajax/user.event-calendar.get.php', {
				month_no : month_no
			}, function (data, status) {
				jQuery('#event-calendar').html(data);
				user.init_calendar_events();
			}, 'json');
		},
		get_date : function(month_no, year_no) {

			jQuery.post(doc_root + 'includes/ajax/user.event-calendar.get.php', {
				month_no : month_no,
				year_no : year_no
			}, function (data, status) {
				jQuery('#event-calendar').html(data);
				user.init_calendar_events();
			}, 'json');
		},
		
		get_date_selectfield : function() {
			var month_selected = jQuery('#month_selected').val();
			var year_selected = jQuery('#year_selected').val();
			user.calendar.get_date(month_selected, year_selected);
		}
	},
	
	init_calendar_events : function() {
//		jQuery('.date_has_event').tooltip({
//		    track: true, 
//		    delay: 0, 
//		    showURL: false, 
//		    showBody: " - ", 
//		    fade: 250 		
//			
//		});
		
		jQuery('.date_has_event').each(function () {
			
			// options
			var distance = 10;
			var time = 250;
			var hideDelay = 100;
	
			var hideDelayTimer = null;
	
			// tracker
			var beingShown = false;
			var shown = false;
	
			// scroll position
			var scroll_top = jQuery(window).scrollTop();
			
			var trigger = jQuery(this);
			var popup = jQuery('.events ul', this).css('opacity', 0);
			
			
			// set the animate parameters
			animate_in_params = {
				left: '+=' + distance + 'px',
				opacity: 1
			};			
			animate_out_params = {
				left: '-=' + distance + 'px',
				opacity: 0
			};			

			
			// set the mouseover and mouseout on both element
			jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {
				jQuery('#mainNavWrapper').css({'z-index': -1});
				// stops the hide event if we move from the trigger to the popup element
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				// don't trigger the animation again if we're being shown, or already visible
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
	
					// reset position of popup box
					popup.css({
						bottom: -60,
						left: 20,
						display: 'block' // brings the popup back in to view
					})
					
					// (we're using chaining on the popup) now animate it's opacity and position
					.animate(animate_in_params, time, 'swing', function() {
						// once the animation is complete, set the tracker variables
						beingShown = false;
						shown = true;
//						popup.dropShadow();
					});
				}
			})
			.mouseout(function () {
				// reset the timer if we get fired again - avoids double animations
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
	
				jQuery('#mainNavWrapper').css({'z-index': 5});
				// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
					popup.removeShadow();
					hideDelayTimer = null;
					popup.animate(animate_out_params, time, 'swing', function () {
						// once the animate is complete, set the tracker variables
						shown = false;
						// hide the popup entirely after the effect (opacity alone doesn't do the job)
						popup.css('display', 'none');
						
					});
				}, hideDelay);
			});
		});		
	},
	
	/**
	 * 
	 * @param {} techId
	 * @param {} wikipedia_url
	 */
	load_wikipedia_url: function(techId, wikipedia_url) {
		util.hide_workingspace();
				
		jQuery('#wiki-box').css({
				'z-index': 9999,
				'position' : 'absolute'
				,
				'top' : 120,
				'width' : '70%',
				'margin-left' : '15%'
				});	

		center_div = 'content-hide-element'; // defined global
		jQuery(window).bind('scroll', util.position);				
		util.position();				
		
//		center_div = 'wiki-box'; // defined global
//		jQuery(window).bind('scroll', util.position);				
//		util.position();				
//				
		jQuery('#wiki-box').prependTo('body');				
		
		jQuery('#mainCol').css('position', '');
		jQuery('#btn-close-wiki').bind('click', function(e) {
			jQuery('#wiki-iframe').attr({src: ""});
			util.close('wiki-box');
		});
		
		jQuery('#wiki-box').css({
			'margin-top' : 20
			});
		jQuery('#wiki-box').show();
		jQuery('#wiki-iframe').attr({src: wikipedia_url});
	},
	
	/**
	 * Opens the mailform for request of a user to add a technology to himself.
	 * @param {} techId
	 */
	load_technology_request_form: function(techId) {
		
		var add_tech = confirm("Wollen Sie die Technologie zu Ihrem Profil hinzufügen?");
		
		if (add_tech) {
			util.hide_workingspace();
			
			jQuery('#mail-form-box').css({
					'z-index': 9999,
					'position' : 'absolute'
					});		
			
			
			jQuery('#mail-form-content').html('<img src="' + doc_root + 'misc/icons/loader.gif" ' +
					'style="padding-left:48%;padding-right:48%;"/><div style="padding:40%;padding-top:0;" >' + gt.gettext("Lade Anfrageformular...") + '</div>');
			
	//		jQuery('#btn-close-mailform').bind('click', function(e) {
	//			jQuery('#mail-form-box').hide('normal');
	//		});
			
			center_div = 'mail-form-box'; // defined global
			jQuery(window).bind('scroll', util.position);				
			util.position();
	
			jQuery('#mail-form-box').prependTo('body');
			jQuery('#mail-form-box').fadeIn('fast');
			
			
			jQuery.post(doc_root + "includes/ajax/technology.request-form.get.php", {
				techId: techId
			}, function (data) {
				jQuery('#mail-form-content').html(data);
			}, 'json');
		}

	},
	
	load_newsarea_archive: function() {
		var area_id = jQuery('#newsarea-select-field').val();
		
		user.box.get('newsarchive-content', area_id)
	},
	
	load_all_events: function(projectroom_id){
		projectroom_id = projectroom_id != undefined ? projectroom_id : false; 
		
		jQuery.post(doc_root + "includes/ajax/user.events-overview.get.php", {
			project_room_id: projectroom_id,
			type: 'show_all_events'
		}, function (data) {
			jQuery('#all-events').html(data);
			
		}, 'json');
		
	}
};
