$(function() {
	var targets = $('.ajaxTarget'), targetsLength = targets.size(), counterForAjaxes = 0;
	targets.each(function() {
		var recipient = $(this);
		var src = recipient.attr('rel');
		$.ajax({
			url: recipient.attr('href'),
			type: 'GET',
			cash: false,
			dataFilter: function(data) {
				return $('.ajaxedBlock', data).html();
			},
			success: function(data) {
				recipient.replaceWith(data);
				if(src != '') {
					$.getScript(src);
				}
				counterForAjaxes++;
				if(counterForAjaxes == targetsLength) {
					setTimeout("$('#wrapJsiBox').click();", 5000);

					window.allHeight=$('#work').outerHeight()+parseInt($('#work').css('top'));
					window.allWidth=$('#layout').width();
					window.visHeight=$('#wrapper').height();
					window.visWidth=$('#wrapper').width();

					var scrWidth=visWidth*visWidth/allWidth-15;
					var scrHeight=visHeight*visHeight/allHeight-15;
					$("#scrollX").css({width: scrWidth+"px"});
					$("#scrollY").css({height: scrHeight+"px"});

					window.maxSLeft=visWidth-allWidth;
					window.maxSTop=visHeight-allHeight;

					/************Доорганизовываем внутреннюю навигацию***************/
					$('.inner-anchor').live('click', function() {
						var rel = $(this).attr('rel'); rel = rel.split('+');
						var block = rel[0], id = rel[1], needAjax = false, opt = {};
						$('#wrapper').stop().scrollTo($('div[id=' + block +']'), 3500, {axis: 'xy'});
						location.href="#show:"+block;
						switch(block) {
							case 'work':
								needAjax = true;
								opt.url = '\/work-.aspx';
								opt.data = {
									article: id
								}
								opt.success = function(data) {
									$('#work').html(data);
								}
								break;
							case 'diary':
								needAjax = true;
								opt.url = '\/dairy.grouped.aspx';
								opt.data = {
									show: 1,
									mode: 'article',
									article: id
								}
								opt.success = function(data) {
									$('#notes-content').html(data);
									$('#notes-headline').find('p').eq(1).text('');
								}
								break;
							default: break;
						}
						opt.type = 'GET';
						opt.cash = false;
						opt.dataFilter = function(data) {
							return $('.ajaxedBlock', data).html();
						}
						if(needAjax)
							$.ajax(opt);
					});
				}
			}
		});	
	});
});