
	$(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);



		var catsel_change = function () {
			var el = $(this);
			var search = [el.parent(), el.parent().parent()];
			var obj = null;
			var value = parseInt(el.val(), 10);

			$.each(search, function (k, val) {
				if ($(val).find('.category-selectsub')) {
					obj = $(val).find('.category-selectsub');
					return false;
				}

				return null;
			});

			if (obj) {
				var lastval = parseInt(obj.val(), 10);
				obj.find("option[value!='']").remove();

				if (value) {
					$.each(aCategories, function (k, val) {
						if (parseInt(val.id_parent, 10) == value) {
							var opt = $('<option value="' + val.id + '">' + val.name + '</option>');
							if (val.id == lastval) {
								opt.attr('selected', 1);
							}
							obj.append(opt);
						}
					});
				}

				if (obj.find("option").length == 1) {
					obj.hide();
				} else {
					obj.show();
				}
			}

			return false;
		};

		$.each($('.category-select'), function (k, val) {ref(val, catsel_change)();});
		$('.category-select').change(catsel_change);

		var PicturesUploader = {
			row: null,

			init: function (row) {
				this.row = row;
				this.form = row.parent('form');
				this.row.find('input[type="file"]').change(function () {
					this.form.find('input[name="onlyupload"]').val(1);
					this.form.submit();
				});
			}
		};

		$.each($('.pinctures-upload'), function (k, val) {
			PicturesUploader.init($(val));
		});

		$.each($('.enable-input'), function (k, val) {
			val = $(val);

			var chb = val.find('input[type="checkbox"]');
			var inp = val.find('input.text');

			if (!chb.is(':checked')) {
				inp.attr('readonly', true).addClass('readonly');
			}

			chb.change(function () {
				var chb = $(this);
				var inp = $(this).parent().find('input.text');

				if (chb.is(':checked')) {
					inp.attr('readonly', false).removeClass('readonly');
				} else {
					inp.attr('readonly', true).addClass('readonly');
				}
			});
		});

		$('.box-element .category-item:first-child').click(function (e) {
			$(this).parent().find('.sub').toggle();
			e.stopPropagation();
			return false;
		});
	});

	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, load_css) {
		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,|,fullscreen,|,code,cleanup",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "center",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_path : false,
			content_css: load_css ? JS_HOST + "view/style/main.css" : null,
			body_id: "content",
			body_class: " " + style
		};

		tinyMCE.init(_editor);
	}

	function load_small_editor_for(f, style, load_css, maxchars) {
		var _editor = {
			mode : "exact",
			theme : "advanced",
			language: "pl",
			elements : f,
			skin_variant : "black",
			plugins : "safari,style,layer,table,advhr,advimage,advlink,emotions,iespell,contextmenu" + (maxchars ? ",maxchars" : ""),
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,|,outdent,indent,|,removeformat",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "center",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_path : false,

			max_chars : maxchars ? maxchars : 99999999,
			max_chars_indicator : "lengthBox",

			content_css: load_css ? JS_HOST + "view/style/main.css" : null,
			body_id: "content",
			body_class: " " + style
		};

		tinyMCE.init(_editor);
	}
