$(document).ready(function(){

	// close all popup function
    $(document).click(function(e){
    
		if( $('.popup-acf').is(':visible') ){
		
			var $_target = $(e.target).parents();
		
			if( $_target.filter('.link-open-cat').length == 0 && $_target.filter('.popup-acf.show').length == 0 ){
			
				if( !$(e.target).hasClass('popup-acf') && !$(e.target).hasClass('link-open-cat') ){
				
					$('.popup-acf').hide();					
					
					stopSellerSlider();
					
					startTimer();
					
				}
					
			}
		
		}

	});

	// урезание слов
	if( $('.good-bl').find('.name').length > 0 ){
		$('.good-bl').find('.name').jTruncate({  
			length: 25,
			minTrail: 0,
			moreText: "",
			lessText: "",
			ellipsisText: " ..."
		});
		$('.good-bl').find('.name').find('.clearboth').remove();
	}
	
    if( !get_cookie('what-is') && $('.what-is-this').length > 0 ){
		whatIsShow();	
    }

	$('.promo-tabs')
        .find('.tabs-in').hover(function(){

            $(this).toggleClass('hover');
            startTimer();

        }).end()
		.find('.tabs-menu table').find('td a')
            .hover(function(){
			//var class_ 	= $(this).attr('class');
			//var what	= class_.substr( 0, class_.length - 4 );

                var active = $('.promo-tabs').attr('class').match(/show-(\S+)/)[1];

                if( !$(this).hasClass(active +'-tab') ) $(this).parents('td').toggleClass('active');

            })
            .click(function(){

                $('.promo-tabs').addClass('stop-change');

            });



	if( $('.promo-tabs').find('.tabs-menu').length > 0 ){
		startTimer();
	}

});

	var start_tabs_timer;
	var tabs_whats	= new Array();

	function startTimer(){

		var $_block	= $('.promo-tabs'),
		    cats	= $_block.find('.all-cats').is(':visible');
		
		if( start_tabs_timer ) clearInterval( start_tabs_timer );
		
		if( !$_block.find('.tabs-in').hasClass('hover') && !cats ){
		
			var $_tab_menu	= $_block.find('.tabs-menu'),
			    active		= $_tab_menu.find('td.active').index(),
			    tab;
			
			$_tab_menu.find('td a').each(function(){
				
				tab = $(this).attr('class');
				tab = tab.substr( 0, tab.length - 4 );
				
				tabs_whats.push( tab );
				
			});

            if( !$('.promo-tabs').hasClass('stop-change') ){

			    start_tabs_timer = setInterval(function(){
			
				    active++;
				
				    if( active == tabs_whats.length ) active = 0;
	
				    changeStartTab( tabs_whats[active] );
				
			    },7000);
                
            }
		
		}
		
			
	}
	
	function changeStartTab(what){
		var $_block		= $('.promo-tabs'),
		    $_tab_menu	= $_block.find('.tabs-menu'),
		    $_tab		= $_tab_menu.find( '.' + what + '-tab').parents('td'),
		    active		= $_tab.index();
		
		$_tab
			.addClass('active')
			.siblings('td').removeClass('active');
		
		$_block
			.removeClass('show-info show-b2b show-b2c show-sale show-c2c show-tender')
			.addClass('show-' + what)
			.find('.all-cats').hide();

		startTimer();

	}
	
	function showCategory(type){
	
		var $_block 	= $('.categories').find('.all-cats'),
		    curr_slider = $('.sellers-type:visible').index();
		
		if( $_block.is(':visible') ){
		
			$_block
				.removeClass('show')
				.hide();
								
			stopSellerSlider();

		}else{

            $.get('/categories/'+type, {}, function(html){

                   $_block
                    .html(html)
                    .addClass('show')
                    .fadeIn('fast');


            });
		
			stopSellerSlider(true);
			
		}
	}
	
	function whatIsShow(el){
		
		var $_show_bl	= $('.what-is-this'),

		    scroll 		= true,
		    offset		= $_show_bl.offset().top - 290,
		
		    $_link 		= $_show_bl.find('.link-round'),
		    $_show_in	= $_show_bl.find('.what-is-in');
		
		if( el ){
			$_show_bl	= $(el).parents('.what-is-this');
			scroll		= false;
		}
		
		if( $_show_in.is(':visible') ){
			
			$_link.removeClass('open');
			$_show_in.hide();
			
			expires = new Date(); // получаем текущую дату
			expires.setTime(expires.getTime() + (1000 * 86400 * 365)); // вычисляем срок хранения cookie
			set_cookie('what-is', 1, expires);

		}else{

			if( scroll ){
				$('body,html').animate({scrollTop:offset}, 'slow', function(){
					$_link.addClass('open');
					$_show_in.fadeIn('fast');
				});
			}else{
				$_link.addClass('open');
				$_show_in.fadeIn('fast');
			}
			
		}		
		
	}	
	

