/**
 * @author ricky
 */

window.addEvent('domready', function() {
/**
 * Субменю каталога
 */
	(function() {
		var catalog_submenu = $('catalog-submenu');
		var li_catalog = $$('li.catalog')[0];
		var position = li_catalog.getPosition();

		catalog_submenu.setStyles({
					'left' : position.x,
					'top' : position.y
				});

		li_catalog.getElement('a').addEvents({
					'click' : function(e) {
						return false;
					},
					'mouseenter' : function() {
						position = li_catalog.getPosition();
						catalog_submenu.setStyles({
									'left' : position.x,
									'top' : position.y
								});
						catalog_submenu.show();
					}
				});
		catalog_submenu.addEvent('mouseleave', function(e) {
					var evt = new Event(e);
					this.hide();
				});
	})();
/**
 * Боковое меню
 */
	(function() {
		var side_menus = $$('.side-menu');
		if (side_menus.length) {
			$each(side_menus, function(item) {
						var open_handlers = item.getElements('a.dashed');

						if (open_handlers.length) {
							$each(open_handlers, function(handler) {
										handler.addEvent('click', function(e) {
													var evt = new Event(e);
													evt.stop();

													if (this.getNext()) {
														this.getNext().toggle();
													}
												});
									});
						}
					});
		}

		var h2 = $$('.left-col h2 a');
		if (!h2.length)
			return;

		h2 = h2[0];
		h2.addEvent('click', function(e) {
					var evt = new Event(e);
					evt.stop();

					var parent = this.getParent();
					parent.getNext().toggle();
					if (parent.get('class').match(/fall\-winter\-gray/)) {
						parent.removeClass('fall-winter-gray')
								.addClass('fall-winter');
						return;
					}
					if (parent.get('class').match(/fall\-winter/)) {
						parent.removeClass('fall-winter')
								.addClass('fall-winter-gray');
						return;
					}
					if (parent.get('class').match(/spring\-summer\-gray/)) {
						parent.removeClass('spring-summer-gray')
								.addClass('spring-summer');
						return;
					}
					if (parent.get('class').match(/spring\-summer/)) {
						parent.removeClass('spring-summer')
								.addClass('spring-summer-gray');
						return;
					}
				});
	})();

/**
 * Клубки товаров
 */
	(function() {
		var color_list = $$('.colors-list');
		if (color_list.length) {
			var lis = color_list[0].getElements('li > a:first-child');
			$each(lis, function(item) {
						item.addEvents({
									'mouseover' : function() {
										this.addClass('opacity');
									},
									'mouseout' : function() {
										this.removeClass('opacity');
									}
								});
					});
		}
	})();
/**
 * Форма логина
 */
	(function() {
		function input_focus(e) {
			this.addClass('focus');
			if (this.id === 'login') {
				if (this.value.trim() === 'Электронная почта') {
					this.value = '';
				}
			}
			if (this.id === 'password') {
				if (this.value.trim() === 'Пароль') {
					this.value = '';
				}
			}
			this.addClass('ok');
		}

		function input_blur(e) {
			this.removeClass('focus');
			if (this.id === 'login') {
				if (!this.value.trim().length) {
					this.value = 'Электронная почта';
					this.removeClass('ok');
				}
			}
			if (this.id === 'password') {
				if (!this.value.trim().length) {
					this.value = 'Пароль';
					this.removeClass('ok');
				}
			}
		}

		var auth_form = $('auth-form');
		if (auth_form) {
			var login_input = $('login');
			login_input.addEvent('focus', input_focus);
			login_input.addEvent('blur', input_blur);
			var password_input = $('password');
			password_input.addEvent('focus', input_focus);
			password_input.addEvent('blur', input_blur);
			auth_form.addEvent('submit', function(e) {
						var evt = new Event(e);
						if (!login_input.value.trim().length
								|| login_input.value.trim() === 'Электронная почта'
								|| !password_input.value.trim().length
								|| password_input.value.trim() === 'Пароль') {
							evt.stop();
							alert('Неправильно заполнена форма логина!');
						}
					});
		}
	})();
});
