function starChange(el){
  el = $(el);
  if(!el) return;
  el.up("fieldset").select(".avatar").invoke("hide");
  $("idol"+el.getValue()).show();
}

function remove_if_exists(el){
  el = $(el);
  if (el) el.remove();
}

function replace_if_exists(el_id, value){
  el = $(el_id);
  if (!el) return;

  var active_selector = null;
  var active = el.select('.active');
  if (active.length == 1){
    active = active.first();
    var cn = $A(active.className.split(' '));
    if (cn.length == 2){
      active_selector = '.' + cn.without('active').first();
    }
  }

  el.replace(value);

  if(active_selector){
    active = $(el_id).select(active_selector);
    if(active.length == 1){
      active = active.first();
      active.addClassName('active');
    }
  }
}

function disable_for_ajax(el, text){
  el.setAttribute('originalValue', el.value);
  el.disabled = true;
  el.value = text;
  setTimeout(function(){
    el.form.onsubmit();
  }, 0);
  return false;
}

function autoFill(el, cleanUp){
  el = $(el);
  if (!el) return;
  if(cleanUp===undefined) cleanUp=true;
    
  var title = el.title;
  if(el.getValue() == "" || el.getValue() == title){
    el.addClassName("readonly").setValue(title);
  }
    
  el.observe('focus',function(){
    if(el.getValue() == title){
      el.setValue("").removeClassName("readonly");
    }
  });
  el.observe('blur', function(){
    if(el.getValue() == ""){
      el.addClassName("readonly").setValue(title);
    }
  });

  if(!cleanUp) return;
  var form = el.up('form');
  if (!form) return;
  form.observe('submit', function(){
    if(el.getValue() == title) el.setValue("");
    return true;
  });
}

function openQuestion(){
  $$("#faq dl dt").each(function(el){
    el.observe("click", function() {
      el.up('dl').toggleClassName("open");
    });
  });
}

autoFill("email");
autoFill("password");
openQuestion();


function replyTo(userName, commentId, url){
  var text = "@"+userName+", ";
  if(url){
    url += "?comment_body=" + encodeURIComponent(text) + "&comment_parent_id=" + commentId + "#add_comment";
    window.location = url;
  } else {
    var comment_body = $('comment_body');
    var comment = comment_body.getValue();
    if (comment && comment.length!=0){
      text = comment + ( comment.endsWith(' ') ? '' : ' ' ) + text;
    }
    comment_body.setValue(text);
    $('comment_parent_id').setValue(commentId);
    $('add_comment').scrollTo();
    setCaretToEndOfField("#comment_body");
  }
}

function setCaretToEndOfField(el){
  el = $$(el).first();
  var pos = el.getValue().length;
  if(el.createTextRange){
    var range = el.createTextRange();
    range.move("character", pos);
    range.select();
  } else if(el.setSelectionRange){
    el.focus();
    el.setSelectionRange(pos, pos);
  } else {
    el.focus();
  }
}

function scrollToIfInvisible(el){
  el = $(el);
  //bug in el.viewportOffset() in Opera
  var viewportOffsetY = el.cumulativeOffset()[1]-document.viewport.getScrollOffsets()[1];
  if(viewportOffsetY<0 || viewportOffsetY>=document.viewport.getHeight()){
    el.scrollTo();
  }
}

Object.extend(Element.Methods, {
  autoheightTextarea: function(el) {
    var original_height = el.rows;
    var updateHeight = function() {
      var strings = el.getValue().split('\n');
      var rows = strings.length;
      strings.each(function(s) { rows += (s.length / 80).round(); });
      el.rows = (rows/0.8 <= original_height) ? original_height : original_height*2;
    };
    el.observe("keypress", updateHeight);
    el.observe("input", updateHeight);
    el.observe("beforepaste", updateHeight);
    updateHeight();
  }
});
Element.addMethods();

function showOnHover(selector, el){
  el = $(el);
  var btn = el.select(selector).first();
  if(btn){
    el.observe('mouseover', function(){ btn.style.visibility="visible"; });
    el.observe('mouseout', function(){ btn.style.visibility="hidden"; });
  }
}

function checkByValue(selector, name){
  $$(selector).select(function(el){ return el.value == name; }).each(function(el) { el.checked = true; });
}

function showAskQuestion(){
  $('add-question').show();
  var self=$('ask-panel-ask');
  self.parentNode.innerHTML=self.innerHTML;
  $('ask_question_textarea').focus();
  return false;
}
