function slidingGallery(rootElem){
	var sidebarSwitch=false;
	var me=this;
	var nav=$('.b-gallery-nav a');
	var imgCol=nav.length;
	var globalSrc=new Array(imgCol);
    var auto,timer;
	var controls={
		rightCont:rootElem.find('.b-gallery-arrow')
	}

	var speed=400;
	var imgContainer=rootElem.find('.b-gallery-pic'),
		currentImage=imgContainer.find('img'),
		containerWidth=imgContainer.width();

	var inMotion=false,
		currentSlide=0,
		leftPos=0;
	currentImage.css('left','0');
	
	$('.b-ln-item a').hover(function(){
		sidebarSwitch=true;
		sidebarSwitching($(this));
		
	});
	
	function sidebarSwitching(link){
		$('.b-ln-item-sel').find('ins').remove();
		$('.b-ln-item-sel').removeClass('b-ln-item-sel');
		var nevPic=nav.eq(link.attr('rel')).attr('href');
		link.append('<ins></ins>');
		link.parent().addClass('b-ln-item-sel');		
		if(sidebarSwitch){
			rootElem.find('.b-gallery-pic img').attr('src',nevPic);		
			rootElem.find('.b-gd-inner')
					.empty()
					.append(nav.eq(link.attr('rel')).html());
			currentSlide=link.attr('rel');
		}
		}
	
	function motion(direction) {
	
		if (inMotion) return false;
		inMotion=true;
		var fakeImg= $('<img src="" alt="">');
		
		if (direction==1) {
			currentSlide++;		
			if (currentSlide==imgCol) currentSlide=0;
			fakeImg.css({left:containerWidth,top:0});
			leftPos=-containerWidth;
		}
		else {
			currentSlide--;
			if (currentSlide<0) currentSlide=imgCol-1;
			fakeImg.css({left:-containerWidth,top:0});
			leftPos=containerWidth;
		}
		sidebarSwitching($('.b-ln-item a').eq(currentSlide));
		imgContainer.append(fakeImg);
		
		fakeImg.load(function(){
			if(!sidebarSwitch){
			fakeImg.animate({left:0},speed);            
			currentImage.animate({left:leftPos+'px'},speed,function(){
					currentImage.remove();
					currentImage=imgContainer.find('img');
					
					inMotion=false;
                    if (auto) {
                     // timer=setTimeout(function(){motion(1);},3000);
                    }
				});
			rootElem.find('.b-gd-inner')
				.empty()
				.append(nav.eq(currentSlide).html());
			}
			
		});
		
		fakeImg.attr('src',nav.eq(currentSlide).attr('href'));
		
	}

	controls.rightCont.click(function(){
      //  clearTimeout(timer);
		sidebarSwitch=false;
        auto=false;
		motion(1);
	});
  // timer=setTimeout(function(){motion(1);auto=true;},2000);
	
	

}

$('.b-partners').find('.b-partners-tape-inner').each(function(){
	var me=$(this);
	me.next=me.parent().find('.b-control-right');
    
	me.back=me.parent().find('.b-control-left');
	me.motionBlock=me[0];
    var scrollingWidth=me.offset().left+me.width()-me.find('.b-partners-list .b-item').eq(0).width();

    var wholeWidth=me.find('.b-partners-list').innerWidth(),
    leftArrowClass_nonact='b-control-left-nonact',
	rightArrowClass_nonact='b-control-right-nonact';
	

    if ($.browser.msie) {
        me.find('ul').width(wholeWidth-13);
    }
    var containerWidth=me[0].clientWidth;

    if (wholeWidth>me.width()){
        me.next.show();
        me.back.show();
    }
	var options={
		acc:1,
		dt:0,
		maxSpeed:4,
		direction:0,
		dS:0,
		timer:0
	}
	var rootWidth=me.width();
	var links=me.find('a');
    
	me.next.hover(function(){
		options.dt=0;
		options.direction=1;
	    me.back.removeClass(leftArrowClass_nonact);
		motion();
	},
	function(){
        me.stop();
		clearTimeout(options.timer);

        if (me.motionBlock.scrollLeft>=wholeWidth-containerWidth) me.next.addClass(rightArrowClass_nonact);


	}).click(function(){return false;});

	me.back.hover(function(){
		me.next.removeClass('b-control-right-nonact');
		options.direction=-1;
		options.dt=0;
		motion();
        me.next.removeClass(rightArrowClass_nonact);
	},
	function(){
        me.stop();
		clearTimeout(options.timer);
		if (me.motionBlock.scrollLeft==0) me.back.addClass(leftArrowClass_nonact);
        if (me.motionBlock.scrollLeft>=wholeWidth-containerWidth) me.next.addClass(rightArrowClass_nonact);
	}).click(function(){return false;});

	function motion() {
		options.dS=wholeWidth;
		if (options.direction==1) {
            motionR=me.motionBlock.scrollLeft+options.dS;
		}
		else {
            motionR=me.motionBlock.scrollLeft-options.dS;

		}
       me.stop().animate({scrollLeft: motionR},wholeWidth);

	}
})

		//show hints





