/*--------------------------------------------------------------------------------\
|                                  IFRAME Support                                 |
|---------------------------------------------------------------------------------|
| Author:          Dan Sutton                                                     |
| Date:            August 19th, 2010                                              |
| Version:         3.0.2                                                          |
| Contact:         dan@opwernby.com                                               |
\--------------------------------------------------------------------------------*/

var iframeSupport =
{
  items: [],

  // Browser Determination
  browser: navigator.userAgent.toLowerCase(),
  firefox: (navigator.userAgent.toLowerCase().indexOf("firefox") >= 0),
  ie: (navigator.userAgent.toLowerCase().indexOf("msie") >= 0),
  opera: (navigator.userAgent.toLowerCase().indexOf("opera") >= 0),
  netscape: (navigator.userAgent.toLowerCase().indexOf("netscape") >= 0),
  safari: (navigator.userAgent.toLowerCase().indexOf("safari") >= 0),

  // Add an IFRAME, with optional startup URL
  add: function (name, url)
  {
    var x = new iframeClass(name, (url ? url : ""));
    document.write(x.toString());
    this.items[name] = x;
    return x;
  },

  // Get the given IFRAME's class
  get: function (name)
  {
    return this.items[name];
  },

  // Execute a page inside the given IFRAME
  execute: function (name, url)
  {
    var r = this.get(name);
    if (r)
    {
      url += (url.indexOf("?") >= 0 ? "&" : "?") + "if_rnd=" + Math.random();
      r.execute(url);
    }
  },

  // Get the actual IFRAME with the given name
  getFrame: function (name)
  {
    var r = null;
    var f = this.get(name);
    if (f)
      r = f.getFrame();
    return r;
  },

  // Get the document.frames[name] object containing the actual IFRAME
  getFramesObject: function (name)
  {
    var r = null;
    var f = this.get(name);
    if (f)
      r = f.getFramesObject();
    return r;
  },

  // Find an IFRAME independently of this structure
  findFrame: function (name)
  {
    if (this.firefox || this.opera || this.safari)
    {
      var f = eval("window.frames." + name);
      if (!f)
        f = document.getElementById(name);
      return f;
    }
    else if (this.safari || this.firefox)
      return document.getElementById(name);
    else
      return document.frames[name];
  }
};

// Class for creating and using IFRAMEs
function iframeClass(name, url)
{
  this.name = name;
  this.url = url;
  this.divName = "idiv__" + name;
}
	
// Get HTML to define the IFRAME
iframeClass.prototype.toString = function ()
{
  var HTML = "";
/*
  // For debugging only
  if (this.name.indexOf("sys_execute") >= 0)
    HTML = "<div id='" + this.divName + "'"
      + " style='position:absolute; top:0; left:0;'>"
      + "<iframe id='" + this.name + "' name='" + this.name + "'"
      + (this.url ? " src=\"" + this.url.replace('"', '\"') + "\"" : "")
      + ">"
      + "</iframe></div>";
  else
*/
    // Live code
    HTML = "<div id='" + this.divName + "'"
	   + " style='position:absolute; top:0; left:0; width:10;"
	   + " height:10; overflow:hidden; visibility:hidden'>"
	   + "<iframe id='" + this.name + "' name='" + this.name + "'"
	   + (this.url ? " src=\"" + this.url.replace('"', '\"') + "\"" : "")
	   + " style='visibility:hidden; width:10; height:10;'>"
	   + "</iframe></div>";
  return HTML;
};

// Get the IFRAME element. Firefox has bugs in it which prevent this from working properly,
// so we have to have a special case...
iframeClass.prototype.getFrame = function ()
{
  var f = null;
  if (iframeSupport.firefox || iframeSupport.opera)
    f = document.getElementById(this.name);
  else
  {
    var d = document.getElementById(this.divName);
    if (d)
      for (var i=0; i<d.children.length; i++)
      {
        var x = d.children[i];
        if (x.tagName.toLowerCase() == 'iframe')
        {
          f = x;
          break;
        }
      }
  }
  return f;
};

// Get the object that's actually sitting inside document.frames
// (object.frameObject == this.getFrame). If you want to execute
// functions, for example, which are within the frame, then you'll
// need to use the object to refer to them, e.g.:
//  frameSupport.getObject("test").doSomething();
iframeClass.prototype.getFramesObject = function ()
{
  var f = null;
  if (iframeSupport.firefox || iframeSupport.opera)
    f = eval("window.frames." + this.name);
  else
    try
    {
      f = this.getFrame().document.frames[this.name];
    }
    catch (e)
    {
      f = document.getElementById(this.name);
    }
  return f;
};

// Execute a page inside the IFRAME
iframeClass.prototype.execute = function (url)
{
  var r = false;
  var f = this.getFrame();
  if (f != null)
  {
    url += (url.indexOf("?") >= 0 ? "&" : "?") + "exec__rnd=" + Math.random();
    f.src = url;
    r = true;
  }
  return r;
};

// This executes a URL inside an invisible IFrame. It appends a random number
// as a parameter to the URL in order to defeat the page caching that browsers
// such as Safari use, and which prevents such pages from being refreshed twice
// in a row.
iframeSupport.add("sys_execute");

function execute(url)
{
  iframeSupport.execute("sys_execute", url);
}

function download(filename)
{
  execute("PushFile.aspx?fn=" + filename);
}
