/**
 * @author Vlad Yakovlev (red.scorpix@gmail.com)
 * @link www.scorpix.ru
 * @requires jQuery
 * @requires jCommon.popupBlock
 */

$(function() {

	var top = 50;

	/**
	 * Попап вопроса.
	 */
	var popupQuestionEl = $('.popup_question');

	if (popupQuestionEl.length) {
		popupQuestionEl = popupQuestionEl.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupQuestionEl.appendTo('body');

		$c.popupBlock(popupQuestionEl, {
			close: popupQuestionEl.find('.close'),
			link: $('.popup_question_link'),
			fader: faderEl,
			beforeShow: function() {
				popupQuestionEl.css('top', $(window).scrollTop() + top);
			}
		});
	}

	/**
	 * Попап отзыва.
	 */
	var popupFeedbackEl = $('.popup_feedback');

	if (popupFeedbackEl.length) {
		popupFeedbackEl = popupFeedbackEl.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupFeedbackEl.appendTo('body');

		$c.popupBlock(popupFeedbackEl, {
			close: popupFeedbackEl.find('.close'),
			link: $('.popup_feedback_link'),
			fader: faderEl,
			beforeShow: function() {
				popupFeedbackEl.css('top', $(window).scrollTop() + top);
			}
		});
	}

	/**
	 * Попап заявки для физ. лица.
	 */
	var popupHumanOrder = $('.popup_human_order');

	if (popupHumanOrder.length) {
		popupHumanOrder = popupHumanOrder.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupHumanOrder.appendTo('body');

		$c.popupBlock(popupHumanOrder, {
			close: popupHumanOrder.find('.close'),
			link: $('.popup_human_order_link'),
			fader: faderEl,
			beforeShow: function(evt) {
				var
					id = $c.attrSuffix($(evt.currentTarget), 'training_id_'),
					date = $(evt.currentTarget).attr('title'),
					place = $(evt.currentTarget).find('.place').text(),
					time = $(evt.currentTarget).find('.time').text(),
					type = $(evt.currentTarget).find('.type').text();

				if (false !== id) {
					var
						selectFieldEl = popupHumanOrder.find('.training .field'),
						selectEl = selectFieldEl.find('select'),
						dateFieldEl = popupHumanOrder.find('.date_field .field'),
						dateEl = dateFieldEl.find('input'),
						placeFieldEl = popupHumanOrder.find('.place_field .field'),
						placeEl = placeFieldEl.find('input'),
						timeFieldEl = popupHumanOrder.find('.time_field .field'),
						timeEl = timeFieldEl.find('input'),
						typeFieldEl = popupHumanOrder.find('.type_field .field'),
						typeEl = typeFieldEl.find('input');

					selectEl.css('display', 'none').val(id);
					dateEl.css('display', 'none').val(date);
					placeEl.css('display', 'none').val(place);
					timeEl.css('display', 'none').val(time);
					typeEl.css('display', 'none').val(type);
					$('<em></em>').text(selectEl.find('option:selected').text()).appendTo(selectFieldEl);
					$('<em></em>').text(date).appendTo(dateFieldEl);
					$('<em></em>').text(place).appendTo(placeFieldEl);
					place || popupHumanOrder.find('.place_field').addClass('hidden');
					$('<em></em>').text(time).appendTo(timeFieldEl);
					time || popupHumanOrder.find('.time_field').addClass('hidden');
					$('<em></em>').text(type).appendTo(typeFieldEl);
					type || popupHumanOrder.find('.type_field').addClass('hidden');
				}

				popupHumanOrder.css('top', $(window).scrollTop() + top);
			},
			afterHide: function() {
				var
					selectFieldEl = popupHumanOrder.find('.training .field'),
					selectEl = selectFieldEl.find('select'),
					dateFieldEl = popupHumanOrder.find('.date_field .field'),
					dateEl = dateFieldEl.find('input'),
					placeFieldEl = popupHumanOrder.find('.place_field .field'),
					placeEl = placeFieldEl.find('input'),
					timeFieldEl = popupHumanOrder.find('.time_field .field'),
					timeEl = timeFieldEl.find('input'),
					typeFieldEl = popupHumanOrder.find('.type_field .field'),
					typeEl = typeFieldEl.find('input');

				selectEl.css('display', '');
				selectFieldEl.find('em').remove();
				dateEl.css('display', '').val('');
				dateFieldEl.find('em').remove();
				placeEl.css('display', '').val('');
				placeFieldEl.find('em').remove();
				popupHumanOrder.find('.place_field').removeClass('hidden');

				timeEl.css('display', '').val('');
				timeFieldEl.find('em').remove();
				popupHumanOrder.find('.time_field').removeClass('hidden');

				typeEl.css('display', '').val('');
				typeFieldEl.find('em').remove();
				popupHumanOrder.find('.type_field').removeClass('hidden');
			}
		});
	}


	/**
	 * Попап заявки для юр. лиц.
	 */
	var popupCorporateOrder = $('.popup_corporate_order');

	if (popupCorporateOrder.length) {
		popupCorporateOrder = popupCorporateOrder.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupCorporateOrder.appendTo('body');

		$c.popupBlock(popupCorporateOrder, {
			close: popupCorporateOrder.find('.close'),
			link: $('.popup_corporate_order_link'),
			fader: faderEl,
			beforeShow: function() {
				popupCorporateOrder.css('top', $(window).scrollTop() + top);
			}
		});
	}

	/**
	 * Попап отзыва.
	 */
	var popupComment = $('.popup_comment');

	if (popupComment.length) {
		popupComment = popupComment.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupComment.appendTo('body');

		$c.popupBlock(popupComment, {
			close: popupComment.find('.close'),
			link: $('.message_header .add_comment a'),
			fader: faderEl,
			beforeShow: function() {
				popupComment.css('top', $(window).scrollTop() + top);
			}
		});
	}

	function people(els, count) {
		els = $(els);

		els.each(function() {
			$(this).addClass('people' + random(1, 35));
		});

		function random(min, max) {
			min = parseInt(min);
			max = parseInt(max);

			return Math.floor(Math.random() * (max - min + 1)) + min;
		}
	}

	function rotateText(el) {
		el = $(el);

		if (el) {
			el = el.eq(0);

			if ($c.browser.msie) {

				var ieRotate = function(el) {

					var text = el.text();
					el.text('');

					var rotateEl = $('<span />').css({
						backgroundColor: el.css('background-color'),
						display: 'block',
						left: 0,
						position: 'absolute',
						top: 0
					}).text(text).appendTo(el);
					rotateEl[0].style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand')";

					var
						rad = -90 * (Math.PI * 2 / 360),
						costheta = Math.cos(rad),
						sintheta = Math.sin(rad),
						item = rotateEl[0].filters.item(0);

		    	item.M11 = costheta;
		    	item.M12 = -sintheta;
		    	item.M21 = sintheta;
		    	item.M22 = costheta;

		    	return rotateEl;
				};

				return ieRotate(el);
			} else if ($c.support.svg) {

				var svgRotate = function(el) {

					var
						width = el.width(),
						height = el.height(),
						fontSize = parseInt(el.css('font-size')),
						text = el.text();

					el.text('');

					var svgEl = $c.svg('svg').svgAttr({
						viewBox: [0, 0, height, width].join(' '),
						width: height,
						height: width
					}).appendTo(el);

					var textEl = $c.svg('text').svgAttr({
						x: 1,
						y: 16,
						fill: el.css('color'),
						transform: 'rotate(-90) translate(-' + width + ', 0)',
						fontSize: fontSize
					}).appendTo(svgEl);

					textEl.append(textEl[0].ownerDocument.createTextNode(text));

					return svgEl;
				}

				return svgRotate(el);
			}

			return el;
		} else {
			return null;
		}
	}

	people('.people', 30);

	$('#left_panel .trainings .section').each(function() {
		var
			sectionEl = $(this),
			el = rotateText(sectionEl.find('h3')),
			height;

		if ($c.browser.msie) {
			height = el.height();
		} else if ($c.support.svg) {
			height = parseInt(el.svgAttr('height'));
		}

		sectionEl.find('h3').height(height);

		if ($c.browser.msie && 6 >= parseInt($c.browser.version)) {
			sectionEl.css('height', height);
		} else {
			sectionEl.css('min-height', height);
		}
	});

	$('form').append('<input type="hidden" name="iamhuman" value="1" />');
});

