// dhtmlmagic.js -- Javascript DHTML animation stuff
// Version 1.01 (28/01/2003)
// ------------------------------------------------------
// Copyright Webree.com Ltd, All rights reserved.
// Written by Matthew Tighe, some code from Dan Steinman's website
// popup functions by dave searle

// initDHTML() - IE4/NS4/DOM
// -------------------------
// Sets ut DHTML pointers

var ns4, ie4, dom;
var mouseX = 0;
var mouseY = 0;

function initDHTML()
{
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false
	ns6 = (!document.all && document.getElementById) ? true: false;

	dom = (document.getElementById&&!document.all) ? true : false;
	
/*	if( dom )
		alert("DOM1 COMPATIBLE (NS6)");
	if( ie4 )
		alert("IE4/5");
	if( ns4 )
		alert("NETSCAPE 4");
*/
}

// getBrowserWidth() - IE4/NS4/DOM
// -------------------------------
// Returns the browsers inner width

function getBrowserWidth()
{
	var nWidth;
	if( dom )
		nWidth = window.innerWidth;
	else
		nWidth = (ns4)? window.innerWidth : document.body.clientWidth;
	
	return( nWidth );
}

// getBrowserHeight() - IE4/NS4/DOM
// -------------------------------
// Returns the browsers inner height

function getBrowserHeight()
{
	var nHeight;
	if( dom )
		nHeight = window.innerHeight;
	else
		nHeight = (ns4)? window.innerHeight : document.body.clientHeight;
	return( nHeight );
}

function getScrollXY() {

	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
	scrOfY = window.pageYOffset;
	scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant

	scrOfY = document.body.scrollTop;
	scrOfX = document.body.scrollLeft;
	} else if( document.documentElement &&
	  ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
	scrOfY = document.documentElement.scrollTop;
	scrOfX = document.documentElement.scrollLeft;
	}

	return [ scrOfX, scrOfY ];
}

// getLayerObj( layerName ) - IE4/NS4/DOM
// -----------------------------------------------------
// Returns an object which can be used to manipulate <layerName>

function getLayerObj( layerName )
{
	var retobj;
	if (ns4) 
	{
		retobj = eval( "document." + layerName );
		
		if(retobj) {
			retobj.xpos = parseInt( retobj.left );
			retobj.ypos = parseInt( retobj.top );
		}
	}
	if (ie4)
	{
		retobj = eval( layerName );
		if(retobj) {	
			retobj.xpos =  parseInt( retobj.offsetLeft);
			retobj.ypos = parseInt( retobj.offsetTop );
		}
	}
	if( dom )
	{
		retobj = document.getElementById( layerName );
		if(retobj) {
			retobj.xpos = parseInt( retobj.offsetLeft );
			retobj.ypos = parseInt( retobj.offsetTop );
		}
	}

	return( retobj );
}

function getLayerParentObj()
{
	if( obj.parentElement )
		return( obj.parentElement );
}

function getLayerXPos( obj )
{
	var x = 0, temp;
	if (ns4) 
		return( parseInt( obj.pageX ) );
	else
	{
		if(obj.offsetParent)
		{
			temp = obj
			//Looping parent elements to get the offset of them as well
			while(temp.offsetParent) 
			{ 
				temp = temp.offsetParent; 
				x += temp.offsetLeft;
			}
		}
		x += obj.offsetLeft;

		if( isNaN( x ) )
			if( obj.style )
				x = obj.style.left
			else
				x = 0;
		
		return( parseInt( x ) );
	}

	return( 0 );
}

function getLayerWidth( obj )		// Returns width of layer currently on screen
{
	var x = 0, temp;
	if (ns4) 
		return( parseInt( obj.width ) );
	else
	{
		if(obj.offsetParent)
		{
			temp = obj
			//Looping parent elements to get the offset of them as well
			while(temp.offsetParent) 
			{ 
				temp=temp.offsetParent; 
				x+=temp.offsetLeft;
			}
		}
		x += obj.offsetLeft
		return( parseInt( x ) );
	}

	return( 0 );
}

