$(document).ready(function () {
	(function () {
		$('#search').autocomplete(
			'service/search', 
			{
				dataType : 'json',
				parse : function (data) {
					var items = [];
					for (var i = 0, length = data.length; i < length; i++) {
						var current = data[i];
						items.push({
							data   : current,
							value  : current.caption,
							result : ''
						});
					}
					return items;
				},
				formatItem : function(item) {
					return item.caption;
				},
				width : 195
			}
		).result(
			function (event, item) {
				window.location.href = item.url;
			}
		);
	})();

	(function () {
		var behaviours = {
			'Buttons' : function () {
				var buttons = this.getElementsByTagName('button');
				var onMouseOverCallback = function () { this.className = 'Highlight'; };
				var onMouseOutCallback = function () { this.className = ''; };
				for (var i = 0, length = buttons.length; i < length; i++) {
					var button = buttons[i];
					button.onmouseover = onMouseOverCallback;
					button.onmouseout = onMouseOutCallback;
				}
			},
			'ExtraImagesHolder' : function () {
				var links = this.getElementsByTagName('a');
				var onClickCallback = function () {
					Predmet.Utils.OpenPopup({
						id : ('extraImage' + i),
						url : this.href,
						specs : {
							width : 800,
							height : 600,
							resizable : true,
							scrollbars : true
						}
					});
					return false;
				};
				for (var i = 0, length = links.length; i < length; i++) {
					links[i].onclick = onClickCallback;
				}
			},
			'ContentSamplesLinks' : function () {
				var container = this.parentNode;

				var topLinksHolder = this;

				var imageHolder = document.createElement('div');
				imageHolder.className = 'ImageHolder';
				imageHolder.style.display = 'none';
				container.appendChild(imageHolder);

				var bottomLinksHolder = this.cloneNode(true);
				bottomLinksHolder.style.display = 'none';
				container.appendChild(bottomLinksHolder);

				var links = container.getElementsByTagName('a');

				var cleanup = function () {
					while (imageHolder.firstChild) {
						imageHolder.removeChild(imageHolder.firstChild);
					}
					for (var i = 0, length = links.length; i < length; i++) {
						var link = links[i];
						link.className = '';
						link.title = 'Показати';
					}
					imageHolder.style.display = 'none';
					bottomLinksHolder.style.display = 'none';
				};

				var onClickCallback = function () {
					var current = imageHolder.firstChild;
					if (current && current.src == this.href) {
						cleanup();
					} else {
						var image = document.createElement('img');
						image.src = this.href;
						image.title = 'Сховати';
						image.onclick = function () { cleanup(); return false; };

						cleanup();
						imageHolder.appendChild(image);
						imageHolder.style.display = '';
						bottomLinksHolder.style.display = '';

						for (var i = 0, length = links.length; i < length; i++) {
							var link = links[i];
							if (link.href == this.href) {
								link.className = 'Active';
								link.title = 'Сховати';
							}
						}
					}
					return false;
				};

				for (var i = 0, length = links.length; i < length; i++) {
					links[i].onclick = onClickCallback;
				}
			}
		};
		var divs = document.getElementsByTagName('div');
		for (var i = 0, length = divs.length; i < length; i++) {
			var div = divs[i];
			for (var key in behaviours) {
				if (div.className.match(new RegExp('\\b' + key + '\\b'))) {
					behaviours[key].call(div);
				}
			}
		}
	})();
});