// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults



function reset_form_field(field_id)
{
  $(field_id).value = "";
}

function reset_form(form) {
  form.reset();
}

function toggle_accordion(el_id) {
  var display = $(el_id).style.display;
  if (display == "none") { //show this element
    Effect.BlindDown(el_id);
  }
  else {
    Effect.BlindUp(el_id);
  }
}

function save_and_redirect(form)
{
  var redirect_field = Builder.node('input', {'type' : 'hidden', 'value' : 'redirect', 'name' : 'redirect'});
  form.appendChild(redirect_field);
  form.submit();
}

// Call this function from your forms using button_to_function() helper
// if you just want to jump to some url by clicking on a button rather than
// a link. Classic example is in every form submit is a button but cancel is
// a link and it makes form look odd.

function jump_to(url, newWindow) {
  if( newWindow != undefined && newWindow )
     window.open(url);
  else
     document.location.href = url;
}

function save_as_new_version(form)
{
  id = form.action.substring(form.action.lastIndexOf('/')+1, form.action.length);
  form.action = '/admin/manage_articles/save_as_new_version/'+id;
  form.submit();
}

function getSelectedValue(el_id, variable)
{
  var select = $(el_id);
  var sel_index = select.options.selectedIndex;
  var val = variable + "=" + select.options[sel_index].value;
  return val;
}

function compare(article_id, src_ver_sel_id, target_ver_sel_id)
{
  var src_ver_sel = $(src_ver_sel_id);
  var tgt_ver_sel = $(target_ver_sel_id);
  var src_version = src_ver_sel.options[src_ver_sel.options.selectedIndex].value;
  var tgt_version = tgt_ver_sel.options[tgt_ver_sel.options.selectedIndex].value;
  if (src_version != "--Select--" && tgt_version != "--Select--")
    {
     var url = "/manage_articles/compare/"+article_id+"?v1id=" + src_version + "&v2id=" + tgt_version;
     document.location.href = url;
    }
  else
    {
      if (src_version == "--Select--" && tgt_version == "--Select--")
        alert("Please select the Source & Target Versions to compare");
      else if (src_version == "--Select--" && tgt_version != "--Select--")
        alert("Please select the Source Version");
      else if (tgt_version == "--Select--" && src_version != "--Select--")
        alert("Please select the Target Version");
    }
}


function show_event_details(title, starts_at, ends_at, description, link)
{
  var content = "<b>" + title + "</b>";
  content = content + "<table width='250px' cellpadding='5px' cellspacing='5px'>";
  content = content + "<tr><td>Starts At</td><td align='right'>:</td><td>" + starts_at + "</td></tr>";
  content = content + "<tr><td>Ends At</td><td align='right'>:</td><td>" + ends_at + "</td></tr>";
  content = content + "<tr><td colspan='3'>" + description + "</td></tr>";
  content = content + "<tr><td colspan='3' align='right'>" + link + "&nbsp;&nbsp;";
  content = content + "<a href='#' onclick='Element.toggle(&quot;form-div&quot;);return false;'>Close</a>";
  content = content + "</td></tr>";
  content = content + "</table>";

  Element.setStyle('form-div',{backgroundColor:'#f2f3f7;', fontSize:'11px;'});
  $('form-div').innerHTML = content;
  Element.toggle('form-div');
}

function highlight_comment(checkbox, comment_id)
{
  highlight = checkbox.checked;
  var url = '/comments/set_highlight/' + comment_id + '?highlight=' + highlight;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete:function(request){Element.toggle('load_status')}, onLoading:function(request){Element.toggle('load_status')}})
}

function approve_comment(checkbox, comment_id)
{
  approve = checkbox.checked;
  var url = '/comments/set_approve/' + comment_id + '?approve=' + approve;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete:function(request){Element.toggle('load_status')}, onLoading:function(request){Element.toggle('load_status')}})
}

