/**
 * WJCProfileManager manages the profile for a CHAP
 *
 * @since Tue Jan 27 2009
 * @author Niels Nijens
 **/
var WJCProfileManager = Class.create({

	/**
	 * initialize
	 *
	 * Creates a new WJCProfile
	 *
	 * @since Tue Jan 27 2009
	 * @access public
	 * @param string name
	 * @param object profile
	 * @return void
	 **/
	initialize: function(name, profile, manager) {
		this._cookie = new WJCookie();
		this._cookiename = name || "chap";
		this._profile = this._cookie.get("profile_" + this._cookiename) || profile || {};
		this._manager = manager;

		if (this._cookie.get(this._cookiename) == undefined || this._cookie.get("profile_" + this._cookiename) == undefined) {
			this.updateProfile();
		}
	},

	/**
	 * updateProfile
	 *
	 * Shows the blocks for the currently set profile
	 *
	 * @since Thu Jan 29 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param object profile
	 * @return void
	 **/
	updateProfile: function() {
		if (this._manager) {
			if (!this._manager._blocks) {
				this.updateProfile.bind(this).delay(0.2);
				return;
			}

			for (var i = 0; i < this._manager._blocks.length; i++) {
				var showing = this._manager._blocks[i].isVisible();
				this._manager._blocks[i].setVisibleByProfile(this._profile);

				if (this._manager._blocks[i].isVisible() && !showing) {
					this._manager._showBlock(this._manager._blocks[i], null);
				}
				else if (!this._manager._blocks[i].isVisible() && showing) {
					this._manager._hideBlock(this._manager._blocks[i]);
				}
			}
			this._manager._draggables.restart();
		}
		this.save();
	},

	/**
	 * showProfiling
	 *
	 * Shows the profiling form
	 *
	 * @since Tue Jan 27 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param htmlelement elem
	 * @param string dt
	 * @return void
	 **/
	showProfiling: function(elem, dt) {
		var dt = dt || "profile";
		var spin = new WJSpin();
		this._profileElement = elem;
		spin.content(new WJUrl({
			ct: "wmdynamic",
			module: "Wmchapblock",
			mode: "profile",
			dt: dt,
			"uft[]": ["chapprofile"],
			"wmtrigger[]": ["requestufts"]
		}), [this._profileElement, this.activateProfileElements.bind(this)]);
	},

	/**
	 * activateProfileElements
	 *
	 * Activates event on the profile form to update the blocks
	 *
	 * @since Tue Jan 27 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param document response
	 * @return void
	 **/
	activateProfileElements: function(response) {
		if (response == undefined) {
			this._profileElement.select("select").each(function(s) {
				$(s).observe("change", this.profileValueChange.bind(this, s) );
				if (this._profile[s.name]) {
					$(s).value = this._profile[s.name];
				}
			}.bind(this) );
			this._profileElement.select("input[type = 'checkbox']").each(function(s) {
				this.fixCheckValue(s);
				$(s).observe("change", this.blockVisibleChange.bind(this, s) );
			}.bind(this) );

			if (this._profile["style"] != undefined) {
				this._profileElement.select("input[type = 'radio']").each(function(s) {
					if (this._profile.style[s.name] == s.value) {
						s.checked = true;
					}
					else {
						s.checked = false;
					}
					$(s).observe("click", this.blockStyleChange.bind(this, s) );
				}.bind(this) );

				if ( $("style_uploadwidget") && window["WJCProfileStyle"] != undefined) {
					this.uploader = new WJCProfileStyle("style_uploadwidget", this.styleBackgroundChange.bind(this) );
				}
			}
		}
		else {
			this.activateProfileElements.bind(this).delay(0.25);
		}
	},

	/**
	 * blockStyleChange
	 *
	 * Changes the background of a container
	 *
	 * @since Thu Jan 29 2009
	 * @access public
	 * @param object response
	 * @return void
	 **/
	styleBackgroundChange: function(response) {
		if ( $("chapprofile_style") ) {
			$("chapprofile_style").setStyle({backgroundImage: "url('" + response.image + "')"});
			$("chapprofile_style").addClassName("customimage");
		}
		this._profile.style["image"] = response.image;
		this.save();
	},

	/**
	 * blockStyleChange
	 *
	 * Changes the styles of the blocks
	 *
	 * @since Thu Jan 29 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param htmlelement elem
	 * @return void
	 **/
	blockStyleChange: function(elem) {
		if ( $("chapprofile_style") ) {
			$("chapprofile_style").removeClassName(this._profile.style[elem.name]);
			$("chapprofile_style").addClassName(elem.value);
			this._profile.style[elem.name] = elem.value;

			if (elem.name == "background") {
				$("chapprofile_style").removeClassName("customimage");
				$("chapprofile_style").setAttribute("style", "");
				this._profile.style["image"] = "";
			}

			this.save();
		}
	},

	/**
	 * blockVisibleChange
	 *
	 * Toggles the visibility of a block
	 *
	 * @since Thu Jan 29 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param htmlelement elem
	 * @return void
	 **/
	blockVisibleChange: function(elem) {
		var blockid = elem.id.replace(/^check_/, "");
		for (var i = 0; i < this._manager._blocks.length; i++) {
			if (this._manager._blocks[i]._id == blockid) {
				if (this._manager._blocks[i].isVisible() && !elem.checked) {
					this._manager._hideBlock(this._manager._blocks[i]);
				}
				else if (!this._manager._blocks[i].isVisible() && elem.checked) {
					this._manager._showBlock(this._manager._blocks[i]);
				}
			}
			this.save();
		}
		this._manager._draggables.restart();
	},

	/**
	 * fixCheckValue
	 *
	 * Fixes the visibility checkbox value
	 *
	 * @since Thu Jan 29 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param htmlelement elem
	 * @return void
	 **/
	fixCheckValue: function(elem) {
		var blockid = elem.id.replace(/^check_/, "");
		if (this._manager.blockLoaded(blockid) ) {
			if ($(blockid) ) {
				elem.checked = true;
			}
			else {
				elem.checked = false;
			}
		}
		else {
			this.fixCheckValue.bind(this, elem).delay(0.25);
		}
	},

	/**
	 * profileValueChange
	 *
	 * Changes a setting in the current profile
	 *
	 * @since Tue Jan 27 2009 (copied from WJCBlockManager)
	 * @access public
	 * @param htmlelement elem
	 * @return void
	 **/
	profileValueChange: function(elem) {
		this._profile[elem.name] = elem.value;

		$$("*[name = '" + elem.name + "']").each(function(s) {
			if(this !== s) {
				if (typeof(s.options) == "object") {
					for (i = 0; i < s.options.length; i++) {
						if (s.options[i].value.toLowerCase() == this.getValue().toLowerCase() ) {
							s.selectedIndex = i;
						}
					}
				}
				else {
					s.value = this.value;
				}

				var otherform = s.up("form");
				if (typeof(otherform) != "undefined" ) {
					otherform.submit();
				}
			}
			if (typeof(s.filterFunction) == "function") {
				s.filterFunction();
			}
		}.bind(elem) );
		this.updateProfile();
		this.save();
	},

	/**
	 * save
	 *
	 * Saves the current settings to the cookie
	 *
	 * @since Tue Jan 27 2009 (copied from WJCBlockManager)
	 * @access public
	 * @return void
	 **/
	save: function() {
		if (this._manager) {
			this._manager._updateSettings();
		}
		this._cookie.set("profile_" + this._cookiename, this._profile);
	}
});