function externalLinks() {   
 if (!document.getElementsByTagName) return;   
 var anchors = document.getElementsByTagName("a");   
 for (var i=0; i<anchors.length; i++) {   
   var anchor = anchors[i];   
   if (anchor.getAttribute("href") &&   
       anchor.getAttribute("rel") == "external")   
     anchor.target = "_blank";   
 }   
}   
window.onload = externalLinks;

$(document).ready(function() {
	var wid = 50;
	var wid1 = 30;
	var wid2 = 20
	if($.browser.msie && parseFloat($.browser.version) < 7){
		wid=25;
		wid1=15;
		wid2=-10;
	}
	
	$('#mainnav-new a').hover(function(){ 
		var $item = $(this).closest('.item');
		if (!$item.hasClass('active')) {
			$item.addClass('hover')
		}
	}, function(){ 
		var $item = $(this).closest('.item');
		$item.removeClass('hover')
		
	});
	
	$('#mainnav li a').hover(
		function(){
			if (!$(this).parent().hasClass('active')) {
			var thisa = $(this);
				$('.navslider').css({'opacity': '1'});
				$('.navslider').css({'left': (thisa.parent().position().left+$('#mainnav').position().left+20)+'px'});
				$('.navslider .slempty').css({'width': (thisa.parent().width()+wid)+'px'});
			
			$(this).css({'color':'#000', 'textDecoration':'none'});

			}
		}, 
		function(){
			if (!$(this).parent().hasClass('active')) {
			$('.navslider').css({'opacity': '0'});
			$(this).css({'color':'#fff', 'textDecoration':'underline'});
	
			}
		}
		
	);
	
	//if ($('#header-inner').width()-990<66) {
	//			$('#logo').css({'left':($('#header-inner').width()-990)+'px'});
	//}
	
	if ($('#mainnav li').hasClass('active')) {
		$('.navslider').after("<div class='navactive'><div class='slleft'></div><div class='slempty'></div><div class='slright'></div></div>");
		
		$('.navactive').css({'left': ($('#mainnav li.active').position().left+$('#mainnav').position().left+wid1)+'px', 'opacity': '1'});
		$('.navactive .slempty').css({'width': ($('#mainnav li.active').width()+wid-wid2)+'px'});
		
		$(window).resize(function() {
		//	$('.navactive').css({'left': ($('#mainnav li.active').position().left+$('#mainnav').position().left+wid1)+'px', 'opacity': '1'});
		//	if ($('#header-inner').width()-990<66) {
		//		$('#logo').css({'left':($('#header-inner').width()-990)+'px'});
		//	} else {
		//		$('#logo').css({'left':'66px'});
		//	}
		});
	}
			
	
	

			
	$('#services-container-inner .services-item:first').before($('#services-container-inner .services-item:last'));
			
			
	$('#services-arr-r').click(function(e){
		e.preventDefault();
		$('#services-container-inner:not(:animated)').animate({'left':'-=313px'},800, function(){
			$('#services-container-inner .services-item:last').after($('#services-container-inner .services-item:first'));
			$('#services-container-inner').css({'left':'-313px'});
		});
	});
			
	$('#services-arr-l').click(function(e){
		e.preventDefault();
		$('#services-container-inner:not(:animated)').animate({'left':'+=313px'},800, function(){
		$('#services-container-inner .services-item:first').before($('#services-container-inner .services-item:last'));
		$('#services-container-inner').css({'left':'-313px'});
		});
	});
			
	$('#clients-container a:first').before($('#clients-container a:last'));
			
			
	$('#clients-arr-r').click(function(e){
		e.preventDefault();
		$('#clients-container:not(:animated)').animate({'left':'-=211px'},800, function(){
			$('#clients-container a:last').after($('#clients-container a:first'));
			$('#clients-container').css({'left':'-211px'})
		});
	});
			
	$('#clients-arr-l').click(function(e){
		e.preventDefault();
		$('#clients-container:not(:animated)').animate({'left':'+=211px'},800, function(){
		$('#clients-container a:first').before($('#clients-container a:last'));
		$('#clients-container').css({'left':'-211px'});
		});
	});
					
	$(document).bind('click', function(e){ 
		var $clicked = $(e.target); 
		if ($clicked.is('#subnav a.form')) { 
			e.preventDefault(); 
			$('#login-form').fadeIn(); 
			$('#login-form .login').focus();
		} else { 
			if ((!($clicked.is('#login-form') || $clicked.parents().is('#login-form'))) || $clicked.is('#login-form .close'))
				$('#login-form:visible').fadeOut(); 
		} 
	}); 
	
	$('#clients-container .clients-item').hover(function(){ $(this).find('.glow').stop().animate({'opacity':0})/*.css({'backgroundPosition': '50% 100%'})*/}, function(){ $(this).find('.glow').stop().animate({'opacity':0.5})/*.css({'backgroundPosition': '50% 0'})*/} );
	
	
	
	$('.add-user, .add-user-link').toggle(function(){ $('.int-add-user-form').slideToggle(); }, function(){ $('.int-add-user-form').slideToggle(); });
	$('.int-users-block .open-arr').toggle(
		function(){ $(this).css({'backgroundPosition':'0 0'}); $('.int-users').slideToggle(); }, 
		function(){ $(this).css({'backgroundPosition':'0 -19px'}); $('.int-users').slideToggle(); }
	);
	
	
	
	
	$('.int-user').toggle(
		function(e){ 
			$('.int-user-contacts-wrap', $(this).next('.int-user-contacts')).slideToggle(); 
			$(this).css({'backgroundColor': '#efe'});
		}, 
		function(e){ 
			$('.int-user-contacts-wrap', $(this).next('.int-user-contacts')).slideToggle();  
			$(this).removeAttr('style');
		}
	);
	
	$('.tree-block .open-arr').toggle(
		function(){ $(this).css({'backgroundPosition':'0 -19px'}).parent().next('.tree-block-wrap').slideToggle(); }, 
		function(){ $(this).css({'backgroundPosition':'0 0'}).parent().next('.tree-block-wrap').slideToggle(); }
	);
	
	$('.tree-block .top-item').toggle(
		function(){ $('.sub-items',$(this).css({'backgroundColor': '#efe'}).next()).slideToggle(); }, 
		function(){ $('.sub-items',$(this).removeAttr('style').next()).slideToggle(); }
	);
	
	$('.tree-block .sub-item').toggle(
		function(){ $(this).css({'backgroundPosition':'0 -44px'}).next('.sub-sub-items').slideToggle(); }, 
		function(){ $(this).css({'backgroundPosition':'0 0'}).next('.sub-sub-items').slideToggle(); }
	);
	
	$('.tree-block .add-service-link a').toggle(
		function(e){ e.preventDefault(); $('.add-service-form' ,$(this).parent().next()).slideToggle(); }, 
		function(e){ e.preventDefault(); $('.add-service-form' ,$(this).parent().next()).slideToggle(); }
	);
	
	$('.user-btns .add-btn').toggle(
		function(e){ e.preventDefault(); $(this).text('Закрыть'); $('.add-user-contact', $(this).next()).slideToggle(); }, 
		function(e){ e.preventDefault(); $(this).text('Добавить контактную информацию'); $('.add-user-contact', $(this).next()).slideToggle(); }
	);
	
	
	
	$('.add-user-btn').click(function() {
		var notEmpty=false;
		$('.add-user-field').each(function(){
			if ($(this).val()=='' && $(this).hasClass('required')) {notEmpty=true}
		});
		
		if (notEmpty) {
			$.fancybox({
				'content': 'Не все поля заполнены.',
				'padding' : 40
			});
		} else {
			$('.jq-add-user').submit();
		}
    });

	$('.user-contact-submit').click(function() {
		
		var notEmpty=false;
		$('.user-contact-field', $(this).parentsUntil('form')).each(function(){
			if ($(this).val()=='' && $(this).hasClass('required')) {notEmpty=true}
		});
		
		if (notEmpty) {
			$.fancybox({
				'content': 'Не все поля заполнены.',
				'padding' : 40
			});
		} else {
			$(this).parentsUntil('form').submit();
		}
    });
	
	var tabs = $('.tabs .tabs-header .tab-1, .tabs .tabs-header .tab-2, .tabs .tabs-header .tab-3');
	var tabsbody = $('.tabs .tabs-body .tab-1, .tabs .tabs-body .tab-2, .tabs .tabs-body .tab-3');
	var tabsbodyw = $('.tabs .tabs-body');
	
	tabs.click(function(){ 
		if (!$(this).hasClass('active')) {
			tabs.removeClass('active');
			tabsbody.removeClass('active');
			
			$('.'+$(this).attr('class'), tabsbodyw).addClass('active');
			
			$(this).addClass('active');
		}
	});
	
	var maxTabHeight =0;
	
	tabs.each(function(){ 
		if ($(this).height()>maxTabHeight) { maxTabHeight=$(this).height() }
	});
	
	tabs.each(function(){
		$(this).css({'height':maxTabHeight+'px'});
	});

	/*
	
	$('.int-user a').click(function(e){ e.stopPropagation(); })
	
	
	$('.add-user-name').defaultValue({'value':'ФИО'});
	$('.add-user-post').defaultValue({'value':'Должность'});
	$('.add-user-phone').defaultValue({'value':'Телефон'});
	$('.add-user-mail').defaultValue({'value':'E-mail'});
	
		
	
	$('#adm-login-form .login').defaultValue({'value':'Логин'});
	$('#adm-login-form .psswd').defaultValue({'value':'Пароль'});
	*/
	
	
	//$('.port-samples .item').hover(function(){ $('.img', this).hide(); }, function(){  $('.img', this).fadeIn(300);  });
	
	$('.port-samples .item').hover(function(){ $('.item-text', this).fadeIn(100); $('.baner-itself', this).fadeOut(100); }, function(){ $('.item-text', this).fadeOut(100); $('.baner-itself', this).fadeIn(100); });
	
//	$('.preview-nav .prev').hover(function(){ $('.preview-nav').animate({'marginLeft':'-30px'},250); }, function(){ $('.preview-nav').animate({'marginLeft':'-25px'},250); });
//	$('.preview-nav .next').hover(function(){ $('.preview-nav').animate({'marginRight':'-5px'},250); }, function(){ $('.preview-nav').animate({'marginRight':'-0px'},250); });

});
