$(document).ready(function(){
	
	$('.text-editor .number-list').each(function(){
    	$(this).find('li').each(function(){
    		$(this).html('<span>' + $(this).text() + '</span>');
    	});
	});
	$('.text-editor .marker-list').each(function(){
    	$(this).find('li').each(function(){
    		$(this).html($(this).text());
    	});
	});
	
	$('.navigation-points-inner-wrap i').click(function(){

		if($(this).parent().hasClass('selected') || window.flagShift) {
			return false;
		}

		oldcolor = $('body').attr('class').replace('-body','').replace(' ','');
		color = $(this).parent().attr('class').replace('-b','').replace(' ','');

		count = 0;
		$(this).parent().parent().find('dd').each(function(){
			if (!$(this).hasClass(color + '-b')) {
				count = count + 1;
			} else {
				return false;
			}
		})
		
		$('.navigation-points-inner-wrap dd.selected').removeClass('selected');
		$(this).parent().addClass('selected');

		$('.background.' + oldcolor).css('z-index',1);
		$('.background.' + color).css('z-index',2).hide();
		
		$('.left-big.' + color + ', .left-small.' + color).css('z-index',3);
		$('.left-big.' + oldcolor + ', .left-small.' + oldcolor).css('z-index',2).hide();
		$('.right-big.' + color + ', .right-small.' + color).css('z-index',3);
		$('.right-big.' + oldcolor + ', .right-small.' + oldcolor).css('z-index',2).hide();
		$('.left-s.' + color).css('z-index',3);
		$('.left-s.' + oldcolor).css('z-index',2).hide();
		$('.right-s.' + color).css('z-index',3);
		$('.right-s.' + oldcolor).css('z-index',2).hide();
		$('.lighting.' + color).css('z-index',-1);
		$('.lighting.' + oldcolor).css('z-index',-2).hide();
		
		window.flagA = 1;
		window.flagShift = 1;
		
		l = -943*count;
		$('.present-departments .columns').animate({left:l},300,function(){
			$('.lighting.' + color).delay(1000).fadeIn(100);
			$('.left-big.' + color + ', .left-small.' + color + ', .left-s.' + color + ', .right-big.' + color + ', .right-s.' + color + ', .right-small.' + color + ', .background.' + color).fadeIn(600,function(){
				if (window.flagA) {
					window.flagA = 0;
					$('.left-big.' + oldcolor + ', .left-small.' + oldcolor + ', .lighting.' + oldcolor +', .left-s.' + oldcolor + ', .right-big.' + oldcolor + ', .right-s.' + oldcolor + ', .right-small.' + oldcolor + ', .background.' + oldcolor).hide();
					$('body').attr('class', color + '-body');
					window.flagShift = 0;
				}
			});
		});
		return false;
	});

	/*Sitemap*/
	$('.sitemap .show-sitemap').click(function(){
		if (click == false) {
			$('.inner-sitemap').slideToggle(600,function(){
				changeNameSitemap();
				$('.top-sitemap').css('visibility','visible');
			}).toggleClass('open');
			click = true;	
		}
		else {
			$('.top-sitemap').css('visibility','hidden');
			$('.inner-sitemap').slideToggle(600,function(){
				changeNameSitemap();
			}).toggleClass('open');
			click = false;
		}
		$('.sitemap .show-sitemap').toggleClass('active');
		return false;
	})

	/*Check feedback-form*/
	jQuery.fn.check = function(){
		var empty = 0;
		$('.necessary').each(function(){
			if ($(this).hasClass('email')) {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); 
				var email = $(this).val();
				if (!pattern.test(email)) {
					$(this).parent().parent().parent().find('dt').addClass('error');
					confirm = false;
				}
				else {
					$(this).parent().parent().parent().find('dt').removeClass('error');
					confirm = true;
				}
			}
			if ($(this).val() == '' || typeof $(this).val() == 'undefined') {
				$(this).parent().parent().parent().find('dt').addClass('error');
				empty = empty + 1;
			}
			else {
				$(this).parent().parent().parent().find('dt').removeClass('error');
				if (confirm == false) {
					$('.email').parent().parent().parent().find('dt').addClass('error');
					empty = empty + 1;
				}
				else {
					$('.email').parent().parent().parent().find('dt').removeClass('error');
				}
			}
			if (empty > 0) {
				$(this).parent().parent().parent().parent().find('.send-btn').addClass('disabled');
			}
			else {
				$(this).parent().parent().parent().parent().find('.send-btn').removeClass('disabled');
			}
		});
	}

	$('.necessary').change(function(){
		$(this).check();
	}).focus(function(){
		$(this).check();
	}).keyup(function(){
		$(this).check();
	}).click(function(){
		$(this).check();
	}).blur(function(){
		$(this).check();
	});

	/*Sending-data*/
	$('.feedback .send-btn').click(function(){
		$('#feedback-form').submit();
		return false;
	});

	$('#feedback-form').submit(function(){
		if ($('.send-btn').hasClass('disabled')) {
			return false;
		}
		$.post('?', $(this).find('input,textarea').serialize(),function(data){
			
			$('.thanks').fadeIn(300);
		});
		return false;
	});

	/*Status-send-btn*/
	$('.send-btn').hover(function(){
		$('.send-btn').addClass('hover');
	},function(){
		$('.send-btn').removeClass('hover');
	}).mousedown(function(){
		$('.send-btn').addClass('selected');
	}).mouseup(function(){
		$('.send-btn').removeClass('selected');
	}).mouseout(function(){
		$('.send-btn').removeClass('selected');
	})

	$('.present-departments,.navigation-points-inner-wrap dd').mouseover(function(){
		window.flagOver = true;
	}).mouseout(function(){
		window.flagOver = false;
		if (window.stopTimer == 0) {
			window.setTimeout("animation()",5000);
			window.stopTimer = 1;
		}
	})
	
	$('a.more-news').click(function(){
		console.log('wow');
		$('.news-list li').show();
		$(this).parent().hide();
		return false;
	});
})

