var globalTexts = {
	ru: {
		close: "Закрыть"
	},
	en: {
		close: "Close"
	}
}

var Device = {};

Device.isiPhone = function() {
   return navigator.userAgent.indexOf('iPhone') != -1;
}

Device.isiPad = function() {
   return navigator.userAgent.indexOf('iPad') != -1;
}


$(function() {
	
	var lang = $('body').hasClass('ru') ? 'ru' : 'en';
	
	
	// Main menu
	
	
	$('.Level1 .tab, .Level1 .notOpenedTab').each(function() {
		if (!$(this).find('ul').hasClass('submenu')) {
			var subMenu = $(this).next('ul');
			subMenu.remove();
			$(this).append(subMenu);
		} 
	});
	
	$('.Level1 li.tab a').not('.Level3 a, .Level2 .notOpenedTab a').click(function() {
		container = $(this).closest('li.tab');
		
		thirdLevel = container.find('ul.Level3');
		
		if (container.hasClass('active')) {
			container.removeClass('active');
			thirdLevel.removeClass('active');	
		} else {	
			$('.Level2 .tab, .Level3').removeClass('active');
			container.addClass('active');
			thirdLevel.addClass('active');
		}
		return false;
	});
	
	$('.Level3 .tab').hover(
		function() {
			var fourthLevel = $(this).find('ul.Level4');
			
			$(this).addClass('active');
			fourthLevel.fadeIn(150);
		
		},
	
		function() {
			var fourthLevel = $(this).find('ul.Level4');
			
			$(this).removeClass('active');
			fourthLevel.fadeOut(150);
		});
	
	
	// Programm
	
	var contentBlock = $('.b-content');
	
	$(document).click(function() {
		$('.b-popup').removeClass('opened').fadeOut(150);
	});
	
	$('.b-program .programEvent .t a').hover(
		function() {
			$(this).closest('.programEvent').addClass('h');
		},
		function() {
			$(this).closest('.programEvent').removeClass('h');
		}
	).click(function() {
		var event_object = $(this).closest('.programEvent'); 
			
		event_object.toggleClass('detailView');
		
		if (event_object.data('loaded'))
			return false;

		var link = $('.programDetails', event_object).attr('rel');

		$('.programDetails', event_object).load(link, function() {
			event_object.data("loaded", true);
		});

		return false;
	});
	
	function getClientHeight() {
	  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	}
	
	$('div.b-popup').livequery(function() {
		var _this = $(this);
		_this.remove();
		$('body').append(_this);
	});
	
	
	
	$('[rel="reporter"]').live("click", function() {
		var popup = $('.b-popup[rel='+$(this).attr('id')+']');
		$('.b-popup').not(popup).css({display: "none"});
		
		var targetTop = $(this).offset().top+40;
		
		popupTop = targetTop-16;
		
		
		if ( (getClientHeight() + $(document).scrollTop()) > (targetTop + popup.height()) ) {
			popupTop = targetTop-16;
		} else {
			popupTop = targetTop-popup.height()-60;	
		}
		
		if (popupTop <= $(document).scrollTop())
			popupTop = $(document).scrollTop() + 5;
		
		popup.addClass('opened').css({top: popupTop + "px"}).fadeIn(150);
		return false;
	});
	
	var forumMap = $('#js-forumMap');
	
	$('.b-popup .i-close').click(function() {
		$(this).closest('.b-popup').removeClass('opened').fadeOut(150);
		$('img', forumMap).removeClass('selected').removeClass('active-object');
	});
	
	// TV-translations
	
	$('.scene .opening a').click(function() {
		$(this).closest('tr').find('ul').toggleClass('hidden');
		return false;
	});
	
	$('.b-questionnaire textarea').focus(function()
	{
		$(this).animate({height: "108px", width: "676px"}, 200);
	});
	
	$('.b-questionnaire textarea').blur(function()
	{
		if ( $(this).val() == '' )
		{
			$(this).animate({height: "18px", width: "266px"}, 200);
		}
	});
	
	$('.b-episodes').each(function()
	{
		var show_hide = $(this).find('.toggle-episodes');
		var list = $(this).find('.b-episodes-list');
		
		show_hide.click(function()
		{
			list.toggleClass('hidden');
			
			return false;
		});
	});
	
	/* Watermarks */
	
	// Watermarks					   
						   
	$('.b-input input, .b-input textarea').each(function (i) {
		if ($(this).val() != "") {
			$(this).prev("label").addClass('hidden');
		}; 
    });
	
	$('.b-input input, .b-input textarea').focus(function () {
		$(this).prev("label").addClass('hidden');
	});

	$('.b-input input, .b-input textarea').blur(function () {
		if ($(this).val() == "")
			$(this).prev("label").removeClass('hidden');
	});
	
	
	/* Поле поиска */
	
	$('.js-search').each(function()
	{
		var searchExample = $(this).val();

		$(this).focus(function()
		{
			
			if ( $(this).val() == searchExample )
			{
				$(this).val("");
			}
			
		}).blur(function()
		{
			
			if ( $(this).val() == "" )
			{
				$(this).val(searchExample);
			}
			
		});
	});
	
	for (i=0; i<=2; i++)
	{
		$($('.js-tempFix p')[i]).css("display", "block");
	}
	
	$(window).load(function()
	{
		$('.b-grayscale-img img').each(function()
		{
			grayscale(this);
		}).livequery(function()
		{
			grayscale(this);
		});
	});
	
	$('.b-quickSearch-result_people__item, .b-peaople-tagged .b-grayscale-img, .b-tm-video-episodes .b-grayscale-img').live('mouseover', function()
	{
		$(this).addClass('hover');
	}).live('mouseout', function()
	{
		$(this).removeClass('hover');
	});
	
	$('.js-personePic').hover(function()
	{
		$(this).find('.desc').slideDown(200);
	},
	function()
	{
		$(this).find('.desc').slideUp(200);
	});
	
	// SlideShow
	
	$('.js-slideShowContainer').each(function()
	{
		var timer = $(this).attr('time') || 7000;
		var Slides = $(this).find('.js-slideShowBlock');

		function participantsSlideShow() {
			var curSlide = Slides.filter('.active');
			curSlide.fadeOut(300, function() {
				var nextSlide;

				if ( $(this).next('.js-slideShowBlock').length == 0 )
				{
					nextSlide = $(Slides[0]);
				} else {
					nextSlide = $(this).next('.js-slideShowBlock');
				}

				$(this).removeClass('active');
				nextSlide.addClass('active').fadeIn(300);
			});

			setTimeout(participantsSlideShow, timer);
		}

		setTimeout(participantsSlideShow, timer);
	});
	
	// Spoiler
	
	$('.js-spoiler').each(function()
	{
		var content = $(this).find('.b-spoiler__content');
		
		$(this).find('.b-spoiler__t').click(function()
		{
			content.slideToggle(300);
		});
	});

});
