var leftInterval = false;
var rightInterval = false;
	
var movingLeft = false;
var movingRight = false;
var RightMenuElNum  = 1;
var menuLevel = '';
$(document).ready(function(){
	path = window.location.pathname;
	
	if(path.indexOf('uslugi')!== -1) {
		jQuery('li#item-101').addClass('current');
//		menuLevel = '2';
	}

	if(path.indexOf('portfolio')!== -1) {
		jQuery('li#item-102').addClass('current');
//		menuLevel = '3';
	}

//	if (menuLevel !== '') {
//		elmts = jQuery('#right > .moduletable > ul.menu').children().each(function(){
			
//			oldHtml = jQuery(this).html();
			
//			path = window.location.pathname;
//			href = jQuery('#' + (jQuery(this).attr('id')) + '> a:first').attr('href');
//			jQuery(this).html('<span class="num2">'+menuLevel+'.'+RightMenuElNum+'</span>'+oldHtml);

//			RightMenuElNum++;

//			if(path.indexOf(href) !== -1){
				//alert (path.indexOf(href));
//				jQuery(this).addClass('current');
//				jQuery(this).addClass('active');
//			}
			
//			jQuery(this).click(function(){
//				href = jQuery('#' + (jQuery(this).attr('id')) + '> a:first').attr('href');
				//alert(href);
//			});
//		});
//	}
	
	
	

	$('#orderSendForm').submit(function(){
		formVars = $("#orderSendForm").serialize();
		
		$('#orderSendFormStatusBox').html('<img src="/images/ajax_loader.gif" alt="sending..." />');

		$.post('/index2.php?option=com_askspecialist',formVars, function(data) {
			errors = data.errors;
			msg = data.msg;
			if(errors) {
			  $('#orderSendFormStatusBox').html(errors);
			}else if(msg){
				$('#orderSendForm').css('display','none');
				$('#orderSendFormStatusBox').html(data.msg);
			}else{
			
			}
		},'json');

		return false;
		
	});




	rotateLeft();
	leftInterval = setInterval(rotateLeft, 5000);
	
	$('a#carouselLeft').click(function(){
		if(movingLeft){
			return false;
		}
		
		if(leftInterval) {
			clearInterval(rightInterval);
			clearInterval(leftInterval);
			rotateLeft();
			leftInterval = setInterval(rotateLeft, 5000);
			return;
		}
		clearInterval(rightInterval);
		rightInterval = false;
		rotateLeft();
		leftInterval = setInterval(rotateLeft, 5000);
	});
	
	$('a#carouselRight').click(function(){
		if(movingRight) {
			return false;
		}
		
		if(rightInterval) {
			clearInterval(rightInterval);
			clearInterval(leftInterval);
			rotateRight();
			rightInterval = setInterval(rotateRight, 5000);
			return;
		}
		clearInterval(leftInterval);
		leftInterval = false;
		rotateRight();
		rightInterval = setInterval(rotateRight, 5000);
	});
});

function rotateRight() {
	/*
	firstEl = $('.fourthimg');
	secondEl = $('.thirdimg');
	thirdEl = $('.secondimg');
	fourthEl = $('.firstimg');
	*/
	movingRight = true;
	
	firstEl = $('.firstimg');
	secondEl = $('.secondimg');
	thirdEl = $('.thirdimg');
	fourthEl = $('.fourthimg');
	
	firstHidden = $('.hiddenimg:first');
	
	firstHidden.css('top', '0px');
	firstHidden.css('left', '0px');
	
	animationDuration = 2000;
	
	$(firstEl).animate({
		left: '845px',
		top: '0px',
		width: '20px',
		height: '20px',
		opacity: 0
	}, animationDuration, function(){});
	
	$(secondEl).animate({
		left: '730px',
		top: '2px',
		width: '100px',
		height: '100px'
	}, animationDuration, function(){});
	
	$(thirdEl).animate({
		left: '545px',
		top: '60px',
		width: '130px',
		height: '130px'
	}, animationDuration, function(){});

	$(fourthEl).animate({
		left: '250px',
		top: '70px',
		width: 200,
		height: 200
	}, animationDuration, function(){});
	
	
	$(firstHidden).animate({
		opacity: 1,
		left: '40px',
		top: '5px',
		width: '130px',
		height: '130px'
	}, animationDuration, function(){
		movingRight = false;
		$("#carouselBox .hiddenimg:last").parent().parent().append($(fourthEl).parent());
	});
	
	/*Make classes changes when animation complete*/
	$(firstEl).removeClass('firstimg');
	$(firstEl).addClass('hiddenimg');
	
	$(secondEl).removeClass('secondimg');
	$(secondEl).addClass('firstimg');
	
	$(thirdEl).removeClass('thirdimg');
	$(thirdEl).addClass('secondimg');
	
	$(fourthEl).removeClass('fourthimg');
	$(fourthEl).addClass('thirdimg');
	
	$(firstHidden).removeClass('hiddenimg');
	$(firstHidden).addClass('fourthimg');
}

function rotateLeft() {
	
	movingLeft = true;
	
	firstEl = $('.firstimg');
	secondEl = $('.secondimg');
	thirdEl = $('.thirdimg');
	fourthEl = $('.fourthimg');
	firstHidden = $('.hiddenimg:first');
	
	firstHidden.css('top', '0px');
	firstHidden.css('left', '845px');
	
	animationDuration = 2000;
	
	$(firstEl).animate({
		left: '545px',
		top: '60px',
		width: '130px',
		height: '130px'
	}, animationDuration, function(){});
	
	$(secondEl).animate({
		left: '250px',
		top: '70px',
		width: '200px',
		height: '200px'
	}, animationDuration, function(){});
	
	$(thirdEl).animate({
		left: '40px',
		top: '5px',
		width: '130px',
		height: '130px'
	}, animationDuration, function(){});

	$(fourthEl).animate({
		opacity: 0,
		left: '0px',
		top: '0px',
		width: 20,
		height: 20
	}, animationDuration, function(){});
	
	
	$(firstHidden).animate({
		opacity: 1,
		left: '730px',
		top: '2px',
		width: '100px',
		height: '100px'
	}, animationDuration, function(){
		movingLeft = false;
//		alert($(firstHidden).parent().parent().html());
		$("#carouselBox .hiddenimg:last").parent().parent().append($(fourthEl).parent());
//		$(fourthEl).parent().remove();
	});
	
	/*Make classes changes when animation complete*/
	$(firstEl).removeClass('firstimg');
	$(firstEl).addClass('secondimg');
	
	$(secondEl).removeClass('secondimg');
	$(secondEl).addClass('thirdimg');
	
	$(thirdEl).removeClass('thirdimg');
	$(thirdEl).addClass('fourthimg');
	
	$(fourthEl).removeClass('fourthimg');
	$(fourthEl).addClass('hiddenimg');
	
	$(firstHidden).removeClass('hiddenimg');
	$(firstHidden).addClass('firstimg');
}
	
