/*
 * JavaScript interface to the SB network.
 * class SB.user of a system
 *
 * Requirements: jQuery library 1.4.2 and above
 * Copyright (C) 2010 Tomas Hnilica.   All Rights Reserved.
 */
 /*TH: some methods depend on certain HTML elements. Would it be better use this class as pure user class and call 
 methods from another customized class that takes care on UI changed? like addActivity does div rewrite, etc...*/
 
/**
Interface to the user related services
*/ 

SB.user = function(uid) {
   this.uid = uid;
   this.su = false;
   /*this.setLoggedUser();*/
	 
	 var context = this;
	 if (uid) {
		 $(document).everyTime("600s","UserTicker", function() {
			  context.tick();
			},0);
	}

}

SB.user.prototype.tick = function() {
	var context = this;
	var tuid = this.uid;
	this.setLoggedUser(function(data){
		if (data != tuid) {
			 $(document).stopTime("UserTicker");
			 thAlert("Systém Vás pro dlouhou nečinnost odhlásil.<br>Aby nedošlo ke ztrátě dat, uložte si prosím případné rozepsané texty do schránky.","Automatické odhlášení");
		}
	});	
}


SB.user.prototype.setLoggedUser = function(callback) {
	var params = {}
	params["ws"] = "loggedUser";
	var o = this;
	$.get("/ws/", params, function(data){
	   o.uid = data;
	   if (callback) callback(data);
	});
};

SB.user.prototype.superUser = function() {
	return this.su;
}

SB.user.prototype.addSKLector = function() {
	thPrompt( "Zadejte uživatelské jméno Vašeho lektora", "", "Přidat lektora", function(r,v){
		if (!r) return false;
			var params = {}
			params["ws"] = "addSKLector";
			params["uid"] = v;
			var o = this;
			$.get("/ws/", params, function(data){
			   if (data == "OK") data = "Požadavek na lektorství byl odeslán.Pokud lektor akci potvrdí, objeví se ve Vašem seznamu lektorů.";
			   thAlert(data, "Přidat lektora");
				 /*
				 $('#userLectors').load('/ws/?ws=lectorsHTML #userLectors', function(){
				 		if (SBprofile) SBprofile.addProfileControls();
				 	});
				 */

			});
	});
	return false;
}

SB.user.prototype.remSKLector = function(uid, callback) {
	thConfirm("Opravdu odebrat lektora " + uid + "?", "Odebrat lektora", function(r){
		if (!r) return false;
			var params = {}
			params["ws"] = "remSKLector";
			params["uid"] = uid;
			var o = this;
			$.get("/ws/", params, function(data){
			   if (data == "OK") data = "Lektor " + uid + " byl odebrán.";
			   thAlert(data, "Odebrat lektora");
				 $('#userLectors').load('/ws/?ws=lectorsHTML #userLectors', function(){
				 		if (SBprofile) SBprofile.addProfileControls();
				 	});
			});
		
	});
	return false;
}

SB.user.prototype.removeSKStudent = function(uid, el) {
		thConfirm("Opravdu odstranit vaše lektorství k " + uid + "?","Odebrat " + uid + " ze seznamu žáků?" ,function(r){
				if (!r) return false;
				var params = {}
				params["ws"] = "removeSKStudent";
				params["uid"] = uid;
				$.get("/ws/", params, function(data){
			    if (el) $(el).hide('slow');
			    if (data == "OK") data = "Již nejste lektorem " + uid;
			    thAlert(data,"Odebrání žáka " + uid);
			  });	
		});
};

SB.user.prototype.acceptSKStudent = function(uid, el) {
		thYesNo("Potvrdit lektorství uživatele " + uid + "?","Potvrdit lektorství" ,function(r){
				var params = {}
				if (!r) params["ws"] = "removeSKStudent";
				else params["ws"] = "acceptSKStudent";
				params["uid"] = uid;
				$.get("/ws/", params, function(data){
			    if (el) $(el).hide('slow');
			    if (data == "OK") {
			    	if (r) data = "Nyní jste lektorem uživatele " + uid;
			    	else data = "Žádost " + uid + " jste zamítl.";
			    }
			    thAlert(data,"Potvrzení lektorství " + uid);
			  });	
		});
};


SB.user.prototype.addFriendRequest = function(uid, el) {
		var params = {}
		params["ws"] = "addFriendRequest";
		params["uid"] = uid;
		$.get("/ws/", params, function(data){
	    if (el) $(el).hide('slow');
	    thAlert(data,"Žádost o přátelství");
	  });	
};

