var ghmo = {
	path: "",
	values: {},
	action: "",
	modul: "",
	recaptcha_public_key: "",
	/* THX to WW */
	ajax: function(settings){		
		var alertBool = settings.abortError||true;
		var callback = Prototype.emptyFunction;
		var callbackError = Prototype.emptyFunction;
		
		if(Object.isFunction(settings.onSuccess))callback = settings.onSuccess.bind(this);
		if(Object.isFunction(settings.onError)){
			callbackError = settings.onError.bind(this);
			alertBool = false;
		}
		
		new Ajax.Request(settings.path,{
			parameters:settings.parameters,
			onSuccess: function(t){		
				var response = t.responseText;
				if(response.isJSON()){
					response = response.evalJSON();
					if(response.error){
						response.error = response.error.gsub("<br>","\n").gsub("<br />","\n").strip();
						if(alertBool)alert(response.error);						
						callbackError(response);
					} else if(callback){
						callback(response);
					}
				} else {
					callbackError(response,t.responseText);
				}
			}
		});
	},
	showCaptcha: function(){
		Recaptcha.create(this.recaptcha_public_key, "dynamic_recaptcha", {
	        theme: "white",
	        tabindex: 0,
	        callback: Recaptcha.focus_response_field
		});
	},	
	validateComment: function(element){
		this.values.text = this.nl2br($$('textarea[name="new_comment"]')[0].value.stripScripts().stripTags());
		this.action = "new_comment";
		this.modul = "comments";
		
		if(this.values.text != ""){
			var fieldset = element.up("fieldset",0).select("input").each(function(e,i){
				this.values[e.name] = e.value;
			}.bind(this));
			
			//$("validate_text").update(this.values.text);
			this.showCaptcha();
			$("validate_layer").show();
		}
	},
	validateSecret: function(element){
		this.values.text = this.nl2br($$('textarea[name="new_secret"]')[0].value.stripScripts().stripTags());
		this.action = "new_secret";
		this.modul = "secrets";
		
		if(this.values.text != ""){
			//$("validate_text").update(this.values.text);
			this.showCaptcha();
			$("validate_layer").show();
		}
	},
	clear: function(bool){
		if(bool){
			$$('textarea[name="' + this.action + '"]')[0].value = "";
		}
		this.action	= "";
		this.modul = "";
	},
	close: function(bool){
		$("validate_layer").hide();
		this.values = {};
		this.clear(bool);
		if(bool == true){
			location.reload();
		}
	},
	save: function(){
		var json = {
				action: this.action,
				secret: this.new_secret,
				challenge: $("recaptcha_challenge_field").value,
				response: $("recaptcha_response_field").value,
				values: this.values
			};
		var settings = {
			path: this.path + "/"+ this.modul +"/ajax",
			parameters: {json:Object.toJSON(json)},
			onSuccess:function(json){
				this.close(true);
			}.bind(this),
			onError: function(json){
				this.close(false);
				alert(json.error);
			}.bind(this)
		};
		
		this.ajax(settings);
	},
	nl2br: function(str){
		return str.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '<br />');
		// http://phpjs.org/functions/nl2br:480
	}
}

function bookmark(url, title){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all) // ie
		window.external.AddFavorite(url, title);
	else
		alert('Your browser does not support this function.');
}