function video(popupVideoEl, info) {

	function player(parentEl, videoFile, width, height, preview) {
		if (!width) {
			width = 420;
		}
		if (!height) {
			height = 260;
		}

		var id = 'video' + Math.round(Math.random() * 10000);

		$('<div id="' + id + '" />').appendTo(parentEl);

		setTimeout(function() {
			swfobject.embedSWF('/f/media/player.swf', id, width, height, "9.0.0", null, {
				file: videoFile,
				image: preview,
				skin: '/f/media/simple.swf',
				dock: 'true'
			}, {
				allowfullscreen: true,
				allowscriptaccess: 'always'
			});
		}, 1);
	}

	if (popupVideoEl.length) {
		popupVideoEl = popupVideoEl.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupVideoEl.appendTo('body');

		var popup = $c.popupBlock(popupVideoEl, {
			close: popupVideoEl.find('.close'),
			fader: faderEl,
			beforeShow: function() {
				popupVideoEl.css('top', $(window).scrollTop() + top);
			},
			afterShow: function() {
				change(0);
			}
		});

		$('.video_panel .pseudo').click(function() {
			setTimeout(function() {
				popup.show();
			}, 1);
		});

		popupVideoEl.find('.item').each(function(index) {
			var el = $(this);

			el.find('.pseudo').click(function() {
				if (!el.hasClass('selected')) {
					change(index);
				}
			});
		});

		function change(index) {
			popupVideoEl.find('.selected').removeClass('selected');
			popupVideoEl.find('.video').empty();
			player(popupVideoEl.find('.video'), info[index].file, info[index].width, info[index].height, info[index].preview);

			var selEl = popupVideoEl.find('.item').eq(index);
			selEl.addClass('selected');
			popupVideoEl.find('h3').text(selEl.find('.name').text());
		}
	}
}

