function open_popup(code) {
  var myw = window.open('/markets/dynamic/market_details.php?code=' + code,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=645,height=470,resizable');
}
function open_popup_other(code, file) {
  var myw = window.open('/markets/dynamic/market_details_other.php?code=' + code + '&file=' + file,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=645,height=470,resizable');
}

function get_jax(p_url, p_element) {
  var el = $(p_element);
  var ajax_loader = '<img src="/markets/dynamic/ajax-loader.gif" class="ajax_loader">';
  $.ajax({
    url: p_url,
    type: "GET",
    dataType: "html",
    beforeSend: function() {
      el.append(ajax_loader);
    },
    complete: function() {
      el.find('.ajax_loader').remove();
    },
    success: function(response, status) {
      el.html(response);
    },
    error: function() {
      alert('Error contacting server to fetch data, please refresh the page to try again.');
    }
  });
}

function init_auto(p_url, p_element) {
  var el = $(p_element);
  var ajax_loader = '<img src="/markets/dynamic/ajax-loader.gif" class="ajax_loader">';
  $.ajax({
    url: p_url,
    type: "GET",
    dataType: "json",
    beforeSend: function() {
      el.after(ajax_loader);
    },
    complete: function() {
      el.next('.ajax_loader').remove();
    },
    success: function(response, status) {
      $(p_element).autocomplete({
        source: response,
        select: function(event, ui) {
          $(this).val(ui.item.id);
          return false;
        }
      })
    },
    error: function() {
      alert('Error contacting server to fetch data, please refresh the page to try again.');
    }
  });
}

function show_graph(element, image) {
  var el = $(element);
  el.html('<img src="http://196.30.144.98/irsites/cnbc/graphs/' + image + '.png?r=' + Math.random() + '" border="0">');
}

$(function() {
  get_jax('/markets/dynamic/get_indices.php?file=indicesshortlist&tag=indicator', '#jse_indicators');
  get_jax('/markets/dynamic/get_options.php?file=indicesshortlist&tag=indicator', '#jse_graphs');
  get_jax('/markets/dynamic/get_indices.php?file=AfricanIndices_Kenia&tag=share', '#kenya_indicators');
  get_jax('/markets/dynamic/get_options.php?file=AfricanIndices_Kenia&tag=share', '#kenya_graphs');
  get_jax('/markets/dynamic/get_indices.php?file=AfricanIndices_Nigeria&tag=share', '#nigeria_indicators');
  get_jax('/markets/dynamic/get_options.php?file=AfricanIndices_Nigeria&tag=share', '#nigeria_graphs');
  get_jax('/markets/dynamic/get_currencies.php?file=currencies', '#currency');
  get_jax('/markets/dynamic/get_currencies.php?file=commodities', '#commodities');
  get_jax('/markets/dynamic/get_currencies_af.php?file=africancurrencies&which=KES', '#kenya_currency');
  get_jax('/markets/dynamic/get_currencies_af.php?file=africancurrencies&which=NGN', '#nigeria_currency');

  show_graph('#graph_jse', 'J203_i');
  show_graph('#graph_kenya', 'Kenia_NSE 20_3m');
  show_graph('#graph_nigeria', 'Nigeria_NSEI_3m');

  init_auto('/markets/dynamic/get_json.php?file=equitiesfull&tag=share', '#jse_autocomplete');
  init_auto('/markets/dynamic/get_json.php?file=AfricanEquitiesfull_Kenia&tag=share', '#kenya_autocomplete');
  init_auto('/markets/dynamic/get_json.php?file=AfricanEquitiesfull_Nigeria&tag=share', '#nigeria_autocomplete');

});