function getLayerYPos( obj )
{
	var y = 0, temp;
	if (ns4) 
		return( parseInt( obj.pageY ) );
	else
	{
		if(obj.offsetParent)
		{
			temp = obj
			//Looping parent elements to get the offset of them as well
			while(temp.offsetParent) 
			{ 
				temp=temp.offsetParent; 
				y+=temp.offsetTop
			}
		}
		y += obj.offsetTop

		if( isNaN( y ) )
			if( obj.style )
				y = obj.style.left
			else
				y = 0;

		return( parseInt( y ) );
	}

	return( 0 );
}

// getLayerClipWidth( obj, layerName ) - IE4/NS4/DOM
// -------------------------------------------------
// Returns the width of the visible portion of the
// layer object obj

function getLayerDisplayWidth( obj )
{
	return( clipValues(obj,'r') - clipValues(obj,'l') );
}

function getLayerDisplayHeight( obj )
{
	return( clipValues(obj,'b') - clipValues(obj,'t') );
}

// getLayerContentWidth( obj, layerName ) - IE4/NS4/DOM
// -------------------------------------------------
// Returns the width of the visible portion of the
// layer object obj

function getLayerContentWidth( obj )
{
	if (ns4) 
		return( parseInt( obj.document.width ) );

	return( parseInt( obj.offsetWidth ) );
}

function getLayerContentHeight( obj )
{
	if (ns4) 
		return( parseInt( obj.document.height ) );

	return( parseInt( obj.offsetHeight ) );
}

function setLayerContentWidth( obj, val )
{
	if (ns4) 
		obj.document.width = val;
	else
	obj.style.width = val;
}

function setLayerContentHeight( obj, val )
{
	if (ns4) 
		obj.document.height = val;

	obj.style.height = val;
}

function setLayerClass( obj, szClass )
{
	obj.className = szClass;
}

// showObject( obj ) - IE4/NS4/DOM
// -------------------------------
// duh?

function showObject(obj)
{
	if (ns4) obj.visibility = "show"
	else if (ie4 || dom) obj.style.visibility = "visible"
}

// hideObject( obj ) - IE4/NS4/DOM
// -------------------------------
// duh?

function hideObject(obj)
{
	if (ns4) obj.visibility = "hide"
	else if (ie4 || dom) obj.style.visibility = "hidden"
}

// displayObject( obj ) - IE4/NS4/DOM
// -------------------------------
// renders a div

function displayObject(obj)
{
	if (ns4) obj.display = "show"
	else if (ie4 || dom) obj.style.display = "inline"
}

// nodisplayObject( obj ) - IE4/NS4/DOM
// -------------------------------
// un-renders a div

function nodisplayObject(obj)
{
	if (ns4) obj.display = "show"
	else if (ie4 || dom) obj.style.display = "none"
}

// Alpha Blending Function

function blend(obj, level)
{
	// First some code to gracefully degrade on older browsers
	if( level <= 0 )
		hideObject( obj );
	else
		showObject( obj );
	
	if( typeof obj.style == "undefined" )
		return;
	
	if(ie4) obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha( Opacity=" + level +", FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)";

	if(ns6) obj.style.MozOpacity = level;
}

// clip( obj, t,r,b,l ) - IE4/NS4/DOM
// ------------------------------------
// Clips a layer (obj) to the values for Top Right, 
// Bottom Left

function clip(obj,l,t,r,b)
{
	if (ns4)
	{
		obj.clip.top = t
		obj.clip.right = r
		obj.clip.bottom = b
		obj.clip.left = l
	}
	else if (ie4 || dom) obj.style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}

// clipTo( obj, t,r,b,l ) - IE4/NS4/DOM
// ------------------------------------
// Adjusts a layers (obj) clip values for Top Right, 
// Bottom Left

function clipBy(obj,l,t,r,b)
{
	if (ns4)
	{
		obj.clip.top = clipValues(obj,'t') + t
		obj.clip.right = clipValues(obj,'r') + r
		obj.clip.bottom = clipValues(obj,'b') + b
		obj.clip.left = clipValues(obj,'l') + l
	}
	else if (ie4 || dom) obj.clip = "rect("+(this.clipValues(obj,'t')+t)+"px "+(this.clipValues(obj,'r')+r)+"px "+Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)"
}

