$(document).ready(function(){
	if($('body').hasClass('in_')){
		placePromos();
		resizePromoFont();
		setTimeout('changeNewsMainPreviewOnce("0")', 6000);
		var twitter_name = $("#tweet").attr('data-username');
		$("#tweet").tweet({
	        refresh_interval: 30,
	        username: twitter_name,
	        join_text: "auto",
	        avatar_size: 32,
	        count: 3,
	        auto_join_text_default: " ",
	        auto_join_text_ed: " ",
	        auto_join_text_ing: " ",
	        auto_join_text_reply: " ",
	        auto_join_text_url: " ",
	        loading_text: "loading tweets..."
	    });
	};
})

$(window).load(function() {
	if($('body').hasClass('in_')){
		movePromos();
		resizePromoFont();
		correctGalleryHeight();
	}
});

$(window).resize(function() {
	movePromos();
	resizePromoFont();
});

function correctGalleryHeight(){
	$('.b-carousel').each(function(){
		var thisHeight = $(this).find('.b-objects-item:first-child').height();
		$(this).find('.b-objects-items').css({'height':thisHeight+'px', 'overflow':'hidden'});
	});
}

$(function(){
	$('.b-carousel-arrow').click(function(){
		if($(this).attr('target')){
			var itemsList = $('#'+$(this).attr('target'))
		} else {
			var itemsList = $(this).parent().find('ul')
		};
		itemsList.css({'overflow':'visible',' height':'auto'});
		var itemsCount = parseFloat(itemsList.find('li').length);
		var itemsWidth = itemsList.width();
		var itemWidth = itemsWidth/itemsCount;
		var currentItem = parseFloat(itemsList.attr('current'));
		var scrollDuration = 500;
		if($(this).hasClass('b-carousel-arrow_left')){
			if(currentItem <= 1){
				currentItem = itemsCount;
				var scrollDuration = 1000;
			} else {
				currentItem = currentItem - 1;
				var scrollDuration = 500;
			}
			itemsList.attr('current',currentItem);
			var driftLeft = -(currentItem-1)*100+'%';
		  	$(itemsList).find('li .b-objects-item-title').hide('slow');
			$(itemsList).stop().find('li').css('visibility','visible').end().animate({
				left: driftLeft
		  	}, scrollDuration, function() {
		  		$(itemsList).find('li:nth-child('+currentItem+')').siblings().css('visibility','hidden');
		  		$(itemsList).find('li:nth-child('+currentItem+') .b-objects-item-title').animate({
		  			height: 'toggle'
		  		}, 300);
		  	});
		} else {
			if(currentItem >= itemsCount){
				currentItem = 1;
				var scrollDuration = 1000;
			} else {
				currentItem = currentItem + 1;
				var scrollDuration = 500;
			}
			itemsList.attr('current',currentItem);
			var driftLeft = -(currentItem-1)*100+'%';
		  	$(itemsList).find('li .b-objects-item-title').hide('slow');
			$(itemsList).stop().find('li').css('visibility','visible').end().animate({
				left: driftLeft
		  	}, scrollDuration, function() {
		  		$(itemsList).find('li:nth-child('+currentItem+')').siblings().css('visibility','hidden');
		  		$(itemsList).find('li:nth-child('+currentItem+') .b-objects-item-title').animate({
		  			height: 'toggle'
		  		}, 300);
		  	});
		};
	});
})


