EventUtil.addEventHandler(window, 'resize', OnReSize);
EventUtil.addEventHandler(window, 'load', GetHashPage);
EventUtil.addEventHandler(window, 'load', OnReSize);
//EventUtil.addEventHandler(BoxLoading, 'mousedown', handleMouseDown);
//onmousedown="handleMouseDown(event)"

var xHRObject = false;

if (window.XMLHttpRequest)
{
	xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
	xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function GetHashPage()
{
	GetPage(QueryString.getHash());
}

function GetCompletePage()
{
    if (xHRObject.readyState == 4)
    {
		if(xHRObject.status == 200 || xHRObject.status == 304)
		{
			MainPage.innerHTML = xHRObject.responseText;
			UnShowWait();
			window.status = xHRObject.statusText; 
		}
		else if(xHRObject.status != 0)
		{
			MainPage.innerHTML = "<p dir=ltr style='color:red;font: 15'>Error: " + xHRObject.statusText + " (" + xHRObject.status + ")"; 
//			MainPage.innerHTML = xHRObject.responseText;
			UnShowWait();
			window.status = "Error: " + xHRObject.statusText + " (" + xHRObject.status + ")"; 
		}
	}
}

function GetPage( Page, bPage )
{
	if (Page == null)
		return null;

	if (Page.substring(0,1) == '#')
		Page = Page.substring(1, Page.length); ;

	Lang = QueryString.get("Lang", "FA");
	if (bPage)
		xHRObject.open("GET", Page + ((Page.indexOf("?")!=-1) ? "&" : "?") + "Lang=" + Lang, true);
	else
		xHRObject.open("GET", "Page.aspx?Src=" + Page + "&Lang=" + Lang, true);
	xHRObject.onreadystatechange = GetCompletePage ;
	xHRObject.send(null);
	ShowWait();
}

function GetPageJS( Page, JS, bPage)
{
	GetPage( Page, bPage );
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = JS ;
	head.appendChild(script);
}

function ShowWait()
{
	DisablePage.style.visibility = 'visible';
	BoxLoading.style.visibility = 'visible';
	OnReSize();
	hTimer = window.setTimeout("UnShowWait();", 60000);
}

function UnShowWait()
{
	DisablePage.style.visibility = 'hidden';
	BoxLoading.style.visibility = 'hidden';
	DisablePage.style.height = 0;
}

function OnReSize()
{
	if (BoxLoading.style.visibility != 'hidden')
	{
		BoxLoading.style.right = document.body.clientWidth  / 2 - BoxLoading.clientWidth  / 2;
		BoxLoading.style.left  = document.body.clientWidth  / 2 - BoxLoading.clientWidth  / 2;		
		BoxLoading.style.top   = document.body.clientHeight / 2 - BoxLoading.clientHeight / 2;	

		if ( document.body.scrollHeight > 2000 && (document.body.scrollHeight - document.body.offsetHeight) > 40 )
			DisablePage.style.height = 2000;
		else
			DisablePage.style.height = document.body.scrollHeight;		
	}

	var loader = document.getElementById('loader');
	if (loader != null && loader.childNodes.length >= 11)
	{	
		loader.childNodes[11].width  = document.body.clientWidth - 2*206;
		loader.childNodes[11].height = loader.parentNode.parentNode.clientHeight ;
	}
}

var iDiffX = 0;
var iDiffY = 0;
        
function handleMouseMove()
{
    var oEvent = EventUtil.getEvent();    
  
	if(	document.dir == 'rtl' )
		BoxLoading.style.right =  document.body.offsetWidth - oEvent.clientX - iDiffX;
    else
		BoxLoading.style.left  = oEvent.clientX - iDiffX;
		
    BoxLoading.style.top       = oEvent.clientY - iDiffY;  
}
            
function handleMouseDown()
{
    var oEvent = EventUtil.getEvent();
    iDiffX     = oEvent.clientX - BoxLoading.offsetLeft;
    iDiffY     = oEvent.clientY - BoxLoading.offsetTop;

    EventUtil.addEventHandler(document.body, "mousemove", handleMouseMove);
    EventUtil.addEventHandler(document.body, "mouseup", handleMouseUp);
}

function handleMouseUp()
{
    EventUtil.removeEventHandler(document.body, "mousemove", handleMouseMove);
    EventUtil.removeEventHandler(document.body, "mouseup", handleMouseUp);
}

