//********************************************************************************
// $HeadURL: https://svn.comnera.com/svn/CMS/main/Comnera.Web.UI/Resources/InternalFramePanel.js $
//
// $Rev: 982 $
// $Date: 2007-09-09 18:06:33 +0200 (sö, 09 sep 2007) $
// $Author: micke $
//

//********************************************************************************
// Used as a universal close function variable
var doClose;

//********************************************************************************

doClose=function(){
  log('doClose');
  iPanel.closeIFrame('imagelist_frame');
}

function show_video(_name, _height)
{
  iPanel.openIFrame('imagelist_frame', 'InternalFramePanel', '/video/show?name=' + _name, 542, 340);
}

function show_video2(_name, _height)
{
  iPanel.openIFrame('imagelist_frame', 'InternalFramePanel', '/video/show_flv?name=' + _name + ".flv", 542, 340);
}

function show_video_flv(_name, _width, _height)
{
  iPanel.openIFrame('imagelist_frame', 'InternalFramePanel', '/video/show_flv?name=' + _name + ".flv&width=" + _width + "&height=" + _height, _width, _height);
}

function show_youtube_video(_name, _height)
{
  iPanel.openIFrame('imagelist_frame', 'InternalFramePanel', '/video/show?name=' + _name, 562, 480);
}

function show_large_youtube_video(_name, _height)
{
  iPanel.openIFrame('imagelist_frame', 'InternalFramePanel', '/video/show?name=' + _name, 742, 580);
}


function InternalFramePanel()
{
}

//********************************************************************************
InternalFramePanel.prototype.reloadWithUrl = function()
{
  if (this.altUrl)
  {
    window.location = this.altUrl;
  }
  else
  {
    var href = window.location;
    window.location = href;
  }
}

//********************************************************************************
InternalFramePanel.prototype.alternateClose = function(url)
{
  if (url)
  {
    this.altUrl = url;
  }
  doClose = this.reloadWithUrl;
}

//********************************************************************************
InternalFramePanel.prototype.openIFrame = function(id, cssClass, url, width, height)
{
  log('openIFrame');

  this.createMask();
    
  var div = document.createElement("div");
  div.innerHTML = "<iframe name=\"" + id + "_iframe\"></iframe>";
  var iframe = div.firstChild;
    
  // Create wrapper div
  div.setAttribute("id", id + "_div");

  iframe.setAttribute("id", id + "_iframe");
  iframe.setAttribute("scrolling", "no");
  iframe.setAttribute("frameBorder", "0");
  iframe.setAttribute("allowTransparency", "true");
  iframe.className = cssClass;
  iframe.setAttribute("src", url);
    
  // Check if the screen resolution is low
  if (screen.height <= 600)
  {
    div.style.position = "absolute";
    div.style.top = "10px";
    div.style.left = "10px";
    div.style.margin = "0";
    div.style.zIndex = "20";
    iframe.style.position = "absolute";
    iframe.style.top = "10px";
    iframe.style.left = "10px";
    iframe.style.margin = "0";
  }
    
  // Check if width and height should be set
  if (width && height)
  {
    div.setAttribute("width", width);
    div.setAttribute("height", height);
    iframe.style.width = width + "px";
    iframe.style.height = height + "px";
    iframe.style.marginLeft = -(width / 2) + "px";
  }
    
  div.appendChild(iframe);
  document.body.appendChild(div);

  log('openIFrame done');
  return iframe;
}

//********************************************************************************
InternalFramePanel.prototype.closeIFrame = function(id)
{
  log('closeIFrame');

  var div = document.getElementById(id + "_div");
  var child;
  while ((child = div.firstChild) != null)
  {
    div.removeChild(child);
  }
  document.body.removeChild(div);
  this.removeMask();
}

//********************************************************************************
InternalFramePanel.prototype.createMask = function() 
{
  log('createMask');

  var id = "EditMask";
  var exists = eval(document.getElementById(id));
  if (!exists)
  {
    maskIFrame = document.createElement("iframe");
    maskIFrame.id = id;
    maskIFrame.className = "EditMask";
    maskIFrame.scrolling = "no";
    maskIFrame.setAttribute("frameBorder", "0");
            
    // Append the IFrame
    body = document.getElementsByTagName("body")[0];
    body.appendChild(maskIFrame);
        
    // get the browser-appropriate document context for content creation
    var doc = maskIFrame.contentDocument ? maskIFrame.contentDocument :
    maskIFrame.Document;
    doc.write('<html><head></head>' +
      '<body style="background-color: black;"></body></html>');
    doc.close();
  }

  log('createMask done');
}

//********************************************************************************
InternalFramePanel.prototype.removeMask = function()
{
  log('removeMask');

  var id = "EditMask";
  var exists = eval(document.getElementById(id));
  if (exists)
  {
    document.body.removeChild(document.getElementById(id));
  }
}

//********************************************************************************
InternalFramePanel.prototype.adjustFrameSize = function(win)
{
  log('adjustFrameSize');

  var elm = document.getElementById(win.name);
    
  if (win.document.compatMode && win.document.compatMode != "BackCompat") 
  {
    elm.style.height = win.document.documentElement.scrollHeight + "px";
    elm.style.width = win.document.documentElement.scrollWidth + "px";
  }
  else
  {
    elm.style.height = win.document.body.scrollHeight + "px";
    elm.style.width = win.document.body.scrollWidth + "px";
  }
  //alert("New size, width: " + elm.style.width + " , height: " + elm.style.height);
  log('adjustFrameSize done');
}

//********************************************************************************
var iPanel = new InternalFramePanel();
