jQuery(document).ready(function() {
	$('.main_area_ajax').delegate('.ajax_action', 'click', function(event) {
		$.fancybox({'href' : this.href, 'autoDimensions' : false, 'showCloseButton' : false, 'modal' : true, 'height' : 550, 'width' : 900, 'onStart' : blockAll, 'onComplete' : unblockAll});
		$.fancybox.hideActivity();
		event.preventDefault();
	});
	
	$('.main_area_ajax').delegate('.action_reload', 'click', function(event) {
		blockAll();
		$.ajax({
			type		: "get",
			cache		: false,
			url			: this.href,
			complete	: function() {
				if(typeof _rtp != 'undefined') {
					window.location.assign(_rtp);
				} else window.location.reload();
			}
		});
		event.preventDefault();
	});
	
	$("body").delegate('.button_reload_page', 'click', function(e) {
		window.location.reload();
	});
	
	$("body").delegate('.button_cancel', 'click', function(e) {
		$.fancybox.close();
	});
		
	$(".fancy_image").fancybox();
	
	$('.fan_img').fancybox({'showCloseButton'   : false,
								'titlePosition' 	: 'inside',
								'titleFormat'		: formatTitle});

	
});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div style="text-align:left;"><span><a href="javascript:;" onclick="$.fancybox.close();" style="color: #333333; float: right; font-size: 12px; font-weight: bold;text-decoration: none;">закрыть</a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' + title ) + '<br />Изображение ' + (currentIndex + 1) + ' из ' + currentArray.length + '</div>';
}

function showFBResponse(responseText, statusText, xhr, form) {
	unblockFancy();
	$.fancybox(responseText, {'autoDimensions'	: false, 'showCloseButton' : false, 'height' : 550, 'width' : 900});
}

function blockAll() {
	$.blockUI({
		message	: '<h1>Подождите окончания запроса...</h1>',css: { 
			width: '350px', 
			border: 'none', 
			padding: '5px', 
			backgroundColor: '#000', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			opacity: .7, 
			color: '#fff' 
		}});
}

function unblockAll() {
	$.unblockUI();
}

function blockFancy() {
	$('#fancybox-outer').block({
							message	: '<h1>Подождите окончания запроса...</h1>',css: { 
								width: '350px', 
								border: 'none', 
								padding: '5px', 
								backgroundColor: '#000', 
								'-webkit-border-radius': '10px', 
								'-moz-border-radius': '10px', 
								opacity: .7, 
								color: '#fff' 
							}
	});
}

function unblockFancy() {
	$('#fancybox-outer').unblock();
}
