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

// Displays an alert if for link_to_unimplimented
function unimplemented() {
  alert("NOTICE\n\nThis feature is not implemented yet. Please check back again soon!");
}

function toggle_strategy_dropdown() {
  $('x_strategy_dropdown_top_amount').toggle();
}

function toggle_year_dropdown() {
  $('x_year_dropdown').toggle();
}

function toggle_fund_type_dropdown() {
  $('x_fund_type_dropdown').toggle();
}

function generate_clone(clone_id) {
  $('x_clone_in_progress').show();
  $('x_clone_results').hide();
  $('x_generate_clone_button').hide();
  new Ajax.Request('/clones/generate?id=' + clone_id + '&active_strategy=' + $('active_strategy').value + '&active_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + $('active_hedging_amount').value)
}

function select_strategy(clone_id, strategy_key) {
  new Ajax.Request('/clones/select_strategy?id=' + clone_id + '&previous_strategy=' + $('active_strategy').value + '&active_strategy=' + strategy_key + '&previous_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + $('active_hedging_amount').value, {asynchronous:true, evalScripts:true}); return false;
}

function select_top_amount(clone_id, top_amount) {
  new Ajax.Request('/clones/select_top_amount?id=' + clone_id + '&active_strategy=' + $('active_strategy').value + '&active_top_amount=' + top_amount + '&previous_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + $('active_hedging_amount').value, {asynchronous:true, evalScripts:true}); return false;
}

function select_market_index(clone_id, market_index_id) {
  new Ajax.Request('/clones/select_market_index?id=' + clone_id + '&market_index_id=' + market_index_id + '&active_strategy=' + $('active_strategy').value + '&active_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + $('active_hedging_amount').value + '&is_marketing_site=' + $('is_marketing_site').value + '&strategy_name=' + $('strategy_name').value, {asynchronous:true, evalScripts:true, onLoading:processing_clone, onComplete:processing_clone_complete}); return false;
}

function select_hedging_amount(clone_id, hedging_amount) {
  new Ajax.Request('/clones/select_hedging_amount?id=' + clone_id + '&active_strategy=' + $('active_strategy').value + '&active_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + hedging_amount + '&previous_hedging_amount=' + $('active_hedging_amount').value, {asynchronous:true, evalScripts:true}); return false;
}

function select_rebalancing_method(clone_id, rebalancing_method) {
  new Ajax.Request('/clones/select_rebalancing_method?id=' + clone_id + '&active_strategy=' + $('active_strategy').value + '&active_top_amount=' + $('active_top_amount').value + '&active_rebalancing_method=' + rebalancing_method + '&previous_rebalancing_method=' + $('active_rebalancing_method').value + '&active_hedging_amount=' + $('active_hedging_amount').value, {asynchronous:true, evalScripts:true}); return false;
}

function select_fund_group_year(fund_group_id, year, quarter) {
  new Ajax.Request('/fund_groups/select_period?id=' + fund_group_id + '&year=' + year + '&quarter=' + quarter, {asynchronous:true, evalScripts:true}); return false;
}

function select_fund_group_year_for_constituents(fund_group_id, year, quarter) {
  new Ajax.Request('/fund_groups/select_period_constituents?id=' + fund_group_id + '&year=' + year + '&quarter=' + quarter, {asynchronous:true, evalScripts:true}); return false;
}

function toggle_feedback_wrapper() {
  var current_state = $('x_feedback_wrapper_opened').value;
  if ( current_state == "1" ) {
    $('x_feedback_widget_wrapper').removeClassName('x_feedback_widget_opened');
    $('x_feedback_widget_wrapper').addClassName('x_feedback_widget_closed');
    $('x_feedback_wrapper_opened').value = '0';
  } else {
    $('x_feedback_widget_wrapper').removeClassName('x_feedback_widget_closed');
    $('x_feedback_widget_wrapper').addClassName('x_feedback_widget_opened');
    $('x_feedback_wrapper_opened').value = '1';
  }
  new Ajax.Request('/feedback_submissions/toggle_open_hide_state', {asynchronous:true, evalScripts:true}); return false;
  return true;
}

function toggle_fund_group_sectors_filter() {
  if ($('x_fund_group_sectors').hasClassName('active')) {
    $('x_fund_group_sectors').removeClassName('active')
    $('x_fund_group_sectors_filter').hide();
  } else {
    $('x_fund_group_cap_groups').removeClassName('active')
    $('x_fund_group_cap_groups_filter').hide()
    $('x_fund_group_sectors').addClassName('active')
    $('x_fund_group_sectors_filter').show();
  }
}

function toggle_fund_group_cap_groups_filter() {
  if ($('x_fund_group_cap_groups').hasClassName('active')) {
    $('x_fund_group_cap_groups').removeClassName('active')
    $('x_fund_group_cap_groups_filter').hide();
  } else {
    $('x_fund_group_sectors').removeClassName('active')
    $('x_fund_group_sectors_filter').hide()
    $('x_fund_group_cap_groups').addClassName('active')
    $('x_fund_group_cap_groups_filter').show();
  }
}

function select_stock_fund_search(fund_id, stock_id) {
  new Ajax.Request('/stocks/select_fund?fund_id=' + fund_id + '&id=' + stock_id, {asynchronous:true, evalScripts:true}); return false;
}

function select_stock_or_fund_search(object_id) {
  window.location = '/site_searches/select_search_result?object=' + object_id
}

function select_search_result(object_id) {
  window.location = '/site_searches/select_search_result?object=' + object_id
}


function processing_clone() {
  $('x_clone_in_progress').show();
  $('x_clone_results').hide();
}

function processing_clone_complete() {
  $('x_clone_in_progress').hide();
  $('x_clone_results').show();
}

function show_clones_tab_content() {
  $('x_clone_tab').addClassName("active")
  $('x_clone_tab_content').show();
  $('x_holdings_tab').removeClassName("active")
  $('x_holdings_tab_content').hide();
}

function show_holdings_tab_content() {
  $('x_clone_tab').removeClassName("active")
  $('x_clone_tab_content').hide();
  $('x_holdings_tab').addClassName("active")
  $('x_holdings_tab_content').show();
}

function show_fund_group_clones_tab_content() {
  $('x_clone_tab').addClassName("active")
  $('x_clone_tab_content').show();
  $('x_funds_in_group_tab').removeClassName("active")
  $('x_funds_in_group_tab_content').hide();
}

function show_fund_group_funds_in_group_tab_content() {
  $('x_clone_tab').removeClassName("active")
  $('x_clone_tab_content').hide();
  $('x_funds_in_group_tab').addClassName("active")
  $('x_funds_in_group_tab_content').show();
}

function leaderboard_top_clones_select_global() {
  reset_top_clones_tabs();
  $('x_top_clones_global').addClassName("active")
  $('x_global_fund_groups').show();
  $('x_single_fund_fund_groups').hide();
  $('x_multi_fund_fund_groups').hide();
  new Ajax.Request('/rankings/select_top_clones_strategy/?strategy=all', {asynchronous:true, evalScripts:true}); return false;
}

function leaderboard_top_clones_select_single_funds() {
  reset_top_clones_tabs();
  $('x_top_clones_sf').addClassName("active")

  $('x_global_fund_groups').hide();
  $('x_single_fund_fund_groups').show();
  $('x_multi_fund_fund_groups').hide();

  $('x_strategy_sf').addClassName("selected");
  $('x_strategy_tab_sf_top_holdings').removeClassName("meta-selected");
  $('x_top_amounts_sf_top_holdings').hide();
  $('x_strategy_tab_sf_best_ideas').removeClassName("meta-selected");
  $('x_top_amounts_sf_best_ideas').hide();

  new Ajax.Request('/rankings/select_top_clones_strategy/?fund_group_type=sf', {asynchronous:true, evalScripts:true}); return false;
}

function leaderboard_top_clones_select_multi_funds_table() {
  reset_top_clones_tabs();
  $('x_top_clones_mf').addClassName("active")

  $('x_global_fund_groups').hide();
  $('x_single_fund_fund_groups').hide();
  $('x_multi_fund_fund_groups').show();

  $('x_strategy_mf').addClassName("selected");
  $('x_strategy_tab_mf_top_holdings').removeClassName("meta-selected");
  $('x_top_amounts_span_mf_top_holdings').hide();
  $('x_top_amounts_mf_top_holdings').hide();
  $('x_strategy_tab_mf_popularity_prom_queens').removeClassName("meta-selected");
  $('x_top_amounts_mf_popularity_prom_queens').hide();
  $('x_strategy_tab_mf_best_ideas').removeClassName("meta-selected");
  $('x_top_amounts_mf_best_ideas').hide();

  new Ajax.Request('/rankings/select_top_clones_strategy/?fund_group_type=mf', {asynchronous:true, evalScripts:true}); return false;
}

function reset_top_clones_tabs() {
  $('x_top_clones_global').removeClassName("active");
  $('x_top_clones_sf').removeClassName("active");
  $('x_top_clones_mf').removeClassName("active");
}

function select_saved_clone_list(saved_clone_list_id) {
  new Ajax.Request('/saved_clone_lists/select_saved_clone_list?id=' + saved_clone_list_id + '&date_span=' + $('date_span').value, {asynchronous:true, evalScripts:true}); return false;
}





/* Admin */

function select_performance_strategy(strategy) {
  new Ajax.Request('/admin/clones/change_performance_criteria/?strategy=' + strategy, {asynchronous:true, evalScripts:true, onLoading:clone_performance_processing()}); return false;
}

function select_performance_rebalancing_method(rebalancing_method) {
  new Ajax.Request('/admin/clones/change_performance_criteria/?rebalancing_method=' + rebalancing_method, {asynchronous:true, evalScripts:true, onLoading:clone_performance_processing()}); return false;
}


function select_performance_hedging_amount(hedging_amount) {
  new Ajax.Request('/admin/clones/change_performance_criteria/?hedging_amount=' + hedging_amount, {asynchronous:true, evalScripts:true, onLoading:clone_performance_processing()}); return false;
}

function admin_select_search_result(object_id) {
  window.location = '/admin/site_searches/select_search_result?object=' + object_id
}

function clone_performance_processing() {
  $('x_clone_performance').fade({duration: 0.1});
  $('x_clone_performance_loading').appear({duration: 0.1});
}

function clone_performance_display_span_data() {
  $('x_clone_performance_annual').hide()
  $('x_clone_performance_date_spans').show()
}

function clone_performance_display_annual_data() {
  $('x_clone_performance_date_spans').hide()
  $('x_clone_performance_annual').show()
}

function market_index_performance_display_span_data() {
  $('x_market_index_performance_annual').hide()
  $('x_market_index_performance_date_spans').show()
}

function market_index_performance_display_annual_data() {
  $('x_market_index_performance_date_spans').hide()
  $('x_market_index_performance_annual').show()
}