var auth_closed = true;
var auth_moving = false;
var tw;
window.addEvent('domready', function() {
    // снег
//    initsnow();
    
	var open_register = $('open-register');
	var close_register = $('close-register');
	if (open_register) {
		tw = new Tween([$('container-header'), $('container-content'),
						$('container-content')], {
					onStart : function() {
						auth_moving = true;
					},
					onComplete : function() {
						auth_moving = false;
						auth_closed = !auth_closed;
					}
				});

		close_register.addEvent('click', function(e) {
					new Event(e).stop();
					if (!auth_moving && !auth_closed) {
						tw.start({
									'0' : {
										property : 'top',
										anim : [0, -60]
									},
									'1' : {
										property : 'top',
										anim : [0, -60]
									},
									'2' : {
										property : 'marginBottom',
										anim : [0, -60]
									}
								});
					}
				});

		open_register.addEvent('click', function(e) {
					new Event(e).stop();
					
					if (!auth_moving && auth_closed) {
						if (window.Fx) {
							new Fx.Scroll($(window)).set(0);
						}
						$('auth-email').focus();
						tw.start({
									'0' : {
										property : 'top',
										anim : [-60, 0]
									},
									'1' : {
										property : 'top',
										anim : [-60, 0]
									},
									'2' : {
										property : 'marginBottom',
										anim : [-60, 0]
									}
								});
					}
					if (!auth_moving && !auth_closed) {
						tw.start({
									'0' : {
										property : 'top',
										anim : [0, -60]
									},
									'1' : {
										property : 'top',
										anim : [0, -60]
									},
									'2' : {
										property : 'marginBottom',
										anim : [0, -60]
									}
								});
					}
				});
	}

	// логинимся
	var login_button = $('login-button');
	if (login_button) {
		login_button.addEvent('click', function(e) {
					var evt = new Event(e);
					evt.stop();

					this.getParent('form').submit();
				});
	}

	// ставим "запомни меня"
	var remember_link = $('remember_link');
	if (remember_link) {
		remember_link.addEvent('click', function(e) {
					var evt = new Event(e);
					evt.stop();
					var original_checkbox = remember_link.getPrevious();

					if (original_checkbox.checked) {
						this.removeClass('checked');
						original_checkbox.checked = false;
					} else {
						this.addClass('checked');
						original_checkbox.checked = true;
					}
				});
	}

	// подменю. попытка №2
	var menu_lis = $$('li.submenu');
	$each(menu_lis, function(li) {
        var div = li.getElement('div');
        var divHeight = 0;
        
        // находим высоты дивов
        div.setStyle('visibility', 'hidden').setStyle('display', 'block');
        divHeight = div.getCoordinates().height;
        div.setStyle('display', '').setStyle('visibility', '').setStyle('height', 0).setStyle('opacity', 0);
	            
        var toggleTween = new Fx.Morph(div, {
            property: 'height',
            duration: 350,
            onStart: function() {
                div.setStyle('overflow', 'hidden');
                if (!div.getStyle('height').toInt()) {
                    li.addClass('hover');
                }
            },
            onComplete: function() {
                if (!div.getStyle('height').toInt()) {
                    li.removeClass('hover');
                }
                div.setStyle('overflow', '');
            }
        });
	            
		li.addEvents({
			mouseenter : function() {
			    toggleTween.cancel();
				toggleTween.start({
				    height: divHeight,
				    opacity: 1
				});
			},
			mouseleave : function() {
			    toggleTween.cancel();
			    toggleTween.start({
			        height: 0,
			        opacity: 0
			    });
			}
		});
	});
	
	
	// блок текста на главной
	(function() {return;
	    var container = $('main-welcome'); if (!container) return;
	    var extra = container.getElement('.extra');
	    var extraHeight = (function() {
	        var h = extra.setStyle('opacity', 0).show().getSize().y;
            extra.setStyles({
                'height': 0,
                'opacity': 1
            });
            return h;
	    })();
	    
	    var hidden = true;
	    
	    var l = container.getElement('a');
	    var fx = new Fx.Tween(extra, {
	        property: 'height',
	        duration: 100,
	        onStart: function() {
	            fxing = true;
	        },
	        onComplete: function() {
	            hidden = !hidden;
	            
	            if (hidden) {
	                l.set('text', 'Подробнее');
	            } else {
	                l.set('text', 'Скрыть');
	            };
	            
	            fxing = false;
	        }
	    });
	    var fxing = false;
	    
	    l.addEvent('click', function(e){
	       var event = new Event(e);
	       event.stop();
	       
	       if (fxing) return;
	       
	       if (hidden) {
	           fx.start(extraHeight);
	       } else {
	           fx.start(0);
	       };
	       
	    });
	})();
	
});
