﻿
var InformationPopup = {};

InformationPopup.ActiveInformationRootId = undefined;

InformationPopup.ShowPopup = function(rootId) {

    var iFrameId = rootId + "_iFrame";
    var wrapper = document.getElementById(rootId);
    var advancedTarget = wrapper.getAttribute('AdvancedTarget');
    var frameTitle = wrapper.getAttribute('FrameTitle');
    var frameWidth = wrapper.getAttribute('FrameWidth');
    var frameHeight = wrapper.getAttribute('FrameHeight');

    var isIE6 = false;
    var ver = getInternetExplorerVersion();
    //alert(document.documentElement.scrollTop);
    //alert(window.pageYOffset);
    //alert(getInternetExplorerVersion());
    if (ver == 6.0) {
        isIE6 = true;
        //alert("Set 6");
    }

    // scroll to the top for IE
    //window.scrollTo(0, 0);
    var ScrollTop = document.documentElement.scrollTop;
    if (ScrollTop == 0) {
        if (window.pageYOffset)
            ScrollTop = window.pageYOffset;
        else
            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }

    // need these extra styles to put the starting box full screen.
    // This also set the background box to be the full page, including the content that scrolls off screen.
    if (isIE6) {
        wrapper.style.cssText = "position: absolute; top: 0px; left: 0px; height: " + document.body.clientHeight + "px; width: " + document.body.clientWidth + ";";
    }
    else {
        wrapper.style.cssText = "position: absolute; top: 0px; left: 0px; height: " + document.body.clientHeight + "px; width: 100%;";
    }

    // create Model box elements 
    var backgroundDiv = document.createElement("div");
    backgroundDiv.className = "InformationModelBoxBackground";
    backgroundDiv.setAttribute("onclick", "InformationPopup.CancelPopup();");
    if (isIE6) {
        backgroundDiv.style.cssText = "height: " + document.body.clientHeight + "px; width: " + document.body.clientWidth + ";";
    }
    wrapper.appendChild(backgroundDiv);

    var floatFix = document.createElement("div");
    floatFix.className = "floatFIX";

    var modelBox = document.createElement("div");
    modelBox.className = "InformationModelBoxPopup";
    modelBox.onclick = function() { InformationPopup.CancelPopup(); };
    if (isIE6) {
        modelBox.style.cssText = "top: " + ScrollTop + "px; height: " + document.body.clientHeight + "px; width: " + document.body.clientWidth + ";";
    }
    else {
        modelBox.style.cssText = "top: " + ScrollTop + "px; ";
    }

    var titleBar = document.createElement("div");
    titleBar.className = "InformationModelTitleBar";
    titleBar.style.cssText = "width: " + frameWidth + "px;";
    titleBar.setAttribute("onclick", "event.cancelBubble = true; return false;");
    titleBar.onclick = function() { event.cancelBubble = true; return false; };
    titleBar.innerHTML = "<div style='float: left;'>" + frameTitle + "</div><div style='float: right;'><a href='#' onclick='InformationPopup.CancelPopup(); return false;'>X</a></div>";
    modelBox.appendChild(titleBar);

    var frame = document.createElement("iframe");
    frame.setAttribute("id", iFrameId);
    frame.setAttribute("src", advancedTarget);
    frame.setAttribute("frameBorder", "0");
    frame.setAttribute("scrolling", "auto");
    frame.setAttribute("onclick", "event.cancelBubble = true; return false;");
    frame.onclick = function() { event.cancelBubble = true; return false; };
    frame.className = "InformationModelIFrame";
    frame.style.cssText = "width: " + frameWidth + "px; height: " + frameHeight + "px;";

    modelBox.appendChild(frame);

    floatFix.appendChild(modelBox);
    wrapper.appendChild(floatFix);

    InformationPopup.ActiveInformationRootId = rootId;
}

InformationPopup.CancelPopup = function() {

    var rootId = InformationPopup.ActiveInformationRootId;
    if (rootId == undefined) return;

    InformationPopup.ActiveInformationRootId = undefined;

    // RESET: for IE we dont want the user to scroll the page in the background
    //bod = document.getElementsByTagName('body')[0];
    //bod.style.height = "auto";
    //bod.style.overflow = "auto";
    //htm = document.getElementsByTagName('html')[0];
    //htm.style.height = "auto";
    //htm.style.overflow = "auto";

    // remove all model box elements
    var wrapper = document.getElementById(rootId);
    wrapper.style.cssText = "position: absolute;";
    var elms = wrapper.getElementsByTagName("div");
    var maxI = elms.length;
    for (var i = maxI - 1; i >= 0; --i) {
        var elm = elms[i];
        elm.parentNode.removeChild(elm);
    }

}

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
// From: http://msdn.microsoft.com/en-us/library/ms537509%28VS.85%29.aspx
function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
