var Tino = Tino || {
	'settings' : {},
	'behaviors' : {},
	'themes' : {},
	'locale' : {}
};

Tino.jsEnabled = document.getElementsByTagName && document.createElement
		&& document.createTextNode && document.documentElement
		&& document.getElementById;

Tino.attachBehaviors = function(context) {
	context = context || document;
	if (Tino.jsEnabled) {
		// Execute all of them.
		jQuery.each(Tino.behaviors, function() {
			this(context);
		});
	}
};
Tino.checkPlain = function(str) {
	str = String(str);
	var replace = {
		'&' : '&amp;',
		'"' : '&quot;',
		'<' : '&lt;',
		'>' : '&gt;'
	};
	for ( var character in replace) {
		var regex = new RegExp(character, 'g');
		str = str.replace(regex, replace[character]);
	}
	return str;
};
Tino.t = function(str, args) {

	if (args) {
		// Transform arguments before inserting them
		for ( var key in args) {
			switch (key.charAt(0)) {
			// Escaped only
			case '@':
				args[key] = Tino.checkPlain(args[key]);
				break;
			// Pass-through
			case '!':
				break;
			// Escaped and placeholder
			case '%':
			default:

				break;
			}
			str = str.replace(key, args[key]);
		}
	}
	return str;
};
Tino.freezeHeight = function() {
	Tino.unfreezeHeight();
	var div = document.createElement('div');
	$(div).css( {
		position : 'absolute',
		top : '0px',
		left : '0px',
		width : '1px',
		height : $('body').css('height')
	}).attr('id', 'freeze-height');
	$('body').append(div);
};

/**
 * Unfreeze the body height
 */
Tino.unfreezeHeight = function() {
	$('#freeze-height').remove();
};
Tino.parseJson = function(data) {
	if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
		return {
			status : 0,
			data : data.length ? data : Tino.t('Unspecified error')
		};
	}
	return eval('(' + data + ');');
};
Tino.theme = function(func) {
	for ( var i = 1, args = []; i < arguments.length; i++) {
		args.push(arguments[i]);
	}

	return (Tino.theme[func] || Tino.theme.prototype[func]).apply(this, args);
};
Tino.ahahError = function(xmlhttp, uri) {
	if (xmlhttp.status == 200) {
		if (jQuery.trim($(xmlhttp.responseText).text())) {
			var message = Tino.t("An error occurred. \n@uri\n@text", {
				'@uri' : uri,
				'@text' : xmlhttp.responseText
			});
		} else {
			var message = Tino.t(
					"An error occurred. \n@uri\n(no information available).", {
						'@uri' : uri,
						'@text' : xmlhttp.responseText
					});
		}
	} else {
		var message = Tino.t("An HTTP error @status occurred. \n@uri", {
			'@uri' : uri,
			'@status' : xmlhttp.status
		});
	}
	return message;
}
if (Tino.jsEnabled) {
	// Global Killswitch on the <html> element
	$(document.documentElement).addClass('js');
	// 'js enabled' cookie
	document.cookie = 'has_js=1; path=/';

	// Attach all behaviors.
	$(document).ready(function() {
		Tino.attachBehaviors(this);
	});
}