function clipValues(obj,which) {

	if (ns4) {
		if (which=="t") return obj.clip.top
		if (which=="r") return obj.clip.right
		if (which=="b") return obj.clip.bottom
		if (which=="l") return obj.clip.left
	}
	else if (ie4 || dom)
	{
		if( typeof obj.style == "undefined" )
			return( 0 );

		if( obj.style.clip )
		{
			var clipv = obj.style.clip.split("rect(")[1].split(")")[0].split(" ")
			if (which=="t") return parseInt(clipv[0])
			if (which=="r") return parseInt(clipv[1])
			if (which=="b") return parseInt(clipv[2])
			if (which=="l") return parseInt(clipv[3])
		}
		else
			if (which=="t") return(0);
			if (which=="r") return( parseInt( obj.offsetWidth ) );
			if (which=="b") return( parseInt( obj.offsetHeight ) );
			if (which=="l") return(0);
	}
}


// moveBy( obj, xinc, yinc ) - IE4/NS4/DOM
// ---------------------------------------
// Moves <obj> by the amount specified in <xinc,yinc>

function moveBy(obj,xinc,yinc)
{
		obj.xpos += xinc;
		obj.left = obj.xpos;		
		obj.ypos += yinc;
		obj.top = obj.ypos;
}

// move( obj, x, y ) - IE4/NS4/DOM
// ---------------------------------
// Moves <obj> to absolute postion <xinc,yinc>

function move(obj,x,y,z)
{
	if( ns4 )
	{
		obj.left = x;		
		obj.top = y;
		obj.zIndex = z;
		return;
	}

	obj.style.left = x + 'px';
	obj.style.top = y + 'px';
	obj.style.zIndex = z;
}


// layerWrite( id, nestref, text ) - IE4/NS4/DOM
// ---------------------------------------------
// Sets the contents of layer <id> which MAY be a child
// of <nestref> to <text>

function layerWrite(id,nestref,text)
{
	if (ns4)
	{
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : eval('document.'+id+'.document');
		lyr.open();
		lyr.write(text);
		lyr.close();
	}
	if (ie4)
		document.all[id].innerHTML = text;
	if(dom)
		document.getElementById( id ).innerHTML = text;
}

// loadSource(id,nestref, url ) - untested
// ----------------------------
// Load the html from <url> into the layer referenced by <id>
// which may be a child of nestref.

function loadSource(id,nestref,url) {
	if( !dom )
	{
		if (ns4) {
			var lyr = (nestref)? eval('document.'+nestref+'.document.'+id) : document.layers[id];
			lyr.load(url,lyr.clip.width);
		}
		else if (ie4) {
			parent.bufferFrame.document.location = url;
		}
	}
	else
		document.getElementById( "bufferFrame" ).document.location = url;
}
// loadSourceFinish( id ) - untested
// ---------------------------------
// Called from within a page loaded with loadSource.
// Transfers the HTML from the buffer frame into <id>

function loadSourceFinish(id) {
	if (ie4) eval(id).innerHTML = parent.bufferFrame.document.body.innerHTML
	if (dom) document.getElementById( id ).innerHTML = document.getElementById( "bufferFrame" ).document.body.innerHTML;
}

// Mouse handling functions
// *****************************************************

// initMouseEvents( argOnUp, argOnDown, argOnMove ) - IE4/NS4/DOM (??????)
// -----------------------------------------------------------------------
// Sets the mouse event handlers to <argOn????>.

function initMouseEvents( argOnUp, argOnDown, argOnMove )
{
	document.onmousedown = eval( argOnDown);
	document.onmousemove = eval( argOnMove );
	document.onmouseup = eval( argOnUp );

	if (ns4)
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)
}

var createdActiveContentHider = false;
var ifrm = null;

function activecontentHider(objLayer, div, state) {

	var ifrm = document.getElementById(div);

	if (ie4) {
		//alert(objLayer.y + ':'  + objLayer.x + ':' + objLayer.width + ':' + objLayer.height)
		ifrm.style.width = objLayer.width + "px";
		ifrm.style.height = objLayer.height + "px";
		ifrm.style.left = objLayer.x + "px";
		ifrm.style.top = objLayer.y + "px";
		ifrm.style.zIndex = objLayer.z-1;
		ifrm.style.display = "none";
		if (state) 
			ifrm.style.display = "block";
	}
}

function mouseMoveHandler( e )
{
	mouseX = (ns4 || ns6)? e.pageX : event.x+document.body.scrollLeft;
	mouseY = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;
}