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

function number_to_currency( number ) {
  return '$' + number.toFixed( 2 );
}

function attach_autoprompt( selector, message ) {
  var o = $(selector);

  // when you set 'defaultValue' it sets 'value' - so we need to first set 
  // 'defaultValue' and then set 'value' if it already had a value
  var val = o.attr('value');
  o.attr( 'defaultValue', message);

  if( val == '' ) {
    o.addClass('dimmed');
    o.attr('value', o.attr('defaultValue') );
  }
  else {
    o.attr('value', val );
  }

  o
    .focus(function() {
      if( this.value == this.defaultValue) {
        $(this).attr('value','');
        $(this).removeClass('dimmed');
      }
    })
    .blur(function() {
      if( this.value == '' ) {
        $(this).attr('value', $(this).attr('defaultValue'));
        $(this).addClass('dimmed');
      }
    });
}

function show_help() {
  var h1 = $('#heading');
  var spot = h1.offset().left + h1.width() + 50;

  var underlap = '-.3em';
  $('#help_tab')
    .css( 'left', spot )
    .mouseover( function(){ $(this).animate({ top: 0 },        50); })
    .mouseout(  function(){ $(this).animate({ top: underlap }, 50); })
    .show()
    .animate({ top: underlap }, 1000 );
}

function has_help( timer ) {
  reset_help( timer );
  $(document).bind( 'mouseup keyup', function() { reset_help( timer ) } );
}

var timeout;
function reset_help( timer ) {
  if( timeout ) clearTimeout(timeout);
  if( $('#help_tab').is(':hidden') ) {
    timeout = setTimeout( show_help, timer);
  }
}

function hide_by_prompt_val() {
  var hide_func = function(p) {
    affected = $('.hides_target').filter(function() {
      return $(this).data('hides_toggler_id') == $(p).attr('id');
    });
    if ($(p).val() == '') {
      affected.show();
    } else {
      affected.hide();
    }
  }
  $('.hides_toggler').change(function(event) {
    hide_func(this);
  });
  $('.hides_toggler').each(function(idx,p) {
    hide_func(p);
  });
}

function add_input_classes() {
  $('input').each( function(i) {
    var type = $(this).attr('type');
    switch( type ) {
      case 'button':
      case 'reset':
      case 'submit':
        $(this).addClass('button');
      case 'checkbox':
      case 'radio':
      case 'file':
      case 'hidden':
      case 'image':
        $(this).addClass(type);
        return;
    }
    $(this).addClass('text').addClass(type);
  });
}

$(function() {
  add_input_classes();

  $('img').each( function() {
    var o = $(this);
    if( ! o.attr('title') && o.attr('alt') ) o.attr('title', o.attr('alt') );
  });

  hide_by_prompt_val();

  if( $('#help_tab') ) has_help( 5000 );

  $('form a.add_child').click(function() {
    var association = $(this).attr('data-association');
    var template = $('#' + association + '_fields_template').html();
    var regexp = new RegExp('new_' + association, 'g');
    var new_id = new Date().getTime();

    $(this).before(template.replace(regexp, new_id));
    return false;
  });

  $('form a.remove_child').live('click', function() {
    var hidden_field = $(this).prev('input[type=hidden]')[0];
    if(hidden_field) {
      hidden_field.value = '1';
    }
    $(this).parents('.fields').hide();
    return false;
  });

  if($('#assistance_link').length > 0){
    $('#assistance_link').click(function(){
      window.open("http://app.helponclick.com/help?lang=en&w=4db8a8e8-63d8-4b47-81c9-0585b86aab83&d=4db8abd7-e6bc-478b-9ac9-058bb86aab83","hoc_chat_login","width=720,height=550,scrollbars=no,status=0,toolbar=no,location=no,resizable=no")
    })
  }

});


