function initContentAccordion() {
  $jq(".section_change dd.sc_closed").hide();
  $jq(".section_change dt span").click(function(){
    // this forces one section to stay open, if we want one always open, just remove the dt/link
    //if($jq(this).attr("open")=="true" && openCount==1) return;
    var elem = $jq(this).parent();
    if(elem.attr("open")=="true")
    {
      openCount--;
      elem.attr("open","false");
      elem.next("dd").removeClass('sc_open').addClass('sc_closed').slideUp(300);
      elem.removeClass("ctrl_line");
      elem.addClass("ctrl_line_close");
    }else
    {
      openCount++;
      elem.attr("open","true");
      elem.next("dd").removeClass('sc_closed').addClass('sc_open').slideToggle(300);
      elem.removeClass("ctrl_line_close");
      elem.addClass("ctrl_line");
    }
  });
}

function accordionExpandAll() {
  var link = $jq('#accordion_control_link');
  if (link.text() == '[Expand all sections]') {
    $jq(".section_change dt.ctrl_line_close .section_change_a").each(function(i) { $jq(this).parent().click(); });
    link.text('[Collapse all sections]');
  } else {
    $jq(".section_change dt.ctrl_line .section_change_a").each(function(i) { $jq(this).parent().click(); });
    link.text('[Expand all sections]');
  }
}

function mceSetup(fieldName, imageUrl, height) {
  if (!height) height = 530;

  tinyMCE_GZ.init({
    themes : "advanced",
    plugins : 'wiserlink,wiserimage,wiserextimg,wiservideo,table',
    languages : 'en',
    disk_cache : true
  }, function() {
    tinyMCE.init({
      mode : "exact",
      elements : fieldName,
      height : height,
      remove_linebreaks : false,
      convert_urls : false,
      content_css : $jq.path+"css/stylesheet.css",
      imageUrl : imageUrl,
      linkUrl : '/media/insert/link',
      baseUrl : $jq.path,
      theme : "advanced",
      plugins : "table,wiserlink,wiserextimg,wiserimage,wiservideo",
      theme_advanced_buttons1: "wiservideo,wiserextimg,wiserimage,wiserlink,link,unlink,bold,italic,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist",
      theme_advanced_buttons2: "fontselect,fontsizeselect",
      theme_advanced_buttons3: "table,row_props,cell_props,row_after,delete_row,col_after,delete_col,code",
      theme_advanced_toolbar_location : "top",
      theme_advanced_toolbar_align : "left",
      theme_advanced_statusbar_location : "bottom",
      theme_advanced_resizing : true,
      spellcheck : true,
      cleanup_on_startup : false,
      cleanup : true,
      convert_fonts_to_spans : false,
      force_br_newlines : true,
      forced_root_block : ''
    });
  });
}

function startEdit(fieldName) {
  if (!fieldName) var fieldName = 'special_text';
  switchToEdit();
  $jq('#view_'+fieldName).hide();
  $jq('#edit_'+fieldName).show();
}

function endEdit(fieldName) {
  if (!fieldName) var fieldName = 'special_text';
  $jq('#view_'+fieldName).show();
  $jq('#edit_'+fieldName).hide();
  //tinyMCE.execCommand('mceRemoveControl', false, 'special_text');
  switchFromEdit();
}

function initTinyMCE() {
  tinyMCE_GZ.init({
    plugins : 'wiserlink,wiserimage,wiserextimg,wiservideo,table',
    languages : 'en',
    themes : 'advanced',
    disk_cache : true,
    debug : true
  });
}

function showTaxonomyPopup(obj, id, type) {
  showPopup(obj, $jq.path+'index.php/aof/browse?select='+id+'&type='+type);
}

function toggleExpertModeForms() {
  var elems = $jq('#form_content .form_tip, #form_content .form_example');
  var link = $jq('#expert_mode_link');
  var enable = elems[0].visible();
  if (enable) {
    elems.hide();
    link.text('Disable Expert Mode');
  } else {
    elems.show();
    link.text('Enable Expert Mode');
  }
  $jq.ajax({
    url: $jq.path+'index.php/user/expert_mode/'+enable,
    type: 'GET',
    timeout: 8000
  });
}

var slideShowIndex = 0;
var slideShowCount = $jq('#slideshow_count').text();
function slideShowSwitch(index, target) {
  if (target == 'slideshow') {
    slideShowIndex += index;
    if (slideShowIndex < 0) slideShowIndex = slideShowCount - 1;
    if (slideShowIndex >= slideShowCount) slideShowIndex = 0;
  }

  $jq('#'+target).html($jq('#'+target+'_'+slideShowIndex).html());
  $jq('#'+target+'_current').text(slideShowIndex+1);
}
