//-----------------------------------------------------------------------------
// WebPart class definition
//-----------------------------------------------------------------------------
function WebPart(page)
{
  //---------------------------------------------------------------------------
  // Properties
  //---------------------------------------------------------------------------
  this.page = page;
  this.elementId = null;
  //---------------------------------------------------------------------------
  // Methods
  //---------------------------------------------------------------------------
  this.getElement = getElement;
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  // Returns WebPart main HTML element.
  function getElement()
  {
    if (this.elementId != null)
    {
      return getElementById(this.elementId);
    }
    return null;
  }
  //---------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Page class definition
//-----------------------------------------------------------------------------
function Page(portal)
{
  //---------------------------------------------------------------------------
  // Properties
  //---------------------------------------------------------------------------
  this.portal = portal;
  this.elementId = null;
  this.webParts = new Array();
  //---------------------------------------------------------------------------
  // Methods
  //---------------------------------------------------------------------------
  this.getElement = getElement;
  this.createWebPart = createWebPart;
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  // Returns portal page HTML element.
  function getElement()
  {
    if (this.elementId != null)
    {
      return getElementById(this.elementId);
    }
    return null;
  }
  //---------------------------------------------------------------------------
  // Creates new WebPart object and adds it to the collection.
  function createWebPart()
  {
    wp = new WebPart(this);
    this.webParts[this.webParts.length] = wp;
    return wp;
  }
  //---------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Portal class definition
function Portal()
{
  //---------------------------------------------------------------------------
  // Properties
  //---------------------------------------------------------------------------
  this.page = new Page(this);
  //---------------------------------------------------------------------------
  // Methods
  //---------------------------------------------------------------------------
  this.openPopup = openPopup;
  this.openFilePopup = openFilePopup;
  this.popupReturnToMainPage = popupReturnToMainPage;
  this.isPopupOpenByPortal = isPopupOpenByPortal;
  this.windowOnLoad = windowOnLoad;
  this.windowOnResize = windowOnResize;
  this.createWebPart = createWebPart;
  this.showReturnToMainPage = true;
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  function getOpenWindowArgs(mode, width, height, menubar)
  {
    var args = "resizable=yes,scrollbars=yes,status=0";
    switch (mode)
    {
      case 1: 
        var w = width == null ? screen.availWidth * 3/4 : width;
        var h = height == null ? screen.availHeight * 3/4 : height;
        var l = (screen.availWidth - w) / 2;
        var t = (screen.availHeight - h) / 2;
        var mb= menubar == null ? "no" : menubar;
        args += ",top=" + t + ",left=" + l + ",width=" + w + ",height=" + h + ",menubar=" + mb;
        break;
    }
    return args;
  }
  //---------------------------------------------------------------------------
  function prepareUrl(url)
  {
    /*
    if (url != null)
    {
      url = url.replace(/\t/g, "%09");
    }
    */
    return url;
  }
  //---------------------------------------------------------------------------
  function prepareWindowName(windowName)
  {
    if (windowName == null)
    {
      windowName = "_blank";
    }
    return windowName;
  }
  //---------------------------------------------------------------------------
  // Opens portal window in popup mode
  function openPopup(url, windowName, mode, width, height, menubar)
  {
    url = prepareUrl(url);
    windowName = prepareWindowName(windowName);
    var args = getOpenWindowArgs(mode, width, height, menubar);
    var popupWindow = window.open(url, windowName, args);
    if (popupWindow != null)
    {
      try
      {
        popupWindow.IS_OPEN_BY_PORTAL = true;
      }
      catch(err)
      {
        //it could be restricted by browser security
      }
    }
  }
  //---------------------------------------------------------------------------
  // Opens file view window in popup mode
  function openFilePopup(url, windowName, mode)
  {
    url = prepareUrl(url);
    windowName = prepareWindowName(windowName);
    var args = getOpenWindowArgs(mode);
    open(url, windowName, args);
  }
  //---------------------------------------------------------------------------
  // Returns from popup window to main portal page.
  // If popup window is open from the portal, closes popup window.
  // If popup window is open independently, redirects to the main page in the same window.
  function popupReturnToMainPage(mainPageUrl)
  {
    if (isPopupOpenByPortal() == true)
    {
      // Popup window was open by portal, return to the main window
      if (window.opener.focus)
      {
        window.opener.focus();
      }
      if (window.close)
      {
        window.close();
      }
    }
    else
    {
      // Popup window was open by the external link, open main page in the same window
      if (window.navigate)
      {
        window.navigate(mainPageUrl);
      }
      else if (window.location)
      {
        window.location = mainPageUrl;
      }
    }
  }
  //---------------------------------------------------------------------------
  // Returns true if current popup window was open by portal, not by external link
  function isPopupOpenByPortal()
  {
    if (window.opener != null)
    {
      try
      {
        var parentUrl = window.opener.location.href;
        var thisUrl = window.location.href;
        if (parentUrl != null && thisUrl != null)
        {
          parentUrl = removeUrlParams(parentUrl);
          thisUrl = removeUrlParams(thisUrl);
          if (parentUrl.toUpperCase() == thisUrl.toUpperCase())
          {
            return true;
          }
        }
      }
      catch(exc)
      {
        if(typeof(IS_OPEN_BY_PORTAL) != "undefined" && IS_OPEN_BY_PORTAL)
          return true;
        else
          return false;
      }
    }
    return false;
  }
  //---------------------------------------------------------------------------
  // Window OnLoad event handler
  function windowOnLoad()
  {
    this.windowOnResize();
  }
  //---------------------------------------------------------------------------
  // Window OnResize event handler
  function windowOnResize()
  {
  }
  //---------------------------------------------------------------------------
  // Creates new WebPart object and adds it to the collection.
  function createWebPart()
  {
    return this.page.createWebPart();
  }
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  // Initialization block
  //---------------------------------------------------------------------------
  //---------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------
function fixWebPartTableForMozilla(webPartId)
{
  if (document != null && document.getElementById != null)
  {
    var wp = document.getElementById(webPartId);
    if (wp != null && wp.tagName != null && wp.tagName.toLowerCase() == "table")
    {
      if (wp.parentNode != null && 
          wp.parentNode.tagName != null &&
          wp.parentNode.tagName.toLowerCase() == "td" &&
          wp.parentNode.setAttribute != null)
      {
        wp.parentNode.setAttribute("valign", "top");
      }
      if (wp.rows != null && 
          wp.rows.length != null && 
          wp.rows.length > 0 &&
          wp.rows[0].cells != null && 
          wp.rows[0].cells.length != null && 
          wp.rows[0].cells.length > 0 &&
          wp.rows[0].cells[0].setAttribute != null)
      {
        wp.rows[0].cells[0].setAttribute("valign", "top");
      }
    }
  }
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Global Portal instance variable.
var portal = new Portal();
//-----------------------------------------------------------------------------
// Window OnLoad event handler
function portalWindowOnLoad()
{
  portal.windowOnLoad();
  initUtilsOnLoad();
}
//-----------------------------------------------------------------------------
// Window OnResize event handler
function portalWindowOnResize()
{
  portal.windowOnResize();
}
//-----------------------------------------------------------------------------
// Attach window events
if (window.attachEvent)
{
  window.attachEvent("onload", portalWindowOnLoad);
  window.attachEvent("onresize", portalWindowOnResize);
}
//-----------------------------------------------------------------------------
