/**
 * @author Vlad Yakovlev (red.scorpix@gmail.com)
 * @link www.scorpix.ru
 * @requires jQuery
 * @requires jCommon
 * @version 0.1
 * @date 2010-05-26
 */

$(function() {

	function eventReport(rootEl) {
		rootEl = $(rootEl).first();

		var faderEl = rootEl.find('.fader').appendTo('body');

		rootEl.find('li').each(function() {
			var
				clonePopupEl = $(this).find('.popup').remove(),
				linkEl = $(this).find('.pseudo');

			linkEl.click(function() {
				var popupEl = clonePopupEl.clone().appendTo('body');

				var func = function() {
					updatePopupSize(popupEl);
				}
				var first = true;

				var popup = $c.popupBlock(popupEl, {
					link: this,
					close: popupEl.find('.icon_close'),
					fader: faderEl,
					beforeShow: function() {
						popupEl
							.css({
								left: -10000
							})
							.removeClass('hidden');
						func();
						popupEl
							.css({
								left: ''
							})
							.addClass('hidden');
					},
					afterShow: function() {
						$c.measurer.bind(func);

						if (first) {
							if (popupEl.hasClass('popup_report_audio')) {
								initAudio(popupEl);
							} else if (popupEl.hasClass('popup_report_video')) {
								initVideo(popupEl);
							} else if (popupEl.hasClass('popup_report_gallery')) {
								images(popupEl);
							}

							first = false;
						}
					},
					afterHide: function() {
						$c.measurer.unbind(func);

						if (popupEl.hasClass('popup_report_audio')) {
							pauseAudio(popupEl);
						}
					}
				}).show();
			});

			if (clonePopupEl.hasClass('popup_report_video')) {
				var hrefs = location.href.split('#');

				if (1 < hrefs.length && 'video' == hrefs[1]) {
					linkEl.click();
				}
			}
		});

		function initAudio(popupEl) {
			popupEl.find('.item').each(function() {
				var
					id = 'id_' + $c.random(0, 1000000),
					playerEl = $(this).find('.player').attr('id', id),
					linkEl = $(this).find('a');

				$f(id, "/f/global/media/flowplayer-3.2.1.swf", {
					clip: {
						autoPlay: false
					},
					playlist: [linkEl.attr('href')],
					plugins: {
						controls: {
							timeColor: "rgba(0, 0, 0, 1)",
							borderRadius: 0,
							bufferGradient: "none",
							slowForward: true,
							backgroundColor: "rgba(255, 255, 255, 1)",
							volumeSliderGradient: "none",
							slowBackward: false,
							timeBorderRadius: 20,
							progressGradient: "none",
							time: true,
							height: 26,
							volumeColor: "rgba(0, 0, 0, 1)",
							tooltips:{
								marginBottom: 5,
								volume: false,
								scrubber: false,
								buttons: false
							},
							fastBackward: false,
							opacity: 1,
							timeFontSize: 12,
							border: "0px",
							bufferColor: "#a3a3a3",
							volumeSliderColor: "rgba(255, 255, 255, 1)",
							buttonColor: "rgba(0, 0, 0, 1)",
							mute: true,
							autoHide: {
								enabled: false,
								fullscreenOnly: true
							},
							backgroundGradient: "none",
							width: "100pct",
							display: "block",
							sliderBorder: "1px solid rgba(128, 128, 128, 0.7)",
							buttonOverColor: "rgba(0, 0, 0, 1)",
							fullscreen: false,
							timeBgColor: "rgb(0, 0, 0, 0)",
							borderWidth: 0,
							scrubberBarHeightRatio: 0.2,
							bottom: 0,
							stop: false,
							zIndex: 1,
							sliderColor: "rgba(0, 0, 0, 1)",
							scrubberHeightRatio: 0.6,
							tooltipTextColor: "rgba(0, 0, 0, 1)",
							sliderGradient: "none",
							timeBgHeightRatio: 0.8,
							volumeSliderHeightRatio: 0.6,
							timeSeparator: " ",
							name: "controls",
							volumeBarHeightRatio: 0.2,
							left: "50pct",
							tooltipColor: "rgba(0, 0, 0, 0)",
							playlist: false,
							durationColor: "rgba(0, 0, 0, 1)",
							play: true,
							fastForward: true,
							progressColor: "rgba(0, 0, 0, 1)",
							timeBorder: "0px solid rgba(0, 0, 0, 0.3)",
							volume: true,
							scrubber: true,
							builtIn: false,
							volumeBorder: "1px solid rgba(128, 128, 128, 0.7)"
						}
					},
					onLoad: function() {
						playerEl.css({
							width: 405
						});
					}
				});
			});
		}

		function initVideo(popupEl) {
			popupEl.find('.item').each(function() {
				var
					id = 'id_' + $c.random(0, 1000000),
					playerEl = $(this).find('.player').attr('id', id),
					width = playerEl.width(),
					height = playerEl.height(),
					imgEl = $(this).find('img');

				swfobject.embedSWF("/f/global/media/cinemaplayer.swf", id, width, height, "9.0.0", null, {
					showlogo: 'true',
					content: $(this).find('.path').text(),
					zholdershowbg: 'false',
					autoplay: 'false',
					allowtimeline: 'true',
					zholdersetmask: 'true',
					allowzoom: 'false',
					zholderpadding: '0',
					timeLineIcon: 'false',
					zholderbgalpha: '0',
					poster: imgEl.length ? imgEl.attr('src') : ''
				}, {
					allowFullScreen: 'true',
					allowScriptAccess: 'sameDomain',
					bgcolor: '#ffffff',
					wmode: 'transparent'
				});
			});
		}

		function pauseAudio(popupEl) {
			popupEl.find('.player').css({
				width: 1
			});
		}

		function updatePopupSize(popupEl) {
			var
				minMargin = 30,
				minContentHeight = 100,
				contentEl = popupEl.find('.content'),
				containerEl = popupEl.find('.container'),
				winHeight = $(window).height(),
				oldContentHeight = contentEl.height(),
				containerHeight = containerEl.height(),
				popupHeight = popupEl.height(),
				halfPopupHeight = popupHeight - oldContentHeight,
				maxContentHeight = winHeight - 2 * minMargin - halfPopupHeight,
				contentHeight = containerHeight + 10;

			if (maxContentHeight < minContentHeight) {
				maxContentHeight = minContentHeight;
			}
			if (contentHeight > maxContentHeight) {
				contentHeight = maxContentHeight;
			}

			var top = Math.round((winHeight - halfPopupHeight - contentHeight) / 2);

			contentEl.css('height', contentHeight);
			popupEl.css('top', top);
		}
	}

	function images(popupEl) {
		var
			visibleCount = 10,
			bigPicturesEl = popupEl.find('.big_pictures'),
			bigPicturesContentEl = bigPicturesEl.find('.big_pictures_content'),
			picturesEl = popupEl.find('.pictures'),
			picturesContentEl = picturesEl.find('.pictures_content'),
			imagesCount = picturesEl.find('img').length,
			leftEl = popupEl.find('.icon_calendar_left'),
			rightEl = popupEl.find('.icon_calendar_right'),
			curIndex = 0,
			bigImageWidth = bigPicturesEl.width(),
			pictureWidth = picturesEl.width(),
			imageWidth = picturesEl.find('.pic:first').outerWidth(),
			imageEls = picturesEl.find('img');

		imageEls.each(function(index) {
			$(this).click(function() {
				if (!$(this).hasClass('selected')) {
					changeImage(index);
				}

				return false;
			});
		});
		leftEl.click(function() {
			var
				moveCount = Math.floor(visibleCount / 2),
				newLeft = parseInt(picturesContentEl.css('left')) - moveCount * imageWidth;

			if (0 > newLeft) {
				newLeft = 0;
			}

			picturesContentEl.animate({
				left: -newLeft
			}, {
				duration: 500,
				easing: 'easeInOutCubic',
				complete: updateArrows
			});
		});
		rightEl.click(function() {
			var
				moveCount = Math.floor(visibleCount / 2),
				newLeft = parseInt(picturesContentEl.css('left')) + moveCount * imageWidth;

			if (imageWidth * imagesCount - pictureWidth < newLeft) {
				newLeft = imageWidth * imagesCount - pictureWidth;
			}

			picturesContentEl.animate({
				left: -newLeft
			}, {
				duration: 500,
				easing: 'easeInOutCubic',
				complete: updateArrows
			});
		});

		updateArrows();

		function changeImage(index) {
			bigPicturesContentEl.animate({
				left: -index * bigImageWidth
			}, {
				duration: 500,
				easing: 'easeInOutCubic'
			});
			imageEls.filter('.selected').removeClass('selected');
			imageEls.eq(index).addClass('selected');

			curIndex = index;
		}

		function updateArrows() {
			if (visibleCount > imagesCount) {
				leftEl.addClass('hidden');
				rightEl.addClass('hidden');

				return;
			}

			var left = parseInt(picturesContentEl.css('left'));

			leftEl[0 <= left ? 'addClass' : 'removeClass']('hidden');
			rightEl[-(imageWidth * imagesCount - pictureWidth) >= left ? 'addClass' : 'removeClass']('hidden');
		}
	}

	eventReport('#content .report');
});
