function MyPageApp(userID,username,introTxt) {

	MyPageApp.baseConstructor.call(this, null, "myPageApp"); 

	this.componentController = new ComponentController();
	
	// Registers this app itself to subscribe to events
		this.componentController.register(this);
		
		
		this.introductionTxtComponent = new Intro($("#userIntroductionTxt"),userID,introTxt);
		this.componentController.register(this.introductionTxtComponent);

		this.myContentComponent = new MyContent($("#my_contents"),userID);
		this.componentController.register(this.myContentComponent);
		
		this.MyTagCloud = new TagCloud($("#my_tagcloud"),"",user_ID);
		this.componentController.register(this.MyTagCloud);
		
		this.loginComponent = new LoginComponent($("#loginComponent"));
		this.componentController.register(this.loginComponent);
		//tracker		
		this.SessionTracker=new Tracker();
		this.componentController.register(this.SessionTracker);
		//messages
		this.Send_messageBox = new MessageBox($("#messageBox", $("#message_eingabe")));
		this.addContact_messageBox = new MessageBox($("#messageBox", $("#addContactFeedback")));
		this.editPassword_messageBox = new MessageBox($("#messageBox", $("#userSettings_editPassword")));
		this.editPrivacy_messageBox = new MessageBox($("#messageBox", $("#userSettings_editPrivacy")));
		this.deleteAccount_messageBox = new MessageBox($("#messageBox", $("#userSettings_deleteAccount")));
		
		
		this.componentController.init();
		
		this.username=username;
		this.userID=userID;
		this.loggedUser= false;
		
		this.initDomElems();
		this.setupFilter();
		
			
}
MyPageApp.extend(Component);

/********************************************************************************/

MyPageApp.prototype.getSubscriptionEvents = function() {
	return [new SubscriptionEvent("login", this.onLogin), new SubscriptionEvent("logout", this.onLogout)];
}
/********************************************************************************/
MyPageApp.prototype.onLogin = function(type, args, me) {
	console.log("onLogin " + type + ", user=" + args[0].userName + ", me.id=" + me.id);
	console.log("onLogin userID" + this.userID+"----"+args[0].id);
	
	this.loggedUser= args[0].id;
    this.checkContact();
    

//tipsy 
  //  $("#contactUtilities").find("button").removeClass("showTooltip");
  //  $("#contactUtilities").find("button").attr({title: ""})
   
   $("#messagingButtons_1_logged").show();
	$("#addContact_1_logged").show();
	$("#messagingButtons_1").hide();
	$("#addContact_1").hide();
   
	//settings_tab
	if(args[0].userName == this.username || args[0].userName == this.username+" "){
		$("#settings_tab").show();
		$("#edit_user_icon").show();
		
	}
	
}
/********************************************************************************/
MyPageApp.prototype.onLogout = function(type, args, me) {
	console.log("onLogout " + type + ", user=" + args[0] + ", me.id=" + me.id);
	this.loggedUser= false;
//	this.checkContact();
	
	$("#settings_tab").hide();
	$("#edit_user_icon").hide();

  //  $("#contactUtilities").find("button").addClass("showTooltip");
  //  $("#contactUtilities").find("button").attr({"title": "Please login first<br />(click)"});
    
    $("#messagingButtons_1_logged").hide();
	$("#addContact_1_logged").hide();
	$("#addContact_2").hide();
	$("#messagingButtons_1").show();
	$("#addContact_1").show();
   
    $(".showTooltip").tipsy({fade: true, gravity:"s"});
}
/********************************************************************************/
MyPageApp.prototype.setupFilter=function(){
	
	var _this=this;
	

	$("#tagged_header").bind("click",	
	  	function() {
			_this.myContentComponent.filterTaggedResults();
			
			return false;
	});
	$("#comment_header").bind("click",	
	  	function() {
			_this.myContentComponent.filterCommentedResults();
			
			return false;
	});
	$("#good_header").bind("click",	
	  	function() {
			_this.myContentComponent.filterGoodResults();
			
			return false;
	});
	$("#bad_header").bind("click",	
	  	function() {
			_this.myContentComponent.filterBadResults();
			
			return false;
	});
	
}

