
	$(document).ready(function () {
		$.each($('.default-value'), function (k, input) {
			input = $(input);
			input.defaultValue = input.attr('value');
			input.focus(ref(input, function () {
				if (this.attr('value') == this.defaultValue)
					this.attr('value', '');
			}));
			input.blur(ref(input, function () {
				if (this.attr('value') == '')
					this.attr('value', this.defaultValue);
			}));
		});

		if ($('.spot').length > 0) {
			var spot = {
				playing: true,
				interval_sec: parseInt($('#spotspeed').html(), 10) * 1000,
				pos: 0,
				count: 0,
				spot: null,

				init: function () {
					this.spot = $('.spot-item');
					this.count = this.spot.length;

					$.each($('.spot-item a.left'), function (k, val) {
						val = $(val);
						if (val.css('background-image')) {
							var img = new Image();
							img.src = val.css('background-image');
						}
					});

					$('.spot-prev').click(ref(this, this.prev));
					$('.spot-pause').click(ref(this, this.pause));
					$('.spot-next').click(ref(this, this.next));

					setInterval(ref(this, this.interval), this.interval_sec);
				},

				prev: function (e) {
					$(this.spot[this.pos]).hide();

					this.pos --;
					if (this.pos < 0)
						this.pos = this.count - 1;

					$(this.spot[this.pos]).show();

					if (e) {
						e.preventDefault();
						this.playing = false;
					}
				},

				next: function (e) {
					$(this.spot[this.pos]).hide();

					this.pos ++;
					if (this.pos >= this.count)
						this.pos = 0;

					$(this.spot[this.pos]).show();

					if (e) {
						e.preventDefault();
						this.playing = false;
					}
				},

				interval: function () {
					if (this.playing)
						this.next();
				},

				pause: function (e) {
					this.playing = !this.playing;
					$('.spot-pause').css('background-image', 'url("' + (this.playing ? JS_HOST + 'view/img/icon-pause.png' : JS_HOST + 'view/img/icon-pause-a.png') + '")');

					if (e)
						e.preventDefault();
				}
			};

			spot.init();
		}

		var xmleditor_click = function (e) {
			e.preventDefault();

			var el = $(this);
			var copynode = $(el.parent().parent().parent().children('.xmleditor-node')[0]);
			var placement = $(el.parent().parent().parent());

			var node = $('<div class="xmleditor-node xmleditor-multiplenode">' + copynode.html() + '</div>');
			node.children('.xmleditor-nodetitle').remove();

			$.each(node.find('.xmleditor-placement'), function (k, val) {
				val = $(val);
				$.each(val.find('.xmleditor-multiplenode'), function (k, sval) {
					sval = $(sval);
					if (k > 0) {
						sval.remove();
					}
				});
			});

			//var path = el.children('span').html();
			//var sp = path.replace(/\[[^\]]\]$/, '');
			var sp = el.children('span').html();

			var r_escape = function(text) {
			  if (!arguments.callee.sRE) {
				var specials = [
				  '/', '.', '*', '+', '?', '|',
				  '(', ')', '[', ']', '{', '}', '\\'
				];
				arguments.callee.sRE = new RegExp(
				  '(\\' + specials.join('|\\') + ')', 'g'
				);
			  }
			  return text.replace(arguments.callee.sRE, '\\$1');
			};

			var nid = 'iid:' + (new Date).getTime() + ':' + Math.random();
			node.html(node.html().replace(new RegExp(r_escape(sp + '') + '.*?\]', 'g'), sp + '[' + nid + ']'));
			//node.html(node.html().replace(path + '[@', sp + '[' + nid + '][@'));
			//node.html(node.html().replace(path + '[~', sp + '[' + nid + '][~'));

			$.each(node.find('input'), function (k, val) {
				val = $(val);
				val.attr('value', '');
				//val.attr('name', val.attr('name').replace(/d/, ''));
			});

			node.find('.xmleditor-addnode').click(xmleditor_click);
			node.find('.xmleditor-remnode').click(xmleditor_remove);
			placement.append(node);
		};

		var xmleditor_remove = function (e) {
			e.preventDefault();

			var el = $(this);
			el.parent().remove();
		};

		$('.xmleditor-addnode').click(xmleditor_click);
		$('.xmleditor-remnode').click(xmleditor_remove);
	});

	function ref(object, method) {
		return function () {
			return method.apply(object, arguments);
		};
	}

	var extlist_last = null;
	function extlist_expand(obj) {
		var ch = obj.parentNode.getElementsByTagName('ul');

		if (ch.length > 0) {
			ch[0].style.display = ch[0].style.display == "none" ? "" : "none";
			obj.style.backgroundImage = "url('" + JS_HOST + "view/images/extselect-expand" + (ch[0].style.display == "none" ? "" : "ed") + ".png')";
		}

		var recshow = function (node) {
			node.style.display = '';

			if (node.parentNode && node.parentNode.style)
				recshow(node.parentNode);
		};

		obj.style.textDecoration = 'underline';

		if (extlist_last)
			extlist_last.style.textDecoration = '';

		recshow(obj);
		extlist_last = obj;
	}

	function debug(v) {
		try {
			if (is_def(console))
				console.info(v);
		} catch (e) {
		}
	}

	function is_def(variable) {
		return typeof(variable) !== 'undefined';
	}

	function load_editor_for(f, style) {
		var _editor = {
			mode : "exact",
			theme : "advanced",
			language: "pl",
			elements : f,
			skin_variant : "black",
			plugins : "safari,style,layer,table,advhr,advimage,advlink,emotions,iespell,contextmenu,directionality,fullscreen",
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,fontsizeselect,|,hr,|,bullist,numlist,|,outdent,indent,|,image,|,cite,abbr,acronym,del,ins,attribs,|,link,unlink,|,forecolor,backcolor,|,code,cleanup",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "bottom",
			theme_advanced_toolbar_align : "center",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_path : false,
			content_css: JS_HOST + "view/style/style.css",
			body_id: "content",
			body_class: "text-editing " + style
		};

		tinyMCE.init(_editor);
	}
