﻿function GetDarkOverlay(boolParent)
{
    var ctlDark=document.getElementById('divJSDarkOverlay');
    
    if (((!ctlDark) || (boolParent)) && (window.parent))
    {
        ctlDark=window.parent.document.getElementById('divJSDarkOverlay');
    }
    
    if (((!ctlDark) || (boolParent)) && (window.opener))
    {
        ctlDark=window.opener.document.getElementById('divJSDarkOverlay');
    }
    
    return ctlDark;
}

function ShowDarkOverlay()
{
    var divBlack = GetDarkOverlay();
    var ctlParent = document.getElementById('hdvContent');

    if (!ctlParent) ctlParent=document.body;
    if (!divBlack)  divBlack = document.createElement('div');

    divBlack.style.position = 'fixed';
    divBlack.style.zIndex = '5';
    divBlack.style.display = 'none';
    divBlack.style.cursor = 'wait';
    divBlack.style.backgroundColor = 'black';
    divBlack.id = 'divJSDarkOverlay';
    
    divBlack.onclick=function() {};
    
    ctlParent.appendChild(divBlack);
   
    setTimeout('document.getElementById(\'divJSDarkOverlay\').style.display = \'\'', 10);
    setTimeout('AdaptDarkOverlaySize();', 100);
    setTimeout('AdaptDarkOverlaySize();', 1000);
    
    SetAlpha(divBlack.id, 80, 80);
}

function SetAlpha(strControlID, intAlpha, intTarget)
{
    var ctlControl=document.getElementById(strControlID);
        
    if (ctlControl)
    {
        
        ctlControl.style.opacity = intAlpha / 100;
        ctlControl.style.filter = 'alpha(opacity=' + intAlpha + ')';

        intAlpha += (intTarget - intAlpha) / 3;
        if (Math.abs(intTarget - intAlpha) < 10) intAlpha=intTarget;
                
        if (intAlpha != intTarget) setTimeout('SetAlpha(\'' + strControlID + '\', ' + intAlpha + ', ' + intTarget + ');', 50);
    } 
}

function AdaptDarkOverlaySize()
{
    var divBlack = GetDarkOverlay();
    
    if (divBlack)
    {
        var intWidth=0;
        var intHeight=0;
            
        intWidth=Math.max(document.body.scrollWidth, document.body.offsetWidth);
        intHeight=Math.max(document.body.scrollHeight, document.body.offsetHeight);
        
        if (window.innerWidth) intWidth=Math.max(intWidth, window.innerWidth);
        if (window.innerHeight) intHeight=Math.max(intHeight, window.innerHeight);
      
        if (document.documentElement.clientWidth) intWidth=Math.max(intWidth, document.documentElement.clientWidth);
        if (document.documentElement.clientHeight) intHeight=Math.max(intHeight, document.documentElement.clientHeight);
          
        divBlack.style.top = '0px';
        divBlack.style.left = '0px';
        divBlack.style.width = intWidth + 'px'; 
        divBlack.style.height = intHeight + 'px'; 

        /*
        if (divBlack.style.display == '')
        {
            setTimeout('AdaptDarkOverlaySize();', 100);
        }
        */
    }
}

function HideDarkOverlay()
{
    var divBlack = GetDarkOverlay();

    if (divBlack) divBlack.style.display = 'none';
}

function autoSetWidth(strControlID)
{
	var ctlControl=document.getElementById(strControlID)
	if (ctlControl)
	{
		if (ctlControl.parentNode)
		{
			if (ctlControl.offsetWidth > 0)
			{
				if (ctlControl.style.width=='100%') ctlControl.style.width='';
				
				var intWidth=ctlControl.offsetWidth;
				var intParentWidth=ctlControl.parentNode.offsetWidth;

				if (intWidth < intParentWidth)
				{
					ctlControl.style.width='100%';
					
					if (ctlControl.parentNode.offsetWidth < intParentWidth)
					{
						ctlControl.style.width='';
					}
				}
			}
			else
			{
				setTimeout('autoSetWidth(\'' + strControlID + '\');', 50);
			}
		}
	} 
}

