var initButtons = function(container) {
	(container || $(document.body)).find('.button')
		.hover(function() {
			var _this = $(this);
			if (_this.hasClass('b-active')) {
				_this.removeClass('b-active').addClass('b-hover');
			} else {
				_this.addClass('b-hover');
			}
		}, function() {
			var _this = $(this);
			if (_this.hasClass('b-active')) {
				_this.removeClass('b-active').removeClass('b-hover');
			} else {
				_this.removeClass('b-hover');
			}
		})
		.mousedown(function() {
			$(this).addClass('b-active');
		})
		.mouseup(function() {
			$(this).removeClass('b-active');
		});
};

$(function() {
	initButtons();
});

$(function() {

	var popupWindowCloseHandler = function(event) {
		event.preventDefault();
		$('body').children('.popup-window-box').fadeOut('fast').remove();
		$('body').children('.popup-window-shadow').fadeOut('fast').remove();
		$(document).unbind('keyup', popupWindowESCHandler);
		calendarManager.close();
	};

	var popupWindowESCHandler = function(event) {
		if (event.keyCode == 27) {
			popupWindowCloseHandler(event);
		}
	};

	var popupWindow = function(url) {

		var box = $(document.createElement('div'));


		box.addClass('popup-window-box');

		var shadow = $(document.createElement('div')).appendTo('body');
		shadow.addClass('popup-window-shadow')
			.append('<span></span>')
			.fadeIn('fast')
			.click(popupWindowCloseHandler);

		var center = function() {
			box.fadeIn('fast')
				.css('left', ($(document).scrollLeft() + (($(window).width() - box.width()) / 2)) +'px')
				.css('top', ($(document).scrollTop() + (($(window).height() - box.height()) / 2)) +'px')
				.find('.popup-window-close')
					.click(popupWindowCloseHandler);
		}

		var onLoad = function() {
			box.appendTo('body');

			initFormValidators(box);
			initButtons(box);
			initAjaxForm(box);
			calendarManager.run(box.get(0));
			
			box.find('.ddlist.ddlist-first:not(.ddlist-one)').ddlist({firstOpened: true, oneOpened: false});
			box.find('.ddlist.ddlist-one:not(.ddlist-first)').ddlist({firstOpened: true, oneOpened: true});
			box.find('.ddlist:not(.ddlist-one):not(.ddlist-first)').ddlist({firstOpened: false, oneOpened: false});
			
			center();
			
			$(document).bind('keyup', popupWindowESCHandler);

			this.onload = null;
		};

		if (/\.jpg$/.test(url)) {
			box.append('<img src=""/>').addClass('popup-window-image');
			var image = box.find('img');
			image.bind('click', popupWindowCloseHandler);
			image.get(0).onload = function() {
				box.appendTo('body');
				center();
				this.onload = null;
			};
			image.get(0).src = url;
		} else {
			box.load(url, null, onLoad);
		}
		

	};

	$('.popup-window').click(function(event) {
		event.preventDefault();
		popupWindow(this.href);
	});
});

$(function() {

	$('.i-toggle-line .i-toggle-line-button').click(function() {
		$(this).parents('.i-toggle-line').toggleClass('i-toggle-line-opened');
	});

});

$(function() {

	var box = $('#equipment');
	var list = $('#equipment ul');


	
	var moveList = function() {
		var offset = ((box.width() - list.width()) / 2);
		if (offset < -60) {
			offset = -60;
		}
		list.css('left', offset +'px');
	};

	moveList();

	$(window).resize(moveList);

	$('#equipment .picture').each(function() {
		var pic = $(this);
		this.overviewBox = pic.next('.overview');
		pic.hover(function() {
			$(this).animate({opacity: 1}).get(0).overviewBox.animate({opacity: 1}).next('.bg').animate({opacity: .5});
		}, function() {
			$(this).animate({opacity: 0}).get(0).overviewBox.animate({opacity: 0}).next('.bg').animate({opacity: 0});
		});
	});

	$('#equipment .overview').each(function() {
		box.append($(this).detach()).append('<div class="bg"/>');
		$(this).next('.bg').css('height', $(this).height() + 'px');
	});

	$.each(['tl', 'tr', 'bl', 'br'], function() {
		box.append('<img class="corner corner-'+ this +'" src="/images/equipment_promo/'+ this +'.png">')
	});

});

var initAjaxForm = function(container) {

	(container || $(document)).find('form.f-form').bind('submit', function(event) {
		var form = $(this);
		if (!form.hasClass('f-with-error')) {
			$.post(this.action, form.serialize(), function(data) {
				form.parent().append(data);
				form.remove();

				if (
					(typeof pageTracker != 'undefined')
						&& (typeof pageTracker._trackPageview == 'function')
				) {
					var trackAlias = form.find('input[type=hidden]').filter(function()  {
						return /trackPageviewAlias/.test(this.name);
					});
					if (trackAlias.size() > 0) {
						pageTracker._trackPageview(trackAlias.val());
					}
				}

			}, 'html');
			event.preventDefault();
		}
	});

};

var inityaMetric = function() {

	var YaMetricID = $(document).find('input[type=hidden]').filter(function()  {
		return /yaMetricID/.test(this.name);
	});
	if (YaMetricID.size() > 0) {
		var Yacode = "yaCounter2921377.reachGoal('"+ YaMetricID.val()+ "');";
		$("form.f-form").attr("onSubmit", Yacode);
	}
	//alert(YaMetricID.val());

};

$(function() {
	initAjaxForm();
	inityaMetric();
	$(".al-index_html a").attr("href","/index.html#utmxid=EAAAACQcMjRbultyCUUeJkNsMJk;utmxpreview=0;utmxreload=0");

	$('.image-gallery a[rel]').colorbox({
		opacity: 0.3,
		close: '<i class="i16 close"></i>',
		inline: false,
		scrolling: false,
		current: 'Фотография {current} из {total}',
		next: '&rarr;',
		previous: '&larr;'
	});
	$('.image-gallery a.show-all span').toggle(
		function (){
			$(this).empty().text('Скрыть все').parent().prev().slideDown();
		},
		function (){
			$(this).empty().text('Показать все').parent().prev().slideUp();
		}
	);
});

