
var mov_line=0;
var cur_width=0;


function setParams(TAB){
	mov_line = 0;
	$('li', $('.bottom', TAB)).each(function(){
		mov_line+=(parseInt($(this).innerWidth()));
	});
	cur_width=parseInt($('li.active', $('.bottom', TAB)).innerWidth())
	//alert("mov_line: "+mov_line)
}

//------------- переключение стрелок назад вперед
function check_pointer(TAB)
{
	var margin=Math.abs(parseInt($('.divac ul', TAB).css('marginLeft')));
	
	setParams(TAB)
	if(margin>=cur_pos && margin<(cur_pos+cur_width))
		$('.bottom div:not(.active) .left-pointer', TAB).parent().addClass('active');
	else
		$('.bottom .active .left-pointer', TAB).parent().removeClass('active');
		
	if((margin+315)>=cur_pos && (margin+315)<(cur_pos+cur_width))
		$('.bottom div:not(.active) .right-pointer', TAB).parent().addClass('active');
	else
		$('.bottom .active .right-pointer', TAB).parent().removeClass('active');

};
var n=false;
var speed=8;

//------------- прокрутка терминалов
function move_left(TAB)
{
	var TAB;
	var margin=parseInt($('ul', $('.bottom', TAB)).css('marginLeft'));
	if((margin<0)&&(n==true))
	$('ul', $('.bottom', TAB)).animate({'marginLeft':(margin+speed+'px')},20,function(){
		move_left(TAB)
	});
};
function move_right(TAB)
{
	var TAB;
	var margin=parseInt($('ul', $('.bottom', TAB)).css('marginLeft'));
	if(((320-margin)<mov_line)&&(n==true))
	$('ul', $('.bottom', TAB)).animate({'marginLeft':(margin-speed+'px')},20,function(){
		move_right(TAB)
	});
};


$(function(){
	$('.left-pointer', '.bottom ').live('mousedown',function(){
		n=true;
		var TAB = $(this).parent().parent().parent()
		setParams(TAB)
		move_left(TAB);
	})
	$('.left-pointer', '.bottom ').live('mouseup',function(){
		n=false;
		var TAB = $(this).parent().parent().parent()
		check_pointer(TAB);
	})
	
	$('.right-pointer', '.bottom ').live('mousedown',function(){
		n=true;
		var TAB = $(this).parent().parent().parent()
		setParams(TAB)
		move_right(TAB)
	})
	$('.right-pointer', '.bottom ').live('mouseup',function(){
		n=false;
		var TAB = $(this).parent().parent().parent()
		check_pointer(TAB);
	})
});

var cur_pos=0;


//--------- выбор терминала
$('.bottom .divac ul div').live('click',function(){
	var TAB = $(this).parent().parent().parent().parent().parent();
	$(this).parent().parent().find('.active').removeClass('active');
	$(this).parent().addClass('active');
	cur_pos=0;
	n=true
	$('li',$('.bottom', TAB)).each(function(i){
		if($(this).is('.active')) n=false;
		if(n)cur_pos+=(parseInt($(this).innerWidth()));
	});
	cur_width=parseInt($('li.active',$('.bottom', TAB)).innerWidth());
	check_pointer(TAB);
});


//---------- переключение закладок
$(function(){
	$("td", '.JS_TAB').click(function(){
		$(this).parent().find("td").removeClass('act');
		$(this).parent().find("td").removeClass('next');
		$(this).addClass('act');
		$(this).next().addClass('next');
		var index = $('td', $(this).parent()).index($(this))
		var Block = $(this).parent().parent().parent().parent()
		$('.content > ul > li', Block).removeClass('active');
		$('.content > ul > li', Block).eq(index).addClass('active');
	});
});