$(function(){

	//** Placeholder
	$('input[placeholder], textarea[placeholder]').placeholder();

	//** Header -> Menu
	$('.b-header__menu__item:has(.b-dropdown-menu)').each(function(){
		var $item = $(this),
			$link = $item.find('.b-header__menu__link'),
			$dropdown = $item.find('.b-dropdown-menu');
		$item.hover(function(){
			$dropdown.show().css({
				left: $link.offset().left + $link.outerWidth()/2 - $dropdown.outerWidth()/2,
				top: $link.offset().top + $link.outerHeight()
			});
			$item.addClass('b-header__menu__item_opened');
		},function(){
			$dropdown.css('left', -2000).hide();
			$item.removeClass('b-header__menu__item_opened');
		});
	});

	//** Footer -> News
	$('.b-footer__news').each(function(){
		var $tabs = $('<div />');
		$(this).find('.b-footer__news__item').each(function(){
			$tabs.append('<a />')
		});
		if ($('a', $tabs).length) {
			$tabs.tabs('.b-footer__news__item',{
				rotate: true,
				effect: 'fade',
				fadeInSpeed: 500
			}).slideshow({
				interval: 5000,
				autoplay: true,
				clickable: false
			});
		}
	});

	//** Menu
	$(window).load(function(){
		$('.b-menu').each(function(){
			var $menu = $(this),
				$title = $('.b-page-title'),
				$root = $('.b-content__data, .b-content:not(:has(.b-content__data))'),
				$first = $root.find('> :visible:first'),
				$last = $root.find('> :visible:last'),
				$section = $root.find('> .b-section:first');
			$('.b-content:not(:has(.b-content__data))').each(function(){
				$first = $title.next();
			});
			if (($section.is($first))&&($section.is($last))) {
				var height = $menu.outerHeight()-$title.outerHeight()-parseInt($section.css('padding-top'),10)-parseInt($section.css('padding-bottom'),10);
				$section.css('min-height', height);
			};
		});
	});

	//** Select
	$('.b-select').selectBox();

	//** Tooltip
	$('[data-title]').qtip({
		content: {
			text: function() {
				return $('#'+$(this).attr('data-title')).html();
			},
			title: {
				text: function() {
					return $('#'+$(this).attr('data-title-header')).html();
				}
			}
		},
		position: {
			my: 'bottom center',
			at: 'top center'
		},
		style: {
			tip: {
				width: 11
			}
		}
	});

	//** Checkbox list
	$('.b-checkbox-list').each(function(){
		var $checkboxes = $(this).find('.b-checkbox-list__checkbox');
		$checkboxes.change(function(){
			if ($(this).is(':checked')) {
				$(this).parent().addClass('b-checkbox-list__item_checked');
			} else {
				$(this).parent().removeClass('b-checkbox-list__item_checked');
			}
		}).change();
	});

	//** Domains registration
	$('.b-domains-registration__domains').each(function(){
		var $all = $(this).find('.b-checkbox-list__checkbox_all'),
			$checkboxes = $(this).find('.b-checkbox-list__checkbox').not($all);
		$all.change(function(){
			if ($all.filter(':checked').length > 0) {
				$checkboxes.attr('checked','checked').change();
			} else {
				$checkboxes.removeAttr('checked').change();
			}
		});
	});

	//** Lightbox
	$.fn.fancybox.defaults = $.extend($.fn.fancybox.defaults, {
		padding: 20,
		overlayOpacity: 0.8,
		overlayColor: '#164476',
		scrolling: 'no',
		showCloseButton: true
	});
	$.fancybox.defaults = $.extend($.fancybox.defaults, {
		padding: 20//,
		//closeBtn: false,
		//fixed: false
	});
	$('.i-lightbox-link').fancybox();

	//** Search
	$('.b-search').each(function(){
		$(this).find('.b-legend .b-link_pseudo').click(function(){
			$(this).parent().next('.b-search__fieldset').toggle();
			return false;
		});
	});

	//** Reset link
	$('.b-reset-link').click(function(){
		var form = $(this).parents('form')[0];
		form.reset();
		$(form).find('input, textarea').change().removeClass('placeholder');
		return false;
	});

	//** Clear link
	$('.b-clear-link').click(function(){
		var form = $(this).parents('form')[0];
		$(form).find(':input').each(function() {
			switch(this.type) {
				case 'password':
				case 'select-multiple':
				case 'select-one':
				case 'text':
				case 'textarea':
					$(this).val('');
					$(this).change().removeClass('placeholder');
					break;
				case 'checkbox':
				case 'radio':
					this.checked = false;
			}
		});
	});

	//** Rubrics list
	$('.b-form-switcher').each(function(){
		var $list = $(this),
			$inputs = $list.find('.b-form-switcher__input').attr('tabindex','-100');
		$inputs.change(function(){
			var $label = $list.find('.b-form-switcher__label').removeClass('b-form-switcher__label_active').filter('[for="'+$(this).attr('id')+'"]');
			if ($(this).is(':checked')) $label.addClass('b-form-switcher__label_active');
		})
		$inputs.filter(':checked').change();
	});

	//** Quick search
	$('.b-quick-search__alphabet').each(function(){
		var $button = $(this).find('.b-button'),
			$inputs = $(this).find('.b-form-switcher__input');
		$button.click(function(){
			$inputs.removeAttr('checked').change();
		});
	});

	//** Tabs
	$('.b-tabs').each(function(){
		var $tabs = $(this),
			$panes = $(this).next('.b-tabs-panes');
		if (!$panes.length) $panes = $(this).parent().next('.b-tabs-panes');
		$(this).tabs($panes.find('.b-tabs-panes__pane'), {
			tabs: '.b-tabs__item:not(.b-tabs__item_deactive)',
			current: 'b-tabs__item_active'
		});
	});

	//** Options
	$('.b-options').each(function(){
		var $table = $(this); 
		$table.find('.b-table__cell:not(.b-table__cell_first)').each(function(){
			var index = $(this).index(),
				$cells = $table.find('tr').find('.b-table__cell:eq('+index+')');
			$(this).hover(function(){
				$cells.addClass('b-table__cell_hovered');
				$cells.find('.b-button').addClass('b-button_theme_gray');
			},function(){
				$cells.removeClass('b-table__cell_hovered');
				$cells.find('.b-button').removeClass('b-button_theme_gray');
			});
		});
	});

	//** Tables
	$('.b-table_mark_row tr:not(.b-table__row_title):not(.b-table__row_separator)').hover(function(){
		$(this).addClass('b-table__row_hovered');
	},function(){
		$(this).removeClass('b-table__row_hovered');
	});
	
	//** Filter -> Dropdown Menu
	$('.b-filter__item_dropdown:has(.b-filter__item__arrow)').each(function(){

		var $item = $(this),
			$link = $item.next('.b-dropdown-menu-filter'),
			$dropdown = $item.next('.b-dropdown-menu-filter'),
			hide_timeout = null,
			close = function(){
				$dropdown.removeClass('b-dropdown-menu-filter__active');
				$item.removeClass('b-filter__item_opened');
			};

		if ($dropdown.height > 200) { // add scroll for long list
			$dropdown.css('overflow-y', 'scroll');
		}

		$item.hover(function(){
			if (hide_timeout)
				clearTimeout(hide_timeout);
			$dropdown.addClass('b-dropdown-menu-filter__active');
			$item.addClass('b-filter__item_opened');
		}, function(){
			hide_timeout = setTimeout(close, 100);
			$dropdown.hover(function(){
				if (hide_timeout)
					clearTimeout(hide_timeout);
			}, function() {
				hide_timeout = setTimeout(close, 100);
			});
		});
	});

	//** Hosting Order
	$('.b-show_all_specs, .b-show_main_specs').click(function(){
		$('.b-hosting__specs, .b-show_all_specs, .b-show_main_specs').toggle();
	});

	//** Profile Settings
	$('#secret_question').change(function() {
		if ( $('#secret_question_self:selected').length ) {
			$('#secret_question_form').show();
		} else {
			$('#secret_question_form').hide();
		}
	});

	//** Hosting Add
	$('#hosting-add_dog_ok').click(function() {
		if ($(this).is(':checked')) {
			$('#hosting-add_submit')
				.removeClass('b-button_theme_gray')
				.removeAttr('disabled')
				.end();
		} else {
			$('#hosting-add_submit')
				.addClass('b-button_theme_gray')
				.attr('disabled', 'disabled')
				.end();
		}
	});

	//** Registration
	$('#registration_dog_ok').click(function() {
		if ($(this).is(':checked')) {
			$('#registration_submit')
				.removeClass('b-button_theme_gray')
				.removeAttr('disabled')
				.end();
		} else {
			$('#registration_submit')
				.addClass('b-button_theme_gray')
				.attr('disabled', 'disabled')
				.end();
		}
	});

	//** Domains get Sertificate
	if ( $('#obtain_cert_1').attr('checked') ) {
		$('.obtain_cert_1').show();
	} else {
		$('.obtain_cert_1').hide();
	}
	
	if ( $('#obtain_cert_2').attr('checked') ) {
		$('.obtain_cert_2').show();
	} else {
		$('.obtain_cert_2').hide();
	}

	$('#obtain_cert_1').click(function(){
		$('.obtain_cert_1').show();
		$('.obtain_cert_2').hide();
	});
 
	$('#obtain_cert_2').click(function(){
		$('.obtain_cert_1').hide();
		$('.obtain_cert_2').show();
	});
	
	
	//** Domains Guarantee
	$('.b-details__documents__item__link').click(function(){
		return false;
	});
	$('.b-details__documents__item__subtitle').click(function(){
		console.log('click');
		$(this).parents('.b-details__documents__item').next('.b-details__documents__sublist').toggle();
	});

});

function format_price(price) {
	return (price + '').replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ')
} 
