{extends file=$smarty.const.SMARTY_LAYOUT_MAIN_GENERAL_PAGINATED_LISTING}

{block name="template-head" append}
	{$CSS->load("{asset path="_css/index.css"}")}
{/block}


{block name="js" append}
	<script type="text/javascript">
		{literal}
		MVC.scheduleOnLoad(function() {
			MVC.listing.prototype.extractAdvancedFilters = function(text) {
				var filters = [];
				if (text.length) {
					if (text[0]=="!") {
						// we have advanced filtering enabled
						text = text.substr(1);
						// \u000B - is tab?
						var rawfilters = text.replace(/([^\\]);/g, '$1\u000B').split('\u000B');

						for (var i=0; i<rawfilters.length; i++) {
							var f = rawfilters[i];
							filters.push({
								raw: f,
							});
						}
					}
					else {
						// we have a literal filter already in place
						filters.push({
							raw: "content:"+escapeStrForFiltering(text),
						});
					}
				}

				return filters;
			}

			MVC.listing.prototype.advancedFiltersAddQuery = function(advancedFiltersContent, text) {
				var filters = this.extractAdvancedFilters(advancedFiltersContent);
				var foundInFilters = false;
				for (var i=0; i<filters.length; i++) {
					if (filters[i].raw==("content:"+text)) {
						foundInFilters = true;
					}
				}
				// only add into existing filters when there isn't already there
				if (!foundInFilters) {
					filters.push({
						raw: "content:"+text,
					});
				}

				return filters;
			}

			MVC.listing.prototype.advancedFiltersRemoveQuery = function(advancedFiltersContent, text) {
				var filters = this.extractAdvancedFilters(advancedFiltersContent);
				var foundInFilters = false;
				for (var i=0; i<filters.length; i++) {
					if (filters[i].raw==("content:"+text)) {
						filters[i] = null;
						foundInFilters = true;
					}
				}
				// only add into existing filters when there isn't already there
				if (!foundInFilters) {
					filters.push({
						raw: "content!:"+text,
					});
				}

				return filters;
			}

			MVC.listing.prototype.buildAdvancedFilters = function(filters) {
				var text = "";
				if (filters.length) {
					text = "!";
					var s = "";
					for (var i=0; i<filters.length; i++) {
						if (filters[i]) {
							text+= s+filters[i].raw;
							s = ";";
						}
					}
				}

				return text;
			}

			MVC.listing.prototype.extractSearchCriterias = function(text) {
				text = text.strip();

				var lines = text.split("\n");
				if (lines.length>1) {
					text = lines[0];
				}
				var subdomains = text.split(/(:|\(|\)|\[|\]|`|'|"|,|\.|\$|\^)/g);
				if (subdomains.length>1) {
					// pick first sentence larger that 5 chars
					for (var i=0; i<subdomains.length; i++) {
						// remove split-delimiters that were used when splitting
						subdomains[i] = subdomains[i].replace(/(\[|\])/g, '').strip();
					}
				}
				else {
					subdomains = [ text ];
				}

				subdomains = subdomains.map(function(a) {
					// remove small words at the begining of the text
					a = a.replace(/^([a-z0-9_]{1,3})(\W)/gi, '$2');

					// remove small words at the end of the text
					a = a.replace(/(\W)([a-z0-9_]{1,3})$/gi, '$1');

					// limit the text-length
					a = a.substr(0, 80);

					return a.strip();
				})
				.filter(function(a) {
					return a && a.length>1;
				});

				// sort by lengths
				subdomains.sort(function(a, b) {
					return b.length - a.length;
				});

				// filter out short messages, escape them, and limit their number to 5
				return subdomains.filter(function(text) {
					return text.length>5;
				}).map(function(a) {
					// escape & strip like the engine
					return escapeStrForFiltering(a);
				})
				.slice(0, 5);
			}

			MVC.listing.prototype.populateShowMoreItemsLikeThisDetailed = function(menuDropdown, li) {
				var tr = $(li).up('tr[data-id]');
				this.extractSearchCriterias(tr.down('td.message').readAttribute('data-message')).each(function(text) {
					var nli = menuDropdown._appendOption(li.down('ul.submenu'), {
						icon: 'moveUp',
						label: text,
					});
					nli.writeAttribute("data-searchQuery", text);
				});

				li.down('ul.submenu').on('click', 'li.menuItem', function(evt, li) {
					$('filters_text_').value = this.buildAdvancedFilters(this.advancedFiltersAddQuery($F('filters_text_'), li.readAttribute("data-searchQuery")));
					this.filters.scheduleSubmit();
				}.bind(this));
			}

			MVC.listing.prototype.populateShowLessItemsLikeThisDetailed = function(menuDropdown, li) {
				var tr = $(li).up('tr[data-id]');
				this.extractSearchCriterias(tr.down('td.message').readAttribute('data-message')).each(function(text) {
					var nli = menuDropdown._appendOption(li.down('ul.submenu'), {
						icon: 'moveDown',
						label: text,
					});
					nli.writeAttribute("data-searchQuery", text);
				});

				li.down('ul.submenu').on('click', 'li.menuItem', function(evt, li) {
					$('filters_text_').value = this.buildAdvancedFilters(this.advancedFiltersRemoveQuery($F('filters_text_'), li.readAttribute("data-searchQuery")));
					this.filters.scheduleSubmit();
				}.bind(this));
			}



			MVC.listing.prototype.showThisTimeline = function(li, intervalInSeconds) {
				var tr = $(li).up('tr[data-id]');
				var dt = Date.parseDate(tr.down('td[data-addTime]').readAttribute('data-addTime'), 'Y-m-d H:i:s');

				var dt1 = new Date(dt.getTime() - 0.25*intervalInSeconds*1000);
				var dt2 = new Date(dt.getTime() + 0.75*intervalInSeconds*1000);

				$('filters_addTime__start__date').value = 	dt1.dateFormat(_inputValidatorSettings.dateFormat);
				$('filters_addTime__start__time').value = 	dt1.dateFormat(_inputValidatorSettings.timeFormat);
				$('filters_addTime__start_').value = 		dt1.dateFormat('Y-m-d H:i:s');

				$('filters_addTime__end__date').value = 	dt2.dateFormat(_inputValidatorSettings.dateFormat);
				$('filters_addTime__end__time').value = 	dt2.dateFormat(_inputValidatorSettings.timeFormat);
				$('filters_addTime__end_').value = 			dt2.dateFormat('Y-m-d H:i:s');

				this.filters.scheduleSubmit();
			}
		});

		function escapeStrForFiltering(str) {
			return str
				.replace(/&amp;/g, '&')
				.replace(/&gt;/g, '>')
				.replace(/&lt;/g, '<')
				.replace(/\\/g, '\\\\\\\\')
				//          [   ] (  )  $  ^  |  {  }  .  -  +  *
				.replace(/(\[|\]|\(|\)|\$|\^|\||\{|\}|\.|\-|\+|\*)/g, '\\\\\\$1');
		}
		{/literal}
	</script>
{/block}