formsHandler = {
	toggleOpenClose : function(block) {
		if ($(block).hasClass('js-opened')) {
			$(block).removeClass('round_blue').removeClass('r-border-blue').removeClass('js-opened').addClass('round_dark_gray').addClass('r-border-dark_gray').addClass('js-closed');
		} else {
			$(block).addClass('round_blue').addClass('r-border-blue').addClass('js-opened').removeClass('round_dark_gray').removeClass('r-border-dark_gray').removeClass('js-closed');
			if ($(block).id == 'add_thing_form') {
				$(block).addClass('url_thing_active').removeClass('local_thing_active');
			}
		}
	},
	openLocalForm : function(block) {
		if ($(block).hasClass('url_thing_active')) { $(block).removeClass('url_thing_active'); }
		$(block).addClass('local_thing_active');
	},
	openUrlForm : function(block) {
		if ($(block).hasClass('local_thing_active')) { $(block).removeClass('local_thing_active'); }
		$(block).addClass('url_thing_active');
	}
};

authForm = {
	reg : false,
	closeForm : function () {
		$('fixed_layout').addClass('hidden');
	},
	showAuthForm : function () {
		$('auth_form').removeClass('hidden');
		$('reg_form').addClass('hidden');
		$('fixed_layout').removeClass('hidden');
	},
	showRegForm : function () {
		$('auth_form').addClass('hidden');
		$('reg_form').removeClass('hidden');
		$('fixed_layout').removeClass('hidden');
	},
	checkAuthForm : function ()  {
		authForm.reg = false;
		authForm.clearErrors();
		if (authForm.checkEmail($('js-input_auth_email')) && authForm.checkPassword($('js-input_auth_password'))) {
			//alert('Все хорошо, отправляем ajax');
			//return true;
		}
	},
	checkRegForm : function ()  {
		authForm.reg = true;
		authForm.clearErrors();
		if (authForm.checkLogin($('js-input_reg_login'))) {
			if (authForm.checkPassword($('js-input_reg_password'))) {
				if (authForm.checkDblPassword($('js-input_reg_password'), $('js-input_reg_password_2'))) { 
					if (authForm.checkEmail($('js-input_reg_e-mail'))) {
						if (authForm.checkAgree($('js-input_reg_agree'))) {
							//alert('Все хорошо, отправляем ajax');
						}
					}
				}
			}
		}
	},
	checkLogin : function (input) {
		if (input.value.length < 1) {
			authForm.setError(input, 'Введите имя');
			return false;
		}
		if (input.value.length < 2) {
			authForm.setError(input, 'Имя должно быть не менее двух символов');
			return false;
		}
		/*if (!input.value.match(/^[0-9a-zA-Z]+$/)) {
			authForm.setError(input, 'Логин должен состоять только из латинских букв и арабских цифр');
			return false;
		}*/
		return true;
	},
	checkPassword : function (input) {
		if (input.value.length < 1) {
			authForm.setError(input, 'Введите пароль');
			return false;
		}
		return true;
	},
	checkDblPassword : function (input1, input2) {
		if (input1.value.length < 1) {
			authForm.setError(input1, 'Введите пароль');
			return false;
		}
		if (input2.value.length < 1) {
			authForm.setError(input2, 'Введите пароль повторно');
			return false;
		}
		if (input2.value.length != input1.value.length) {
			authForm.setError(input1, 'Введенные пароли не совпадают');
			authForm.setError(input2, '');
			return false;
		}
		return true;
	},
	checkEmail : function (input) {
		if (input.value.length < 1) {
			authForm.setError(input, 'Введите e-mail');
			return false;
		}
		if (!input.value.match(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)) {
			authForm.setError(input, 'Введите правильный e-mail');
			return false;
		}
		return true;
	},
	checkAgree : function (input) {
		if (!input.checked) {
			authForm.setError(input, 'Вы не согласились с условиями пользовательского соглашения');
			return false;
		}
		return true;
	},
	setError : function (input, message) {
		if (authForm.reg) {
			if (message.length > 0) {
				$('js-reg_errors').innerHTML += message + '<br>';
			}
			$('js-reg_errors').removeClass('hidden');
		} else {
			if (message.length > 0) {
				$('js-auth_errors').innerHTML += message + '<br>';
			}
			$('js-auth_errors').removeClass('hidden');
		}
		input.getParent('.field').addClass('error');
	},
	clearErrors : function () {
		if (authForm.reg) {
			$('reg_form').getElements('form .error').removeClass('error');
			$('js-reg_errors').innerHTML = '';
			$('js-reg_errors').addClass('hidden');
		} else {
			$('auth_form').getElements('form .error').removeClass('error');
			$('js-auth_errors').innerHTML = '';
			$('js-auth_errors').addClass('hidden');
		}
	}
};
noticeHandler = {
	input : function (text, target, type) {
		if (type == 'blur') {
			if (target.value.length == 0) {
				$(target).removeClass('input_active');
				target.value = text;
			}
		}
		if (type == 'focus') {
			if (target.value == text) {
				target.value = ''; 
				$(target).addClass('input_active');
			}
		}
	}
};
initAltTitle = function ($book_id) {
	var wait;
	$('js-book'+$book_id+'-alt_title').addEvent('mouseenter', function (event) {
		$('js-book'+$book_id+'-alt_title').addClass('book_alt_title_m_over');
		if (Browser.Engine.trident) {
			var w = $('js-book'+$book_id+'-alt_title').getElement('.inner').getSize().x;
			$('js-book'+$book_id+'-alt_title').getElements('.tb, .bb').setStyles({'width': w + 'px'});
		}
		$clear(wait);
	});
	$('js-book'+$book_id+'-alt_title').addEvent('mouseleave', function (event) {
		wait = (function () {$('js-book'+$book_id+'-alt_title').removeClass('book_alt_title_m_over');}).delay(1);
	});
};