MyPageApp.prototype.initDomElems=function(){
	//urls
	var editBuddyIconUrl = MACEConstants.rootURL+"pages/mypage/php/editBuddyIcon.php?id="+user_ID;
	var newPassAction= MACEConstants.rootURL+"pages/mypage/php/changePass.php";
	var privacyAction = MACEConstants.rootURL+"pages/mypage/php/setPrivacy.php";
    //tipsy tooltips
	//$(".showTooltip").tipsy({fade: true, gravity:"s"});
	
	var deleteAccountUrl= MACEConstants.rootURL+"pages/mypage/php/deleteAccount.php";
	this.$editPasswordForm=$("#editPasswordForm");
	var _this=this;
	
	$("#messagingButtons_1_logged").hide();
	$("#addContact_1_logged").hide();
	
	
    //fill networkTab
    this.getNetwork();
    // muss raus plättet aloe
    //this.getSimilarUsers();
    
	//
	$("#openMessageBox").bind("click",	
	  	function() {
	  	   
  		        app.loginComponent.openLoginDialog()
  	      
		
			
	});
	$("#openMessageBox_logged").bind("click",	
	  	function() {
	  	    
			    $("#message_eingabe").slideDown(330);
			    $("#messagingButtons_1_logged").hide();
			    $("#messagingButtons_2").show();
			  
			    $("#messageContainer").show();
                 $("#messageContainer2").hide();
		
			
	});
	//
	$("#sendMessage").bind("click",	
	  	function() {
	  	    
			_this.send_message();
			return false;
	});
	//message restrict to 140 chars
	$("#message").keydown(function(event){
	    if($(this).val().length > 140){
	        newTxt=$(this).val().substring(0,140);
	        $(this).val(newTxt);
        }
        if(event.keyCode ==13) {
            return false;
        }
	        
    });
    $("#message").click(function(){
	    if($(this).val().length > 140){
	        newTxt=$(this).val().substring(0,140);
	        $(this).val(newTxt);
        }
	        
    });
	
	//
	$("#msg_cancelButton").bind("click",	
	  	function() {

			$("#message_eingabe").slideUp(330);
		    $("#messagingButtons_1_logged").show();
			$("#messagingButtons_2").hide();
			$("#message").val("");
			//nach dem senden
			$("#messageContainer").show();
    		$("#messageContainer2").hide();
    		  
			return false;
	});
	//addContact
	$("#addContact").bind("click",	
	  	function() {
	  	    
	  	    
		        app.loginComponent.openLoginDialog()
	      
		        
			return false;
	});
	//addContact
	$("#addContact_logged").bind("click",	
	  	function() {
	  	    
	  	   
	  	    	_this.addContact();
		    
		        
			return false;
	});
	//removeContact
	$("#removeContact").bind("click",	
	  	function() {
	  	    
	  	    if(_this.loggedUser != false )
	  	    	_this.removeContact();
		    else{
		        app.loginComponent.openLoginDialog()
	        }
		        
			return false;
	});
    
    
    
    
	$("#editProfileForm").attr({action: editBuddyIconUrl});
	$("#editProfil").bind("click",	
	  	function() {
		    _this.SessionTracker.saveAction("MyPage", "editProfil","", window.location.href);
			$("#editProfileForm").submit();
	
			
			return false;
	});
	//tabs
	$('#tab_container').tabs();
	
	//newPass	
	$("#newPassButton").bind("click",	
	  	function() {
		
			var o={};
			o.newPass=$("#newPassword").val();

		   $.getJSON(newPassAction, o,function (result) {
				if(result.success){
				    _this.editPassword_messageBox.showOk(result.message,true);
		          //  $("#messageField").show();
				  //  $("#password_form").hide();
				    
				}
				else{
				    
				    _this.editPassword_messageBox.showError(result.message,true)
				   // $("#messageField").show();
			
				}
		   });
			
			return false;
	});
	//privacy	
	$("#savePrivacyButton").bind("click",	
	  	function() {
		
			var o={};
		
			$(".privacy").each( function () {
				
				if(this.checked == true)
					o.privacy=this.value;
					
			});

		   $.getJSON(privacyAction, o,function (result) {
		
				if(result.success){
				    
				    _this.editPrivacy_messageBox.showOk(result.message,true);
					
				
				}
				else{
				    _this.editPrivacy_messageBox.showError(result.message,true)
					
				}
		   });
			
			return false;
	});
	//deleteAccount
	$("#deleteAccount").bind("click",	
	  	function() {
		
			var o={};
			o.uid=_this.loginComponent.user.id;



        var r=confirm("Do you really want to leave MACE?");
        if (r==true)
          {
          		$.getJSON(deleteAccountUrl, o,function (result) {
        		  	if(result.success){
        		  	    
        		  	    _this.deleteAccount_messageBox.showOk(result.message,false);
        			    
		
        		  		_this.loginComponent.logout();
	  	
	  		
        		  	}
        		  	else{
        		  	    _this.deleteAccount_messageBox.showError(result.message,true)
        			    
	  	
        		  	}
        		 });
          }

		 
			
			return false;
	});
	
	
}

