(function() {
	this.J = {};
	if (!String.prototype.escape) {
		var ESCAPE_SYMBOLS = {
			'&' : '&amp;',
			'<' : '&lt;',
			'>' : '&gt;',
			'"' : '&quot;'
		};
		var ESCAPE_REGEXP = [];
		for (var i in ESCAPE_SYMBOLS) {
			ESCAPE_REGEXP.push({
						regexp : new RegExp(i, 'g'),
						replace_symbol : ESCAPE_SYMBOLS[i]
					});
		}
		/**
		 * Escapes a string.
		 */
		String.prototype.escape = function() {
			var i;
			// var e ={'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'};
			var t = this;
			for (var i = 0, len = ESCAPE_REGEXP.length; i < len; i++) {
				var regexp = ESCAPE_REGEXP[i].regexp;
				var replace_symbol = ESCAPE_REGEXP[i].replace_symbol;
				t = t.replace(regexp, replace_symbol);
			}
			return t;
		};
	}
	J.Class = function() {};
	J.Class.init = function() {
		if (this.init && typeof this.init == 'function') {
			this.init.apply(this, arguments);
		}
	}
	J.Class.extend = function(props) {
		function prototype() {};
		prototype.prototype = this.prototype;
		prototype = new prototype();
		
		var __super__ = this.prototype;
		var fnTest = /xyz/.test(function() {
					xyz;
				}) ? /\b_super\b/ : /.*/;

		for (var name in props) {
			prototype[name] = name != 'constructor'
					&& typeof props[name] == 'function'
					&& typeof __super__[name] == 'function'
					&& fnTest.test(props[name]) ? (function(name, fn) {
				return function() {
					var tmp = this._super;
					this._super = __super__[name];
					var r = fn.apply(this, arguments);
					this._super = tmp;
					return r;
				};
			})(name, props[name]) : props[name];
		}
		function Class() {
			J.Class.init.apply(this, arguments);
		};

		__super__.constructor = this;

		Class.__super__ = __super__;
		Class.prototype = prototype;
		Class.prototype.__super__ = __super__;
		Class.prototype.constructor = Class;

		Class.extend = J.Class.extend;
		
		return Class;
	}
	J.copy = function(obj) {
		// shallow objects copy
		var new_obj = (obj instanceof Array) ? [] : {};
		for (prop in obj) {
			new_obj[prop] = obj[prop]
		}
		return new_obj;
	}
	J.observable_manager = {
		listeners : {},
		attach_listener : function(type, func) {
			if (!type || !func)
				return this;
			if (!this.listeners)
				this.listeners = {};
			if (!this.listeners[type])
				this.listeners[type] = [];
			this.listeners[type].push({
						func : func
					});
			return this;
		},
		detach_listener: function(type, func) {
			if (!type || !this.listeners[type])
				return this;
			if (func) {
				// create a function to test an event
				var test = function(func) {
					if (this.func == func)
						return true;
					return false;
				}
				// go through all added listeners of a specified type
				for (var i = 0, len = this.listeners[type].length; i < len; i++) {
					var listener = this.listeners[type][i];
					if (test.apply(listener, [func])) {
						this.listener[type].splice(i, 1);
						i -= 1;
						len -= 1;
					}
				}
			} else {
				this.listener[type] = [];
			}
			return this;
		},
		notify: function(type, data) {
			if (!this.listeners || !this.listeners[type])
				return;
			for (var i = 0, len = this.listeners[type].length; i < len; i++) {
				var func = this.listeners[type][i].func;
				if (typeof func != 'function')
					continue;
				func_r = (data) ? func(data) : func();
			}
		}
	}
}());


(function() {
	this.utils = {};
	utils.format_currency = function(c, b_add_copeaks) {
		c = parseFloat(c);
		c = Math.floor(c * 100 + 0.50000000001);
		var intCopeaks = c % 100;
		var strCopeaks = intCopeaks.toString();
		c = Math.floor(c / 100).toString();
		if (intCopeaks < 10)
			strCopeaks = "0" + strCopeaks;
		for (var i = 0; i < Math.floor((c.length - (1 + i)) / 3); i++) {
			c = c.substring(0, c.length - (4 * i + 3)) + ' '
					+ c.substring(c.length - (4 * i + 3));
		}
		if (b_add_copeaks) {
			return (c + ',' + strCopeaks);
		} else {
			return (c);
		}
	}
}());


var lightbox = (function(){
	return {
		get_box: function(){
			var box = $('#lightbox');
			if (!box.length){
				this.append_box();
				this.bind_box();
				return this.get_box();
			}
			return box;
		},
		append_box: function(){
			$('body').append('<div id="lightbox" class="lightbox" />');
		},
		bind_box: function(){
			this.get_box().click(function(){$(this).hide()});
		},
		unbind_tbox: function(){
			this.get_box().unbind();
		},
		show_image_click: function(e){
			var fullsrc = this.getAttribute('fullsrc');
			lightbox.show_image(fullsrc);
		},
		show_image: function(fullsrc){
			var r = [];
			r.push('<div id="lightbox" class="lightbox" title="Закрыть">');
			r.push('<div class="lightbox-shadow"></div>');
			r.push('<div class="lightbox-content">');
				r.push('<div class="lightbox-image-box">');
				r.push('<img src="' + fullsrc + '" />');
				r.push('</div>');
			r.push('</div>');
			r.push('</div>');

			var box = this.get_box();
			
			box.html(r.join(''));
			box.fadeIn('normal');
		}
	}
}());
