﻿
var gPopupLoading=null;
var gPopupLoadingMask = null;
var loadingObj = null;
var showHeight=0;
var popimgmask=null;
function addEventLoading(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEventLoading(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}


function getLoadingHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getLoadingWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getLoadingScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}

function getLoadingScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


if (!document.all) {
	document.onkeypress = keyDownHandler;
}


function centerPopWinLoading()
{
	//var theBody = document.documentElement;
	var theBody = document.getElementsByTagName("BODY")[0];
	//theBody.style.overflow = "hidden";
	var scTop = parseInt(getLoadingScrollTop(),10);
	var scLeft = parseInt(theBody.scrollLeft,10);

	setLoadingSize();

	var fullHeight = getLoadingHeight();
	var fullWidth = getLoadingWidth();
	
	if(gPopupLoading)
	{
		//gPopupLoading.style.top = (scTop + ((fullHeight) / 2)) + "px";
		//gPopupLoading.style.left =  (scLeft + ((fullWidth) / 2)) + "px";
		var tempPos=findPos( getObjLoading(loadingObj) );
		gPopupLoading.style.left =  tempPos[0]+"px";
		gPopupLoading.style.top = tempPos[1]+parseInt(showHeight)+"px";		
	}
	if(popimgmask)
	{
		popimgmask.innerHTML=''+
			'<img id="imgloading" name="imgloading" src="images/website/loading.gif">&nbsp;<span class=body_text>Loading '+loadingText+'...</span>';
	}
}
function findPosX(obj)
{
	var curleft = 0;
	if(obj)
	{
		if(obj.offsetParent)
		{
			while(1)
			{
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		}
		else if(obj.x)
		{
		curleft += obj.x;
		}
		
		obj.style.position = "static";
	}
	return curleft;
}

function findPosY(obj) {
  var curtop = 0;
if(obj)
{
  if(obj.offsetParent) {
    while(1) {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  } else if(obj.y) {
    curtop += obj.y;
  }
}
  return curtop;
}

function findPos(obj) {
  var left = findPosX(obj);
  var top = findPosY(obj);

  return [left , top];
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function setLoadingSize()
{
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getLoadingHeight();
	var fullWidth = getLoadingWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	popWidth=1003;
	
	if(gPopupLoadingMask)
	{
		gPopupLoadingMask.style.height = popHeight + "px";
		gPopupLoadingMask.style.width = popWidth + "px";
	}
}

function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

function getObjLoading(objID) 
{
	if (document.getElementById)
	{
		if (document.getElementById(objID)==null)
			return window.parent.document.getElementById(objID);			
		
		return document.getElementById(objID)
	}
	else if (document.all)
	{
		if (document.all(objID)==null)
			return window.parent.document.all[objID];
		
		return document.all[objID];
	}
	else if (document.layers)
	{
		if (document.layers(objID)==null)
			return window.parent.document.layers[objID];
		
		return document.layers[objID];
	}
}

	
	function createLoadingModal()
	{
		theBody = document.getElementsByTagName('BODY')[0];
		poploadingmask = document.createElement('div');
		poploadingmask.id = 'popupLoadingMask';
		theBody.appendChild(poploadingmask);
		
		popimgmask = document.createElement('div');
		popimgmask.id = 'popupLoadingImage';
		popimgmask.innerHTML=''+
		'<img id="imgloading" name="imgloading" src="images/website/loading.gif">&nbsp;<span class=body_text>Loading '+loadingText+'...</span>';
				
		theBody.appendChild(popimgmask);
		
		/*
		 <div id="divPopupModal" runat="server" style="position: absolute; z-index: 10000;
                    visibility: hidden;">
					<img id="imgloading" name="imgloading" src="images/loading.gif">
					</div>
		*/
		
		gPopupLoadingMask = getObjLoading("popupLoadingMask");
		gPopupLoading=getObjLoading("popupLoadingImage");
	}
	var loadingText="";
	
	function ShowLoadingModal(objLoad,sHeight,sText)
    {  
		loadingText="";
		loadingText=sText;
		loadingObj=objLoad;
		showHeight=sHeight;
		
		if(gPopupLoadingMask)
			gPopupLoadingMask.style.display = "block";
		if(gPopupLoading)
        	gPopupLoading.style.display='block';
		//getObjLoading('divPopupModal').style.display='block'; 
		centerPopWinLoading();
		
        return false; 
    }

	function HideLoadingModal()
    {
        if(gPopupLoadingMask)
			gPopupLoadingMask.style.display='none';
		if(gPopupLoading)
			gPopupLoading.style.display='none'; 
        return false;
    } 
	
	addEventLoading(window, "load", createLoadingModal);
	addEventLoading(window, "resize", centerPopWinLoading);
	addEventLoading(window, "scroll", centerPopWinLoading);
	window.onscroll = centerPopWinLoading;
    
    