function debug(e) {
	var s = t = "";
	if ((typeof(e) == "object") || (typeof(e) == "array"))
		for (var i in e) {
			switch (t = typeof(e[i])) {
				case "function": case "object": case "array": break;
				default: t = e[i];
			}
			s += i + " = " + t + "\n";
		}
	else
		s = e;
	alert(s);
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/* array functions */

Array.prototype.addUnique = function (val) {
	if (val)
		for (i in this)
			if (typeof(this[i]) != "function")
				if (this[i] == val) {
					val = "";
					break;
				}
	if (val)
		this.push(val);
		
	return this;
}

Array.prototype.switchUnique = function (val) {
	var res = new Array;
	
	if ((this.length > 1) || ((this.length == 1) && this[0]))
		if (val)
			for (i in this)
				if (typeof(this[i]) != "function") {
					if (this[i] == val) {
						val = "";
					} else {
						res.push(this[i]);
					}
				}
				
	if (val)
		res.push(val);
		
	return res;
}

Array.prototype.removeUnique = function (val) {
	var res = new Array;
	if (val)
		for (i in this)
			if (typeof(this[i]) != "function")
				if (this[i] != val)
					res.push(this[i]);
	return res;
}

Array.prototype.implode = function (delim) {
	res = "";
	if (this.length)
		for (i in this)
			if (typeof(this[i]) != "function")
				res += toString(this[i]) + delim;

	return res.length ? res.substr(0, res.length - delim.length) : res;
}


// tagedit

tagEdit = {

	processMinus: function() {
		$(this).removeClass("list-minus").addClass("list-plus").unbind("click").click(tagEdit.processPlus).parent().parent().children("ul").hide();
		return false;
	},
	
	processPlus: function() {
		$(this).removeClass("list-plus").addClass("list-minus").unbind("click").click(tagEdit.processMinus).parent().parent().children("ul").show();
		return false;
	},
	
	showToolbox: function() {
		$(this).children("span.toolbox").show();
	},
	
	hideToolbox: function() {
		$(this).children("span.toolbox").hide();
	},

	showTaglist: function(e) {
		$(this).parent().children("div.taglist").show();
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	},
	
	hideTaglist: function() {
		$(".taglist").hide();
	},
	
	removeTag: function(e) {
		var id = $(this).attr("rel");
		var list = $(this).parent().siblings("div.taglist");
		var t = list.parent().children("div.tags");
		var v = list.parent().children("input.values");
		var vals = v.val().split(",").removeUnique(id);

		var a = list.find("a.tagname");
		
		var res = new Array;
		for (i in vals)
			if (typeof(vals[i]) != "function") {
				tx = a.filter("[rel="+vals[i]+"]").text();
				res.push( '<a href="#" rel="' + vals[i] + '" title="Убрать тэг «' + tx + '»">' + tx + '<\/a>' );	
			}
		v.val(vals.join(","));
		t.html(res.join(", "));

		$("div.tags a").unbind("click").click(tagEdit.removeTag);

		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

	},
	
	processTagClick: function(e) {
		var id = $(this).attr("rel");
		var list = $(this).parents("div.taglist");
		var t = list.parent().children("div.tags");
		var v = list.parent().children("input.values");
		var vals = v.val().split(",").switchUnique(id);
		var a = list.find("a.tagname");

		var res = new Array;
		for (i in vals)
			if (typeof(vals[i]) != "function") {
				tx = a.filter("[rel="+vals[i]+"]").text();
				res.push( '<a href="#" rel="' + vals[i] + '" title="Убрать тэг «' + tx + '»">' + tx + '<\/a>' );	
			}
		v.val(vals.join(","));
		t.html(res.join(", "));

		$("div.tags a").unbind("click").click(tagEdit.removeTag);
	},
	
	init: function() {
		$("div.taglist a.list-minus").click(tagEdit.processMinus);
		$("div.taglist a.list-plus").click(tagEdit.processPlus);
		$("div.taglist div.line").mouseover(tagEdit.showToolbox).mouseout(tagEdit.hideToolbox);
		$("div.tags").click(tagEdit.showTaglist).focus(tagEdit.showTaglist);
		
		$("div.taglist a.tagname").click(tagEdit.processTagClick);
		$("div.tags a").click(tagEdit.removeTag);
	
		$("div.taglist").click(function(){return false});
	}

}

//************************
//		tagCloud (x) Sb, 2k8
//

function tagCloud(params) {
	this.init(params);
}

tagCloud.prototype.init = function(params) {
		
	for (var i in params)
		if (typeof(this[i]) != "function")
			this[i] = params[i];
	
	if ($(this.id).length) {
	
		this.tagInput = $(this.input);
		this.tagValues = $(this.values);
		(this.tagCloud = $(this.cloud)).click(function(){return false});
	
		this.a = this.tagCloud.find("a");
	
		$(this.cloud + " a").bind("click", {self:this}, this.clickTag);
		$(this.input + " a").bind("click", {self:this}, this.removeTag);
			
		$(this.input).bind("click", {self:this}, this.show).bind("focus", {self:this}, this.show);
		
		this.update();
		
		$(document).bind("click", {self:this}, this.hide);
	}
}

tagCloud.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this;
	self.tagCloud.show();

	$(document).bind("click", {self:self}, self.hide);
	e.cancelBubble = true;
	e.stopPropagation && e.stopPropagation();
	return false;
}
	
