/*
 * Nested Attributes
 *
 * */


function remove_fields(link, matching_up_selector) {
  $(link).prev("input[type=hidden]").val("1");
  $(link).parents(matching_up_selector).hide();
}

function add_fields(link, association, content, callback) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g");
  $(content.replace(regexp, new_id)).insertBefore($(link).parent());

  if (jQuery('.child select').length!=0) {
    jQuery('.child select').selectbox();
  }

  if(callback != "null") {
    eval(callback.replace(regexp, new_id));
  }
}

/*
 * Family Structure
 *
 * */
var next_family_parents_index      = 0;
var next_family_grandparents_index = 0;
var next_family_dogs_index         = 0;
var next_family_cats_index         = 0;
var increment_index;

/* Look at select and input fields under the given collection_kind div class
 * to replace the appropriate DUMMY_INDEX string by the correct next index value */
function replace_family_index(collection_kind, next_index) {
  var next_family_index = eval("next_family_" + collection_kind + "_index");

  if( next_index > next_family_index ) {
    next_family_index = next_index;
  }

  text_to_replace = collection_kind.toUpperCase() + "_INDEX";
  increment_index = false;
  $('div.' + collection_kind.replace(new RegExp("s$"), '')).each(function(div) {
    $(div).filter("input, select").each(function(e) {
      var name = e.attr('name');
      if( name.include(text_to_replace) ) {
        e.attr('name', name.gsub(text_to_replace, next_family_index))
        increment_index = true;
      }
    });
  });

  if( increment_index ) {
    eval("next_family_" + collection_kind + "_index = " + (next_family_index + 1));
  }
}

/* Replace dummy index for story images upload */
var next_story_image_index = 0;

function replace_story_image_index(next_index) {
  if(next_index > next_story_image_index) {
    next_story_image_index = next_index;
  }

  $$('div.story_image').each(function(div) {
    div.select('input[@type=file]').each(function(e) {
        e.writeAttribute('name', e.readAttribute('name').gsub(/REPLACE_STORY_IMAGE_INDEX/, next_story_image_index))
    });
  });

  next_story_image_index++;
}

/*
 * Rating System
 *
 * */

var sMax;  // Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function rating(num){
  sMax = 0;  // Isthe maximum number of stars
  for(n=0; n<num.parentNode.childNodes.length; n++){
    if(num.parentNode.childNodes[n].nodeName == "A"){
      sMax++;
    }
  }

  if(!rated){
    s = num.id.replace("_", ''); // Get the selected star
    a = 0;
    for(i=1; i<=sMax; i++){
      if(i<=s){
        document.getElementById("_"+i).className = "on";
        document.getElementById("rateStatus").innerHTML = num.title;
        holder = a+1;
        a++;
      }else{
        document.getElementById("_"+i).className = "";
      }
    }
  }
}

// For when you roll out of the the whole thing //
function off(me){
  if(!rated){
    if(!preSet){
      for(i=1; i<=sMax; i++){
        document.getElementById("_"+i).className = "";
        document.getElementById("rateStatus").innerHTML = me.parentNode.title;
      }
    }else{
      rating(preSet);
      document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
    }
  }
}

// When you actually rate something //
function rateIt(me){
  if(!rated){
    document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
    preSet = me;
    rated=1;
    sendRate(me);
    rating(me);
  }
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
  alert("Your rating was: "+sel.title);
}
