!function ($) {
	"use strict";

	$.AnnouncesListClasses = {
		VIEW_COMPACT:					'announces-list_view_compact',
		BANNER:							'announces-list__banner',
		HEADING:						'announces-list__heading',
		HEADING_WRAP:					'announces-list__heading-wrap',
		HEADING_DATE:					'announces-list__heading-date',
		HEADING_DATE_INACTIVE:			'announces-list__heading-date_inactive',
		HEADING_DATE_ICON:				'announces-list__heading-date-icon',
		HEADING_DATE_SPINNER:			'announces-list__heading-date-spinner',
		//HEADING_DATE_SPINNER_ACTIVE:	'announces-list__heading-date-spinner_active',
		HEADING_PARENT_TITLE:			'announces-list__heading-parent-title',
		VIEW_TOGGLER:					'announces-list__vtoggler',
		VIEW_TOGGLER_DEFAULT:			'announces-list__vtoggler_default',
		VIEW_TOGGLER_COMPACT:			'announces-list__vtoggler_compact',
		VIEW_TOGGLER_DISABLED:			'announces-list__vtoggler_disabled',
		VIEW_TOGGLER_LOADING:			'announces-list__vtoggler_loading',
		CONTENT:						'announces-list__content',
		CONTENT_WRAP:					'announces-list__content-wrap',
		CONTENT_HEADING:				'announces-list__content-heading',
		ANNOUNCE:						'announces-list__announce',
		ANNOUNCE_SIDE_LEFT:				'announces-list__announce_float_left',
		ANNOUNCE_SIDE_RIGHT:			'announces-list__announce_float_right'
	}

	var AnnouncesList = function (elem, options) {
		this.options = $.extend({

		}, options);

		this.$elem = $(elem);

		this.$elem.find('script').remove();
		this.lazyDefaults = $.extend({}, this.$elem.data('lazy'));

		this.initializeHeading();
	}

	AnnouncesList.prototype.initializeHeading = function () {
		this.$heading = this.$elem.find('.' + $.AnnouncesListClasses.HEADING);
		this.$headingWrap = this.$elem.find('.' + $.AnnouncesListClasses.HEADING_WRAP);
		this.$headingDate = this.$elem.find('.' + $.AnnouncesListClasses.HEADING_DATE);
		this.$headingDateIcon = this.$headingDate.find('.' + $.AnnouncesListClasses.HEADING_DATE_ICON);
		this.$headingDateLabel = this.$headingDate.children('span');
		this.$headingParentTitle = this.$elem.find('.' + $.AnnouncesListClasses.HEADING_PARENT_TITLE);
		this.$viewToggler = this.$elem.find('.' + $.AnnouncesListClasses.VIEW_TOGGLER);

		if(!this.$heading.length) return;

		var   $elem = this.$elem
			, $heading = this.$heading
			, $headingDate = this.$headingDate
			, $headingDateIcon = this.$headingDateIcon
			, $headerNavi = $('.header .navi')
			, $viewToggler = this.$viewToggler
			, lazyDefaults = this.lazyDefaults;

		$viewToggler
			.data('defaultView', $viewToggler.is('.' + $.AnnouncesListClasses.VIEW_TOGGLER_DEFAULT))
			.click(function(){
				if ($(this).is('.' + $.AnnouncesListClasses.VIEW_TOGGLER_DISABLED)) return false;

				/** YANDEX.METRIKA REACH GOAL **/
				yaCounter12602857.reachGoal('toggle-compact-button');

				var defaultView = $(this).data('defaultView');
				var lazyloadId = $elem.data('lazy-load-id');

				$.lazyload._reloadAlist(lazyloadId, {
					id: $elem.attr('id'),
					pattern_name: defaultView ? lazyDefaults.pattern_name : 'compact'
				});

				$(this).data('defaultView', !defaultView);
				$(this).addClass($.AnnouncesListClasses.VIEW_TOGGLER_LOADING);

				return false;
			})
			.bind('changestate', function(){
				var $this = $(this);

				if ($this.data('defaultView') != $this.is('.' + $.AnnouncesListClasses.VIEW_TOGGLER_DEFAULT)) {
					$this.toggleClass($.AnnouncesListClasses.VIEW_TOGGLER_DEFAULT + ' ' + $.AnnouncesListClasses.VIEW_TOGGLER_COMPACT);
				}

				$this.removeClass($.AnnouncesListClasses.VIEW_TOGGLER_DISABLED + ' ' + $.AnnouncesListClasses.VIEW_TOGGLER_LOADING);
			});

		if($headingDateIcon.length) {
			this.$datePickerTip = $('<div class="date-picker-tip"></div>').appendTo(this.$headingWrap);
			this.$datePickerTipArrow = $('<div class="date-picker-tip__arrow"></div>').appendTo(this.$datePickerTip);
			this.$datePicker = $('<div></div>').appendTo(this.$datePickerTip);

			var   $datePickerTip = this.$datePickerTip
				, $datePickerTipArrow = this.$datePickerTipArrow
				, $datePicker = this.$datePicker;


			$datePicker
				.datepicker({
					maxDate: new Date(),
					onSelect: function(dateText, inst){
						var lazyloadId = $elem.data('lazy-load-id');
						var dateFormat = $(this).datepicker('option', 'dateFormat');

						$headingDateIcon.addClass($.AnnouncesListClasses.HEADING_DATE_SPINNER);
						$headingDate.addClass($.AnnouncesListClasses.HEADING_DATE_INACTIVE);

						$.lazyload._reloadAlist(lazyloadId, {
							id: $elem.attr('id'),
							date: dateText
						});

						$datePickerTip.fadeOut();
					}
				});

			$datePickerTip.hide().click(function(e){ e.stopPropagation(); });

			$headingDateIcon.click(function(e){
				if($headingDate.is('.' + $.AnnouncesListClasses.HEADING_DATE_INACTIVE)) return false;

				$datePickerTip
					.fadeIn()
					.position({
						of: $(this),
						my: "center top",
						at: "center bottom+10",
						using: function(position, feedback){
							$(this)
								.css(position)
								.removeClass('date-picker-tip_ver_top date-picker-tip_ver_bottom date-picker-tip_hor_left date-picker-tip_hor_right')
								.addClass('date-picker-tip_hor_' + feedback.horizontal + ' date-picker-tip_ver_' + feedback.vertical);

							$datePickerTipArrow
								.css({
									left:  feedback.horizontal == 'left'  ? feedback.target.width / 2 : feedback.horizontal == 'center' ? ($(this).width() + $datePickerTipArrow.width()) / 2 : 'auto',
									right: feedback.horizontal == 'right' ? feedback.target.width / 2 : 'auto'
								})
						}
					});

				e.stopPropagation();

				$(document)
					.one('click.datepickertip scroll.datepickertip', function(e){
						$datePickerTip.fadeOut();
					});

				return false;
			});
		}

		this.$elem.on('connected', function(){
			var lazyloadId = $elem.data('lazy-load-id');

			$($.lazyload._getInstById(lazyloadId)).on('changeState.alist-datepicker', function(){
				if (this.state == $.lazyload.state.IDLE) {
					$viewToggler.trigger('changestate');
					$headingDateIcon.removeClass($.AnnouncesListClasses.HEADING_DATE_SPINNER);
					$headingDate.removeClass($.AnnouncesListClasses.HEADING_DATE_INACTIVE);
				} else {
					$viewToggler.addClass($.AnnouncesListClasses.VIEW_TOGGLER_DISABLED);

				}
			});
		});


		this.$window = $(window).on('scroll.announces-list', $.proxy(this.checkPosition, this));
		this.$elem.on('idleState.lazyload', $.proxy(this.checkPosition, this));

		/*this.$heading
			.waPin({
				offset: function(){
					return {
						top: 10 + ($headerNavi.is('.wa-pin') ? ($headerNavi.outerHeight() + $headerNavi.find('.lvl-2').outerHeight()) : 0),
						bottom: $elem.outerHeight() - $heading.outerHeight()
					}
				}
			});*/
	}

	AnnouncesList.prototype.checkPosition = function () {
		var headingBottom = this.$heading.offset().top + this.$heading.outerHeight();
		var $contentHeadings = this.$elem.find('.' + $.AnnouncesListClasses.CONTENT_HEADING).not('.' + $.LazyLoadClasses.BUFFERED);
		var $currentHeading = $contentHeadings.eq(0);

		$contentHeadings.each(function(){
			var $this = $(this);

			$this.offset().top < headingBottom && ($currentHeading = $this);
		});

		if($currentHeading.length) {
			this.$headingDateLabel.text($currentHeading.data('date'));
			this.$headingParentTitle.text($currentHeading.data('parent-title'));
		} else {
			this.$headingDateLabel.text('');
			this.$headingParentTitle.text('');
		}
	}

	$.fn.announcesList = function (options) {
		return this.each(function () {
			var   $this = $(this)
				, data = $this.data('announces-list');

			if (!data) $this.data('announces-list', (data = new AnnouncesList(this, options)));
		})
	}
}(window.jQuery)