SB.user.prototype.showFriendRequests = function() {
		var params = {}
		params["ws"] = "getTheirFriendRequests";
		var context = this;
		$.get("/ws/", params, function(data){
			context.friendsRequestsDialog = thDialog(data, "Žádosti o přátelství");
			$(".jq-button").button();
	  });	
  
};

SB.user.prototype.friendsListHTML = function() {
		var params = {}
		params["ws"] = "friendsListHTML";
		params["uid"] = this.uid;
	  $.get("/ws/", params, function(data){
	    $('#profile-friends-list').replaceWith(data);
	  });
};

SB.user.prototype.acceptFriendRequest = function(uid, el) {
		var params = {}
		params["ws"] = "acceptFriendRequest";
		params["uid"] = uid;
		var context = this;
		$.get("/ws/", params, function(data){
			/*work with the DHTML could be separated from the model part? */
			if (el) {
				$(el).parent().hide('slow', function(){
			     $(el).parent().remove();
			     if ($('.friendRequest').size() < 1 ) {
			        context.friendsRequestsDialog.dialog('close');
			     }
			     
				});
			}
			/*try to update friends list, if exists*/
			context.friendsListHTML();
			/*if no more requests, close*/
	  });	
};

SB.user.prototype.ignoreFriendRequest = function(uid, el) {
		var params = {}
		params["ws"] = "ignoreFriendRequest";
		params["uid"] = uid;
		var context = this;
		$.get("/ws/", params, function(data){
			/*work with the DHTML could be separated from the modle part? */
			if (el) {
				$(el).parent().hide('slow', function(){
			     $(el).parent().remove();
			     if ($('.friendRequest').size() < 1 ) {
			        context.friendsRequestsDialog.dialog('close');
			     }
			     
				});
			}
			/*try to update friends list, if exists*/
			context.friendsListHTML();
	  });	
};

SB.user.prototype.removeFriend = function(uid, el) {
		thConfirm("Opravdu odstranit " + uid + " ze seznamu přátel?","Odebrat " + uid + " ze seznamu přátel?" ,function(r){
				if (!r) return false;
				var params = {}
				params["ws"] = "removeFriend";
				params["uid"] = uid;
				$.get("/ws/", params, function(data){
			    if (el) $(el).hide('slow');
			    thAlert(data,"Odebrání " + uid + " z přátel.");
			  });	
		});
};





/** add activity dialog */
SB.user.prototype.addActivityDlg = function(txt, ref_subject, ref_id, ref_url, ref_users, callback) {
   if (this.uid == "") return false;
   if (!txt) txt = "";
	 $('#fx-dialog-div').remove();
	 var context = this;
   var code = "<textarea id='user-share-activity' name='user-share-activity' class='profile-add-activity-dialog-area'>" + txt + "</textarea>";
	 dlg = jQuery("<div />", { id:"fx-dialog-div", html: code, "title": "Podělte se se svými přáteli" }).dialog({
		buttons: {
			"Sdílet": function() { 
				$(this).dialog("close");
        context.addActivity($('#user-share-activity').val(), ref_subject, ref_id, ref_url, ref_users, callback);				
			},
			"Zrušit": function() { 
				$(this).dialog("close");
			}
		},
		height: "auto",
		width: "auto",
		maxWidth: 900,
		maxHeight: 700,
		minWidth: 300,
		minHeight: 150,
		modal: true
	});

}

/** Add activity from the wall /depends on certain HMTL elements / */
SB.user.prototype.addActivity = function(message, ref_subject, ref_id, ref_url, ref_users, callback) {
		var params = {}
		params["ws"] = "addActivity";
		if (message == "") return;
		params["msg"] = message;
		params["ref_subject"] = ref_subject;
		params["ref_id"] = ref_id;
		params["ref_url"] = ref_url;
		params["ref_users"] = ref_users;
		var context = this;
		$.get("/ws/", params, function(data){
			if (callback) callback(data);
	  });
}

SB.user.prototype.deleteActivity = function(actid, callback) {
		var params = {}
		params["ws"] = "deleteActivity";
		params["actid"] = actid;
		var context = this;
		$.get("/ws/", params, function(data){
					if (callback) callback(data);		
	  	});
	
}


SB.user.prototype.saveUserProperties = function(params, callback) {
		$.post("/ws/?ws=saveUserProperties", params, function(data){
					if (callback) callback(data);	
	  	});
};