tagCloud.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;
	self.tagCloud && self.tagCloud.hide();

	$(document).unbind("click", self.hide);
	return false;
}
	
tagCloud.prototype.update = function (e) {
	var self = (e && e.data && e.data.self) || this;
	
	var vals = self.tagValues.val().split(",");
	var res = new Array;

	self.a.removeClass("selected");

	for (i in vals)
		if (typeof(vals[i]) != "function") {
			tx = self.a.filter("[rel="+vals[i]+"]").addClass("selected").text();
			res.push( '<a href="#" rel="' + vals[i] + '" title="Убрать тэг «' + tx + '»">' + tx + '<\/a>' );	
		}
		
	self.tagInput.html(res.join(", "));

	$(self.input + " a").unbind("click").bind("click", {self:self}, self.removeTag);
}

tagCloud.prototype.clickTag = function(e) {
	var self = (e && e.data && e.data.self) || this;
	var id = $(this).attr("rel");
	
	self.a.filter("[rel="+id+"]").toggleClass("selected");
	var vals = self.tagValues.val().split(",").switchUnique(id);
	var res = new Array;

	for (i in vals)
		if (typeof(vals[i]) != "function") {
			tx = self.a.filter("[rel="+vals[i]+"]").text();
			res.push( '<a href="#" rel="' + vals[i] + '" title="Убрать тэг «' + tx + '»">' + tx + '<\/a>' );	
		}
		
	self.tagValues.val(vals.join(","));
	self.tagInput.html(res.join(", "));

	$(self.input + " a").unbind("click").bind("click", {self:self}, self.removeTag);
	return false;
}

tagCloud.prototype.removeTag = function(e) {
	var self = (e && e.data && e.data.self) || this;
	var id = $(this).attr("rel");
	
	self.a.filter("[rel="+id+"]").toggleClass("selected");
	var vals = self.tagValues.val().split(",").removeUnique(id);
	var res = new Array;

	for (i in vals)
		if (typeof(vals[i]) != "function") {
			tx = self.a.filter("[rel="+vals[i]+"]").text();
			res.push( '<a href="#" rel="' + vals[i] + '" title="Убрать тэг «' + tx + '»">' + tx + '<\/a>' );	
		}
		
	self.tagValues.val(vals.join(","));
	self.tagInput.html(res.join(", "));

	$(self.input + " a").unbind("click").bind("click", {self:self}, self.removeTag);
	
	e.cancelBubble = true;
	e.stopPropagation && e.stopPropagation();

	return false;
}




