

var ENVIRONMENT_MAIN_WINDOW_NAME = "BIERLIEB_Mainwindow";
//var ENVIRONMENT_ALLOWED_HOST     = "www.bierliebhaber.at";
var ENVIRONMENT_ALLOWED_HOST     = window.location.host;
var ENVIRONMENT_DEFAULT_URL      = "http://" + ENVIRONMENT_ALLOWED_HOST + "/mod_home/home.php";
var ENVIRONMENT_INDEX_URL        = "http://" + ENVIRONMENT_ALLOWED_HOST + "/index.html";

function Environment()
{
  this.url_              = window.location.href;
  this.main_window_name_ = ENVIRONMENT_MAIN_WINDOW_NAME;
  this.index_url_        = ENVIRONMENT_INDEX_URL;
  this.default_url_      = ENVIRONMENT_DEFAULT_URL;
  this.allowed_host_     = ENVIRONMENT_ALLOWED_HOST;

  this.key_track_        = '';
  this.key_actions_      = new Array();

  this.is_IE  = ((document.all) ? (true):(false));
  this.is_NS4 = ((document.layers) ? (true):(false));
  this.is_NS6 = (((document.getElementById) && (!this.is_IE)) ? (true):(false));


  this.initialize             = Environment_initialize;
  this.finalize               = Environment_finalize;
  this.setWindowName          = Environment_setWindowName;
  this.isFrameSet             = Environment_isFrameSet;
  this.checkFrameSet          = Environment_checkFrameSet;
  this.loadFrameSet           = Environment_loadFrameSet;
  this.loadUrlIntoContent     = Environment_loadUrlIntoContent;
  this.allowedUrl             = Environment_allowedUrl;
  this.openWindow             = Environment_openWindow;
  this.reloadOpener           = Environment_reloadOpener;
  this.closeWindow            = Environment_closeWindow;
  this.ask                    = Environment_ask;
  //this.onKeyDown              = Environment_onKeyDown;
  this.startKeyActionListener = Environment_startKeyActionListener;
  this.addKeyAction           = Environment_addKeyAction;
  this.handleKeyActions       = Environment_handleKeyActions;
}


function Environment_initialize()
{
  window.focus();
}

function Environment_finalize()
{

}

function Environment_setWindowName(name)
{
  var windowname = this.main_window_name_;
  if (typeof(name)== 'String')
  {
    windowname = name;
  }
  window.name = windowname;
}

function Environment_isFrameSet()
{
  if ((top) &&
      (top.content) &&
      (top.logo) &&
      (top.name == this.main_window_name_))
  {
    return true;
  }
  return false;
}

function Environment_checkFrameSet()
{
  if ( ! this.isFrameSet())
  {
    this.loadFrameSet();
  }
}

function Environment_loadFrameSet()
{
  top.location.href = this.index_url_ + "?LoadUrl=" + this.url_;
}

function Environment_loadUrlIntoContent()
{
  if (this.url_.indexOf("?LoadUrl=") != -1)
  {
    top.content.location.href = this.allowedUrl(this.url_.substring(this.url_.indexOf("?LoadUrl=")+9, this.url_.length));
  }
}

function Environment_allowedUrl(url)
{
  if (url.indexOf("http://" + this.allowed_host_ + "/") == 0)
  {
    return url;
  }
  else
  {
    return this.default_url_;
  }
}

function Environment_openWindow(parameter)
{
  var name = parameter.name || 'dialog';
  var url  = parameter.url  || '';
  var width = parameter.width || '500';
  var height = parameter.height || '500';
  var scrollbars = parameter.scrollbars || 'yes';
  var menu = parameter.menu || 'no';
  var win = window.open(url,name,'width='+width+',height='+height+',menu='+menu+',scrollbars='+scrollbars);
}

function Environment_reloadOpener()
{
  if (window.opener)
  {
    if (window.opener.location)
    {
      window.opener.location.reload();
    }
  }
}

function Environment_closeWindow()
{
  window.top.close();
}

function Environment_ask(question)
{
  var q = question || 'Wollen Sie das wirklich?';
  return confirm(q);
}


function Environment_onKeyDown(e)
{
  var this_obj = null;
  if (typeof(ENVIRONMENT)!='undefined')
  {
    this_obj = ENVIRONMENT;
  }
  else
  {
    ENVIRONMENT = new Environment();
    this_obj = ENVIRONMENT;
  }
  var realkey = '';
  if ((this_obj.is_NS4) || (this_obj.is_NS6))
  {
    realkey = String.fromCharCode(e.which)
  }
  if (this_obj.is_IE)
  {
    realkey = String.fromCharCode(event.keyCode)
  }
  this_obj.handleKeyActions(realkey.toLowerCase());
}


function Environment_startKeyActionListener()
{
  document.onkeydown = Environment_onKeyDown;
  if ((this.is_NS4) || (this.is_NS6))
  {
    document.captureEvents(Event.KEYDOWN);
  }
}

function Environment_addKeyAction(parameter)
{
  this.key_actions_[this.key_actions_.length] = parameter;
}

function Environment_handleKeyActions(key)
{
  this.key_track_ += key;
  if (this.key_track_.length > 50)
  {
    this.key_track_ = this.key_track_.substring(1,this.key_track_.length);
  }
  //window.status = this.key_track_;
  for (var count=0; count<this.key_actions_.length; count++)
  {
    var obj = this.key_actions_[count];
    var k = obj.key || '';
    if (k != '')
    {
      if (this.key_track_.indexOf(k)>=0)
      {
        this.key_track_ = '';
        // perform action
        if (typeof(obj.openwindow) != 'undefined')
        {
          this.openWindow(obj.openwindow);
        }
        else
        {
          if (typeof(obj.action) != 'undefined')
          {
            eval(obj.action);
          }
        }
      }
    }
  }
}

var ENVIRONMENT = new Environment();