var lightBoxCfg = {
	imageLoading: '/i/lightbox-ico-loading.gif',
	imageBtnClose: '/i/lightbox-btn-close.gif',
	imageBtnPrev: '/i/lightbox-btn-prev.gif',
	imageBtnNext: '/i/lightbox-btn-next.gif',
	txtImage: 'Изображение',
	txtOf: 'из'
};

(function($) {

	$(window).bind('load', function() {
	
		$('.online-consult').click(function(){
			window.open(this.href, 'online-consult', 'width=750,height=450,toolbar=no,location=no,directories=0,menubar=0,status=0');
			return false;
		});
	
		$('.content a[target=_blank]').filter('[href$="jpg"],[href$="jpeg"],[href$="png"],[href$="gif"]').lightBox(lightBoxCfg);

		$('.city a').click(function(){
			$('.dialog-city').fadeIn('fast');
			$('.dialog-city .controls .first').focus();
			return false;
		});

		$('.dialog-city input[type=submit]').click(function(){

			var thisForm = $('.dialog-city form');

			$.ajax({
				type: 'post',
				url: '/ajaxCity',
				data: thisForm.serialize(),
				dataType: 'text',
				success: function(response){
					if (response && response != 'Error')
					{
						$('.dialog-city-close').click();
						$('.city a').html(response);
						document.location.reload();
					}
				},
				error: function(xhr){
				}
			});

			return false;
		});

		$('.dialog-city-close').bind('click', function() {
			if('\v' == 'v') {
				$('.dialog-city').hide();
			} else {
				$('.dialog-city').fadeOut('fast');
			}
			return false;
		});
		
		// Nav
		$(".nav > li").hover(
			function () {
				$(this).children('a').addClass('current');
				$(this).find('ul').show('fast');
			}, 
			function () {
				$(this).children('a').removeClass('current');
				$(this).find('ul').hide('fast');
			}
		);
		
		// Dialog
		$('.free-link').click(function() {

			$('.free-dialog .dialog-success').hide();
			$('.free-dialog .dialog-failure').hide();
			$('.free-dialog .failure-fields').hide();
			$('.free-dialog form').show();
			
			$('.free-dialog .question_label').removeClass('field_error');

			$(".free-dialog").find('input:not(input[type=checkbox]), textarea').each(function(i, o){
				$(o).val('');
			});

			$('.free-dialog').fadeIn('fast');

			return false;
		});

		$(".send-again").bind("click", function(){
			$(".free-dialog").find('input:not(input[type=checkbox]), textarea').each(function(i, o){
				$(o).val('');
			});
			$(".send-again-edit").click();
		});

		$(".send-again-edit").bind("click", function(){
			$(".free-dialog .captcha a").click();
			$(".free-link").click();
		});
		
		$(".captcha a").bind('click', function() {
			$.get('/site/index.php?r=site/captcha&refresh=1', function(src){
				$('.captcha img').attr('src', src+'&theme=dim');
			});
			return false;
		});

		$('.dialog-send input').click(function() {
		
			$('.free-dialog .failure-fields').hide();
			$('.free-dialog .question_label').removeClass('field_error');

			$.ajax({
				type: 'post',
				url: '/ajaxAddQuestion',
				data: $('.free-dialog').find('textarea:not(.empty), input').serialize(),
				dataType: 'text',
				success: function(response){
					if (response == 'OK')
					{
						$('.free-dialog .dialog-success').show();

						$('.free-dialog form').hide();
						$('.free-dialog .failure-fields').hide();
						$('.free-dialog .dialog-failure').hide();
					}
				},
				error: function(xhr){
					var response = xhr.responseText;
					var fields = response.split('|');
					if (fields.length < 2)
					{
						$('.free-dialog form').hide();
						$('.free-dialog .dialog-success').hide();
						$('.free-dialog .dialog-failure').show();
					}
					else
					{
						$('.free-dialog .failure-fields').show();
						$('.free-dialog .question_label').removeClass('field_error');
						$($(fields).map(function(i, o){
							if (o.length == 0) return null;
							else return '.label_'+o;
						}).get().join(',')).addClass('field_error');
					}
				}
			});

			return false;
		});
		$('.dialog-callback-send input').click(function() {
			var thisForm = $(this).parents('form.backcall');

			$.ajax({
				type: 'post',
				url: '/ajaxBackcall',
				data: thisForm.serialize(),
				dataType: 'text',
				success: function(response){
					if (response == 'OK')
					{
						thisForm.find('.controls').hide();
						thisForm.find('.backcall-success').show();
					}
				},
				error: function(xhr){
				}
			});

			return false;
		});
		$('.dialog-close, .dialog-close-form').bind('click', function() {
			if('\v' == 'v') {
				$('.free-dialog').hide();
			} else {
				$('.free-dialog').fadeOut('fast');
			}
			return false;
		});
		$(document).bind('keydown', function (event) {
			if (event.keyCode == 27) {
				if('\v' == 'v') {
					$('.free-dialog').hide();
					$('.dialog-callback').hide();
				} else {
					$('.free-dialog').fadeOut('fast');
					$('.dialog-callback').fadeOut('fast');
				}
			}
		});
		
		$('.order-call-inner').click(function() {
			$('.backcall .controls').show();
			$('.backcall .backcall-success').hide();
			
			$('.dialog-callback').fadeIn('fast');
			$('#d-username').focus();
			return false;
		});
		$('.dialog-callback-close').bind('click', function() {
			if('\v' == 'v') {
				$('.dialog-callback').hide();
			} else {
				$('.dialog-callback').fadeOut('fast');
			}
			return false;
		});

		$('.survey input[type=submit]').click(function(){
			var thisForm = $('.survey');

			thisForm.find('.survey-inner').show();
			thisForm.find('.survey-failure').hide();
			thisForm.find('.survey-success').hide();
			
			$.ajax({
				type: 'post',
				url: '/ajaxPoll',
				data: thisForm.serialize(),
				dataType: 'json',
				success: function(json){
					if (!json || !json.success)
					{
						thisForm.find('.survey-inner').hide();
						thisForm.find('.survey-success').hide();
						thisForm.find('.survey-failure').show();
					}
					else
					{
						thisForm.find('.poll-option').hide();
						thisForm.find('.survey-success').show();
						$('.survey input[type=submit]').hide();
						if (json.results)
						{
							thisForm.find('.poll-result').map(function(i, o){
								$(o).html(json.results[$(o).attr('variant')]).show();
							});
						}
					}
				},
				error: function(xhr){
				}
			});

			return false;
		});
		
		$('.faq a.answer-opener').click(function(){
			$(this).next('.answer').toggle('fast');
			return false;
		});

		/*
		$(".services a").hover(
					function () {
						var $this = $(this).children('img');
						var $w = $this.width();
						var $h = $this.height();
						var $l = $this.css('left');
						$l = parseInt($l);
						var $t = $this.css('top');
						$t = parseInt($t);
						$this.stop(true).animate({width: $w+20+'px', height: $h+20+'px', left: $l-10+'px', top: $t-10+'px'}, 200);
					}, 
					function () {
						var $this = $(this).children('img');
						var $w = $this.width();
						var $h = $this.height();
						var $l = $this.css('left');
						$l = parseInt($l);
						var $t = $this.css('top');
						$t = parseInt($t);
						$this.stop(true).animate({width: $w-20+'px', height: $h-20+'px', left: $l+10+'px', top: $t+10+'px'}, 200);
					}
				);*/
		
		
	});

})(jQuery);