function show_preview()
{
  content = "<h2>Comment Preview</h2><table>"
  content = content + "<tr><td><b>Name:</b></td><td>" + $('comment_name').value +"</td></tr>";
  content = content + "<tr><td><b>Email:</b></td><td>" + $('comment_email').value +"</td></tr>";
  content = content + "<tr><td valign='top'><b>Comment:</b></td><td align='justify'>" + format_comment_text($('comment_content').value)+ "</td></tr>";
  content = content + "<tr><td colspan='2' align='right'>";
  content = content + "<a href='#' onclick='Element.hide(&quot;comment-preview&quot;);Element.show(&quot;comment-form&quot;);return false;'><button>Close</button></a>";
  content = content + "</td></tr>";
  content = content + "</table>";
  $('comment-preview').innerHTML = content;
  Element.show('comment-preview');
  Element.hide('comment-form');
  Element.setStyle('comment-preview',{backgroundColor:'#f2f3f7;', fontSize:'12px;'});

}
/* returns a formatted comment text
   see simple_format in rails
   */
function format_comment_text(text){

    text.replace(/\r\n?/gi, "\n");  //# \r\n and \r -> \n
    text.replace(/\n\n+/gi, "</p>\n\n<p>");  // 2+ newline  -> paragraph
    text.replace(/([^\n]\n)(?=[^\n])/gi, '\1<br />'); // 1 newline   -> br

    return text;
}
/*
 * This function will construct the hidden input fields
 * for the categories selected for the current article.
 * Also displays the full path to the selected category node.
 */
function set_categories(category_nodes, type)
{
  top_container = null;
  param_name = "";
  if( type == 'topic' ) {
    top_container = $('article-categories');
    param_name = 'topics[]';
  }
  else if( type == 'locality' ){
    top_container = $('article-localities');
    param_name = 'localities[]';
  }

  top_container.innerHTML = "";

  for(var i=0;i<category_nodes.length;i++)
    {
      var cont_id = 'container-' + category_nodes[i].id;
      container = Builder.node('div', {'id' : cont_id});

      var label_id = 'label-' + category_nodes[i].id;
      label = Builder.node('label', {'id' : label_id});
      path = category_nodes[i].getPath('text');
      path = path.substring(path.indexOf('/')+1, path.length);
      path = path.substring(path.indexOf('/')+1, path.length);
      label.innerHTML = path;

      var input_id = 'input-' + category_nodes[i].id;
      input = Builder.node('input', {'id' : input_id, 'type' : 'hidden', 'value' : category_nodes[i].id, 'name' : param_name});

      container.appendChild(label);
      container.appendChild(input);

      top_container.appendChild(container);
    }
}

function set_series(series_select) {

  var id = series_select.value;
  var caption = series_select.innerHTML;
  var selected = series_select.selected;

  // Either unselected a location or if this location is already
  // selected, then remove it.
  if( !selected ) {
    Element.remove('series-' + id );
    children = Element.immediateDescendants('article-series');
    if (children.length == 1)
     {
        Element.show('series_empty_msg');
     }
    else
     {
        Element.hide('series_empty_msg');
     }
    series_select.selected = false;
    return;
  }

  top_container = Ext.get("article-series");

  var cont_id = 'series-' + id;

  var current_series = Element.immediateDescendants("article-series");
  for(var i=0;i<current_series.length;i++) {
      if(cont_id == current_series[i].id) {
          return;
      }
  }

  container = Builder.node('div', {'id' : cont_id});

  var label_id = 'label-' + id;
  label = Builder.node('label', {'id' : label_id});
  label.innerHTML = caption;

  var input_id = 'input-' + id;
  input = Builder.node('input', {'id': input_id, 'type' : 'hidden', 'value' : id, 'name' : 'series[]'});

  container.appendChild(label);
  container.appendChild(input);

  top_container.appendChild(container);

    children = Element.immediateDescendants('article-series');
    if (children.length == 1)
     {
        Element.show('series_empty_msg');
     }
    else
     {
        Element.hide('series_empty_msg');
     }
}

