/**
 * Inheritance plugin 1.0.7
 *
 * Copyright (c) 2009 Filatov Dmitry (alpha@zforms.ru)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function($) {

var
	hasIntrospection = (function(){_}).toString().indexOf('_') > -1,
	emptyFn = function() {}
	;

$.inherit = function() {

	var
		hasBase = $.isFunction(arguments[0]),
		base = hasBase? arguments[0] : emptyFn,
		methods = arguments[hasBase? 1 : 0],
		statical = arguments[hasBase? 2 : 1],
		result = function() {
			if(this.__constructor) {
				this.__constructor.apply(this, arguments);
			}
		},
		inheritance = function() {}
		;

	$.extend(result, base, statical);

	inheritance.prototype = base.prototype;

	result.prototype = new inheritance();
	result.prototype.constructor = result;
	result.prototype.__self = result;

	var _methods = [];
	$.each(methods, function(i) {
		if(methods.hasOwnProperty(i)) {
			_methods.push(i);
		}
	});
	// fucking ie hasn't toString in for
	if(methods.toString && $.inArray('toString', _methods) == -1) {
		_methods.push('toString');
	}
	$.each(_methods, function() {
		if(hasBase && $.isFunction(base.prototype[this]) && $.isFunction(methods[this]) &&
			(!hasIntrospection || methods[this].toString().indexOf('.__base') > -1)) {

			(function(methodName) {
				var
					__baseMethod = base.prototype[methodName],
					__overrideMethod = methods[methodName]
					;
				result.prototype[methodName] = function() {
					var __baseSaved = this.__base;
					this.__base = __baseMethod;
					var result = __overrideMethod.apply(this, arguments);
					this.__base = __baseSaved;
					return result;
				};
			})(this);

		}
		else {
			result.prototype[this] = methods[this];
		}
	});

	return result;

};

})(jQuery);