/*  (x) Sb, 2k8
 * ---------------------------------------------------------------------------
 *
 * simple dialog manager
 *
 * required: jQuery
 */

iMgr = function(owner) {
	this.owner = owner ? owner : 0;
	this.dlgs = [];
	this.modal = 0;
	this.inAchtung = false;
}

iMgr.prototype.lookfor = function(d) {
	var parent = (d && d.parent) || window;
	
	if (parent.xMgr)
		for (var i in parent.xMgr.dlgs)
			if (parent.xMgr.dlgs[i] == d) return i;
	return false;
}

iMgr.prototype.remove = function(d) {
	var parent = (d && d.parent) || window;
	var res = [];
	
	if (parent.xMgr && parent.xMgr.dlgs && parent.xMgr.dlgs.length) {
		for (var i in parent.xMgr.dlgs)
			if (parent.xMgr.dlgs[i] != d) res.push(parent.xMgr.dlgs[i]);
		parent.xMgr.dlgs = res;
	}
}

iMgr.prototype.registerDialog = function(d) {
	var parent = (d && d.parent) || window;
	if (parent.xMgr)
		if (parent.xMgr.lookfor(d) === false)
			parent.xMgr.dlgs.push(d);
	return d;
}
	
iMgr.prototype.unregisterDialog = function(d) {
	var parent = (d && d.parent) || window;
	if (parent.xMgr)
		parent.xMgr.remove(d);
}

iMgr.prototype.achtung = function() { xMgr.inAchtung = true }
iMgr.prototype.unachtung = function() { xMgr.inAchtung = false }
	
iMgr.prototype.show = function(d) {

	if (xMgr.inAchtung) return true;
	
	var self = (d && d.data && d.data.self) || d;
	var parent = (self && self.parent) || window;

	if (self && (self.visible === false)) {
		if (parent.xMgr && parent.xMgr.dlgs && parent.xMgr.dlgs.length)
			for (var i in parent.xMgr.dlgs)
				if ( (typeof(parent.xMgr.dlgs[i]) != "function") && (parent.xMgr.dlgs[i] != self))
					parent.xMgr.dlgs[i].hide();

		self.show(d);
	}
	return false;
}
	
iMgr.prototype.hide = function(d) {
	
	if (xMgr.inAchtung) return true;
	
	var self = (d && d.data && d.data.self) || (d && d.hide && d), temp;
	var me = (self && self.parent && self.parent.xMgr) || (this && this.dlgs && this) || window.xMgr;

	if (self && (typeof(self.hide) == "function")) {	// closing specified dialog
		self.hide(d);
		return false;
	}

	if (me && me.dlgs && me.dlgs.length) {
		for (var i in me.dlgs)
			if (typeof(me.dlgs[i]) != "function") {
				me.dlgs[i].hide();
			}
	}

	return false;
}

iMgr.prototype.setModal = function(d) {
	var me = (this && this.dlgs && this) || window.xMgr;
	me.modal = d;
	me.owner && me.owner.parent && me.owner.parent.xMgr && me.owner.parent.xMgr.setModal(d);
}

iMgr.prototype.hideModal = function(d) {

	if (xMgr.inAchtung) return true;
	
	var self = (d && d.data && d.data.self) || (d && d.hide && d), temp;
	var me = (self && self.xMgr) || (this && this.dlgs && this) || window.xMgr;

	if (temp = me.modal)	// closing topmost modal dialog
		me.modal.hide(me.modal);
		
	return false;
}

/* xMgr initialization 
 *
 * required: jQuery
 */
 
xMgr = new iMgr();
jQuery(function($){ $(document).click(function(e){ xMgr.hide() }) })



/*  (x) Sb, 2k8
 * ---------------------------------------------------------------------------
 *
 * simple dialog template
 *
 * required: jQuery
 */

