/**
 * Contacts block in header and popup form there
 */
(function () {
	var init = function () {
		// Init $ manually to have common jQuery version even on map pages
		var $ = jQuery;

		// Get all contacts and first ones
		var cities = $('#menu p span.city');
		var contacts = cities.parents('li:first');
		var targetCity = cities.first();
		var targetPhone = targetCity.parent().children('span.phone');

		// Cities switch handler
		var switchContact = function () {
			var city = $(this);
			if (city.hasClass('active')) {
				return;
			}
			var key = city.attr('__a2contactKey');
			var container = city.parent();
			container.children('.city').removeClass('city-active');
			container.children('.phone').removeClass('phone-active');
			city.addClass('city-active');
			container.children('.' + key).addClass('phone-active');
		}

		// Add class to style rich interface
		contacts.addClass('js');

		// Prevent Yandex.Maps handlers on contacts block
		if (typeof YMaps != 'undefined') {
			contacts.bind(
				"contextmenu click dblclick mouseup mousedown mousemove wheel",
				function (e) {e.stopPropagation()}
			);
		}

		// Move cities and phones under one elements
		cities.each(function (i) {
			// Define current elements
			var city = $(this);
			var container = city.parent();
			var phone = container.children('span.phone');

			// Add link attributes
			var key = 'contact' + i;
			city.attr('__a2contactKey', key);
			city.addClass(key);
			phone.addClass(key);
			city.click(switchContact);
			// Set active and exit if first (active by default) pair
			if (! i) {
				city.addClass('city-active');
				phone.addClass('phone-active');
				return;
			}

			// Move city to cities line
			targetCity.after(city);
			targetCity = city;
			targetCity.before(' / ');

			// Move phone to phones line
			targetPhone.after(phone);
			targetPhone = phone;

			// Remove empty container
			container.remove();
		});

		// Select actual default city
		if (typeof window.__a2defaultCity != 'undefined') {
			$('#menu p span.city.' + window.__a2defaultCity).not('.city-active').click();
		}

		// Form show code
		contacts.children('form').submit(function (e) {
			var $this = $(this);
			var containerClass = 'contacts-form';
			var contentId = 'contacts-form-container';
			var container = jQuery('#wrapper .' + containerClass);

			var addDecorum = function (container) {
				// Close link
				var close = $('<div class="close">&nbsp;</div>');
				container.append(close);
				close.click(function () {container.remove()});
				// Pointing triangle
				container.append('<div class="arrow"></div>');
				// Webkit file field
				if (jQuery.browser.webkit) {
					container.find('input[type=file]').css({border: 'none', color: '#fff'});
				}
			}

			if (container.length) {
				// Remove form container
				container.remove();
				e.preventDefault();
				return false;
			}
			else {
				// Create form container
				var offset = $this.offset();
				container = $('<div class="' + containerClass + '"><div id="' + contentId + '"></div></div>');
				container.css({
					top: offset.top + $this.outerHeight() + 30,
					right: $(document).width() - offset.left - $this.outerWidth() + 40
				});
				$('#wrapper').append(container);
				addDecorum(container);
				// Prevent Yandex.Maps handlers on form container
				if (typeof YMaps != 'undefined') {
					container.bind(
						"contextmenu click dblclick mouseup mousedown mousemove wheel",
						function (e) {e.stopPropagation()}
					);
				}
			}

			// Load form
			container.children('#' + contentId).addClass('loader');
			container.load(
				$this.attr('action'),
				function () {
					var form = container.find('form');
					addDecorum(container);
					form.find("[placeholder]").placeholder();
					container.removeClass('loader');

					var options = {
						target : container,
						beforeSerialize : function ($form, options) {
							$form.append('<input type="hidden" name="data[Contact][ajax]" value="1" />');
							$form.find('[placeholder]').placeholder('clear');
						},
						beforeSend : function () {
							container.children('#' + contentId).addClass('loader');
						},
						success : function () {
							container.find('[placeholder]').placeholder();
							container.children('#' + contentId).removeClass('loader');
							addDecorum(container);
							container.find('form').ajaxForm(options);
						}
					}
					form.ajaxForm(options);
				}
			);

			e.preventDefault();
			return false;
		});
	}

	// Run on normal document.ready or Yandex.Maps one if defined
	typeof YMaps == 'undefined' ? jQuery(init) : YMaps.jQuery(init);
})();

