
var userPrefereces = {
	JSON: { prefereces: {} },
	onLoad: null,
	settings: {
		controller: '/user_json.php'
	},
	getJSON: function(params) {
		var jsonUrl = this.settings.controller;
		if (params) jsonUrl += '?' + params;
		$.getJSON(jsonUrl, function(data) {
			userPrefereces.processingJSON(data);
		});
	},
	processingJSON: function(JSON) {
		if ($.sfJson.parse(JSON)) {
			this.JSON = $.extend(true, this.JSON, JSON);
			if (this.onLoad) this.onLoad();
			this.onLoad = null;
		}
	},
	init: function(onLoad) {
		if (typeof onLoad === 'function') this.onLoad = onLoad;
		this.getJSON();
	},
	set: function(param, value) {
		value = escape(value);
		this.JSON.prefereces[param] = value;
		this.getJSON(param + '=' + value);
	},
	get: function(param) {
		if (typeof this.JSON.prefereces[param] === 'undefined')
			return null;
		else return unescape(this.JSON.prefereces[param]);
	}
};

var script = {
	js: [],
	get: function(url, success) {
		if ($.inArray(url, this.js) < 0) {
			this.js.push(url);
			$.getScript(url, ($.isFunction(success)?success:null));
		}
	}
}

function initAttrFromRel(selector) {
	var el = $(selector);
	if (!el.data('inited')) {
		var str = el.attr('rel');
		el.removeAttr('rel');
		var lines = str.split(';');
		for (line in lines) {
			var v = lines[line].split(':');
			el.data(v[0], v[1]);
		}
		el.data('inited', true);
	}
}

function objFromRel(selector) {
	var obj = {};
	var el = $(selector);
	var str = el.attr('rel');
	el.removeAttr('rel');
	if (typeof str !== 'undefined') {
		var lines = str.split(';');
		for (line in lines) {
			var v = lines[line].split(':');
			obj[v[0]] = v[1];
		}
	}
	return obj;
}

function secureSubmit (selector) {
	$(selector)
		.attr('action', $(selector).attr('rel'))
		.submit();
}

function fillVariables (string, variables) {
	for (variable in variables)
		string = string.split('#' + variable + '#').join(variables[variable]);
	return string;
}

function formHtmlTagsRemove (form) {
	var tagsRemoved = false;
	$(form).find('input:text, textarea').each(function() {
		var noTags = $(this).val().replace(/<[a-zA-Z\/][^>]*>/g, '');
		if (noTags != $(this).val()) tagsRemoved = true;
		$(this).val(noTags);
	});
	if (tagsRemoved) {
		$.sfWindow.alert(i18n.iface.alert, i18n.iface.htmlTagsRemovedAlert);
		return false;
	} else return true;
}

function simpleFormatTitle (title) {
var formatTrans = {
	'|': '<br />',
	' *': ' <b>',
	'* ': '</b> ',
	'*.': '</b>.'
	};
	for (format in formatTrans)
		title = title.split(format).join(formatTrans[format]);
	return title;
}

function rand() {
	return Math.ceil(1000*Math.random());
}

function wordwrap( str, width, brk, cut ) {
	brk = brk || '\n';
	width = width || 75;
	cut = cut || false;
	if (!str) { return str; }
	var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
	return str.match( RegExp(regex, 'g') ).join( brk );
}

