$(document).ready(function() {

tmp = 0;

	if($('.js-lang-form').length > 0) {
		// Ve verzi stylu 3.* zachovat pouze tuto vetev
		$('.js-lang-form').each(function() {
			initJsLangForm($(this));
		});

	}else {
		// Vetev pro starsi verze, abychom to nemuseli predelavat
		$('.js-lang-form-content').each(function() {
			$('.lang-forms__switcher__options').append('<span class="lang-forms__switcher__option" data-language="'+$(this).data('language')+'" style="background-image: url(/admin/styles/www/css/flags/'+$(this).data('flag')+'.png)");">'+$(this).data('language-name')+'</span>')
		});

		$('.lang-forms__switcher').click(function() {
			if($(this).hasClass('open')) {
				$(this).removeClass('open');
			}else {
				$('.lang-forms__switcher.open').removeClass('open');
				$(this).addClass('open');
			}
		});

		$('.lang-forms__switcher__option').click(function() {
			if(!$(this).hasClass('active')) {
				$('.lang-forms__switcher__option[data-language="'+$(this).data('language')+'"]').removeClass('active');
				$('.lang-forms__storage').append($(this).closest('.lang-forms__switcher').next().find('.js-lang-form-content'));
				$(this).parent().find('.active').removeClass('active');
				$(this).addClass('active');
				$(this).closest('.lang-forms__switcher').next().append($('.js-lang-form-content[data-language="'+$(this).data('language')+'"]'));
			}
		});


		$('.lang-forms__switcher__option').first().click();
		$('.lang-forms__switcher__options').eq(1).find('.lang-forms__switcher__option').eq(1).click();
		$('.lang-forms__switcher').removeClass('open');

		$(document).click(function(e) {
			if($(e.target).closest('.lang-forms__switcher').length < 1) {
				$('.lang-forms__switcher').removeClass('open');
			}
		});

	}

});


function initJsLangForm($that) {
	$that.find('.js-lang-form-content').each(function() {
		$that.find('.lang-forms__switcher__options').append('<span class="lang-forms__switcher__option" data-language="'+$(this).data('language')+'" style="background-image: url(/admin/styles/www/css/flags/'+$(this).data('flag')+'.png)");">'+$(this).data('language-name')+'</span>');
	});

	$that.find('.lang-forms__switcher').click(function() {
		if($(this).hasClass('open')) {
			$(this).removeClass('open');
		}else {
			$that.find('.lang-forms__switcher.open').removeClass('open');
			$(this).addClass('open');
		}
	});

	$that.find('.lang-forms__switcher__option').click(function() {
			if(!$(this).hasClass('active')) {
				$that.find('.lang-forms__switcher__option[data-language="'+$(this).data('language')+'"]').removeClass('active');
				$that.find('.lang-forms__storage').append($(this).closest('.lang-forms__switcher').next().find('.js-lang-form-content'));
				$(this).parent().find('.lang-forms__switcher__option.active').removeClass('active');
				$(this).addClass('active');
				$(this).closest('.lang-forms__switcher').next().append($that.find('.js-lang-form-content[data-language="'+$(this).data('language')+'"]'));
			}
		});

	$that.find('.lang-forms__switcher__option').first().click();
	$that.find('.lang-forms__switcher__options').eq(1).find('.lang-forms__switcher__option').eq(1).click();
	$that.find('.lang-forms__switcher').removeClass('open');
}
