<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta content="IE=Edge" http-equiv="X-UA-Compatible">
		<script type="text/javascript">
			window.eAu_stub_function = function () { return true; }
		</script>
		<title></title>
		<link type="text/css" charset="UTF-8" rel="stylesheet" href="stylesheets/screen.css">
		<link rel="stylesheet" media="screen" type="text/css" href="stylesheets/user.css">
		<link type="text/css" charset="UTF-8" rel="stylesheet" href="stylesheets/custom.css">
		<style type="text/css">
			html, body {
				width: 100%;
				height: 100%;
				margin: 0px;
				padding: 0px;
			}
			#cont {
				height: 100%;
				overflow-x: auto;
				overflow-y: auto;
			}
			.tbl-wrapper {
				width: 100%;
				height: 100%;
				border-collapse: collapse;
				border-spacing: 0;
				border: 0 solid;
			}
			.tbl-wrapper td.td-tbl-wrapper {
				padding: 0;
				text-align: center;
				vertical-align: middle;
			}
			.resizer-preview {
				position: absolute;
				top: 0px;
				left: 0px;
				z-index: 2000;
				display: none;
			}
			.resizer-preview-window {
				position: absolute;
				top: 0px;
				left: 0px;
				z-index: 2010;
				overflow: hidden;
			}
			.media-object-placeholder iframe,
			.media-object-placeholder embed,
			.media-object-placeholder object {
				display: block;
			}
			.media-object-placeholder,
			.media-object-placeholder iframe {
				display: block;
				width: 100%;
				height: 100%;
				margin: 0;
				padding: 0;
				border: 0;
			}
			#move-image {
				position: absolute;
				cursor: pointer;
			}
			.hide-objects object, .hide-objects iframe {
				display: none;
			}
		</style>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/external/jquery-1.7.1.min.js"></script>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/external/jquery-ui-1.8.17.custom.min.js"></script>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/external/underscore.js"></script>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/external/swfobject.js"></script>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/eau.js"></script>
		<script type="text/javascript" charset="UTF-8" src="scripts/lib/libutils.js"></script>
		<script type="text/javascript">
			self.QParams = eAu.String.toQueryParams(document.location.search.toString());

			function getWindowOpener () {
				var wo = window.opener;
				if (QParams['embed'] == 'yes') {
					wo = window.parent;
				}
				return wo;
			}

			var flashObjects = [];
			function embedFlashMovie(id) {
				var wo = getWindowOpener();
				if (!wo) {
					return;
				}

				var element = _Xml.$(wo.head, id);
				if (!element) {
					return;
				}

				$('#cont').css('overflow', 'hidden');
				options = wo.get_flash_options_for_element(element);
				$('#'+options.safeId).parent()
					.css({width: '100%', height: '100%'});
				swfobject.embedSWF(
					options.src,
					options.safeId,
					'100%',
					'100%',
					options.minVersion,
					options.install,
					options.flashvars,
					options.params,
					options.attributes
				);

				flashObjects.push(options.safeId);
				
			}
			function unloadFlashObjects() {
				for (var i = 0, length = flashObjects.length; i < length; ++i) {
					swfobject.removeSWF(flashObjects[i]);
				}
				flashObjects = [];
			}

			function processInnerLinks() {
				var wo = getWindowOpener();
				if (!wo) {
					return;
				}

				try {
					wo.processInnerLinks.apply(this, arguments);
				} catch (error) {}
			}

			try {
				if (getWindowOpener() && getWindowOpener().block_content_copy) {
					getWindowOpener().eAu.contentBlocker.enable(document);
				}
			} catch (error) {}

			var previewSize = 0.25;

			function fitRectIntoViewport(rect, viewport) {
				var fit = {
					width: rect.width,
					height: rect.height
				};

				if (fit.height > viewport.height) {
					fit.width = fit.width * viewport.height / fit.height;
					fit.height = viewport.height;
				}
				if (fit.width > viewport.width) {
					fit.height = fit.height * viewport.width / fit.width;
					fit.width = viewport.width;
				}
				return fit;
			}
			function getViewport() {
				return {
					width: $(window).width(),
					height: $(window).height()
				};
			}
			function getImageRect(image) {
				return {
					width: parseInt(image.attr('orig_width'), 10),
					height: parseInt(image.attr('orig_height'), 10)
				};
			}
			function getPreviewRect(fittedImageRect) {
				return {
					width:  fittedImageRect.width  * previewSize,
					height: fittedImageRect.height * previewSize
				};
			}
			function getPreviewWindowRect(rect, viewport, previewRect) {
				var imageViewPort = {
					width:  viewport.width  < rect.width  ? viewport.width  : rect.width,
					height: viewport.height < rect.height ? viewport.height : rect.height
				};
				return {
					width:  (imageViewPort.width  * previewRect.width)  / rect.width,
					height: (imageViewPort.height * previewRect.height) / rect.height
				};
			}
			function getPreviewWindowPosition(rect, viewport, rectPosition, previewRect) {
				var tl = {
					top: 0,
					left: 0
				}
				if (viewport.width < rect.width) {
					tl.left = previewRect.width * Math.abs(rectPosition.left) / rect.width;
				}
				if (viewport.height < rect.height) {
					tl.top = previewRect.height * Math.abs(rectPosition.top) / rect.height;
				}

				return tl;
			}
			function embedImage(imageSrc) {
				var img = new Image();
				img.onload = function() {
					var moveImage, resizerPreview, resizerPreviewWindow;

					$(document.body)
						.bind('mousedown selectstart', function(event) {
							event.preventDefault();
						});

					$(window).bind('resize', function(event, thread) {
						if (!thread) {
							if (arguments.callee.t) {
								clearTimeout(arguments.callee.t);
							}
							arguments.callee.t = setTimeout(function() {
								$(window).trigger('resize', [true])
							}, 10);
							return;
						}

						var cImg = moveImage;
						var isCollapsed = cImg.attr('collapsed') == 'yes';

						var vp = getViewport();
						var i  = getImageRect(cImg);
						var ii = fitRectIntoViewport(i, vp);

						if (isCollapsed) {
							moveImage
								.css({
									width:    Math.round(ii.width),
									height:   Math.round(ii.height),
									top:      parseInt((vp.height - ii.height) / 2),
									left:     parseInt((vp.width - ii.width) / 2)
								});
							moveImage.addClass('soft-image');
						} else if (vp.width >= i.width && vp.height >= i.height) {
							moveImage
								.trigger('dblclick');
						} else {
							moveImage.removeClass('soft-image');
							var correctedImagePosition = cImg.offset();
							if ((vp.height < i.height) && (Math.abs(correctedImagePosition.top) + vp.height) > i.height) {
								correctedImagePosition.top = vp.height - i.height;
							}
							if ((vp.width < i.width) && (Math.abs(correctedImagePosition.left) + vp.width) > i.width) {
								correctedImagePosition.left = vp.width - i.width;
							}
							var previewRect = getPreviewRect(ii);
							var resizerPreviewWindowRect = getPreviewWindowRect(i, vp, previewRect);

							resizerPreview
								.css({
									width:  Math.round(previewRect.width),
									height: Math.round(previewRect.height)
								});

							var MaxLeft = vp.width < i.width
								? previewRect.width * Math.abs(vp.width - i.width) / i.width
								: 0;
							var MaxTop = vp.height < i.height
								? previewRect.height * Math.abs(vp.height - i.height) / i.height
								: 0;
							var w = Math.ceil(resizerPreviewWindowRect.width) + Math.round(MaxLeft) > Math.round(previewRect.width)
								? Math.round(previewRect.width) - Math.round(MaxLeft)
								: Math.ceil(resizerPreviewWindowRect.width);
							var h = Math.ceil(resizerPreviewWindowRect.height) + Math.round(MaxTop) > Math.round(previewRect.height)
								? Math.round(previewRect.height) - Math.round(MaxTop)
								: Math.ceil(resizerPreviewWindowRect.height);

							resizerPreviewWindow
								.css({
									width:  w,
									height: h
								});

							resizerPreviewWindow
								.draggable('option', 'containment', [0, 0, Math.round(MaxLeft), Math.round(MaxTop)]);
							moveImage
								.css({
									top: Math.floor(correctedImagePosition.top),
									left: Math.floor(correctedImagePosition.left)
								})
								.draggable('option', 'containment', [
									Math.round(vp.width  < i.width  ? vp.width  - i.width  : (vp.width  - i.width)  / 2),
									Math.round(vp.height < i.height ? vp.height - i.height : (vp.height - i.height) / 2),
									Math.round(vp.width  < i.width  ? 0 : (vp.width  - i.width)  / 2),
									Math.round(vp.height < i.height ? 0 : (vp.height - i.height) / 2)
								]);

							sync();
						}
					});

					var html = [
						'<div id="resizer-preview" class="resizer-preview">',
						'	<img galleryimg="no" src="'+ imageSrc +'" width="100%" height="100%">',
						'</div>',
						'<div id="resizer-preview-window" class="resizer-preview-window" style="background: white;"></div>',
						'<img id="move-image" galleryimg="no" src="'+ imageSrc +'" collapsed="yes" orig_width="'+ img.width +'" orig_height="'+ img.height +'">'
					].join('');

					img.onload = null;
					img.onerror = null;
					img = null;

					$('#cont')
						.css('overflow', 'hidden')
						.html(html);

					moveImage = $('#move-image');

					resizerPreview = $('#resizer-preview');
					resizerPreviewWindow = $('#resizer-preview-window');

					resizerPreviewWindow.css({'opacity': '0.8'});

					resizerPreview
						.bind('mousedown', function(event) {
							var imageDims = {
								width: resizerPreview.width(),
								height: resizerPreview.height()
							};
							var previewDims = {
								width: resizerPreviewWindow.width(),
								height: resizerPreviewWindow.height()
							};
							var left = event.clientX - previewDims.width / 2;
							var top = event.clientY - previewDims.height / 2;

							resizerPreviewWindow.css({
								left: left < 0
									? 0
									: left > imageDims.width - previewDims.width
										? imageDims.width - previewDims.width
										: left,
								top: top < 0
									? 0
									: top > imageDims.height - previewDims.height
										? imageDims.height - previewDims.height
										: top
							});

							sync(true);
						});

					resizerPreviewWindow
						.draggable({
							scroll: false,
							drag: function(event, ui) {
								sync(true);
							}
						});
					moveImage
						.attr('title', 'Щёлкните 2 раза, чтобы развернуть картинку')
						.bind('dblclick', function(event) {
							var cImg = moveImage;
							var isCollapsed = cImg.attr('collapsed') == 'yes';

							var vp = getViewport();
							var i  = getImageRect(cImg);

							if (isCollapsed && (vp.width < i.width || vp.height < i.height)) {
								cImg.attr('collapsed', 'no');
								cImg.css('cursor', 'move');
								cImg.attr('title', '');
								cImg.css({
									width:  parseInt(cImg.attr('orig_width'),  10),
									height: parseInt(cImg.attr('orig_height'), 10),
									top:    parseInt(vp.height < i.height ? 0 : (vp.height - i.height) / 2),
									left:   parseInt(vp.width  < i.width  ? 0 : (vp.width  - i.width)  / 2)
								});
								moveImage
									.draggable('enable');

								resizerPreview
									.show();
								resizerPreviewWindow
									.show();
							} else {
								cImg.attr('collapsed', 'yes');
								cImg.css('cursor', 'pointer');
								cImg.attr('title', 'Щёлкните 2 раза, чтобы развернуть картинку');
								moveImage
									.draggable('disable');

								resizerPreviewWindow
									.hide();
								resizerPreview
									.hide();
							}
							$(window).trigger('resize', [true]);
							event.preventDefault();
						})
						.draggable({
							zIndex: 1000,
							scroll: false,
							stop: function(event, ui) {
							},
							drag: function(event, ui) {
								sync();
							}
						})
						.draggable('disable');

					$(window).trigger('resize', [true]);
					function sync(isPreviewDragged) {
						var vp = getViewport();
						var i  = getImageRect(moveImage);
						var ii = fitRectIntoViewport(i, vp);

						if (isPreviewDragged) {
							var previewRect = getPreviewRect(ii);

							var css = {};
							if (vp.width < i.width) {
								css.left = -Math.ceil(parseFloat(resizerPreviewWindow.css('left')) * i.width / previewRect.width);
								css.left = vp.width - i.width > css.left
									? vp.width - i.width
									: css.left;
							}
							if (vp.height < i.height) {
								css.top = -Math.ceil(parseFloat(resizerPreviewWindow.css('top')) * i.height / previewRect.height);
								css.top = vp.height - i.height > css.top
									? vp.height - i.height
									: css.top;
							}

							moveImage.css(css);
						} else {
							var tl = getPreviewWindowPosition(i, vp, {
								top:  parseFloat(moveImage.css('top')),
								left: parseFloat(moveImage.css('left'))
							}, getPreviewRect(ii));

							resizerPreviewWindow
								.css({
									top:  Math.round(tl.top),
									left: Math.round(tl.left)
								});
						}

						
					}
				}
				img.onerror = function() {
					img.onload = null;
					img.onerror = null;
					img = null;
				}
				img.src = imageSrc;
			}

			var head = null,
			    getTargetNode = null;

			$(function() {
				var wo = getWindowOpener();

				if (!wo || !QParams['id']) {
					$('#cont')
						.css({ padding: 30 })
						.html('<div class="eU-error"><h2 class="eU-error-title">Невозможно отобразить элемент</h2><div class="eU-error-text">Возможно вы закрыли окно с курсом.</div></div>');
					return;
				}

				head = wo.head;
				getTargetNode = wo.getTargetNode;

				var currentObject = null;
				var currentNode = null;
				var objectsHash = wo.object_write_to_window.objectsHash;

				currentNode = wo._Xml.$(wo.head, QParams['id']);
				if (objectsHash && objectsHash[QParams['id']]) {
					currentObject = objectsHash[QParams['id']];
				}
				if (!currentNode || !currentObject) {
					$('#cont')
						.css({ padding: 30 })
						.html('<div class="eU-error"><h2 class="eU-error-title">Невозможно отобразить элемент</h2><div class="eU-error-text">Возможно вы закрыли окно с курсом.</div></div>');
					return;
				}

				document.title = currentObject.title;

				if (_Xml.attr(currentNode, 'type') == 'image') {
					embedImage(_Xml.attr(currentNode, 'src'));
				} else {
					$('#cont')
						.html(currentObject.text);
					
					$('div.flash-media-object-placeholder').each(function () {
						var $this = $(this)
						  , options;
						if (!$this.data('flash-is-loading')) {
							$this.data('flash-is-loading', 'yes');
							options = {
								width: $this.data('width'),
								height: $this.data('height')
							};
							embedFlashMovie($this.data('dbid'), options);
						}
					});
					
					if (_Xml.name(currentNode) == 'object') {
						$('#cont').css('overflow', 'hidden');
					}
					monitorInnerLinks();
				}
				self.focus();
			});

			function checkRights(element) {
				var wo = getWindowOpener();
				if (!wo) {
					return false;
				}
				return wo.checkRights(element);
			}
			function canNavigateToNodeOnServer(element) {
				var wo = getWindowOpener();
				if (!wo) {
					return false;
				}
				return wo.canNavigateToNodeOnServer(element);
			}

			function monitorInnerLinks() {
				try {
					$('#cont').bind('mousemove click keyup', checkInnerLinks);
				} catch (error) {}
			}
		</script>
	</head>
	<body onunload="unloadFlashObjects();" class="object-popup">
		<div id="cont" class="content"></div>
	</body>
</html>