function CreateImageViewer(strImages, strThumbnails, intShow)
{
    if ((strImages.length) && (strImages.length > 0))
    {
        var ctlElevator=document.createElement("div");
        var ctlBox=document.createElement("div");
        var ctlSmallBox=document.createElement("div");
        var ctlLarge=document.createElement("img");
        var ctlClose=document.createElement("img");
        var ctlCloseBR=document.createElement("br");
        
        if (!intShow) intShow=0;
        
        ctlElevator.className="elevator";
        ctlElevator.id="divIV_Elevator";
        //ctlElevator.style.top=(GetScrollTop() + 50) + 'px';
        ctlBox.className="imageviewer";
        ctlBox.id="divIV_Box";
        ctlClose.onclick=new Function("document.body.removeChild(this.parentNode.parentNode); HideDarkOverlay();");
        ctlClose.src="../images/close.gif";
        ctlClose.className="close";
        ctlSmallBox.className="smallbox";
        ctlSmallBox.id="divIV_SmallBox";
        ctlLarge.className="large";
        ctlLarge.id="imgIV_Large";
        ctlLarge.src=strImages[intShow];
       
        ctlBox.appendChild(ctlClose);
        ctlBox.appendChild(ctlCloseBR);
        ctlBox.appendChild(ctlLarge);
        ctlBox.appendChild(ctlSmallBox);
        ctlElevator.appendChild(ctlBox);
        document.body.appendChild(ctlElevator);
    
        for (intIndex=0; intIndex < strImages.length; intIndex ++)
        {
            var ctlSmall=document.createElement("img");
            var strImage=strImages[intIndex];
            
            ctlSmall.className="small";
            ctlSmall.src=strThumbnails[intIndex];
            ctlSmall.onclick=new Function("ImageViewer_SetLarge('" + strImage + "');");
            ctlSmallBox.appendChild(ctlSmall);
        }
        
        ShowDarkOverlay();
    }
}

function ImageViewer_SetLarge(strImage)
{
    var ctlLarge=document.getElementById('imgIV_Large');
    
    if (ctlLarge) ctlLarge.src=strImage;
}

function GetScrollTop()
{
    var intReturn;
    
    if (typeof window.pageYOffset != 'undefined')
    {
       intReturn = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
    {
       intReturn = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined')
    {
       intReturn = document.body.scrollTop;
    }
    
    return intReturn;
}

function MoveBelow(ctlMove, ctlTarget, intAlign, boolDontShow, boolAlignToTop, boolAbove)
{
	if ((ctlMove) && (ctlTarget))
	{
		var intLeft=getLeft(ctlTarget);
		var intTop=0;
		
		if (!intAlign) intAlign=2;
		if (!boolDontShow) ctlMove.style.display='';
		
		switch (intAlign)
		{
			case 1:
				intLeft=intLeft - (ctlMove.offsetWidth - ctlTarget.offsetWidth);
				break;

			case 2:
				intLeft=intLeft - ((ctlMove.offsetWidth - ctlTarget.offsetWidth) / 2);
				break;

			case 3:
				intLeft=intLeft;
				break;
		}
		
		if (intLeft < 0) intLeft=0;
		
	
		intTop=getTop(ctlTarget);
		if (!boolAlignToTop) intTop+=ctlTarget.offsetHeight;
		if (boolAbove) intTop-=ctlMove.offsetHeight;
		
		ctlMove.style.position='absolute';
		ctlMove.style.top=intTop+'px';
		ctlMove.style.left=intLeft+'px';
	}
}

function getTop(ctlControl)
{
	var intTop = 0;
	
	while(ctlControl)
	{
		intTop += ctlControl.offsetTop;

		ctlControl = ctlControl.offsetParent;
	}
	
	return intTop;
}

function getLeft(ctlControl)
{
	var intLeft = 0;
	
	while(ctlControl)
	{
		intLeft += ctlControl.offsetLeft;
		
		ctlControl = ctlControl.offsetParent;
	}
	
	return intLeft;
}


function ExecuteScriptsInText(strText)
{     
    var rgxRegex = new RegExp(/[^\]<script[^>]*?(?:type|language)="?(?:text\/)?javascript"?[^>]*?>((?:\S|\s)+?)<\/script>/igm);
    var match = null;
    
    while (match = rgxRegex.exec(strText))
    {
        var strScript = match[1];

        if (strScript)
        {
            var fncTmp = new Function(strScript);
            fncTmp();
        }
    }
}

function GeoCheck()
{
    return !!navigator.geolocation;
}
