$(function() {
	$('a').each(function() {
		// ссылки, у которых не указан адрес или протокол отличается от http - пропускаются
		if(!this.href || this.protocol !== 'http:' ) {
			return;
		}

		// если у ссылки в адресе стоит пустой якорь, то делаем скролл к шапке
		if(this.href.search(/#$/) !== -1 && this.onclick === null) {
			$(this).click(function() {
				window.scrollTo(0,0);
				return false;
			});
		}

		// если ссылаемся на другой домен и не указан target ...
		if(this.hostname != window.location.host && !this.target.length) {
			// ... и домен второго уровня не совпадает, то открываем ссылку в новом окне
			if(this.hostname.match(/(\w+\.\w+)$/)[0] !== window.location.host.match(/(\w+\.\w+)$/)[0]) {
				$(this).attr('target', '_blank');
				$(this).attr('title', this.hostname+' in new window');
			}
		}

		// можно открывать popup окна указанного размера
		this.popup = $(this).attr('popup');

		if(this.popup != null && this.popup != '') {
			$(this).click(function() {
				var popup = this.popup.split(' ');

				var w = popup[0];
				// если высота не указана, то открываем квадратное окно
				var h = typeof popup[1] != 'undefined' ? popup[1] : popup[0];
				
				var toolbar = ($(this).attr('print') != null) ? 'yes' : 'no';

				var href = this.href;

				if (typeof popup[2] != 'undefined') {
					switch(popup[2]) {
						// окно под картинки должно быть без полей
						case 'image':
							// можно указать заголовок окна
							if(popup.length > 3) {
								var t = '';
								for (k=3; k<popup.length; k++) {
									t += ' '+popup[k];
								}

								return openImagePopupWindow(href, w, h, t);

							} else {
								return openImagePopupWindow(href, w, h, href);
							}
							break;
					}

				} else {
					return openPopupWindow(href, w, h, toolbar);
				}
			});
		}
	});
});

function openPopupWindow(url, width, height, toolbar) {
	justPopupWindow(url, width, height, toolbar);
	return false;
}

function justPopupWindow(url, width, height, toolbar) {
	var w = window.open(url,'_blank','toolbar='+toolbar+',status=no,location=no,menubar=no,resizable=yes,scrollbars=yes,width='+width+',height='+height);
	w.focus();
}

function openImagePopupWindow(url, width, height, title) {
	var w = window.open(url, null, 'width='+width+', height='+height+', toolbar=no, status=no, location=no, menubar=no, resizable=yes, scrollbars=no');
	w.document.open();

	w.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'+"\n");
	w.document.write('<html><head><title>'+title+'</title>');
	w.document.write('<style>html, body { padding: 0; margin: 0; background: #FFFFFF;  height: 100%; position: relative; cursor: hand;}');
	w.document.write(' img {position: absolute; left: 50%; top: 50%; margin-left: -'+Math.ceil(width / 2)+'px; margin-top: -'+Math.ceil(height / 2)+'px;} </style>');
	w.document.write('</head><body onclick="window.close();" title="'+title+"\n\n"+'Нажмите на картинку'+"\n"+'чтобы закрыть окно">');
	w.document.write('<img src="'+url+'" width="'+width+'" height="'+height+'" border="0" alt="'+title+"\n\n"+'Нажмите на картинку'+"\n"+'чтобы закрыть окно"></body></html>');

	w.document.close();
	w.focus();
	return false;
}