window.flagOver = false;
window.click = false;
window.confirm = false;
window.firstStart = false;

function changeNameSitemap(){
	$('.show-sitemap span').toggleClass('dnone');
}

function animation(){
	window.stopTimer = 0;
	if (window.flagOver) {
		return false;
	}
	lanim = parseInt($('.present-departments .columns').css('left'));
	c = $('.navigation-points-inner-wrap dd').length;
	if (firstStart == false)	{
		$('.present-departments .columns').css('left',0);
	} else {
		if (lanim <= ((c - 1) * (-943))) {
			$('.present-departments .columns').animate({left:lanim - 943},600,function(){
				$('.present-departments .columns').css('left',0);
				switchingBg();
			})
			switchingPoints()
		} else {
			$('.present-departments .columns').animate({left:lanim - 943},600,function(){
				switchingBg();
			})
			switchingPoints()
		}
	}
	window.setTimeout("animation()",5000);
	window.stopTimer = 1;
	firstStart = true;
}

function switchingPoints(){
	numsel = 0;
	$('.navigation-points-inner-wrap dd').each(function(){
		if (!$(this).hasClass('selected')) {
			numsel = numsel + 1;
		} else {
			return false;
		}
	})
	$('.navigation-points-inner-wrap dd.selected').removeClass('selected');
	if (numsel == ($('.navigation-points-inner-wrap dd').length - 1)) {
		$('.navigation-points-inner-wrap dd:eq(0)').addClass('selected');
	} else {
		numsel = numsel + 1;
		p = 1;
		$('.navigation-points-inner-wrap dd:eq(' + numsel + ')').addClass('selected');
	}
}

function switchingBg(){
	oldcolor = $('body').attr('class').replace('-body','').replace(' ','');
	color = $('.navigation-points-inner-wrap dd.selected').attr('class').replace('-b','').replace('selected','').replace(' ','');

	$('.background.' + oldcolor).css('z-index',1);
	$('.background.' + color).css('z-index',2).hide();

	$('.left-big.' + color + ', .left-small.' + color).css('z-index',3);
	$('.left-big.' + oldcolor + ', .left-small.' + oldcolor).css('z-index',2).hide();
	$('.right-big.' + color + ', .right-small.' + color).css('z-index',3);
	$('.right-big.' + oldcolor + ', .right-small.' + oldcolor).css('z-index',2).hide();
	$('.left-s.' + color).css('z-index',3);
	$('.left-s.' + oldcolor).css('z-index',2).hide();
	$('.right-s.' + color).css('z-index',3);
	$('.right-s.' + oldcolor).css('z-index',2).hide();
	$('.lighting.' + color).css('z-index',-1);
	$('.lighting.' + oldcolor).css('z-index',-2).hide();

	window.flagA = 1;
	window.flagShift = 1;

	$('.lighting.' + color).delay(1000).fadeIn(100); 
	$('.left-big.' + color + ', .left-small.' + color + ', .left-s.' + color + ', .right-big.' + color + ', .right-s.' + color + ', .right-small.' + color + ', .background.' + color).fadeIn(600,function(){
		if (window.flagA) {
			window.flagA = 0;
			$('.left-big.' + oldcolor + ', .left-small.' + oldcolor + ', .lighting.' + oldcolor +', .left-s.' + oldcolor + ', .right-big.' + oldcolor + ', .right-s.' + oldcolor + ', .right-small.' + oldcolor + ', .background.' + oldcolor).hide();
			$('body').attr('class', color + '-body');
			window.flagShift = 0;
		}
	})
	
}

$(function(){
	window.setTimeout("animation()",5000);
	window.stopTimer = 1;
	if ($('body').attr('class') == '') {
		$('.crn.right-s.white,.crn.left-s.white').css('z-index',3);
		$('.crn.right-s.white,.crn.left-s.white').css('display','block');
	}
})