function resizePromoFont(){
	if($('body').hasClass('in_') && $('#b-promos').length > 0){
		var defaultTitleSize = 32;
		var defaultDescSize = 19;
		var defaultTitleTop = 60;
		
		var defaultTitleLength = 47;
		var defaultDescLength = 81;
		
		var titleWidth = $('#b-promos .b-promos-item_active .b-objects-item-title').width();

		var titleLength = $('#b-promos .b-promos-item_active .b-objects-item-title').text().length;
		var descLength = $('#b-promos .b-promos-item_active .b-objects-item-desc').text().length;
		var titleTextWidth = Math.ceil(titleLength*16.9);
		var descTextWidth = Math.ceil(descLength*9.7);
		
		var defaultTitleHeight = $('#b-promos .b-promos-item_active .b-objects-item-title').height();
		var defaultDescHeight = $('#b-promos .b-promos-item_active .b-objects-item-desc').height();
		
		var k = 1, newTitleHeight = defaultTitleHeight, newDescHeight = defaultDescHeight;
		
		if(titleTextWidth > titleWidth){
			k =  titleWidth/titleTextWidth;
			if(k < 1){
				var newTitleSize = Math.floor(k*defaultTitleSize-1);
				newTitleHeight = $('#b-promos .b-promos-item_active .b-objects-item-title').css({'font-size':newTitleSize+'px'}).height();
			} else {
				newTitleHeight = $('#b-promos .b-promos-item_active .b-objects-item-title').css({'font-size':defaultTitleSize+'px'}).height();
			};
		} else {
			newTitleHeight = $('#b-promos .b-promos-item_active .b-objects-item-title').css({'font-size':defaultTitleSize+'px'}).height();
		};
		if(descTextWidth > titleWidth){
			k =  titleWidth/descTextWidth;
			if(k < 1){
				var newDescSize = Math.floor(k*defaultDescSize-1);
				newDescHeight = $('#b-promos .b-promos-item_active .b-objects-item-desc').css({'font-size':newDescSize+'px'}).height();
			} else {
				newDescHeight = $('#b-promos .b-promos-item_active .b-objects-item-desc').css({'font-size':defaultDescSize+'px'}).height();
			};
		} else {
			newDescHeight = $('#b-promos .b-promos-item_active .b-objects-item-desc').css({'font-size':defaultDescSize+'px'}).height();
		};
		var newTitleTop = 55 + (85 - newTitleHeight - newDescHeight)/2;
		$('#b-promos .b-promos-item_active .b-objects-item-desc, #b-promos .b-promos-item_active .b-objects-item-title').css({'top':newTitleTop+'px'});
		
		var newTitleTextWidth = $('#b-promos .b-promos-item_active .b-objects-item-title .wrap').innerWidth();
		var newDescTextWidth = $('#b-promos .b-promos-item_active .b-objects-item-desc .wrap').innerWidth();
		if(newTitleTextWidth >= newDescTextWidth){
			var newTextWidth = newTitleTextWidth;
		} else {
			var newTextWidth =  newDescTextWidth;
		};
		
		var itemWidth = $('#b-promos .b-promos-item_active').width();
		var imageWidth = $('#b-promos .b-promos-item_active .b-objects-item-img img').width();
		
		var imageMarginLeft = Math.floor((itemWidth-newTextWidth-imageWidth-10)/2)

		$('#b-promos .b-promos-item_active .b-objects-item-img').width(imageWidth).css({'margin-left':imageMarginLeft+'px','margin-right':parseFloat(-imageWidth-10)+'px'});
		$('#b-promos .b-promos-item_active .b-objects-item-title, #b-promos .b-promos-item_active .b-objects-item-desc').css({'margin-left':parseFloat(imageWidth+10+imageMarginLeft)+'px'});

	}
}


function placePromos(){
	if($('body').hasClass('in_') && $('#b-promos').length > 0){
		movePromos();
		placePromosArrows();
		
		var iBlocksCount = $('#b-promos .b-promos-item').length;
		$('#b-promos .b-promos-arrow').click(function(){
			var currentBlock = $('#b-promos-items .b-promos-item').index($('.b-promos-item_active'))+1;
			if($(this).hasClass('b-promos-arrow_right')){
				if(currentBlock >= iBlocksCount){
					var nextBlock = 1;
				} else {
					var nextBlock = currentBlock + 1;
				};
				$(this).find('.b-promos-arrow-text').fadeOut('700');
			} else {
				if(currentBlock <= 1){
					var nextBlock = iBlocksCount;
				} else {
					var nextBlock = currentBlock - 1;
				};
				$(this).find('.b-promos-arrow-text').fadeOut('700');
			};
			$('#b-promos-items .b-promos-item:nth-child('+nextBlock+')').addClass('b-promos-item_active').siblings().removeClass('b-promos-item_active');
			movePromos();
		})
		
		$('#b-promos .b-promos-arrow').hover(function(){
		    if($(this).hasClass('b-promos-arrow_left')){
		    	$(this).addClass('b-promos-arrow_left_hover');
		    } else {
		    	$(this).addClass('b-promos-arrow_right_hover');
		    };
		}, function(){
		    $(this).removeClass('b-promos-arrow_left_hover').removeClass('b-promos-arrow_right_hover');
		});
	}
}

function movePromos(){
	if($('body').hasClass('in_') && $('#b-promos').length > 0){
		var currentBlock = $('#b-promos-items .b-promos-item').index($('.b-promos-item_active'))+1;
		var iBlockWidth = $('#b-promos .l-wrapper').width()-340;
		var iBlocksCount = $('#b-promos .b-promos-item').length;
		var iBlocksWidth = iBlockWidth*iBlocksCount;
		$('#b-promos-items').width(iBlocksWidth).find('.b-promos-item').width(iBlockWidth);
		resizePromoFont();
		var drift = -Math.floor(iBlockWidth*(currentBlock-1));
		
		$('#b-promos-items').stop().animate({
			left: drift
		}, 1000, function(){
			placePromosArrows();
			resizePromoFont();
		});
	}
}