$(document).ready(function(){
    $('a[rel="external"]').click(function(){
        
    });
    if ($('.b-gallery').length){
    	var g=slidingGallery($('.b-gallery'));
    }
    if($('.b-features-list_picture').length){
    	var imageDescr;
    	
    	$('.b-flp-inner-item').mouseover(function(){
    		
    		imageDescr=$(this).find('.b-flp-ii-descr');
    		imageDescr.show();    
    		$(this).css('z-index','99');
    		var idRightPoint=imageDescr.offset().left+imageDescr.width()+parseInt(imageDescr.css('padding-right'))+parseInt(imageDescr.css('padding-left'));
    		var layoutRightPoint=$('.b-layout').width()+parseInt($('.b-layout').css('padding-left'));
    		//alert(idRightPoint);
    		//alert(layoutRightPoint);
    		if (idRightPoint>layoutRightPoint) {
    			var newLeft=parseInt(imageDescr.css('left'))-(idRightPoint-layoutRightPoint);
    			imageDescr.css('left',newLeft+'px')};
    		//imageDescr.css('left')-(idRightPoint-layoutRightPoint)
    	});
    	$('.b-flp-inner-item').mouseleave(function(){
    		$(this).find('.b-flp-ii-descr').hide();
    		$(this).css('z-index','1');
    	});
    }
    //var pg=new Array();
    /*if ($('.b-partners-tape').length) {
        $('.b-partners').find('.b-partners-tape-inner').each(function(i,el){
            //pg[i]=partnersGallery($(this));
        })

    }*/
    if ( $(".lightbox").length){ 
    	$(".lightbox").lightbox()}
});


/*------------------------------ sortament-switcher ---------------------------*/
	/*
	html-structure
	<div class="b-sortament-switching">
		<ul class="g-list b-sortament-switcher">
			<li class="b-ss-item-sel b-ss-item"><a href="#circle">text</a></li> {0,*}
		</ul>

		<div class="b-sort-sw-container">
			<div id="circle" class="b-sort-sw-item b-sort-sw-opened">  {0,*}
		</div>
	</div>
	*/
	$(document).ready(function(){
		$('.b-sortament-switching').each(function(){
				var me=$(this);
				var blocks={
					switchPanel:me.find('.b-sortament-switcher'),
					switcherItems:me.find('.b-sortament-switcher .b-ss-item'),
					switchItems:me.find('.b-sort-sw-container .b-sort-sw-item')
				};
				blocks.switcherItems.click(function(){
					if ($(this).hasClass('b-ss-item-sel')) {
						return false;
						}
					var selectedItem=blocks.switcherItems.filter('.b-ss-item-sel');
					var selIndex=blocks.switcherItems.index(selectedItem);		
					selectedItem.removeClass('b-ss-item-sel');
					$(this).addClass('b-ss-item-sel');
					var index=blocks.switcherItems.index($(this));
					blocks.switchItems.eq(selIndex-1).hide();
					blocks.switchItems.eq(index-1).show();
					return false;
				});


			});
	});
