var disable_mask_created = false;
var disable_mask_visible = false;
var zoom_image_loaded;
var zoom_timer;

/*
i think bd are always in CSS1Compat -  could lose the ? clause.
but i can't find anything which confirms this for sure
*/

/*position we're scrolled to*/
function getScrollY()
{
   if (browser == BROWSER_IE)
   {	/* IE model*/
      var ieBox = document.compatMode != "CSS1Compat";
      var cont = ieBox ? document.body : document.documentElement;
	  return cont.scrollTop;
   }
   else
    {
      return window.pageYOffset;
   }
}

function getScrollX()
{
   if (browser == BROWSER_IE)
   {	/* IE model*/
      var ieBox = document.compatMode != "CSS1Compat";
      var cont = ieBox ? document.body : document.documentElement;
	  return cont.scrollLeft;
   }
   else
    {
      return window.pageXOffset;
   }
}


/*dims of doc (determined by html elements)*/
function getDocumentHeight() 
{
            var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;

            var h;
			if (browser != BROWSER_IE)
				h = Math.max(scrollHeight, window.innerHeight);
			else
				h = Math.max(scrollHeight, document.documentElement.offsetHeight);
            return h;
}
        
function getDocumentWidth() 
{
            var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
			var w;
			if (browser != BROWSER_IE)
				w = Math.max(scrollWidth, window.innerWidth);
			else
				w = Math.max(scrollWidth, document.documentElement.offsetWidth);
            return w;
}

/*dims of actual visible bit (determined by sizing window)*/
function getViewportHeight()
{
	var height = self.innerHeight; // Safari, Opera
    var mode = document.compatMode;
        
    if (mode || browser == BROWSER_IE)
	{ 
		height = (mode == 'CSS1Compat') ? 
		document.documentElement.clientHeight : // Standards
        document.body.clientHeight; // Quirks
    }        
     return height;
}
        
      
function getViewportWidth()
{
     var width = self.innerWidth;  // Safari
     var mode = document.compatMode;
            
     if (mode || browser == BROWSER_IE)// IE, Gecko, Opera
	 {
	       width = (mode == 'CSS1Compat') ?
           document.documentElement.clientWidth : // Standards
           document.body.clientWidth; // Quirks
     }
     return width;
}



function getElementHeight(dom_Elem) 
{
	return dom_Elem.offsetHeight;
}

function getElementWidth(dom_Elem)
{
	return dom_Elem.offsetWidth;
}

function getElementLeft(dom_Elem)
{
	if (!dom_Elem)
		return -1;

	var xPos = dom_Elem.offsetLeft;
	tempEl = dom_Elem.offsetParent;
	while (tempEl != null) 
	{
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}

	return xPos;
}


function getElementTop(dom_Elem) 
{
	if (!dom_Elem)
		return -1;

	yPos = dom_Elem.offsetTop;
	tempEl = dom_Elem.offsetParent;
	while (tempEl != null)
	{
		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
	}
	return yPos;
}


/*
function getBrowserWidth()
{
	if (browser != BROWSER_IE)
		return window.innerWidth;
	else
		return document.body.clientWidth;
}

function getBrowserHeight()
{
	if (browser != BROWSER_IE)
		return window.innerHeight;
	else
		return document.body.clientHeight;
}
*/



function GetDialogX(dlgWidth)
{
	var gotWidth;
	gotWidth = getViewportWidth();
	var xval = getScrollX() + (gotWidth-dlgWidth)/2;
	if (xval < 0 )
		xval = 0;

	return xval;
}

function GetDialogY(dlgHeight)
{
	
	var gotHeight;
	gotHeight = getViewportHeight();
	//var yval = mouseY - (dlgHeight/2);
	scrollY = getScrollY();

	if (dlgHeight > gotHeight-250)
		return scrollY + 20;
	else
		return scrollY + 20 + ((gotHeight-dlgHeight)/2);

	if (mouseY - scrollY - (dlgHeight/2) < 0 )
		yval = scrollY + 10;
	if (mouseY - scrollY + (dlgHeight/2) > gotHeight )
		yval = scrollY + gotHeight - dlgHeight-10;

	if (yval < 0 )
		yval = 0;
	return yval;

}


function _GetDialogY(dlgHeight)
{
	var gotHeight;
	gotHeight = getViewportHeight();
	var yval = mouseY - (dlgHeight/2);
	scrollY = getScrollY();

	if (mouseY - scrollY - (dlgHeight/2) < 0 )
		yval = scrollY + 10;
	if (mouseY - scrollY + (dlgHeight/2) > gotHeight )
		yval = scrollY + gotHeight - dlgHeight-10;

/*leave room for the tooltips at top of screen*/
/*
	if (yval - scrollY < 150)
		yval = 150 + scrollY;
		*/
	return yval;
}


function CreateDisableMask()
{
	var dm = document.createElement('div');
	dm.setAttribute("id", "disable_mask");
	dm.style.zIndex = 1;
	dm.style.position = 'absolute';
	dm.style.top = 0;
	dm.style.left = 0;
	dm.style.display = 'none';
	
	var gotWidth;	
	var gotHeight;
		
	dm.style.width = GetPageWidth() + "px";
	dm.style.height = GetPageHeight() + "px";


	if (browser == BROWSER_IE)
		dm.style.filter = 'alpha(opacity=' + 50 + ')';
	else
		dm.style.opacity = 0.5;
	dm.style.backgroundColor = '#aaaaaa';
	document.body.insertBefore(dm, document.body.firstChild);		

	disable_mask_created = true;
}

function ResizeMaskScroll()
{
	if (!disable_mask_visible)
		return;
	ShowDisableMask();
}

function ShowDisableMask()
{
	if (!disable_mask_created)
	{
		CreateDisableMask();

		if(window.addEventListener)
		{
			window.addEventListener("scroll", ResizeMaskScroll, false); 
			window.addEventListener("resize", ResizeMaskScroll, false); 
		}
		else 
		{
			window.attachEvent("onscroll", ResizeMaskScroll);
			window.attachEvent("onresize", ResizeMaskScroll);
		}

	}	

	var dm = gbi('disable_mask');

	dm.style.width = GetPageWidth() + "px";
	dm.style.height = GetPageHeight() + "px";

	dm.style.display = "block";
	disable_mask_visible = true;	
}


function GetPageHeight()
{
     var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;

     var h = Math.max(scrollHeight, getViewportHeight());
     return h;
}
        
function GetPageWidth()
{
      var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
      var w = Math.max(scrollWidth, getViewportWidth());
      return w;
}

function HideDisableMask()
{
	disable_mask_visible = false;
	var dm = gbi('disable_mask');
	dm.style.display = "none";
}

function hide_zoomed_image()
{
	hide_zoom_click();
}

function show_gallery_zoomed(img_url)
{
	document.body.style.cursor = 'wait';
	var str = img_url;
	var zi = document.getElementById('zoomed_image');
	gbi('zoomed_image_img').onload = imonload;
	gbi('zoomed_image_img').src = str;
	gbi('zoomed_image_img').style.visibility = "hidden";

	zoom_image_loaded = false;

	zoom_timer = setTimeout('clear_zoom()', 10000);
}

function imonload()
{
	clearTimeout(zoom_timer);
	zoom_image_loaded = true;
	go_zoom_click();
}

function clear_zoom()
{
	if (zoom_image_loaded)
		return;
	document.body.style.cursor = 'default';
	zoom_image_loaded = false;
}


	
	



