window.onload = ShowAnketa;

function ShowAnketa()
{
var tmp = new Array();      // два вспомагательных
var tmp2 = new Array();     // массива
var param = new Array();
var get = location.search;  // строка GET запроса

if(get != '') {
  tmp = (get.substr(1)).split('&');   // разделяем переменные
  for(var i=0; i < tmp.length; i++) {
  tmp2 = tmp[i].split('=');       // массив param будет содержать
  param[tmp2[0]] = tmp2[1];       // пары ключ(имя переменной)->значение
}}


if (param["no_form"] != "Y")
{
  var d = document.getElementById("opros_");
  if (d) {d.style.display='block';}
}

/*
  var d = document.getElementById("opros_");
  if (d) {d.style.display='block';}
  */
}

function nva(id) {
  var d = document.getElementById(id);
  if (d) {d.style.display='none';}
}


$(function() {
	var previousControl = $('.navigation__control_type_carousel.navigation__control_previous'),
		nextControl = $('.navigation__control_type_carousel.navigation__control_next'),
		carousel = $('.navigation__products:not(.navigation__products_bg)');
	
	
	if (carousel.length) {
		$('.navigation__products:not(.navigation__products_bg)').carousel({
			items: '.navigation__products-link',
			previous: previousControl,
			next: nextControl
		});
		$('.navigation__products_bg').carousel({
			items: '.navigation__products-img_bg',
			previous: previousControl,
			next: nextControl
		});
		
		$('.clouds').addClass('clouds_animated');
	}
					
	$('.cut__link').click(function(event) {
		event.preventDefault();
		
		$(this).closest('.cut').toggleClass('cut_expanded');
	});
	
    $('.navigation__container').each(function() {
        var t = $(this),
            items = $('li', t);

        t.width(items.width() * items.length);
    });
	$('.navigation__products-wrapper').hover(function() {
		$(this).toggleClass('ui-state-hover');
	})
	setInterval(function() {
		$('.navigation__control[data-interval-call]').each(function() {
			var self = $(this),
				counter = self.data('inverval-counter') || 0,
				navigation = self.closest('.navigation__products-wrapper');
			
			if (navigation.hasClass('ui-state-hover')) {
				return;
			}
			
			if (counter >= self.data('interval-call')) {
				self.click();
				counter = 0;
			}
			counter++;
			
			self.data('inverval-counter', counter);
		});
	}, 1000);
	
	$('.navigation').hover(function() {
		$(this).toggleClass('ui-state-hover');
	});
		
    $('.navigation__control:not(.navigation__control_type_carousel)').live('state_update', function() {
		var t = $(this),
            navigation = t.closest(t.data('navigation') || '.navigation'),
            containerSelector = t.data('container'),
            containers = $(containerSelector, navigation),
            controls = $('.navigation__control[data-container="' + containerSelector + '"]', navigation);
		
		if (t.hasClass('navigation__control_next')) {
            t[navigation.width() + Math.abs(containers.position().left) >= containers.width() ? 'addClass' : 'removeClass']('navigation__control_disabled');
        }
        if (t.hasClass('navigation__control_previous')) {
            t[!containers.position().left ? 'addClass' : 'removeClass']('navigation__control_disabled');
        }
	}).live('click', function() {
        var t = $(this),
            navigation = t.closest(t.data('navigation') || '.navigation'),
            containerSelector = t.data('container'),
            containers = $(containerSelector, navigation),
            controls = $('.navigation__control[data-container="' + containerSelector + '"]', navigation),
            direction = t.hasClass('navigation__control_next') ? 1 : 0;

        if (navigation.hasClass('js-animated') || t.hasClass('navigation__control_disabled')) {
            return;
        }
        
        containers.each(function() {
            navigation.addClass('js-animated');

            var container = $(this);
            container.animate({left: (direction ? '-' : '+') + '=' + $(':first-child', container).width() + 'px' }, function() {
                navigation.removeClass('js-animated');
                
                controls.trigger('state_update');
            });
        });
    }).trigger('state_update');
    
    $('form .button_submit').click(function() { $(this).closest('form').submit(); });
    $('#branches__select').live('change', function() { $(this).closest('form').submit(); });

	$('> li', $('.content_page ul:not(.list-stylled):not(.gallery_photos):not(.branches-list)').addClass('list-stylled')).html(function(i, h) {
		return '<span class="list-stylled__content">' + h + '</span>';
	});
	$('.gallery_photos a[rel="gallery"]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Фото ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$(".gallery_photos a[data-preview]").mouseover(function() {
		var self = $(this),
			tooltip, tooltip__image,
			offset;
		
		if (!(tooltip = self.data('tooltip'))) {
			tooltip = $('<div class="h-tooltip" style="top: -1000px;"><div class="tooltip"><div class="tooltip__link"><img src="" class="tooltip__image" /></div><div class="tooltip__tip"></div></div></div>').appendTo(document.body);
			tooltip__image = $('.tooltip__image', tooltip);
			offset = self.offset();
			
			tooltip__image.load(function() {
				var img = $(this);
				if (img.width() === 0 && img.height() === 0) {
					setTimeout( function (){ tooltip__image.load(); }, 10 );
					return;
				}
				tooltip.width($('.tooltip', tooltip).outerWidth()).css({
					top: offset.top,
					left: offset.left - Math.abs(tooltip.outerWidth() - self.outerWidth()) / 2
				});
			}).attr('src', self.data('preview'));

			
			self.data('tooltip', tooltip);
		}
		
		tooltip.show();
	}).mouseout(function() {
		$('.h-tooltip').hide();
	});
	
	$('.structure-branches__point__link').click(function(event) {
		event.preventDefault();
		var point = $(this).closest('.structure-branches__point');
		$('.structure-branches__point').not(point).removeClass('structure-branches__point_with-info');
		point.toggleClass('structure-branches__point_with-info');
	})
	$('.structure-branches__list a').click(function(event) {
		var href = $(this).attr('href'),
			anchor = href.split('#')[1] || null;

		if (anchor) {
			event.preventDefault();
			$('.structure-branches__point a[href="#' + anchor + '"]').click();
		}
	});
	
	$('a[href="#login"], .login__close').live('click', function(event) {
		event.preventDefault();
		$('#login').toggle();
	});
});