iDlg = function (params) {
	this.init(params);
}

iDlg.prototype = {

	prepare : function (params) {
		this.prepared = true;

		if (params)
			for (var i in params)
				if (typeof(this[i]) != "function")
					this[i] = params[i];

		if (this.id)
			(this.me = $(this.id)).attr("self", this);

		if (typeof(params['modal']) == "undefined") this.modal = true;	// dialog is modal by default

		this.parent = this.parent || window;
		this.xMgr = new iMgr(this);

		this.win = $(window);
		
		this.visible = false;
		
		this.ph = params['ph'] || 'def';
	},

	init : function (params) {
		if (!this.prepared) 
			if (params)	this.prepare(params); else	return;
	
		this.phase(this.ph);

		this.btActivator && $(this.btActivator).bind('click', {self:this, phase:this.ph}, this.xMgr.show);
		
		this.btActivatorAlt && $(this.btActivatorAlt).bind('click', {self:this, phase:this.ph}, this.xMgr.show);

		$(this.id).bind("click", {self:this}, function(e) {
			e.data.self.xMgr && e.data.self.xMgr.hide();
			e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
			return true;
		});

		this.parent.xMgr.registerDialog(this);
	},
	
	show : function (e) {	// !handler
	
		if (xMgr.inAchtung) return true;

		var self = (e && e.data && e.data.self) || this;
		var ph = (e && e.data && e.data.phase) || "def";
		if (self && (self.visible === false) ) {
			self.btActivator && $(self.btActivator).unbind('click').bind("click", {self:self}, self.hide);
			if (self.phase(ph)) {	// default phase
				$(self.id).show(); 
				self.visible = true;
			}
		}
		if (self.modal) self.xMgr.setModal(self);
		return false;
	},
	
	hide : function (e) {	// !handler
	
		if (xMgr.inAchtung) return true;
	
		var self = (e && e.data && e.data.self) || this;
		var ph = (e && e.data && e.data.phase) || "def";

		if (self.xMgr) self.xMgr.hide();
		
		if (self && (self.visible === true) ) {
			$(self.id).hide(); 
			self.btActivator && $(self.btActivator).unbind('click').bind("click", {self:self, phase:ph}, xMgr.show);
			self.visible = false;
		}
		
		if (self.modal) 
			self.xMgr.setModal(self.nextModal());
			
		return false;
	},

	phase : function(p) {
	
		if (xMgr.inAchtung) return true;
		
		$(this.id + " .diags p").hide();
		$(this.id + " .diags " + this.pre + "_" + p).show();
		return true;	
	},
	
	processInputClick : function () {
	
		if (xMgr.inAchtung) return true;
		
		$(this).css("color", "#000");
		if ($(this).val() == $(this).attr("defValue")) $(this).val('');
	},
	
	processInputBlur : function () {
	
		if (xMgr.inAchtung) return true;
		
		var c = $(this).val(), v = $(this).attr("defValue");
		if ( !c || (c == v) ) 
			$(this).val(v).css("color", "#888");
		else
			$(this).css("color", "#000");
	},
	
	centerMe : function (e) {
		var self = (e && e.data && e.data.self) || this, temp;
	
		self.st = self.win.scrollTop();	self.sl = self.win.scrollLeft();
		self.wh = self.win.height();		self.ww = self.win.width();
		
		self.h = self.me.height();
		self.w = self.me.width();
	
		self.top = ((temp = self.st + (self.wh - self.h) / 2 ) > 0 ? temp : 0);
//		self.left = ((temp = sl + (ww - self.w) / 2 ) > 0 ? temp : 0);
	
//		self.me.css({top: self.top + "px", left: self.left + "px"});
		self.me.css({top: self.top + "px", left: "50%", margin: "0 0 0 " + (-self.w / 2) + "px"});
	},
	
	nextModal : function() {
		return (this.parent != window) && ((this.parent.modal && this.parent) || this.parent.nextModal());
	},

	xVal : function(n) { var v = $(n).val(), d = $(n).attr("defValue") || ""; return (v && (v != d)) ? v : ""; }
}