/********************************************************************************************/
MyPageApp.prototype.send_message=function(){
	
	var messageAction = MACEConstants.rootURL+"pages/mypage/php/sendMessage.php";
	console.log("user--:"+this.userID);
    
    $("#messageContainer").hide();
     $("#messageContainer2").show();
    
    var _this=this;
    
    if($("#message").val() !=""){
        
    	var o={};
    	o.msg=$("#message").val();
    	o.empfaenger_uid=this.userID;
    	$.getJSON(messageAction, o,function (result) {
	        
    		  if(result.success){
    		      //  console.log("hier????--:"+result.message);
      			  $("#message").val("");
    
  		  	 
		        
    		  	    _this.Send_messageBox.showOk(result.message,false);  //true =ausblenden
		  	
		
    		  }
    		  else{
    		      _this.Send_messageBox.showError(result.message,false)
		  
    		  }
    	   });
       }
       else
            _this.Send_messageBox.showError("there is no message!",false)
            
        
	  console.log("hier????--:");  
	  //setTimeout("this.hideMessageContainer()", 6000);
	  setTimeout(function() {_this.hideMessageContainer()}, 6000);
	  
}
/********************************************************************************************/
MyPageApp.prototype.hideMessageContainer=function(){
        $("#message_eingabe").slideUp(300);
	    $("#messagingButtons_1_logged").show();
	    $("#messagingButtons_2").hide();
}
/********************************************************************************************/
MyPageApp.prototype.addContact=function(){
	
	var addContactUrl = MACEConstants.rootURL+"pages/mypage/php/addContact.php";
	console.log("user--:"+this.userID);
    
    var _this=this;
	var o={};
	o.userID=this.userID;
	o.userName =userFullname;
	$.getJSON(addContactUrl, o,function (result) {
		
		  if(result.success){
		
			 _this.addContact_messageBox.showOk(result.message,true)
			_this.toogleFriends(1);
		  }
		  else{
		     
		      _this.addContact_messageBox.showError(result.message,true)
		  
		  }
	   });

}
/********************************************************************************************/
MyPageApp.prototype.removeContact=function(){
	
	var removeContactUrl = MACEConstants.rootURL+"pages/mypage/php/deleteContact.php";
	console.log("user--:"+this.userID);
    
    var _this=this;
	var o={};
	o.userID=this.userID;
	o.userName =userFullname;
	$.getJSON(removeContactUrl, o,function (result) {
		
		  if(result.success){
		
			 _this.addContact_messageBox.showOk(result.message,true)
			_this.toogleFriends(2);
		  }
		  else{
		     
		      _this.addContact_messageBox.showError(result.message,true)
		  
		  }
	   });

}
/********************************************************************************************/
MyPageApp.prototype.checkContact=function(){
    //TODO!!
    var checkContactUrl = MACEConstants.rootURL+"pages/mypage/php/checkContacts.php";
	console.log("checkContact--:"+this.userID);
    
    var _this=this;
	var o={};
	o.userID=this.userID;

	$.getJSON(checkContactUrl, o,function (result) {
		
		  if(result.friends){
		      _this.toogleFriends(1);
		    
		  }
		  else{
		      
		     _this.toogleFriends(2);
		      
		  
		  }
	   });
}
/********************************************************************************************/
MyPageApp.prototype.toogleFriends=function(ok){
    
    if(ok ==1){
        $("#addContact_1_logged").hide();
		$("#addContact_2").show();
    }
    else{
        $("#addContact_2").hide();
		  $("#addContact_1_logged").show();
    }
    
    
}
/***********************************************************************/	


