function teasers() {
	var
		jFader = $("#teasers-fader"),
		jPage = $("#teaser-page"),
		jPageContent = jPage.find(".content:eq(0)"),

		jVerticalWrapper = $(".v-opened-teaser-wrapper"),
		jHorizontalWrapper = $(".h-opened-teaser-wrapper"),
		
		jWrapper,
		jClone,
		
		oVisibleTransparentCss = {
			opacity: 0,
			display: "block"
		},
		
		oTeaserType,
		
		sTeaserHover = ".teaser-hover";
	
	resetTeaserType();
	attachEvents();
	
	
	function resetTeaserType() {
		oTeaserType = {
			vertical: false,
			horizontal: false,
			left: false,
			right: false
		}
	}
	

	function attachEvents() {
		$(".h-teaser")
			.not(".no-content").not(".delta-bank-logo").click(function(e){
				openFromHorizontal($(this));
				return false;
			})
			.end()
			.filter(".inner-content").click(function(e){
				showHorizontalInnerContent($(this));
				return false;
			})
		
		$(".v-teaser").not(".no-content").click(function(){
			openFromVertical($(this));
			return false;
		});
		
		jFader.click(close);
		
		$(sTeaserHover).css("opacity", 0);
		
		$(".h-teaser, .v-teaser")

			.mouseenter(mouseEnter)
			.mouseleave(mouseLeave)
			.click(track)
	}
	
	function openFromHorizontal(jTeaser) {
		var
			newHeight,
			currentHeight = 100;
		
		showFader();
		
		oTeaserType.horizontal = true;
		oTeaserType.vertical = false;
		
		jWrapper = jHorizontalWrapper;
		
		jClone = $("<div />", {
			className: jTeaser.get(0).className,
			html: jTeaser.html()
		});
		

		oTeaserType.bottom = jTeaser.closest(".h-teasers").hasClass("bottom");
		oTeaserType.top = !oTeaserType.bottom;
		
		if (oTeaserType.top) {
			jPage.addClass("top").removeClass("bottom");
			jWrapper.removeClass("bottom");
		} else {
			jPage.addClass("bottom").removeClass("top");
			jWrapper.addClass("bottom");
		}
		
		setHorizontalWrapper(jTeaser);
		jWrapper.html(jClone);
		
		jWrapper.fadeIn();
		
		$(window).resize(function(){
			setHorizontalWrapper(jTeaser);
		});
		
		jPage.removeClass("opened hidden");
		jPageContent.animate({ height: "100%" }, "linear", tidyOpenedPage);
	}
	
	function showHorizontalInnerContent(jTeaser) {
		oTeaserType.horizontal = true;
		oTeaserType.vertical = false;
		
		jWrapper = jHorizontalWrapper;
		
		oTeaserType.bottom = jTeaser.closest(".h-teasers").hasClass("bottom");
		oTeaserType.top = !oTeaserType.bottom;
		
		if (oTeaserType.top) {
			jWrapper.removeClass("bottom");
		} else {
			jWrapper.addClass("bottom");
		}
		
		setHorizontalWrapper(jTeaser);
		jWrapper.html($(".teaser-inner-content-holder").html());
		
		jWrapper.fadeIn();
		
		jWrapper.find(".tooltip-close").one("click", closeInnerHorizontalContent);
		
		$(window).resize(function(){
			setHorizontalWrapper(jTeaser);
		});
	}
	
	function openFromVertical(jTeaser) {
		var
			newWidth,
			currentWidth = jTeaser.width();
		
		jWrapper.hide();
		tidyClosedTeaser();
		resetTeaserType();
		
		showFader();
		
		oTeaserType.vertical = true;
		
		jWrapper = jVerticalWrapper;
		
		jClone = $("<div />", {
			className: jTeaser.get(0).className,
			html: jTeaser.html(),
			width: currentWidth,
			click: function(e){ e.stopPropagation(); }
		});
		

		oTeaserType.right = jTeaser.closest(".v-teasers").hasClass("right");
		oTeaserType.left = !oTeaserType.right;

		
		if (oTeaserType.left) {
			jPage.addClass("left").removeClass("right");
			jWrapper.removeClass("right");
		} else {
			jPage.addClass("right").removeClass("left");
			jWrapper.addClass("right");
		} 
		
		jWrapper
			.removeAttr("style")
			.css({ width: currentWidth })
			.html(jClone);
		

		newWidth = getFullTeaserWidth(jTeaser, currentWidth);
		
		if (newWidth != currentWidth) {
			jWrapper
				.css(oVisibleTransparentCss)
				.animate({
					width: newWidth,
					opacity: 1
				}, tidyOpenedTeaser);
		} else {
			jWrapper.fadeIn(tidyOpenedTeaser);
		}
		
		jPage.removeClass("opened hidden");
		jPageContent.animate({ width: "100%" }, "linear", tidyOpenedPage);
		
		jWrapper.bind("click", close);
	}
	
	function close() {
		if (oTeaserType.horizontal) {
			closeFromHorizontal();
		}
		if (oTeaserType.vertical) {
			closeFromVertical();
		}
	}
	
	function closeFromHorizontal() {
		hideFader();
	
		jPageContent.animate({ height: "0" }, "linear", tidyClosedPage);
		jWrapper.fadeOut(tidyClosedTeaser);
	
		resetTeaserType();
	}
	
	function closeFromVertical() {
		var
			jTeaser = jWrapper.find(".v-teaser"),
			teaserWidth = jTeaser.width(),
			fullTeaserWidth = getFullTeaserWidth(jTeaser, teaserWidth);
		
		hideFader();
		
		jTeaser.width(teaserWidth);
		
		jPageContent
			.animate({ width: "0" }, tidyClosedPage);
		
		if (fullTeaserWidth != teaserWidth) {
			jWrapper
				.width(fullTeaserWidth)
				.animate({
					width: teaserWidth,
					opacity: 0
				}, tidyClosedTeaser);
		} else {
			jWrapper.fadeOut(tidyClosedTeaser);
		}
		
		jWrapper.unbind("click", close);
		
		resetTeaserType();
	}
	
	function closeInnerHorizontalContent() {
		jWrapper.fadeOut(tidyClosedTeaser);
		resetTeaserType();		
	}
	
	function showFader() {
		jFader
			.css(oVisibleTransparentCss)
			.animate({ opacity: 0.2 });			
	}
	
	function hideFader() {
		jFader.fadeOut();
	}
	
	function getFullTeaserWidth(jTeaser, teaserWidth) {
		var
			eBg = jTeaser.find(".img-teaser-background").get(0),
			width = 0,
			teaserWidth = teaserWidth || jTeaser.width();
		
		if (eBg) {
			if (oTeaserType.right) {
				width = teaserWidth - eBg.offsetLeft;
			} else {
				width = eBg.offsetLeft + eBg.offsetWidth;
			}
		}
		
		width = Math.max(width, teaserWidth);
		
		return width;
	}
	
	function tidyOpenedTeaser() {
		jWrapper.removeAttr("style").addClass("opened");
		jClone.removeAttr("style");
	}
	
	function tidyOpenedPage() {
		jPage.addClass("opened");
		jPageContent.removeAttr("style");		
	}
	
	function tidyClosedTeaser() {
		jWrapper.removeClass("opened").removeAttr("style").html('');
		jClone && jClone.remove();
	}
	
	function tidyClosedPage() {
		jPage
			.addClass("hidden")
			.removeClass("opened top bottom left right");
		jPageContent.removeAttr("style");
	}
	
	function setHorizontalWrapper(jTeaser){
		jWrapper
			.css({
				left: jTeaser.position().left + jTeaser.parent().position().left,
				width: jTeaser.width()
			});
	}
	
	function mouseEnter() {
		$(this).find(sTeaserHover).stop().animate({ opacity: 0.33 }, 200)
	}
	
	function mouseLeave() {
		$(this).find(sTeaserHover).stop().animate({ opacity: 0 }, 200)
	}
	
	function track(event){
		_gaq.push(['_trackEvent','Переход на сайт', 'По баннеру', $(this).attr('href')]);
	}
}



