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);
}

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 = {

	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];
			
	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();
}

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;
	
		var st = self.win.scrollTop(), sl = self.win.scrollLeft(), wh = self.win.height(), ww = self.win.width();
		self.h = self.me.height();
		self.w = self.me.width();
	
		self.top = ((temp = st + (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"});
	},
	
	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
 * ---------------------------------------------------------------------------
 *
 * Cart processing
 *
 */

iCart = function (params) {
	this.superClass.init.call(this, params);
	
	this.hTotal = this.hTotal || ".cartTotal";
	
	this._title = $(this.id + " .title");
	this._total = $(this.hTotal);
	this._contents = $(this.id + " .contents ." + this.hActivePhase);
	this._buttons = $(this.id + " .buttons ." + this.hActivePhase);
	this._btLeft = $(this.id + " .btLeft").filter('.' + this.hActivePhase);
	this._btRight = $(this.id + " .btRight").filter('.' + this.hActivePhase);
	this._image = $(this.hImage);

	$.ajax({	url: "/cart/get-total/", success: this.update, type: "POST", cache: false, data: { l0l: 169083 }, dataType: "json" });
	
	$(this.order + " .field").bind("click", {self:this}, this.processInputClick).bind("focus", {self:this}, this.processInputClick).bind("blur", {self:this}, this.processInputBlur).blur();
	(this._orderFields = $(this.order + " .required")).bind("blur", {self:this}, this.checkOrderForm).bind("change", {self:this}, this.checkOrderForm).bind("keypress", {self:this}, this.checkOrderForm);
}

iCart.prototype = new iDlg();
iCart.prototype.superClass = iDlg.prototype;
iCart.prototype.constructor = iCart;

iCart.prototype.phase = function(p) {
	var temp;
	switch(p) {
		case "def":
			$.ajax({	url: "/cart/get-contents/", success: this.fullRedraw, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
			break;
		
		case "order":
			$(this.order + " .field").blur();
			this.orderDelivery($(this.order + " input[name=delivery]:checked").val());
			this.checkOrderForm();
			break;
			
		case "approve":
			if (!this.checkOrderForm()) {
				alert("Для продолжения заполните пол" + ((xData.cartOrderIncomplete.length > 1) ? "я" : "е") + ": " + xData.cartOrderIncomplete.join(", "));
				return false;
			}

			xData.cartOrderDetails = {
				name: this.xVal(this.order + " input[name=name]"),
				phone: this.xVal(this.order + " input[name=phone]"),
				mail: this.xVal(this.order + " input[name=mail]"),
				
				delivery: $(this.order + " input[name=delivery]:checked").val(),
				d_shop: $(this.order + " select[name=d_shop] option:selected").val(),
				d_name: this.xVal(this.order + " input[name=d_name]"),
				d_phone: this.xVal(this.order + " input[name=d_phone]"),
				d_event: this.xVal(this.order + " input[name=d_event]"),
				d_city: $(this.order + " input[name=d_city]:checked").val(),
				d_address: this.xVal(this.order + " input[name=d_address]"),
				d_comment: this.xVal(this.order + " textarea[name=d_comment]"),
				d_notify: $(this.order + " input[name=d_notify]:checked").val(),
				
				bill_type: $(this.order + " select[name=bill_type] option:selected").val(),

				date: $(this.order + " input[name=date]").val(),
				time: $(this.order + " input[name=time]").val(),

				accept: $(this.order + " input[name=accept]:checked").val()
			}
			
			var data = xData.cartOrderDetails;
			data["l0l"] = "169083";

			$.ajax({	url: "/cart/get-approved/", success: this.showApproved, type: "POST", cache: false, data: data, dataType: "json" });

			$(this.id + " .approve .data").text("Подождите, идет загрузка...");
			$(this.id + " .approve .name").text(xData.cartOrderDetails.name);
			$(this.id + " .approve .phone").text(xData.cartOrderDetails.phone);
			
			if (xData.cartOrderDetails.mail) {
				$(this.id + " .approve .if-mail").show();
				$(this.id + " .approve .mail").text(xData.cartOrderDetails.mail);
			} else {
				$(this.id + " .approve .if-mail").hide();
			}
			
			if (Number(xData.cartOrderDetails.delivery)) {
				$(this.id + " .approve .if-delivery").show();
				$(this.id + " .approve .if-no-delivery").hide();
				
				$(this.id + " .approve .d_city").text(xData.city[xData.cartOrderDetails.d_city].where);
				$(this.id + " .approve .d_address").text(xData.cartOrderDetails.d_address);

				$(this.id + " .approve .d_name").text(xData.cartOrderDetails.d_name);

				if (xData.cartOrderDetails.d_notify) {
					$(this.id + " .approve .if-notify").show();

					if (xData.cartOrderDetails.d_notify) {
						var temp = xData.notify[xData.cartOrderDetails.d_notify];
						switch(xData.cartOrderDetails.d_notify) {
							case "1":
							case "2":
								temp += "<strong>" + xData.cartOrderDetails.phone + "</strong>";
								break;
							case "3":
								temp += "<strong>" + xData.cartOrderDetails.mail + "</strong>";
								break;
						}
						$(this.id + " .approve .d_notify").html(temp);
					}

					if (t = xData.cartOrderDetails.d_comment) {
						$(this.id + " .approve .if-comment").show();
						$(this.id + " .approve .d_comment").text(xData.cartOrderDetails.d_comment);
					} else
						$(this.id + " .approve .if-comment").hide();

				} else {
					$(this.id + " .approve .if-notify").hide();
				}
				
			} else {
				$(this.id + " .approve .if-no-delivery").show();
				$(this.id + " .approve .if-delivery").hide();
				
				$(this.id + " .approve .d_shop").text(xData.shops[xData.cartOrderDetails.d_shop].where);
			}

			$(this.id + " .approve .bill").hide();
			$(this.id + " .approve .bill-" + xData.cartOrderDetails.bill_type).show();

			$(this.id + " .approve .date").text(xData.cartOrderDetails.date);
			$(this.id + " .approve .time").text(xData.cartOrderDetails.time);

			break;
	}
	
	$(this.id + " .phase").hide().filter("."+p).show();
	$(this.id + " .buttons ."+p+" .default").focus();
	return true;	
}

iCart.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this, temp;

	self.centerMe();
	
	$("select").not($(self.id + " select")).css("visibility", "hidden");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document

	$(self.id).before('<div id="cart-fader" class="fader"></div>');
	$("#cart-fader").height($(document).height()).css("z-index", $(this.id).css("z-index")-1).bind("click", {self:self}, xMgr.hideModal);


	self.superClass.show.call(self, e);
	return false;
}

iCart.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible) {
		self.superClass.hide.call(self, e);
		$("#cart-fader").remove();
		$("select").not(self.id + " select").css("visibility", "visible");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document
	}

	return false;
}

iCart.prototype.check = function(data) {
	return (data && data.check && (data.check == 939074));
}

iCart.prototype.update = function(data) {

	if (xCart.check(data)) {
		xCart._image.attr('src', data.image);
		xCart._total.text(data.total);
		return true;
	}
	return false;
}

iCart.prototype.redraw = function(data) {
	if (xCart.update(data)) {
		xCart._title.text(data.title);
		xCart._contents.html(data.contents);
		return true;
	}
	return false;
}

iCart.prototype.fullRedraw = function(data) {
	if (xCart.redraw(data)) {
		xCart._btLeft.html(data.buttons.left);
		xCart._btRight.html(data.buttons.right);
		$(xCart.id + " .buttons ."+xCart.hActivePhase+" .default").focus();
		return true;
	}
	return false;
}


iCart.prototype.increase = function(id) {
	$.ajax({ url: "/cart/increase-"+id+"/", success: this.redraw, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
}

iCart.prototype.decrease = function(id) {
	$.ajax({ url: "/cart/decrease-"+id+"/", success: this.redraw, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
}

iCart.prototype.kill = function(id) {
	$.ajax({ url: "/cart/kill-"+id+"/", success: this.fullRedraw, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
}

iCart.prototype.clear = function(id) {
	$.ajax({ url: "/cart/clear-all/", success: this.fullRedraw, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
}


iCart.prototype.checkOrderForm = function(e) {
	var self = (e && e.data && e.data.self) || this;
	
	xData.cartOrderComplete = 1;
	xData.cartOrderIncomplete = [];
	
	$(self.order + " .required").each(function(){
		var e = $(this);
		var v = e.val(), d = e.attr("defValue");
		if ((v == '') || (v == d)) {
			xData.cartOrderComplete = 0;
			xData.cartOrderIncomplete.push(d);
		}
		return true;
	});
	
	if (!($(self.order + ' input[name=accept]:checked').val())) {
		xData.cartOrderComplete = 0;
		xData.cartOrderIncomplete.push("С условиями заказа ознакомлен(а) и согласен(на)");
	}
	
	if (xData.cartOrderComplete)
		$(this.id + " .buttons .order .submit").css("opacity", "1").attr("title", "Подтвердить заказ");
	else
		$(this.id + " .buttons .order .submit").css("opacity", "0.5").attr("title", "Необходимо заполнить отмеченные поля формы");

	return xData.cartOrderComplete;
}

iCart.prototype.orderDelivery = function(p) {
	if (Number(p)) {
		$(this.order + " .deliveryOn").css("display", "block");
		$(this.order + " .deliveryOff").css("display", "none");
		$(this.order + " input[name=d_name], " + this.order + " input[name=d_phone], " + this.order + " input[name=d_address]").addClass("required");
	} else {
		$(this.order + " .deliveryOff").css("display", "block");
		$(this.order + " .deliveryOn").css("display", "none");
		$(this.order + " input[name=d_name], " + this.order + " input[name=d_phone], " + this.order + " input[name=d_address]").removeClass("required");
	}
}

iCart.prototype.processOrder = function() {
	$.ajax({ url: "/cart/send-order/", success: this.finished, type: "POST", cache: false, data: { l0l: "169083" }, dataType: "json" });
	this.phase("sent");
}

iCart.prototype.showApproved = function(data) {
//debug(data);
	var self = xCart;
	if (self.update(data)) {
		self._title.text(data.title);
		$(self.id + " .contents .approve .data").html(data.contents);
		return true;
	}
	return false;
}

iCart.prototype.finished = function(data) {
	var self = xCart;
	
	if (self.update(data)) {
		$(self.id + " .contents .sent .data").html(data.contents);
		return true;
	}
	return false;
}


/*  (x) Sb, 2k9
 * ---------------------------------------------------------------------------
 *
 * Add item dialog
 *
 */

iAdd = function (params) {
	this.superClass.prepare.call(this, params);

	this.btAdd = this.btAdd || this.id + " .add";
	this.btClose = this.btClose || this.id + " .close";

	this.btPlus1 = this.btPlus1 || this.id + " .plus1";
	this.btMinus1 = this.btMinus1 || this.id + " .minus1";

	this.hForm = this.hForm || this.id + " .form";
	this.hThanks = this.hThanks || this.id + " .thanks";
	this.hError = this.hError || this.id + " .error";

	this.hName = this.hName || "Товар не определен";
	this.hPrice = parseFloat(this.hPrice) || 0;
//	this.hCurrency = " " + (this.hCurrency || "грн");
	this.hNum = 1;
	this.hTotal = 0;

	this._name = $(this.id + " .name");
	this._price = $(this.id + " .price");
	this._num = $(this.id + " .popup_num_input");
	this._total = $(this.id + " .total");

	$(this.btAdd).bind("click", {self:this}, this.add);
	$(this.btClose).bind("click", {self:this}, xMgr.hide);

	$(this.btPlus1).bind("click", {self:this}, this.plus1);
	$(this.btMinus1).bind("click", {self:this}, this.minus1);

	this.superClass.init.call(this);
}

iAdd.prototype = new iDlg();
iAdd.prototype.superClass = iDlg.prototype;
iAdd.prototype.constructor = iAdd;

iAdd.prototype.phase = function(p) {
	switch (p) {

		case 'def':
			this._name.text(this.hName);
			this._price.text(this.hPrice.toFixed(2));
			this._num.val(this.hNum);
			break;
	}

	$(this.id + " .phase").hide().filter("."+p).show();
	return true;	
}

iAdd.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this, temp;

	self.centerMe();
	$("select").not($(self.id + " select")).css("visibility", "hidden");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document
	self.superClass.show.call(self, e);
	$(self.id).before('<div id="add-fader" class="fader"></div>');
	$("#add-fader").height($(document).height()).css("z-index", $(this.id).css("z-index")-1).bind("click", {self:self}, xMgr.hide);

	this.calc();

	return false;
}

iAdd.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible) {
		self.superClass.hide.call(self, e);
		$("#add-fader").remove();
		$("select").not(self.id + " select").css("visibility", "visible");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document
	}

	return false;
}

iAdd.prototype.calc = function() {
	this.hNum = Number(this._num.val());
	this._total.text((this.hNum * this.hPrice).toFixed(2));
}

iAdd.prototype.plus1 = function(e) {
	var self = (e && e.data && e.data.self) || this;

	self._num.val(++self.hNum);
	self.calc();
}

iAdd.prototype.minus1 = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.hNum > 1) {
		self._num.val(--self.hNum);
		self.calc();
	}
}

iAdd.prototype.add = function(e) {
	var self = (e && e.data && e.data.self) || this;

	$.ajax({
   	url: "/cart/take-" + self.hId + "/",
		success:	function(data) {
			if (xCart.update(data))
				xMgr.hide();
//				xAdd.phase("thanks");
		},
		
//		error:function (XMLHttpRequest, textStatus, errorThrown) {
//			debug(textStatus);			
		  // typically only one of textStatus or errorThrown 
		  // will have info
//		  this; // the options for this ajax request
//		},

		type: "POST", cache: false, data: { l0l: "169083", num:self.hNum }, dataType: "json" /*, timeout:5000*/ });
	
	return false;
}



/*  (x) Sb, 2k9
 * ---------------------------------------------------------------------------
 *
 * static image viewer
 *
 */

iImage = function (params) {
	this.superClass.prepare.call(this, params);

	this.btClose = this.btClose || this.id + " .close";
	$(this.btClose).bind("click", {self:this}, xMgr.hide);
	
	$(this.id + " img").noContext();

	this.superClass.init.call(this);
}

iImage.prototype = new iDlg();
iImage.prototype.superClass = iDlg.prototype;
iImage.prototype.constructor = iImage;

iImage.prototype.phase = function(p) {
	return true;	
}

iImage.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this, temp;

	self.centerMe();
	$("select").not($(self.id + " select")).css("visibility", "hidden");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document
	self.superClass.show.call(self, e);
	$(self.id).before('<div id="image-fader" class="fader"></div>');
	$("#image-fader").height($(document).height()).css("z-index", $(this.id).css("z-index")-1).bind("click", {self:self}, xMgr.hide);

	$(window).bind("resize", {self:self}, self.fixResize);
	
	return false;
}

iImage.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible) {
		self.superClass.hide.call(self, e);
		$(window).unbind("resize", self.fixResize);
		$("#image-fader").remove();
		$("select").not(self.id + " select").css("visibility", "visible");	// fix for IE bug: it shows SELECT, OBJECT and IFRAME on top of the rest of document
	}

	return false;
}

iImage.prototype.fixResize = function(e) {
	var self = (e && e.data && e.data.self) || this;
	$("#image-fader").height($(document).height());
	self.centerMe();
}





/*  (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 = 0;
	
	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;
	
	if (self.fader && !$(".fader").length) return false;

	self.image.show();
	self.centerMe();
	self.superClass.show.call(self, e);
	
	$(".fader").height($(document).height())	
	return false;
}

iImageViewer.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible) {
		self.superClass.hide.call(self, e);
		$(window).unbind("resize", self.fixResize);
	}
	if (self.fader)
		self.fader.remove();
	else
		$(".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) {
	var self = this, obj = $(self.id), z = obj.css("z-index");

	if ($("div.fader").length)
		self.fader = false;
	else {
		obj.before('<div id="iImageViewer-fader" class="fader"></div>');
		(self.fader = $("div.fader")).hide().height($(document).height()).css("z-index", z-2).bind("click", {self:self}, xMgr.hide).show();
	}

	$(window).bind("resize", {self:self}, self.fixResize);

	if (this.image) {
		this.image.hide().attr("src", src);
	} else {
		$(this.id + " .logo").before('<img class="current">');
		(this.image = $(this.id + " .current")).bind("load", {self:this}, this.show).noContext().attr("src", src);
	}

	return false;
}


/*  (x) Sb, 2k9
 * ---------------------------------------------------------------------------
 *
 * dynamic panel viewer
 *
 */

iPanelViewer = function (params) {
	this.superClass.prepare.call(this, params);

	$(this.id).appendTo("body");

	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.superClass.init.call(this);
}

iPanelViewer.prototype = new iDlg();
iPanelViewer.prototype.superClass = iDlg.prototype;
iPanelViewer.prototype.constructor = iPanelViewer;

iPanelViewer.prototype.phase = function(p) {
	return true;	
}

iPanelViewer.prototype.show = function(e) {
	var self = (e && e.data && e.data.self) || this, temp, obj = $(self.id), z = obj.css("z-index");
	
	if (self.fader && !$(".fader").length) return false;

	if ($("div.fader").length)
		self.fader = false;
	else {
		obj.before('<div id="iPanelViewer-fader" class="fader"></div>');
		(self.fader = $("div.fader")).hide().height($(document).height()).css("z-index", z-2).bind("click", {self:self}, xMgr.hide).show();
	}

	$(window).bind("resize", {self:self}, self.fixResize);

	self.centerMe();
	self.superClass.show.call(self, e);
	
	$(".fader").height($(document).height())	
	return false;
}

iPanelViewer.prototype.hide = function(e) {
	var self = (e && e.data && e.data.self) || this;

	if (self.visible) {
		self.superClass.hide.call(self, e);
		$(window).unbind("resize", self.fixResize);
	}
	if (self.fader) {
		self.fader.remove();
		self.fader = false;
	} else
		$(".fader").height($(document).height());

	return false;
}

iPanelViewer.prototype.fixResize = function(e) {
	var self = (e && e.data && e.data.self) || this;
	self.centerMe();
}




/*  (x) Sb, 2k8
 * ---------------------------------------------------------------------------
 *
 * Some date-manipulation functions
 *
 * Provides correct date for date field.
 *
 */

Date.prototype.midnight = function() { this.setHours(0); this.setMinutes(0); this.setSeconds(0); this.setMilliseconds(0); return this; }
Date.prototype.timeDefined = function() { return this.getHours() || this.getMinutes() }

today = new Date().midnight();
rightNow = new Date();
dformat = "%d.%m.%Y";
tformat = "%d.%m.%Y %H:%M";
yRange = [today.getFullYear(), (new Date(today.getTime() + Date.DAY*30)).getFullYear()];



function checkDate(f) {
	var dd;
	if (dd = ((typeof(f) == "string") ? document.getElementById(f).value : f.value).match(/(\d{1,2})\.(\d{1,2})\.(\d{2,4})/)) {
		res = new Date();
		res.setFullYear(dd[3]);
		res.setMonth(dd[2] - 1)
		res.setDate(dd[1]);
		return res.midnight();
	}
	else return false;
}

function checkDateTime(f) {
	var dd;
	if (dd = ((typeof(f) == "string") ? document.getElementById(f).value : f.value).match(/(\d{1,2})\.(\d{1,2})\.(\d{2,4}) (\d{1,2}):(\d{2})/)) {
		res = new Date();
		res.setFullYear(dd[3]);
		res.setMonth(dd[2] - 1)
		res.setDate(dd[1]);
		res.setHours(dd[4], dd[5], 0, 0);
		return res;
	}
	else return false;
}

function fixDate(field) {
	var dt, el = (typeof(field) == "string") ? document.getElementById(field) : field;

	if (dt = checkDateTime(field)) {
		if (dt.getTime() < rightNow.getTime())
			dt = rightNow;
	} else if (dt = checkDate(field)) {
		if (dt.getTime() < today.getTime())
			dt = today;
	} else {
		dt = today;	
	}

	el.value = dt.timeDefined() ? dt.print(tformat) : dt.print(dformat);
}

function disableBadDate(d) { var t,n; return ((t = d.getTime()) < (n = rightNow.midnight().getTime())) || (t > n + Date.DAY*30) }

function fixDateOpen(cal) {
}

function fixDateClose(cal) {
	cal.hide();
}

function switchCurrency() {
	$.post("/cart/set-currency/", {currency:$(this).attr('class'), l0l:169083}, function() { location.href = location.href });
	return false; 
}