function placePromosArrows(){
	if($('body').hasClass('in_') && $('#b-promos').length > 0){
		
		var iBlocksCount = $('#b-promos-items .b-promos-item').length;
		var sNextTitle = $('#b-promos-items .b-promos-item_active').next().find('.b-objects-item-title').text();
		if(sNextTitle == ''){
			sNextTitle = $('#b-promos-items .b-promos-item:first-child .b-objects-item-title').text();
		}
		var sPrevTitle = $('#b-promos-items .b-promos-item_active').prev().find('.b-objects-item-title').text();
		if(sPrevTitle == ''){
			sPrevTitle = $('#b-promos-items .b-promos-item:last-child .b-objects-item-title').text();
		}
		$('#b-promos .b-promos-arrows')
			.find('.b-promos-arrow_left .b-promos-arrow-text').text(sPrevTitle).end()
			.find('.b-promos-arrow_right .b-promos-arrow-text').text(sNextTitle);
		if(sNextTitle != ''){
			$('#b-promos .b-promos-arrow_right').show();
			var iNextTitleHeight = $('#b-promos .b-promos-arrow_right .b-promos-arrow-text').innerHeight();
			var iNextTitleTop = Math.floor((85-iNextTitleHeight)/2);
			$('#b-promos .b-promos-arrow_right .b-promos-arrow-text').css({'top':iNextTitleTop+'px'}).fadeIn('500');
		} else {
			$('#b-promos .b-promos-arrow_right').hide();
		};
		if(sPrevTitle != ''){
			$('#b-promos .b-promos-arrow_left').show();
			var iPrevTitleHeight = $('#b-promos .b-promos-arrow_left .b-promos-arrow-text').innerHeight();
			var iPrevTitleTop = Math.floor((85-iPrevTitleHeight)/2);
			$('#b-promos .b-promos-arrow_left .b-promos-arrow-text').css({'top':iPrevTitleTop+'px'}).fadeIn('500')
		} else {
			$('#b-promos .b-promos-arrow_left').hide();
		};
	}
}

$(function() {
	$('#w-mp_claster_media').each(function(){
		$(this)
			.find('.b-mp_claster:nth-child(1)').css({'left':'0','margin-right':'-31%'}).end()
			.find('.b-mp_claster:nth-child(2)').css({'left':'34.5%','margin-right':'-31%','margin-left':'1px'}).end()
			.find('.b-mp_claster:nth-child(3)').css({'float':'right','margin':'0'}).end()
	})
});


$(function(){
	$('#b-news_main-switcher .b-news_main-switcher-item').click(function(){
		$('#b-news_main-switcher').removeClass('autoswitch');
		var selectedItemLine = parseFloat($(this).attr('line'));
		changeNewsMainPreview(selectedItemLine);
	});
})


function changeNewsMainPreview(selectedItemLine){
	var currentItemLine = parseFloat($('#b-news_main-switcher .b-news_main-switcher-item_active').attr('line'));
	if($('#b-news_main-switcher').hasClass('autoswitch')){
		var changeSpeed = 2000;
	} else {
		var changeSpeed = 500;
	};
	if(selectedItemLine === undefined || selectedItemLine <= 0){
		var itemsCount = parseFloat($('#b-news_main-switcher .b-news_main-switcher-item').length);
		if(currentItemLine >= itemsCount || selectedItemLine <= 0){
			var selectedItemLine = 1;
		} else {
			var selectedItemLine = currentItemLine + 1;
		};
	};
	if(currentItemLine != selectedItemLine){
		$('#b-news_main-fresh-items .b-objects-item-img img.picture_'+currentItemLine).fadeOut(changeSpeed);
		$('#b-news_main-fresh-items .b-objects-item-img img.picture_'+selectedItemLine).addClass('next').fadeIn(changeSpeed,function(){
			$('#b-news_main-fresh-items .b-objects-item-img img.picture_'+currentItemLine).removeClass('current');
			$(this).removeClass('next').addClass('current').css('display','block');
		});
		$('#b-news_main-switcher .b-news_main-switcher-item[line='+selectedItemLine+']').addClass('b-news_main-switcher-item_active').siblings().removeClass('b-news_main-switcher-item_active');
	}
}

function changeNewsMainPreviewOnce(selectedItemLine){
	if($('#b-news_main-switcher').hasClass('autoswitch')){
		changeNewsMainPreview();
		setTimeout('changeNewsMainPreviewOnce()', 6000);
	}
}