function teasersScroll() {
	var
		jTeasersContainer = $(".h-teasers"),
		jTeasersWrapper = $(".h-teasers-wrapper"),
		jjTeasers,
		jSampleTeaser,
		
		jArrowLeft = jTeasersContainer.find(".icon-big-arrow-left"),
		jArrowRight = jTeasersContainer.find(".icon-big-arrow-right"),
		
		jFader = jTeasersContainer.find(".hover-fader");
	
	if (jArrowLeft.length && jArrowRight.length) {
		init();
	}
	
	
	function init() {
		findTeasers();
		jSampleTeaser = jjTeasers.not(".delta-bank-logo").eq(0),
		
		attachEvents();
	}
	
	function findTeasers() {
		jjTeasers = jTeasersWrapper.find(".h-teaser");
	}
	
	
	function attachEvents() {
		jArrowLeft.click(scrollRight);
		jArrowRight.click(scrollLeft);
		
		jArrowLeft.add(jArrowRight).hover(function(){
			jFader
				.css("opacity", 0)
				.show()
				.animate({ "opacity": 0.2 }, 200);
		}, function(){
			jFader.animate({ "opacity": 0 }, 200, function(){ jFader.hide(); });
		});
	}
	
	
	function scrollRight() {
		if (!jTeasersWrapper.is(":animated")) {
			do {
				checkRightTeasers();
			} while (jjTeasers.get(0).getBoundingClientRect().left - jjTeasers.last().get(0).clientWidth + teaserWidth() > 0);
	
			jTeasersWrapper.animate({
				left: "+=" + teaserWidth() + "px"
				//marginLeft: "+=25%"
			})
		}
	}
	
	function scrollLeft() {
		if (!jTeasersWrapper.is(":animated")) {
			jTeasersWrapper.animate({
				left: "-=" + teaserWidth() + "px"
				//marginLeft: "-=25%"
			}, function(){
				do {
					var hasLeft = checkLeftTeasers();
				} while (hasLeft)
			});
		}
	}
	
	function checkLeftTeasers() {
		if (jjTeasers.get(0).getBoundingClientRect().right < 0) {
			jTeasersWrapper
				.append(jjTeasers.eq(0))
				.css("left", jTeasersWrapper.get(0).offsetLeft + jjTeasers.get(0).clientWidth);
			findTeasers();
			
			return true;
		} else {
			return false;
		}
	}
	
	function checkRightTeasers() {
		findTeasers();
		
		jTeasersWrapper
			.prepend(jjTeasers.last())
			.css("left", jTeasersWrapper.get(0).offsetLeft - jjTeasers.last().get(0).clientWidth);
	}
	
	function teaserWidth() {
		return jSampleTeaser.get(0).clientWidth;
	}
}



function teaserRadio() {
	//document.title = 'hello';
	
	var
		jRadioTeaser = $(".h-teasers-texts .id-21, .h-teasers-texts .id-32, .h-teaser.bid-21, .h-teaser.bid-32"),
		jRadioNowPlaying;

	jRadioTeaser.each(function(){
		$(this).append("<span class='radio-now-playing smaller'></span>");
	})
		
	jRadioNowPlaying = jRadioTeaser.find(".radio-now-playing");
	
	printRadioNowPlaying();
	setInterval(printRadioNowPlaying, 60000);
	
	function printRadioNowPlaying() {
		var sUrl = '/ajax/radio/' + ($.browser.msie ? ('?' + (new Date()).getTime()) : '');
		
		$.getJSON(sUrl, function(hCurrentSong) {
			if (hCurrentSong != null) {
				jRadioNowPlaying.html(hCurrentSong.artist + ' — '+ hCurrentSong.title);
			}
		});		
	}
}



$(function(){

	teasers();
	teasersScroll();
	
	teaserRadio();
	
});
