////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// User Object
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// Represents a Webwag User
// 
var User = Class.create();
User.prototype = {	
		
	// Constructor
	//
	initialize: function(settings, tabs) {
		var s;//,p = this.prefs;
		Object.extend(this, settings);
		this.backup();
		this.tabs 		= tabs;
		this.currentTab = null;
		
		// Try to see if the user has a mobile client v2.
		// This should not break the process in any way.
		this.hasMobileV2 = false;
		try	{
			if (settings.mobile_version.startsWith('2') || settings.mobile_version.startsWith('3')) {
				this.hasMobileV2 = true;
			}
		}
		catch (e) {
		}
		
		//this.colsizes = p?((p.split("|")[0]).split(",")):new Array(this.nb_columns);
	},
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Methods
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	addTab: function() {
	},
	
	save: function() {
		dataHandler.saveUser();
	},

	// By default this method calls a function on the current object or on the object 
    // defined as the 'bind' parameter:
    //
    // For instance : 
    // 		- Calling user.calback("myFunction", myArgs) would be the same as calling user.myFunction(myArgs).
    // 		- Calling user.calback("myFunction", myArgs, myObject) would be the same as calling myObject.myFunction(myArgs).
    //
    // If no function can be found, this method won't do anything.
    //
    callback: function(name, args, bind) {
	
        if (args == undefined) {
			args = [];
		}
            
        if (typeof bind == 'undefined') {
            bind = this;
        }

        try {
            if (this[name]) {
                this[name].apply(bind, [args]);
            }
        } catch(e) {
            logger.log("user.callback: <b>Internal Error :</b> " + e.name + "\n\n" + e.message + "\n" + e.stack, ERROR);
        }
    },


	// Retrieve a user tab by its id
	//
	getTabById: function(id) {
		for (var i=0;i<user.tabs.length;++i){
			if(user.tabs[i].id == id)
				return user.tabs[i]; 
		}
		return null;
	},

	// Retrieve a user tab by its id
	//
	getWidgetById: function(id) {
		var i;
		for (i in global.widgets){
			if(global.widgets[i].id == id)
				return global.widgets[i]; 
		}
		return null;
	},

	getCurrentTab: function() {
		if (this.currentTab) {
			return this.currentTab;
		}
		else{
		 var t = user.getTabById(App.cookie?App.cookie[6]:user.tabs[0].id);
		 debugEcho("----------------------------------    tab from cookie="+(t?t.id:-1));
		 return t?t:this.tabs[0];
		}
	},
	
	setCurrentTab: function(tab) {
		this.currentTab = tab;
		tab.show();
	},
	
	switchTab: function(tabId) {
		for (var i=0; i < this.tabs.length; i++) {
			if (this.tabs[i].id == tabId) {
				this.setCurrentTab(this.tabs[i]);
				break;
			}
		}
	},
	
	backup: function(){
		this.bak = Object.clone(this);
	},
	
	isAnonymous: function(){
		return this.email=="";
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// End methods
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};
