// Should be called by OnResize to redraw the current page
var ReDrawFunc = null
var _ScreenResizing = false
function ReDraw()
{
	if ( ReDrawFunc )
		eval( ReDrawFunc )
}

function GetActualWindowWidth() {
	if(self.innerWidth)																										// Netscape
		return self.innerWidth;
	if(document.documentElement && document.documentElement.clientWidth)	// Explorer 6
		return document.documentElement.clientWidth;
	if(document.body)
		return document.body.clientWidth;
	return 500;
}

function GetActualWindowHeight() {
	if(self.innerHeight)																									// Netscape
		return self.innerHeight;
	if(document.documentElement && document.documentElement.clientHeight)	// Explorer 6
		return document.documentElement.clientHeight;
	if(document.body)
		return document.body.clientHeight;
	return 500;
}

// Set the default minimum window width
var MinWindowWidth = 800
var MinWindowHeight = 600

function GetWindowWidth() {
	var value = GetActualWindowWidth()
	return value < MinWindowWidth ? MinWindowWidth : value;
}

function GetWindowHeight() {
	var value = GetActualWindowHeight()
	return value < MinWindowHeight ? MinWindowHeight : value;
}


function getXmlHttp() {
	try
	{  // Firefox, Opera 8.0+, Safari
  		return new XMLHttpRequest();
  }
	catch (e)
	{  // Internet Explorer
		try
		{
			return new ActiveXObject("Msxml2.XMLHTTP")
		}
	  catch (e)
	  {
		 	try
		 	{
		    return new ActiveXObject("Microsoft.XMLHTTP")
		 	}
			catch (e)
			{
				alert("Your browser does not support AJAX!")
			}
		}
	}
	return 0;
}

//check if the first node is an element node
function GetFirstChild(node)
{
	var x = node.firstChild;
	while ( x.nodeType != 1 )
  	x = x.nextSibling;

	return x;
}

function GetPreviousSibling(node)
{
	var prevNode = node.previousSibling
	while ( prevNode.nodeType != 1 )
		prevNode = prevNode.previousSibling
	return prevNode
}

function GetNextSibling(node)
{
	var	nextNode = node.nextSibling
	while ( nextNode && nextNode.nodeType != 1 )
		nextNode = nextNode.nextSibling

	return nextNode;
}

function RandomMax(Max)
{
	Num = Math.floor( Math.random() * Max )
	if( Num == Max )
		Num--
	return Num
}

// Given and index into an array 'ar' and an increment (ie +1 or -1) return the next valid index
function NextElement(index, ar, inc)
{
	max = ar.length - 1
	index += inc
	// Check for negative wrap
	if( index < 0 )
		return max
	// Check for positive wrap
	if( index > max )
		return 0
	return index
}

function SetObjectPos( Obj, left, top, setVisible )
{
	Obj.style.top = top + 'px'
	Obj.style.left = left + 'px'
	if ( setVisible )
		Obj.style.visibility = 'visible'
}

// Resize the image to exactly fit the given size and maintain the aspect ratio
function ResizeImage( Img, RequiredWidth, RequiredHeight )
{
	if( RequiredWidth < 10 )
		RequiredWidth = 10
	if( RequiredHeight < 10 )
		RequiredHeight = 10

	var TargetRatio = RequiredWidth / RequiredHeight
	if (Img.AspectRatio > TargetRatio)
	{
		Img.width = RequiredWidth
		Img.height = Math.floor(RequiredWidth / Img.AspectRatio)
	}
	else
	{
		Img.height = RequiredHeight
		Img.width =  Math.floor(RequiredHeight * Img.AspectRatio)
	}
}

// Reduce the size of the image, if its too big to fit the given maximums
function ReduceImage( Img, MaxWidth, MaxHeight )
{
	if ( Img.width > MaxWidth || Img.height > MaxHeight )
		ResizeImage( Img, MaxWidth, MaxHeight )
}

// Reduce the size of the image, if its too big to fit the given maximums
function ReduceOriginalImage( ImgObj, MaxWidth, MaxHeight )
{
	SaveImageSizes(ImgObj)
	if ( ImgObj.OriginalWidth > MaxWidth || ImgObj.OriginalHeight > MaxHeight )
		ResizeImage( ImgObj, MaxWidth, MaxHeight )
}

function SaveImageSizes( ImgObj )
{
	if( ImgObj.OriginalWidth == undefined )
	{
		ImgObj.OriginalHeight = ImgObj.height
		ImgObj.OriginalWidth = ImgObj.width
		ImgObj.AspectRatio = ImgObj.width / ImgObj.height
	}
}

function AddEvent(imageObject, eventString, funct)
{
	if (imageObject.attachEvent)
		imageObject.attachEvent('on' + eventString, funct )
	else
		imageObject.addEventListener(eventString, funct, false)
}

function ClipRect(Obj, top, right, bottom, left)
{
	Obj.style.clip = "rect(" + top + "px," + right + "px," + bottom + "px," + left + "px)"
}

function UnClipRect(Obj)
{
	Obj.style.clip = "auto"
}

// NOTE use style 'filter:alpha(opacity=100)' otherwise the IE version will fail
function SetOpacity( Obj, percentOpacity )
{
	if(self.innerHeight == null)										// IE uses filter. Firefox etc uses opacity
		Obj.filters.alpha.opacity = percentOpacity		// IE
	else
		Obj.style.opacity = percentOpacity / 100			// Firefox etc
}