function pass_double(){
	var pass=  $("#retype").val();	  

	if(pass !=  $("#newPassword").val() ){
		displayMessage("adviceMessage", mypage_dialogs.passMatch);
		return false;
	}
	else{
		displayMessage();
		return true;
		}	
		
}
function displayMessage(className, text){
	if(!className || !text){
		// hide
		$("#messageField").hide();
	} else {
		// show advice
		$("#messageField").show();	
		$("#messageField").html("<div class='"+className+"Message'>"+text+"</div>");
	}
}


/******************************************/	
MyPageApp.prototype.getNetwork = function( ){
console.log("getNetwork----!");
    var getNetworkUrl = MACEConstants.rootURL+"pages/mypage/php/getContacts.php";
	var _this=this;
 	var o={};
 	o.userID=this.userID;


	 $.getJSON(getNetworkUrl, o, function(results){
	 		if(!results.success){
	 			
	 		} else {
				$("#friendsList").html("");
				var members=results.resultList;
				if(results.numResults >0){
					
				  for (var i = 0; i < members.length; i++) {
			  
				  	$("#friendsList").append("<li class='memListElement'><a  class='memberListLink' href='"+MACEConstants.rootURL+"user/"+members[i].nickName+"' title=\""+members[i].introText+"\"> <div class='userTile' style='background-image: url("+members[i].icon+"); height: 104px; width:130px; margin-bottom: 36px;'><div class='userTile_name'>"+members[i].fullname+"</div></div></a></li>");
		
				  }
 
    			}
				else{
					$("#friendsList").append('<span>'+mypage_dialogs.Nobody_found+'</span>');
			
				}
				//tipsy
               $(".memberListLink").tipsy({fade: true, gravity:"s"});
	 		}
	 	}
	 	);
}
/******************************************/	
MyPageApp.prototype.getSimilarUsers = function( ){
console.log("getNetwork----!");
    var getSimilarUsersUrl = MACEConstants.rootURL+"pages/mypage/php/getSimilarUsers.php";
	var _this=this;
 	var o={};
 	o.userID=this.userID;


	 $.getJSON(getSimilarUsersUrl, o, function(results){
	 		if(!results.success){
	 		
	 		} else {
				$("#similarUsersList").html("");
				var members=results.resultList;
				if(results.numResults >0){
					
				  for (var i = 0; i < members.length; i++) {
			  
				  	$("#similarUsersList").append("<li class='memListElement'><a  class='memberListLink' href='"+MACEConstants.rootURL+"user/"+members[i].nickName+"' title=\""+members[i].introText+"\"> <div class='userTile' style='background-image: url("+members[i].icon+"); height: 104px; width:130px; margin-bottom: 36px;'><div class='userTile_name'>"+members[i].fullname+"</div></div></a></li>");
		
				  }
 
    			}
				else{
					$("#similarUsersList").append('<span>'+mypage_dialogs.Nobody_found+'</span>');
			
				}
				//tipsy
               $(".memberListLink").tipsy({fade: true, gravity:"s"});
	 		}
	 	}
	 	);
}