function set_location(location_select) {

  var id = location_select.value;
  var caption = location_select.innerHTML;
  var selected = location_select.selected;

  // Either unselected a location or if this location is already
  // selected, then remove it.
  if( !selected || $('location-'+ id) ) {
    Element.remove('location-' + id );
    children = Element.immediateDescendants('article-locations');
    if (children.length == 1)
     {
        Element.show('locations_empty_msg');
     }
    else
     {
        Element.hide('locations_empty_msg');
     }
    location_select.selected = false;
    return;
  }

  top_container = Ext.get("article-locations");

  var cont_id = 'location-' + id;
  container = Builder.node('div', {'id' : cont_id});

  var label_id = 'label-' + id;
  label = Builder.node('label', {'id' : label_id});
  label.innerHTML = caption;

  var input_id = 'input-' + id;
  input = Builder.node('input', {'id': input_id, 'type' : 'hidden', 'value' : id, 'name' : 'locations[]'});

  container.appendChild(label);
  container.appendChild(input);

  top_container.appendChild(container);

    children = Element.immediateDescendants('article-locations');
    if (children.length == 1)
     {
        Element.show('locations_empty_msg');
     }
    else
     {
        Element.hide('locations_empty_msg');
     }
}

function set_genre(genre) {
  if (genre.id == 'source') {
    Ext.get('genre-caption').dom.innerHTML = "";
    Ext.get('genre-value').dom.value = "";

    Ext.get('genre_empty_msg').dom.innerHTML = " This article doesn't have a Genre";
    Ext.get('genre_empty_msg').show();
  }
  else {
    Ext.get('genre-caption').dom.innerHTML = genre.text;
    Ext.get('genre-value').dom.value = genre.id;

    Ext.get('genre_empty_msg').hide();
  }
}

function add_article_for_newsletter(article_id, article_title)
{
  var container = $('newsletter_articles');
  var sel_article_el = $('article-checkbox-'+ article_id);
  if (!sel_article_el)
    {
      var input_el = Builder.node('input', {type: 'checkbox',
                                      id: 'article-checkbox-'+article_id,
                                      name: 'articles[]',
                                      value: article_id,
                                      checked: true});
      var label_el = Builder.node('label');
      label_el.innerHTML = article_title;
      var el_container = Builder.node('div');
      el_container.appendChild(input_el);
      el_container.appendChild(label_el);
      container.appendChild(el_container);
    }
  else
    {
      sel_article_el.checked = '';
    }
  children = Element.immediateDescendants('newsletter_articles');
  if (children.length == 1)
    {
      Element.show('emptyMsg');
    }
  else
    {
      Element.hide('emptyMsg');
    }
}

function show_caption_field(select_box){

  value = select_box.options[select_box.options.selectedIndex].value;

  if( value == "pics" ) {
    Element.show('pics_caption');
    Element.hide('docs_caption');

    $('pics_caption').setAttribute('name', 'asset[caption]');
    $('docs_caption').setAttribute('name', '');

  }
  else if( value == "docs" ){
    Element.hide('pics_caption');
    Element.show('docs_caption');

    $('docs_caption').setAttribute('name','asset[caption]');
    $('pics_caption').setAttribute('name', '');

  }

}

// This function is called from change_state.rhtml
// while changing the state of an Article.
function toggle_publish_date(change_state_select)
{
   sel_index = change_state_select.options.selectedIndex;
   sel_state = change_state_select.options[sel_index].value;

   if( sel_state == "published" )
      Element.show('publish_date');
   else
      Element.hide('publish_date');

}


function show_popup(popup_id, context_id, popupheader, popuptext, width)  {
    var popup = new YAHOO.widget.Panel(popup_id, { context:[context_id,"tr","br"],
                                                visible:"false",
                                                width:width+"px",
                                                draggable:"true"} );
    popup.setHeader(popupheader);

                popup.setBody(popuptext);
                popup.render(document.body);
                popup.show();

}