/* Author: James Haigh */

var Core = Core || {};

Core.force = [];

Core.forms = function() {
	$('input').each(function() {
		var $this = $(this);
		var type = $this.attr('type');
		var class_name = 'type_' + type;

		if(!$this.hasClass(class_name))
		{
			$this.addClass(class_name);
		}
	});

	var $clever_inputs = $('input.clever_input');
	if($clever_inputs.length)
	{
		$clever_inputs.each(function() {
			$(this).addClass('inactive').focus(function() {
				var $this = $(this);
				$this.addClass('active').removeClass('inactive');
				var original_value = $this.attr('original_value') || false;
				if(original_value)
				{
					var current_value = $this.attr('value');
					if(current_value == original_value || current_value == '')
					{
						$this.attr('value', '');
					}
				}
				else
				{
					$this.attr('original_value', $this.attr('value')).attr('value', '');
				}
			}).blur(function() {
				var $this = $(this);
				$this.removeClass('active').addClass('inactive');
				if($this.attr('value') == '')
				{
					$this.attr('value', $this.attr('original_value'));
				}
			});
		});
	}
};

Core.modalWindow = function(data) {
	var data = data || {};

	if(typeof data == 'string')
	{
		data = {href: data};
	}

	if(!data.type)
	{
		data.type = 'ajax';
	}

	if(!data.margin)
	{
		data.margin = 0;
	}

	if(!data.padding)
	{
		data.padding = 0;
	}

	if(!data.transitionIn)
	{
		data.transitionIn = 'none';
	}

	if(!data.transitionOut)
	{
		data.transitionOut = 'none';
	}

	$.fancybox(data);
};

Core.hooks = Core.hooks || [];
Core.hookData = Core.hookData || [];

Core.hook = function(fn, data) {
	Core.hooks.push(fn);
	Core.hookData.push(data);

	return Core;
};

(function() {
	for(var fn in Core.force)
	{
		Core.force[fn]();
	}
})();

Core.init = function () {
	Core.forms();

	if(Core.hooks)
	{
		for(i in Core.hooks)
		{
			Core.hooks[i](Core.hookData[i]);
		}
	}
};

$(Core.init);

String.prototype.endsWith = function (suffix) {
  return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function(prefix) {
  return (this.substr(0, prefix.length) === prefix);
}

String.prototype.rot13 = function(){ //v1.0
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};
var_dump = print_r;