/*--------------------------- /sortament-switcher ----------------------------*/
/*----------------------- sortament table background -------------------------*/
$('.b-sort-table tbody tr:even').addClass('');
$('.b-sort-table tbody tr:odd').addClass('b-st-second');
/*----------------------- /sortament table background ------------------------*/
/*--------------------------- features-switcher ----------------------------*/
    /*
     html-structure
	 <section class="b-features">
		<div class="b-features-header">
			<div class="b-features-title">�����������</div>
			<div class="b-features-switcher">
				<!--noindex-->
				<nofollow>
				<span class="b-fs-item b-fs-item-sel" id="thesys"><ins class="thesys-icon "></ins><a href="#" >text</a></span>  {0,*}
		</div>
		<div class="cl"></div>
		<div class="b-flw-container">
			<div class="b-features-list-wrapper b-flw-opened" id="thesys">  {0,*}
        </div>
    </section>
    */
    $(document).ready(function(){
        $('.b-features').each(function(){
                var me=$(this);
                var blocks={
                   switchPanel:me.find('.b-features-switcher'),
                   switcherItems:me.find('.b-features-switcher .b-fs-item'),
                   switchItems:me.find('.b-flw-container .b-features-list-wrapper')
                };
                blocks.switcherItems.click(function(){
                     if ($(this).hasClass('b-fs-item-sel')) {
                       return false;
                       }
                     var selectedItem=blocks.switcherItems.filter('.b-fs-item-sel');
                     var selIndex=blocks.switcherItems.index(selectedItem);
                     selectedItem.removeClass('b-fs-item-sel');
                     $(this).addClass('b-fs-item-sel');
                     var index=blocks.switcherItems.index($(this));
                     blocks.switchItems.eq(selIndex).hide();
                     blocks.switchItems.eq(index).show();
					 return false;
                });


            });
    });
/*--------------------------- /features-switcher ----------------------------*/
/*------------------------- Partners-Clients ---------------------------------*/
	    /*
     html-structure
    <div class="b-partners">
        <div class="b-partners-switcher">
			<!--noindex-->
			<nofollow>
            <span class="b-ps-title b-ps-title-sel" id="partners"><a href="#partners">text</a></span> {0,*}
			</nofollow>
			<!--/noindex-->
        </div>

        <div class="b-partners-tape">
			<div class="b-control-right"></div>
			<div class="b-control-left b-control-left-nonact"></div>
            <div id="partners" class="b-partners-tape-inner b-opened-item">  {0,*}
        </div>
    </div>
    */
    $(document).ready(function(){
        $('.b-partners').each(function(){
                var me=$(this);
                var blocks={
                   switchPanel:me.find('.b-partners-switcher'),
                   switcherItems:me.find('.b-partners-switcher .b-ps-title'),
                   switchItems:me.find('.b-partners-tape')
                };
                blocks.switcherItems.find('a').click(function(){
                     if ($(this).parent().hasClass('b-ps-title-sel')) {
                       return false;
                       }
                     var selectedItem=blocks.switcherItems.filter('.b-ps-title-sel');
                     var selIndex=blocks.switcherItems.index(selectedItem);
                     selectedItem.removeClass('b-ps-title-sel');
                     $(this).parent().addClass('b-ps-title-sel');
                     var index=blocks.switcherItems.index($(this).parent());
                     blocks.switchItems.eq(selIndex).hide().removeClass('b-partners-tape-open');
                     blocks.switchItems.eq(index).show().addClass('b-partners-tape-open');
                     $('.b-partners-tape').find('.b-control-left').addClass('b-control-left-nonact');
                     $('.b-partners-tape').find('.b-control-right').removeClass('b-control-right-nonact');
                     blocks.switchItems.eq(index)[0].scrollLeft=0;
					 return false;
                });


            });
    });
/*------------------------- /Partners-Clients ---------------------------------*/
