// Common JavaScript code across your application goes here.

function dialog(txt, options) {
  // If foo.html, render page
  if(/^\w+\.html$/.test(txt))  return dialog_from_template(txt, options);

  options = options || {};

  if(options.elephant) txt = "<img src='http://cdn.memorize.com/images/dialog_elephant.gif'> " + txt;

  dialog_haze();
  var window_top = $(window).scrollTop();

  var result = '<div class="dialog_wrapper" style="z-index:'+(gl.gray_haze_z+1)+';"><div class="dialog" style="top:'+window_top+'px">';

  if(options.no_close)
    options.ignore_bg_click = true;
  else
    result += '<div class="close"><img src="http://cdn.memorize.com/images/cancel.gif"><a href="#">close</a></div>';

  result += txt + '</div></div>';

  $('body').append(result);

  var close_function = options.close_function || close_dialog;
  if(! options.ignore_bg_click) {
    $('.dialog_haze:last').click(close_function);
    $('.dialog_wrapper:last').click(close_function);
    $('.dialog:last').click(function(e){e.stopPropagation();});   // Block clicks on dialog
  }
  $('.dialog:last .close a').click(close_function);

  return false;
}

function close_dialog_default() {
  $('.dialog_wrapper:last').remove();
  $('#home_glower').remove();   // In case it's there
  dialog_haze_close();
  return false;
}

function dialog_haze() {
  if(! gl.gray_haze_z) gl.gray_haze_z = 190;
  gl.gray_haze_z += 10;
  $('body').append('<div class="dialog_haze" style="height:'+($(document).height()-4)+'px; z-index:'+gl.gray_haze_z+';"></div>');
}

function dialog_haze_close() {
  $('.dialog_haze:last').remove();
  gl.gray_haze_z -= 10;
}

function close_dialog() {
  close_dialog_default();
  return false;
}

function p(s) {
  if(s == null)
    s = "[blank]";

  // Start at 0 if first
  try {prepend_index++;}
  catch(e) { prepend_index = 0; }

  $('body').append('<div style="top:'+(prepend_index*13)+'px; margin-left:5px; position:absolute; font-size:10px; z-index:1002; color:#000; filter: alpha(opacity=85); opacity: .85; background-color:#999;">'+s+'</div>');
}

