﻿function OpenPopup (ContentUrl, TitlePictureUrl)
{
    CloseAllPopups ();

    document.getElementById("Popup").style.display = "block";
    document.getElementById("PopupTitle").src = TitlePictureUrl;
    
    LoadPageIntoDiv(document.getElementById("PopupContent"), ContentUrl);
    
    if ( document.getElementById("RightContentFlash") )
        document.getElementById("RightContentFlash").style.visibility = "hidden";
        
    var selectBoxes = document.getElementsByTagName("select");
    for ( i = 0; i < selectBoxes.length; ++i )
        selectBoxes[i].style.visibility = "hidden";

}

function ClosePopup ()
{
    CloseAllPopups();
}

function LoadPageIntoDiv ( Obj, Url )
{

    var http = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    
    http.onreadystatechange = function ()
    {
        if (http.readyState == 4 && http.status == 200)
        {
            Obj.innerHTML = http.responseText;
            PopupScroller.init();
        }
    }
    
    http.open("GET", Url, true);
    http.send(null);
}

var mouseOnPopup = false;

function CloseAllPopups ()
{
    popups = document.getElementsByTagName("div");
    for ( i = 0; i < popups.length; ++i )
        if ( popups[i].id == "Popup" || popups[i].id.substring(0,2) == "p0" )
        {
            popups[i].style.display = "none";
            if ( !popups[i].onmouseover )
            {
                popups[i].onmouseover = function () { mouseOnPopup = true; };
                popups[i].onmouseout = function () { mouseOnPopup = false; };
            }
        }

    if ( document.getElementById("RightContentFlash") )
        document.getElementById("RightContentFlash").style.visibility = "visible";

    var selectBoxes = document.getElementsByTagName("select");
    for ( i = 0; i < selectBoxes.length; ++i )
        selectBoxes[i].style.visibility = "visible";
}

function GetDocumentHeight()
{
	docH = 780;
	if (self.innerHeight)
		docH = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		docH = document.documentElement.clientHeight;
	else if (document.body)
		docH = document.body.clientHeight;
	return docH;
}

function MatchSize ()
{
	var docH = GetDocumentHeight();
	var topH = 121;
	document.getElementById("PageHolder").style.height = ((((docH>600)?docH:630)) + "px");
}

function DoOnResize(args)
{
    MatchSize();
}

function DoOnLoad(args)
{
    MatchSize();
}

onload = DoOnLoad;
window.onresize = DoOnResize;
document.onclick = DoOnClick;

function DoOnClick(args)
{
    if ( !mouseOnPopup )    
        CloseAllPopups();
}

var PopupScroller = 
{
  init:   function() 
  {
    PopupScroller.docH = document.getElementById("PopupContent").offsetHeight;
    PopupScroller.contH = document.getElementById("PopupContentContainer").offsetHeight;
    PopupScroller.scrollAreaH = document.getElementById("PopupScrollContainer").offsetHeight;
    
    if ( PopupScroller.docH > PopupScroller.contH )
	{
		document.getElementById("PopupScrollContainer").style.visibility = "visible";	
	    PopupScroller.scrollH = (PopupScroller.contH * PopupScroller.scrollAreaH) / PopupScroller.docH;
	    document.getElementById("PopupScrollBar").style.height = Math.round(PopupScroller.scrollH) + "px";
	    
	    PopupScroller.scrollDist = Math.round(PopupScroller.scrollAreaH-PopupScroller.scrollH);
	    
	    Drag.init(document.getElementById("PopupScrollBar"),null,0,0,-1,PopupScroller.scrollDist);
	    
	    document.getElementById("PopupScrollBar").onDrag = function (x,y) 
	    {
	      var scrollY = parseInt(document.getElementById("PopupScrollBar").style.top);
	      var docY = 0 - (scrollY * (PopupScroller.docH - PopupScroller.contH) / PopupScroller.scrollDist);
	      docY = Math.round(docY);
	      document.getElementById("PopupContent").style.top = docY + "px";
		}
 	}
 	else
 	{
 		document.getElementById("PopupScrollContainer").style.visibility = "hidden";	
 	}
  }
}
