/**
 * The class provides method for saving the individual state and position of
 * the AJAX-boxes, the sidebar and some other stuff. It has a methods to get
 * this information from the server and to store this information onto the
 * server.
 *
 * @author maj <martin.jahn@communardo.de>
 * @since 2007-07-10
 * @access public
 */
var UserProfile = new Class({
	initialize: function(activateSidebar) {
		this.boxStatus = {};
		this.activateSidebar = activateSidebar;
		this.arrAjaxBoxes = {};
		this.url = document.URL;
		this.url = this.url.replace('index.php?', 'ajax.php?handler=AjaxProfileHandler&');
		this.url = this.url.replace('eventdata', 'event-_data');
		this.url = this.url.replace('eventid', 'event-_id');
	},
	
	addBox: function(box) {
		this.arrAjaxBoxes[box.sysid] = box;
	},
	
	delBox: function(sysid) {
		if(this.arrAjaxBoxes[sysid]) {
			this.arrAjaxBoxes[sysid] = null;
		}
	},
	
	/**
	 * sort the ajaxboxes related to the userprofile
	 */
	sortBoxes: function() {
	},

	/**
	 * get the status of the boxes
	 */
	getAllBoxStatus: function() {
		if(this.activateSidebar) {
			if(this.arrAjaxBoxes['sysidAPPSidebarright'].status && this.arrAjaxBoxes['sysidAPPSidebarright'].status != "") {
				this.boxStatus['sysidAPPSidebarright'] = this.arrAjaxBoxes['sysidAPPSidebarright'].status;
			} else {
				this.boxStatus['sysidAPPSidebarright'] = 'close';
			}
		}
		for(id in this.arrAjaxBoxes) {
			if(id == '') {
				continue;
			}
			el = this.arrAjaxBoxes[id];
			arrTemp = new Array();
			if(this.arrAjaxBoxes[id].status) {
				this.boxStatus[id] = this.arrAjaxBoxes[id].status;
			} else {
				this.boxStatus[id] = '';
			}
		}
	},

	/**
	 * get the status of a single box
	 */
	getBoxStatus: function(sysid) {
		this.boxStatus[sysid] = this.arrAjaxBoxes[sysid].status;
	},

	/**
	 * set the status of the boxes
	 */
	setAllBoxStatus: function() {
		if(this.activateSidebar) {
			this.setBoxStatus('sysidAPPSidebarright');
		}
		$$('#col2_content .ajaxbox').each(function (el) {
			if(el.getProperty('id') != '') {
				this.setBoxStatus(el.getProperty('id'));
			}
		}.bind(this));
	},
	
	/**
	 * set the status of a single box
	 */
	setBoxStatus: function(sysid) {
		if(this.boxStatus && this.boxStatus[sysid]) {
			status = this.boxStatus[sysid];
	
			if(status == 'min') {
				this.arrAjaxBoxes[sysid].minimize(true);
			}
	
			if(status == 'max') {
				this.arrAjaxBoxes[sysid].maximize(true);
			}
	
			if(status == 'close') {
				this.arrAjaxBoxes[sysid].close();
			}
		} else {
			
		}
	},
	
	/**
	 * get current userprofile from the server
	 */
	syncWithServer: function() {
		this.getAllBoxStatus();
		try {
			this.ajax = new Ajax (this.url+'&readonly', {
				method: 'post',  // send via HTTP POST
				data: this.boxStatus,  // send the status-array of all boxes
				onComplete: function (responseText, responseXML) {
					if(responseText != '') {
						eval('this.boxStatus = '+responseText);
					}
					
					if(this.activateSidebar) {
						if(this.boxStatus['sysidAPPSidebarright'] == '') {
							if(this.arrAjaxBoxes['sysidAPPSidebarright'].options.defaultStatus == 'max') this.boxStatus['sysidAPPSidebarright'] = 'max';
							if(this.arrAjaxBoxes['sysidAPPSidebarright'].options.defaultStatus == 'close') this.boxStatus['sysidAPPSidebarright'] = 'close'; 
						}
					}
					this.setAllBoxStatus();
				}.bind(this)
			}).request();
		} catch (e) {
		}
	},
	
	/**
	 * send current userprofile to the server
	 */
	updateServer: function(sysid) {
		this.getBoxStatus(sysid);
//		this.ajaxPost = {sysid: this.boxStatus[sysid]};
		try {
			this.ajax = new Ajax (this.url+'&sysid='+sysid, {
				method: 'post',
				data: {sysid: this.boxStatus[sysid]},
				onComplete: function (responseText, responseXML) {
					this.arrAjaxBoxes[sysid].load();
				}.bind(this)
			}).request();
		} catch (e) {
		}
	}
});