var CSelects = [];

var CSelect = new Class({
	Implements: Options,
	options: {
        limit:10
    },
    initialize: function(el){
		CSelects.push(this);
        this.el = $(el);
		this.el.addClass('hidden');
		this.opened = false;
		this.closefunc = (this.close).bind(this);
		this.active_text = this.el.options[this.el.selectedIndex].text;
		if (this.el.options[0].disabled && this.el.selectedIndex == 0) {
			var no_active_index = true;
		} else {
			var no_active_index = false;
		}
		// make html
		var iHTML = '';
		iHTML += '<span class="csel_bgl"><span class="csel_bgr"><span class="csel_inner">' + this.active_text + '</span></span></span>';
		iHTML += '<div class="csel_opts r-border-shape"><div class="tb"><div class="cn l"></div><div class="cn r"></div></div><div class="csel_opts_inner">';
		
		var active_opt_index = 0; // for marking first active option
		var no_active_index = false;
		for (var i = 0; i < this.el.length; i++) {
			if (this.el.options[i].disabled) {
				if (this.el.selectedIndex == i) {
					iHTML += '<a href="#" class="csel_opt csel_opt_disabled" style="display:none;">' + this.el.options[i].text + '</a>';
				} else {
					iHTML += '<a href="#" class="csel_opt csel_opt_disabled">' + this.el.options[i].text + '</a>';
				}
			} else {
				iHTML += '<a href="#" class="csel_opt">' + this.el.options[i].text + '</a>';
			}
			
			if (this.el.selectedIndex == i) {
				active_opt_index = i;
			}
		}
		iHTML += '</div><div class="bb"><div class="cn l"></div><div class="cn r"></div></div></div>';
		if (Browser.Engine.trident) {
			this.csel = new Element('span', {'class':'cselect'});
		} else {
			this.csel = new Element('div', {'class':'cselect'});
		}
		this.csel.innerHTML = iHTML;
		this.csel.inject(this.el, 'after');
		
		// mark first active option
		this.active_opt = this.csel.getElements('.csel_opt')[active_opt_index];
		this.active_opt.addClass('csel_opt_a');
		
		// options click
		var opts = $A(this.csel.getElements('.csel_opt'));
		opts.each((function (opt, i) {
			$(opt).addEvent('click', (function (e) {
				e = new Event(e);
				e.preventDefault();
				e.stopPropagation();
				if (!opt.hasClass('csel_opt_disabled')) {
					this.csel.getElement('.csel_inner').innerHTML = opt.innerHTML;
					this.active_text = opt.innerHTML;
					this.setSize();
					this.el.selectedIndex = i;
					opt.addClass('csel_opt_a');
					if (this.active_opt) {
						this.active_opt.removeClass('csel_opt_a');
					}
					this.active_opt = opt;
					this.close();
					if (this.el.onchange) {
						this.el.onchange();
					}
				}
			}).bind(this));
		}).bind(this));
		
		this.csel.getElement('.csel_opts').addEvent('click', (function (e) {
			e = new Event(e);
			e.preventDefault();
			e.stopPropagation();
		}).bind(this));
		
		this.csel.addEvent('click', (function (e) {
			e = new Event(e);
			e.preventDefault();
			e.stopPropagation();
			if (this.csel.hasClass('cselect_opened')) {
				this.close();
			} else {
				this.open();
			}
		}).bind(this));
		
    },
	
	setSize : function () {
		this.csel.getElement('.csel_inner').style.paddingRight = '0';
		
		var csel_width = this.csel.getSize().x;
		var csel_opts_width = this.csel.getElement('.csel_opts').getSize().x;
		var csel_opts_height = this.csel.getElement('.csel_opts').getSize().y;
		
		if (csel_width >= csel_opts_width) {
			this.csel.getElement('.csel_opts').style.width = csel_width + 'px';
			this.csel.getElement('.csel_opts .bb').style.width = csel_width + 'px';
			this.csel.getElement('.csel_opts .tb').style.width = csel_width + 'px';
			// ie 7
			this.csel.getElement('.csel_opts .tb .r').style.marginLeft = csel_width + 'px';
			this.csel.getElement('.csel_opts .bb .r').style.marginLeft = csel_width + 'px';
		} else {
			this.csel.getElement('.csel_inner').style.paddingRight = csel_opts_width - csel_width + 'px';
			this.csel.getElement('.csel_opts').style.width = csel_opts_width + 'px';
			this.csel.getElement('.csel_opts .bb').style.width = csel_opts_width + 'px';
			this.csel.getElement('.csel_opts .tb').style.width = csel_opts_width + 'px';
			// ie 7
			this.csel.getElement('.csel_opts .tb .r').style.marginLeft = csel_opts_width + 'px';
			this.csel.getElement('.csel_opts .bb .r').style.marginLeft = csel_opts_width + 'px';
		}
		
		var place_at_the_bottom = window.getSize().y - this.csel.getPosition().y + window.getScroll().y - this.csel.getSize().y;
		var place_at_the_top =  this.csel.getPosition().y - window.getScroll().y;
		
		if (csel_opts_height <= place_at_the_bottom || csel_opts_height >= place_at_the_top) {
			// options at the bottom
			this.csel.removeClass('cselect_opened_top');
			this.csel.getElement('.csel_opts').removeClass('csel_opts_top');
			this.csel.getElement('.csel_opts').style.top = '24px';
		} else {
			// options at the top
			this.csel.addClass('cselect_opened_top');
			this.csel.getElement('.csel_opts').addClass('csel_opts_top');
			this.csel.getElement('.csel_opts').style.top = -csel_opts_height + 5 + 'px';
		}
		
		
		
	},
	
	close : function () {
		this.opened = false;
		this.csel.removeClass('cselect_opened');
		this.csel.removeClass('cselect_opened_top');
		this.csel.getElement('.csel_inner').style.paddingRight = '2px';
		document.removeEvent('click', this.closefunc);
	},
	
	open : function () {
		for (var i = 0; i < CSelects.length; i++) {
			if (CSelects[i].opened) {
				CSelects[i].close();
			}
		}
		this.opened = true;
		this.csel.addClass('cselect_opened');
		this.setSize();
		document.addEvent('click', this.closefunc);
	}
});
