
// vars
var promoPeriodTime = 5000 // время анимации промо-блока
var promoAnimTime = 600 // время анимации промо-блока
var newsPeriodTime = 5000 // период смены новости
var newsAnimTime = 600 // время анимации новости
var logosPeriodTime = 3000 // период смены логотипов
var logosAnimTime = 600 // время анимации логотипов


// easing
jQuery.extend(jQuery.easing, {
	swap1: function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;

		}
	});


var promoAnimTO = false
function promoAnim() {
	$(".b-promo__rarr").click()
	promoAnimTO = setTimeout("promoAnim()", promoPeriodTime)
	}

var logosAnimTO = false
function logosAnim() {
	
	var cur = $(".b-logos TABLE:visible")
	var next = cur.nextAll("TABLE").length ? cur.next() : cur.siblings("TABLE").first()

	cur.fadeOut(logosAnimTime, function(){
		next.fadeIn(logosAnimTime)
	})

	logosAnimTO = setTimeout("logosAnim()", logosPeriodTime)
	}


var newsAnimTO = false
function newsAnim() {

	cur = $(".b-news .cur")

	cur.nextAll("LI").length
		? next = cur.next()
		: next = cur.siblings().first()
	cur.fadeOut(newsAnimTime).removeClass("cur")
	next.fadeIn(newsAnimTime).addClass("cur")

	cur.parent().animate({height:next.height()}, newsAnimTime, "swap1")

	var span = next.find("SPAN")
	var text = span.text()
	span.text("")

	jQuery({count:0}).animate({count:text.length}, {
		duration: text.length < 100 ? text.length*10 : 1000,
		step: function() {
			span.text( text.substring(0, Math.round(this.count)) );
		}
	});

	newsAnimTO = setTimeout("newsAnim()", newsPeriodTime)
	}



$(document).ready(function(){

	// ie6 hover
	$(".b-nav LI").hover(
		function () {$(this).addClass("hover");}, 
		function () {$(this).removeClass("hover");}
		);


	// nav anim
	if (!$.browser.msie) {
		$(".b-nav LI .b-drop__tale").show().fadeOut(0)
		$(".b-nav LI .dh__t").show().fadeOut(0)
		$(".b-nav LI").hover(
			function () {
				$(this).find(".b-drop__tale,.dh__t").stop().clearQueue().fadeTo(200, 1)
				$(this).siblings().find(".b-drop__tale,.dh__t").stop().clearQueue().fadeTo(50, 0, function(){$(this).hide()})
			}, 
			function () {$(this).find(".b-drop__tale,.dh__t").clearQueue().fadeTo(200, 0, function(){$(this).hide()})}
			);
		}
	else {
		$(".b-nav LI").hover(
			function () {$(this).find(".b-drop__tale,.dh__t").show().siblings();}, 
			function () {$(this).find(".b-drop__tale,.dh__t").hide();}
			);
		$(".b-nav").hover(
			function () {},
			function () {$(this).find(".b-drop__tale,.dh__t").hide();}
			);
		}

	$(".b-nav LI").each (function (){
		drop = $(this).find(".b-drop__tale")
		drop.width() > drop.parent().width()
			? drop.addClass("b-drop__tale_wide")
			: drop.removeClass("b-drop__tale_wide")
		drop.find("LI A").each(function(){
			$(this).text().length > 30
				? drop.addClass("b-drop__tale_twide") : null
		})
		drop.find("LI:last-child").addClass("last")
	})



	// text inputs hints
	$(".text_hint[value!=]")
		.each(function(){this.hint=this.value})
		.bind("focus",function(){if(this.hint==this.value){$(this).attr("value","").removeClass("text_hint").parents(".search-input:eq(0)").removeClass("search-input_hint")}})
		.bind("blur",function(){if(this.value==""){	$(this).attr("value",this.hint).addClass("text_hint").parents(".search-input:eq(0)").addClass("search-input_hint")}})


	// promo
	$(".b-promo").each(function(){
		var a = $(this).find(".b-promo__i")
		a.eq(Math.floor(Math.random()*a.length)).addClass("cur")
			.siblings(".cur").removeClass("cur")
	})


	$(".b-promo__i").not(".cur").show().fadeOut(0)
	$(".b-promo__i.cur").hide().fadeIn(0)
	$(".b-promo__rarr").click(function(){
		cur = $(this).siblings(".cur")
		cur.nextAll(".b-promo__i").length
			? next = cur.next()
			: next = cur.siblings(".b-promo__i").first()
		cur.fadeOut(promoAnimTime).removeClass("cur")
		next.fadeIn(promoAnimTime).addClass("cur")
		return false
	})
	$(".b-promo__larr").click(function(){
		cur = $(this).siblings(".cur")
		cur.prevAll(".b-promo__i").length
			? next = cur.prev()
			: next = cur.siblings(".b-promo__i").last()
		cur.fadeOut(promoAnimTime).removeClass("cur")
		next.fadeIn(promoAnimTime).addClass("cur")
		return false
	})
	$(".b-promo").hover(
		function(){if (promoAnimTO) clearTimeout(promoAnimTO)},
		function(){promoAnimTO = setTimeout("promoAnim()", promoPeriodTime)})
	promoAnimTO = setTimeout("promoAnim()", promoPeriodTime)
	
	
	// news
	$(".b-news UL").height($(".b-news LI.cur").height())
	$(".b-news").hover(
		function(){if (newsAnimTO) clearTimeout(newsAnimTO)},
		function(){newsAnimTO = setTimeout("newsAnim()", newsPeriodTime)})
	newsAnimTO = setTimeout("newsAnim()", newsPeriodTime)

	// logos
	$(".b-logos A").hover(
		function(){
			$(this).find("SPAN").stop().clearQueue().fadeTo(600,0)
		},
		function(){
			$(this).find("SPAN").stop().clearQueue().fadeTo(600,1)
		})
		
	$(".b-logos TABLE:not(:first)").hide()
	$(".b-logos").hover(
		function(){if (logosAnimTO) clearTimeout(logosAnimTO)},
		function(){logosAnimTO = setTimeout("logosAnim()", logosPeriodTime)})
	logosAnimTO = setTimeout("logosAnim()", logosPeriodTime)

});