/*  (x) Sb, 2k9
 * ---------------------------------------------------------------------------
 *
 * dynamic image viewer
 *
 */

iImageViewer = function (params) {
	this.superClass.prepare.call(this, params);

	this.btClose = this.btClose || this.id + " .close";
	$(this.btClose).bind("click", {self:this}, function(e) {
		var self = (e && e.data && e.data.self) || this;
		self.xMgr.hide(self);
		e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
	});

	this.image = this.timer = 0;
	this.state = "";
	
	
	this.superClass.init.call(this);
}

iImageViewer.prototype = new iDlg();
iImageViewer.prototype.superClass = iDlg.prototype;
iImageViewer.prototype.constructor = iImageViewer;

iImageViewer.prototype.phase = function(p) {
	return true;	
}

iImageViewer.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this, temp;

	clearTimeout(self.timer);

	if ((self.state == "loading") || (self.state == "fail")) {

		self.state = "show";

		self.image.show();
		self.centerMe();
		self.image.hide();

		self.superClass.show.call(self, e);
		
		self.image.fadeIn(400);
		
		
		
		$("#iImageViewer-preloader").remove();
	}
	
	return false;
}

iImageViewer.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible || (self.state == "fail")) {
		self.superClass.hide.call(self, e);
		$(window).unbind("resize", self.fixResize);
		
		self.preloader && self.preloader.remove() && (self.preloader = 0);
		$("#iImageViewer-fader").remove();
		self.state = "";
	}

//	$(".fader").height($(document).height())	
	return false;
}

iImageViewer.prototype.fixResize = function(e) {
	var self = (e && e.data && e.data.self) || this;
	self.centerMe();
}

iImageViewer.prototype.view = function(src, w, h) {
	var self = this, obj = $(self.id), z = obj.css("z-index"), size = (w && h) ? ' width="'+w+'" height="'+h+'"' : '';

	obj.before('<div id="iImageViewer-fader" class="fader"></div><div id="iImageViewer-preloader" class="preloader"></div>');
	
	(self.preloader = $("#iImageViewer-preloader")).css("z-index", z-1).show();
	$(".fader").height($(document).height()).css("z-index", z-2).bind("click", {self:self}, xMgr.hide).fadeIn(100);

	self.preH = self.preloader.height();
	self.preW = self.preloader.width();
	
	self.centerMe();
	
	self.preloader.hide();

	$(window).bind("resize", {self:self}, self.fixResize);

	if (self.image) {
		self.image.attr("src", src);
		if (w && h)
			self.image.attr("width", w).attr("height", h);
	} else {
		obj.prepend('<img class="current" src="'+src+'"'+size+'>');
		(self.image = $(self.id + " .current")).bind("load", {self:self}, self.imgLoaded).bind("click", {self:self}, xMgr.hide).noContext().hide();
	}

	self.timer = setTimeout("xViewer.maybeFail()", 200);
	self.state = "loading";

	return false;
}

iImageViewer.prototype.maybeFail = function(e) {
	if (this.state = "loading") {
		this.state = "fail";
		this.preloader.fadeIn(400);
//		xMgr.hide();
	}
}

iImageViewer.prototype.imgLoaded = function(e) {
	var self = (e && e.data && e.data.self) || this;
	setTimeout("xViewer.show()", 50);
}

iImageViewer.prototype.centerMe = function (e) {
		var self = (e && e.data && e.data.self) || this, temp;

		self.superClass.centerMe.call(self, e);

		if (self.preloader) {
			var t = ((temp = self.st + (self.wh - self.preH) / 2 ) > 0 ? temp : 0);
			self.preloader.css({top: t + "px", left: "50%", margin: "0 0 0 " + (-self.preW / 2) + "px"});
		}
}