function audio(popupAudioEl, files) {

	function player(parentEl, audioFile) {
		var id = 'audio' + Math.round(Math.random() * 10000);

		$('<div id="' + id + '" />').appendTo(parentEl);

		setTimeout(function() {
			swfobject.embedSWF('/f/media/player_mp3_maxi.swf', id, 200, 20, "9.0.0", null, {
				mp3: audioFile,
				showvolume: 1,
				showstop: 1
			}, {
				allowscriptaccess: 'always'
			});
		}, 1);
	}

	if (popupAudioEl.length) {
		popupAudioEl = popupAudioEl.eq(0);

		var faderEl = $('body>.fader');

		if (!faderEl.length) {
			faderEl = $('<div class="fader hidden"></div>').appendTo('body');
		}

		popupAudioEl.appendTo('body');

		var popup = $c.popupBlock(popupAudioEl, {
			close: popupAudioEl.find('.close'),
			fader: faderEl,
			beforeShow: function() {
				popupAudioEl.css('top', $(window).scrollTop() + top);
			},
			afterShow: function() {
				change(0);
			}
		});

		$('.audio_panel .pseudo').click(function() {
			setTimeout(function() {
				popup.show();
			}, 1);
		});

		popupAudioEl.find('.item').each(function(index) {
			var el = $(this);

			el.find('.pseudo').click(function() {
				if (!el.hasClass('selected')) {
					change(index);
				}
			});
		});

		function change(index) {
			popupAudioEl.find('.selected').removeClass('selected').find('.audio').empty();
			var selEl = popupAudioEl.find('.item').eq(index);
			player(selEl.addClass('selected').find('.audio'), files[index]);
		}
	}
}