function toggle_minus_plus(bool_show, img_hide, img_show, content_id) {
  if (bool_show) {
    Element.hide(img_show);
    Element.show(img_hide);
    Element.show(content_id);
  }
  else 
  {
    Element.hide(img_hide);
    Element.show(img_show);
    Element.hide(content_id);  
  }
}

function openPreviewWindow(url, o) {
  tb_show('Forum Post Preview', url + '?o=' + o + '&KeepThis=true&TB_iframe=true&width=560&height=205', false);
}

function openSignatureEdit(url) {
  tb_show('Edit Forum Signature', url + '?KeepThis=true&TB_ReloadParent=true&TB_iframe=true&width=560&height=205', false);
}

function getFCKInstance(obj_name) {
  return FCKeditorAPI.GetInstance(obj_name + '_body_editor') ;    
}

function updateReplyContainer() {
  $('post-reply-form').innerHTML = 'Loading... Please wait.'; 
  $('edit-post-target').innerHTML = ''; 
  Element.hide('edit-form');
  Element.show('post-reply');
  scrollBottom();
}

function updateEditContainer() {
  $('edit-post-target').innerHTML = 'Loading... Please wait.'; 
  $('post-reply-form').innerHTML = ''; 
  Element.hide('post-reply');
  Element.show('edit-form');
  scrollBottom();  
}

function scrollBottom() {
  if ($("edit-form") != null && Element.visible("edit-form")) {
    Element.scrollTo("edit-form"); 
  } else if ($("post-reply") != null && Element.visible("post-reply")) {
    Element.scrollTo("post-reply");   
  }  
}

function hideSearchBox(e) {
  clickedArea = Event.element(e);
  searchBox = $("forum-search-box");
  if (clickedArea.id != "fsearch-link-top" && clickedArea.id != "fsearch-link-bottom" && !Element.descendantOf(clickedArea, searchBox)) {
    Element.hide(searchBox);
    Event.stopObserving(window, 'click', hideSearchBox);
  }
}

function toggleForumSearchBox(position) {
  searchLink = $("fsearch-link-" + position);
  if (searchLink != null) {
    pos = Element.cumulativeOffset(searchLink);
    searchBox = $("forum-search-box");
    if (searchBox != null) {
      if (position  == "top") {
        searchBox.setStyle({'top':(pos.top + searchLink.getDimensions().height + 3) + "px", 'left':pos.left + "px"});
      } else {
        searchBox.setStyle({'top':(pos.top - searchBox.getDimensions().height - 3) + "px", 'left':pos.left + "px"});
      }
      Element.toggle(searchBox);
      if (Element.visible(searchBox)) { 
        Event.observe(document, 'click', hideSearchBox);
      }
    }
  }
}

function showImageFS(e) {
  ele = $(Event.element(e)).up('.resized-img-header');
  window.open(Element.readAttribute(ele, 'alt'), "ForumImageFSWin");  
}

function resizeForumImagesPreview() {
  resizeForumImages('#preview-post-body');
}

function resizeForumImagesMain() {
  resizeForumImages('div.forum-post-text-container');
  resizeForumImages('div.signature');
}

function resizeForumImages(contentClass) {
  contentContainers = $$(contentClass);
  if (contentContainers != null && contentContainers.length > 0) {
    for (i=0; i<contentContainers.length; i++) {
      imgs = contentContainers[i].select('img');
      if (imgs.length > 0) {
        for (j=0;j<imgs.length;j++) {
          img = imgs[j];
          origW = img.width;
          origH = img.height;
          w = img.width;
          h = img.height;

          containerPos = Element.cumulativeOffset(contentContainers[i]);
          imgPos = Element.cumulativeOffset(img);
          maxWidth = contentContainers[i].getDimensions().width;
          maxWidth -= (imgPos.left - containerPos.left);

          if (img.width > maxWidth) {
            maxWidth -= 70;
            f = 1 - ((w - maxWidth) / w);
            newWidth = Math.round(w * f);
            newHeight = Math.round(h * f);
            img.width = newWidth;
            img.height = newHeight;

            Element.setStyle(img, {'width':newWidth + 'px', 'height':newHeight + 'px'});
            Element.setStyle(img, {'display':'block'});
            
            // captionID = 'fimgrs-' + i + '-' + j
            imgCaption = new Element('div', {'class':'resized-img-header', 'title':'Click to view full size', 'alt':img.src});
            imgCaption.setStyle({width: img.width + "px"});
            imgCaption.update("<b class='rounded-box'><b class='fimg-resize-l1'></b><b class='fimg-resize-l2'></b><b class='fimg-resize-l3'></b></b><div class='fimg-resize-content'>" +
                              "<img src='/images/forums/image_link.png' class='resized-img-icon'></img>" +
                              "<span class='resized-img-text'>Reduced: " + Math.round(f * 100) + "% of original size [" + origW + " x " + origH + "] - " +
                              "click here for full size</span></div>");
            imgParent = Element.up(img);
            if (imgParent.tagName == "A") {
              Element.insert(imgParent, {'before':imgCaption});
            } else {
              Element.insert(img, {'before':imgCaption});
            }
            Event.observe(imgCaption, 'click', showImageFS)
          }
        }
      }
    }
  }
}
