// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function rating_button_for(name, rating) {
  return document.getElementById(name + "_rating_" + rating);
}

function rating_buttons_upto(name, upto) {
  buttons = new Array();
  for (i=1;i<=upto;i++) {
    buttons[i] = rating_button_for(name, i);
  }
  return buttons;
}

function swapout_image(elem, newsrc) {
  elem.oldsrc = elem.src;
  elem.src = newsrc;
}

function restore_image(elem) {
  if (elem.oldsrc) {
    elem.src = elem.oldsrc;
  }
}

function light_rating_buttons_for(name, rating, newsrc) {
  unlight_rating_buttons_for(name);
  rating_buttons_upto(name, rating).forEach(function(button) {
    swapout_image(button, newsrc);
  });
}

function unlight_rating_buttons_for(name) {
  rating_buttons_upto(name, 5).forEach(function(button) {
    restore_image(button);
  });
}

function rating_button_details(button) {
  matches = button.id.match(/(\w+)_rating_(\d+)/);
  matches.name = matches[1];
  matches.rating = new Number(matches[2]);
  return matches;
}

function light_fellow_rating_buttons(button, newsrc) {
  details = rating_button_details(button);
  light_rating_buttons_for(details.name, details.rating, newsrc);
}

function unlight_fellow_rating_buttons(button) {
  details = rating_button_details(button);
  unlight_rating_buttons_for(details.name);
}

function hide_element(element) {
  $(element).animate({ opacity: 'hide' }, 'slow');
}

// Set the jQuery Accept header so that we can use Rails' respond_to functionality
jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} });

// Report links
$(function() {
  $('.timeline_comment .report').hide();
});

$(function() {
  $('.timeline_comment').bind('mouseenter', function(){ $('.report', this).animate({ opacity: 'show' }, 'fast');  } )
                        .bind('mouseleave', function() { $('.report', this).hide(); } );
});

// Idea new form
$(function () {
  $('form#new_idea').find('#section_novel, #section_scalable, #section_feasible').hide().end()
                    .find('#section_summary').after(
                      $('<li>The sections for novelty, scalability and feasibility are optional.  You can come back at any time and edit any section. To fill in these sections now, please click here <img src="/images/icons/mojo-arrowright-small.png" style="vertical-align:middle"/></li>')
                      .css("cursor","pointer").click(function () {
                        $('#section_novel, #section_scalable, #section_feasible').slideToggle();
                      })
                    );

});

// Setup the cluetips
$(document).ready(function() {
  $('.cluetip').livequery(function() {
    $(this).cluetip({
      splitTitle: '|' // use the invoking element's title attribute to populate the clueTip...
                      // ...and split the contents into separate divs where there is a "|"
    , cluezIndex: 2000  // so it appears above JQuery UI dialogs (z-index 1000)
    });
  });
});

// To support our link_to_destroy helper
function confirm_destroy(element, message, action, token_name, token_value) {
  if (confirm(message || "Are you sure?")) {
    var f = document.createElement('form');
    f.style.display = 'none';
    element.parentNode.appendChild(f);
    f.method = 'POST';
    // Copy in the URL
    f.action = action;
    var m = document.createElement('input');
    m.setAttribute('type', 'hidden');
    m.setAttribute('name', '_method');
    m.setAttribute('value', 'delete');
    f.appendChild(m);
    var s = document.createElement('input');
    s.setAttribute('type', 'hidden');
    s.setAttribute('name', token_name);
    s.setAttribute('value', token_value);
    f.appendChild(s);
    f.submit();
  }
  return false;
}
