/*******************************************************************************
	DHTML API
	Hand-crafted by Chris Nott using time-honoured, traditional techniques

	file:			dhtml.js
	home:			//Picchi/BlastShare/Resource library/cool code/JavaScript/.
	author:		Chris Nott (cnott@blastradius.com).
	history:		1999/11/18     Created
					1999/11/23		Fixed a bug in NS4 that caused getElement() to 
										return null if the element was in a nested layer.
					1999/12/01		Improved NS5 support.
	
 ******************************************************************************/

/** MUST FIX THIS FOR FRAMESET **/
if (document.layers){
	originalWindowWidth = innerWidth;
	originalWindowHeight = innerHeight;
	onresize=function() { if(innerWidth != originalWindowWidth || innerHeight != originalWindowHeight) location.reload() };
}

// Reference creation functions (existing elements)

function getElement(id, frame) {
	if (frame == null || frame == '') frame = self;
	if (document.getElementById && !is.ie5) return frame.document.getElementById(id);
	else if (document.all) return frame.document.all[id]; 
	else if (document.layers) {
		if (frame.document.layers[id]) return frame.document.layers[id];
		for (var i = 0; i < frame.document.layers.length; i++) {
			if (frame.document.layers[i].document && frame.document.layers[i].document.layers[id]){
       return getElement(id, frame.document.layers[i]);
       }else{
        for (var j = 0; j < frame.document.layers[i].document.layers.length; j++) {
          if (frame.document.layers[i].document.layers[j].document && frame.document.layers[i].document.layers[j].document.layers[id]){
            return getElement(id, frame.document.layers[i].document.layers[j]);
            }
        }
       }
		}
	}
	return null;
}

// Content write functions

function writeContents(element, html) {
	if (typeof(element) != 'object' || element == null) return null;
	if (document.layers) {
		element.document.open();
		element.document.write(html);
		element.document.close();
	}
	else if (element.innerHTML != null) element.innerHTML = html;
	else if (element.childNodes) element.childNodes[0].nodeValue = html;
	return null; 
}

// Visibility functions

function show(element) {
	if (typeof(element) != 'object' || element == null) return null;
	if (document.layers && element.parentLayer != window) element.visibility = 'inherit';
	else if (document.layers) element.visibility = 'show';
	else element.style.visibility = 'visible';
	return null; 
}

function hide(element) {
	if (typeof(element) != 'object' || element == null) return null;
	if (document.layers) element.visibility = 'hide';
	else element.style.visibility = 'hidden';
	return null; 
}

// Position functions

function getLeft(element) {
	if (typeof(element) != 'object' || element == null) return null;
	return (document.layers) ? element.left : parseInt(element.style.left);
}

function setLeft(element, value) {
	if (typeof(element) != 'object' || element == null || isNaN(parseInt(value))) return null;
	if (document.layers) element.left = parseInt(value);
	else element.style.left = parseInt(value) + 'px';
	return null; 
}

function getTop(element) {
	if (typeof(element) != 'object' || element == null) return null;
	return (document.layers) ? element.top : parseInt(element.style.top);
}

function setTop(element, value) {
	if (typeof(element) != 'object' || element == null || isNaN(parseInt(value))) return null;
	if (document.layers) element.top = parseInt(value);
	else element.style.top = parseInt(value) + 'px';
	return null; 
}

function moveTo(element, xpos, ypos) {
	setLeft(element, xpos);
	setTop(element, ypos);
	return null; 
}

function moveBy(element, xshift, yshift) {
	moveTo(element, getLeft(element) + xshift, getTop(element) + yshift);
	return null; 
}

// Clipping functions

function clipTo(element, top, right, bottom, left) {
	if (typeof(element) != 'object' || element == null) return null;
	if (isNaN(parseInt(top))) top = getClipValue(element, 'top');
	if (isNaN(parseInt(right))) right = getClipValue(element, 'right');
	if (isNaN(parseInt(bottom))) bottom = getClipValue(element, 'bottom');
	if (isNaN(parseInt(left))) left = getClipValue(element, 'left');
	if (document.layers) {
		element.clip.top = parseInt(top);
		element.clip.right = parseInt(right);
		element.clip.bottom = parseInt(bottom);
		element.clip.left = parseInt(left);
	}
	else element.style.clip = 'rect(' + parseInt(top) + 'px ' + parseInt(right) + 'px ' + parseInt(bottom) + 'px ' + parseInt(left) + 'px)';
	return null; 
}

function clipBy(element, top, right, bottom, left) {
	clipTo(element, parseInt(getClipValue(element, 'top') + top), parseInt(getClipValue(element, 'right') + right), parseInt(getClipValue(element, 'bottom') + bottom), parseInt(getClipValue(element, 'left') + left));
	return null; 
}

function getClipValue(element, side) {
	if (!document.layers && !(is.mac && is.ie4)) var clipv = element.style.clip.split('rect(')[1].split(')')[0].split('px');
	if (is.mac && is.ie4) var clipv = element.style.clip.split('rect(')[1].split(')')[0].split(' ');	
	if (side == 'top') return (document.layers) ? element.clip.top : Number(clipv[0]);
	if (side == 'right') return (document.layers) ? element.clip.right : Number(clipv[1]);
	if (side == 'bottom') return (document.layers) ? element.clip.bottom : Number(clipv[2]);
	if (side == 'left') return (document.layers) ? element.clip.left : Number(clipv[3]);
	return null; 
}
