var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var LoginForm = {
  setToPassword: function() {
    $('openid_fields').hide();
    $('password_fields').show();
  },
  
  setToOpenID: function() {
    $('password_fields').hide();
    $('openid_fields').show();
  }
}

var EditForm = {
  // show the form
  init: function(postId) {
    $('edit-post-' + postId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current post id we're editing
  setReplyId: function(postId) {
    $('edit').setAttribute('post_id', postId.toString());
    $('post_' + postId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },
  
  // clears the current post id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('post_' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },
  
  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },
  
  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}

if (typeof Event.addBehavior != "undefined") {
    Event.addBehavior({
      '#search,#monitor_submit': function() {this.hide();}
    })
}

var submenu_current = null;
var submenu_visible = false;
var submenu_keepalive = false;
var submenu_timer = -1;

function showSubmenu(submenu) {
    if (submenu != submenu_current) {
        if (submenu_current != null) {
            hideSubmenu(submenu_current);
        }

        submenu_current = submenu;
        submenu_keepalive = false;
        setTimeout(function() {
            if (submenu_timer > 0) {
                clearTimeout(submenu_timer);
            }

            $('submenu_' + submenu_current + '_content').show();
            submenu_timer = setTimeout(function() {
                if (submenu_current != null && !submenu_keepalive) {
                    hideSubmenu(submenu_current);
                }
            }, 3000);
        }, 100);
    }
}

function hideSubmenu() {
    $('submenu_' + submenu_current + '_content').hide();
    submenu_current = null;
}

$j(document).ready(function() {
    $j('#submenu_labs').mouseenter(function() {
        showSubmenu('labs');
    });

    $j('#submenu_labs_content').mouseenter(function () {
        submenu_keepalive = true;
    }).mouseleave(function() {
        hideSubmenu();
    });

    $j('#submenu_about').mouseenter(function() {
        showSubmenu('about');
    });

    $j('#submenu_about_content').mouseenter(function () {
        submenu_keepalive = true;
    }).mouseleave(function() {
        hideSubmenu();
    });

    $j('#submenu_support').mouseenter(function() {
        showSubmenu('support');
    });

    $j('#submenu_support_content').mouseenter(function () {
        submenu_keepalive = true;
    }).mouseleave(function() {
        hideSubmenu();
    });
});

function beforeLoginSubmit() {
    var url = 'o1host.net';

    if ($('use_ssl').checked) {
        $('login').action = 'https://' + url + '/login';
    } else {
        $('login').action = 'http://' + url + '/login';
    }
    
    $('login').submit();
    return false;
}

function sendFeedback(button) {
    $(button).disable();
    feedback_email = $('feedback_email').getValue();
    feedback_text = $('feedback_text').getValue();
    new Ajax.Request('/users/send_feedback', {
        method: 'post',
        parameters: {
            email: feedback_email,
            text: feedback_text
        },
        onSuccess: function(tx) {
            alert('Ваше сообщение успешно отправлено!');
            $('feedback_text').setValue('');
            $(button).enable();
        }
    })
}
