$(function() {

	var ajaxLoaderImg = new Image();
	ajaxLoaderImg.src = "/i/ajax-loader-small.gif";
	var ajaxLoader = '<img id="ajaxLoader" src="/i/ajax-loader-small.gif" width="16" height="16" alt="" />';

	function ambessyToggle() {
		$('#ambessy-list a.title').click(function() {
			var $this = $(this);
			var slideContainer = $this.siblings('.ambessy-inner');
			if (slideContainer.is(':hidden')) {
				$('.ambessy-inner').slideUp();
				slideContainer.slideDown();
			} else {
				slideContainer.slideUp();
			}
		});
	}

	ambessyToggle();

	//*********** Validation **************
	if ($('#visa-order-frm').length > 0) {
		var
			mesReq = 'REQUIRED_FIELD'.translate(),
			mesEmail = 'INCORRECT_EMAIL'.translate()
		;
		$.validator.messages.required = mesReq;
		var validator = $('#visa-order-frm').validate({
			messages: {
				email: {
					email: mesEmail
				}
			},
			errorClass: 'invalid',
			errorPlacement: function(error, element) {
				error.insertAfter(element);
			}
		});
	}

	function clearContent() {
		$('#country-visa-def').hide();
	}

	//*******************

//	$('#' + $('#visa-locations li a.active').parents('li').attr('class')).show();

	function removeRequestSuccessBlock() {
		$('#success-block').remove();
	}

	$('#visa-locations li a').bind('click', function() {
		var $this = $(this);
		var continent = $this.parents('li').attr('class');
		removeRequestSuccessBlock();
		$('#success-block').remove();
		$('#visa-locations li a.active').removeClass('active');
		$this.addClass('active');
		if ($('#' + continent).is(':hidden')) {
			$('div.countries-holder:visible').fadeOut(100);
			setTimeout(function() {
				$('#' + continent).fadeIn();
			}, 100);
		}
		$('#visa-country-description').hide();
		$('div.countries-holder ul li a.active').removeClass('active');
		clearContent();
		if ($('#is-admin').val() != 1) return false;
	});

	setScrollControls();

	$('div.countries-holder ul li a').bind('click', getAjaxContentCountry);

	function getAjaxContentCountry() {

		var $this = $(this);
		var countryCode = $this.metadata().countryCode;
		removeRequestSuccessBlock();
		if (!$this.hasClass('active')) {
			$this.after(ajaxLoader);
			//$('#country-visa-def').hide();
			if ($('#is-admin').val() != 1) ajaxContentCountryRequest(countryCode, $this.text());
		}
		$('div.countries-holder:visible ul li a.active').removeClass('active');
		$this.addClass('active');
		if ($('#is-admin').val() != 1) return false;
	};

	function bindScroll() {
		$('a.totop').bind('click', function() {
			$.scrollTo('#country-visa-def', 1200);
		});
		$('a.toform').bind('click', function() {
			$.scrollTo('#visa-order-frm', 1200);
		});
	}

	function setScrollControls() {
		$('#country-visa-def h3.waved a.totop').remove();
		$('#contentText h3.waved').prepend('<a class="toform" href="javascript:void(0)">Оформить визу</a>');
		$('#country-visa-def h3.waved').prepend('<a class="totop" href="javascript:void(0)">К началу</a>');
		bindScroll();
	}

	function ajaxContentCountryRequest(countryCode, elmText) {
		$.ajax({
			async   : true,
			type    : "GET",
			url     : "/a_ajax/content/visa",
			dataType: "json",
			data    : {countryCode: countryCode},
			success : function(data){
				if (data.error == 0) {
					$('#country-visa-def h2 span').text(elmText);
					$('#country-visa-def h2 p.subtitle').html(data.title);
					$('#ambessy-list #location-ua').html(data.embassy_ua_in_country + data.embassy_country_in_ua);
					$('#ambessy-list #location-ru').html(data.embassy_ru_in_country + data.embassy_country_in_ru);
					ambessyToggle();
					$('#contentText').html(data.text);
					$('#visa-order-frm .country-name-holder').text(elmText);
					$('#visa-order-frm input[name="country_code"]').val(countryCode);
					$('#visa-order-frm input[name="continent"]').val($('#visa-locations li a.active').parents('li').attr('class'));
					setScrollControls();
					$('#country-visa-def').fadeIn();
				} else if (data.error == 1) {
					clearContent();
				}
				$('#ajaxLoader').remove();
			},
			error  : function() {
				alert('Error with requesting ajax data.');
			}
		});
	}

});
