$(document).ready(function() {
	
	var globalWidth = 0;
	var rMode = 0;
	var wH = $(window).width();
	if (wH < 1200)	{ var globalWidth = 1000; rMode = 1; }
	if (wH > 1200)	{ var globalWidth = 1144; rMode = 2; }
	if (wH > 1420)	{ var globalWidth = 1344; rMode = 3; }
	
	// Setup Layout
	$('.wrapper').attr('id', 'wrapper-' + rMode);
	$('.contacts').attr('id', 'contacts-' + rMode);
	
	// Menu hover 
	$('#menu ul li:not(".active")').hoverIntent({ over: function() { $(this).find('img.over').animate({ opacity: 1 }, 300, 'easeInOutQuart'); }, timeout: 200, out: function() { $(this).find('img.over').animate({ opacity: 0 }, 300, 'easeInOutQuart'); } });
	
	// Center info block paragraph
	var info = $('#info');
	if (info) {
		var p = $(info).find('p');
		var pH = $(p).height();
		if (pH < 43) {
			var pM = (43 - pH)/2;
			$(p).css('margin-top', pM + 'px');
		}
	}
	
	// Contacts slide
	$('#contacts-handle').click(function() {
		if ($('#contacts-wrapper').is(':animated')) return false;
		
		if (!$(this).hasClass('active')) {
			$(this).addClass('active');
			$('.wrapper').css('z-index', 7000);
			$('#contacts-handle').animate({ marginTop: 514 }, 500);
			$('#contacts-wrapper').animate({ marginTop: 0 }, 500);
		}
		
		else {
			$(this).removeClass('active');
			$('#contacts-handle').animate({ marginTop: -36 }, 500);
			$('#contacts-wrapper').animate({ marginTop: -550 }, 500, function() {
				$('.wrapper').css('z-index', 8000);
			});
		}
		
		return false;
	});
	$('.footer_contact').click(function() {
		if ($('#contacts-wrapper').is(':animated')) return false;
		
		if (!$('#contacts-handle').hasClass('active')) {
			$('#contacts-handle').addClass('active');
			$('.wrapper').css('z-index', 7000);
			$('#contacts-handle').animate({ marginTop: 514 }, 500);
			$('#contacts-wrapper').animate({ marginTop: 0 }, 500);
		}
		
		else {
			$('#contacts-handle').removeClass('active');
			$('#contacts-handle').animate({ marginTop: -36 }, 500);
			$('#contacts-wrapper').animate({ marginTop: -550 }, 500, function() {
				$('.wrapper').css('z-index', 8000);
			});
		}
	})
	
	// Clients
	$('#clients-handle').click(function() {
		if (!$(this).hasClass('active')) {
			var innerHeight = $('#clients-inner').height();
			$(this).addClass('active');
			$('#clients-wrapper').animate({ height: innerHeight }, 500, 'easeInOutQuart');
		}
		
		else { $('#clients-wrapper').animate({ height: 60 }, 500, 'easeInOutQuart', function() { $('#clients-handle').removeClass('active'); }); }
		
		return false;
	});
	
	// News Item
	$('#news-wrapper p:first').css('height', '60px').css('overflow', 'hidden');
	$('#news-wrapper p:not(":first")').animate({ opacity: 0 }, 1);
	
	$('#news-wrapper').hoverIntent({
		over: function() { $('#news-handle').addClass('slided'); $('#news-wrapper').animate({ marginRight: 0 }, 500, 'easeInOutQuart'); },
		timeout: 500,
		out: function() { 
			if ($('#news-handle').hasClass('clicked')) {
				$('#news-handle').removeClass('clicked');
				$('#news-wrapper').animate({ width: 396, height: 137 }, 500, 'easeInOutQuart', function() {
					$('#news-wrapper').animate({ marginRight: -354 }, 500, 'easeInOutQuart', function() { $('#news-handle').removeClass('slided'); });
					$('#news-wrapper p:first').css('height', '60px').css('overflow', 'hidden');
					$('#news-wrapper p:not(":first")').animate({ opacity: 0 }, 1);
				});
			}
			
			else {
				$('#news-wrapper').animate({ marginRight: -354 }, 500, 'easeInOutQuart', function() { $('#news-handle').removeClass('slided'); });
			}
		}
	});
	
	$('#news-handle').click(function() {
		if ($(this).hasClass('slided')) {
			if (!$(this).hasClass('clicked')) {
				$(this).addClass('clicked');
				$('#news-wrapper p:first').css('height', '100%').css('overflow', 'hidden');
				$('#news-wrapper p:not(":first")').animate({ opacity: 1 }, 100);
				var innerWidth = $('#news-left').outerWidth() + $('#news-right').outerWidth();
				var innerHeight = $('#news-left').outerHeight() + 100;
				$('#news-wrapper').animate({ width: innerWidth, height: innerHeight }, 500, 'easeInOutQuart');
			}
			
			else { 
				$('#news-wrapper').animate({ width: 396, height: 137 }, 500, 'easeInOutQuart', function() { 
					$('#news-wrapper p:first').css('height', '60px').css('overflow', 'hidden');
					$('#news-wrapper p:not(":first")').animate({ opacity: 0 }, 1);
					$('#news-handle').removeClass('clicked'); 
				}); 
			}
		}
		
		return false;
	});

	// Carousel Navigation
	$('.carousel-item')
	.hoverIntent({ over: function(e) { $(this).animate({ opacity: 1 }, 500, 'easeInOutQuart') }, timeout: 500, out: function(e) { $(this).animate({ opacity: 0.55 }, 500, 'easeInOutQuart') }})
	.focus(function(e) {
		var id = $(this).attr('id');
		$('#' + id).find('.small-status').animate({ opacity: 0 }, 300, function() {
			$('#big-' + id).animate({ opacity: 1 }, 300).css('z-index', 600);;
		});
	})
	.blur(function(e) {
		var id = $(this).attr('id');
		$('#big-' + id).animate({ opacity: 0 }, 300, function() {
			$('#big-' + id).css('z-index', 500);
			$('#' + id).find('.small-status').animate({ opacity: 1 }, 300);
		});
	});
	
	// Carousel 
	$('#carousel').roundabout({
		childSelector: '.carousel-item',
		easing: 'easeInOutQuart',
		startingChild: 1,
		duration: 1500,
		minScale: 1,
		maxScale: 1,
		minOpacity: 0.4,
		maxOpacity: 1,
		tilt: 0,
		rMode: rMode
		//debug: true
	});
});

