/*
JavaScript Library for Multi Platform Dynamic HTML
Version:	1.2
Authors:	Gordon McMullan

Version History:

1.2 Added image Pre-Loading function
1.1 Added NS4 resize bug handling (GM)
1.0	First Production Version (GM)


Filename: dynamichtml.js;
language: JavaScript1.1
Script functions in this file:
  resizeFix()  
  showElement(element)
  hideElement(element)
  positionElement( element, Xpos , Ypos )
  openWindow(pageURL, windowName, width, height, atts)
  swapImage(image, swapImageURL)
  preLoadImages([image1[, image2[,...[,itemN]]]])

*/
// set DHTML to true if Browser supports it
var DHTML = (document.layers || document.all || (parseInt(navigator.appVersion) >= 5)) ? true : false;
// set DOM to TRUE if we can use a W3C DOM model (NS6, IE5, and Opera 4 plus known at the moment).
var DOM = (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf("MSIE 5") != -1) ? true : false;
var dhtmlPrefix = "";
var styleSuffix = "";

// Netscape Resize Fix
if (document.layers) {
	windowWidth = window.innerWidth;
	windowHeight = window.innerHeight;
	window.onResize = resizeFix();
}

// how to manipulate a dhtml element
dhtmlPrefix = document.layers ? "document.layers[" : "document.all[";
dhtmlPrefix = DOM ? "document.getElementById(" : dhtmlPrefix;
styleSuffix = document.layers ? "]" : "].style";
styleSuffix = DOM ? ").style" : styleSuffix;
dhtmlpagePosX = document.layers ? ".pageX" : ".left";
dhtmlpagePosY = document.layers ? ".pageY" : ".top";
dhtmlrelativePosX = document.layers ? ".left" : ".left";
dhtmlrelativePosY = document.layers ? ".top" : ".top";
var hideValue = document.layers ? "hide" : "hidden";
var showValue = document.layers ? "show" : "visible";
//var relative = 'relative';

/* resizeFix
	Fix for Netscape 4 bug when Browser Window is resized which causes
	css presentation data to become corrupted plus a few other little oddities
*/
function resizeFix() {
	if (windowWidth != window.innerWidth || windowHeight != window.innerHeight)
	document.location.href = document.location.href
}


function showElement(element){
	if (DHTML){
		eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix);
		styleObj.visibility = showValue;
	}
}

function hideElement(element){
	if (DHTML){
		eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix);
		styleObj.visibility = hideValue;
	}
}

function positionElement( element, Xpos , Ypos ){
	if (DHTML){
		eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlpagePosX + "=" + Xpos);
		eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlpagePosY + "=" + Ypos);
	}
}


function NEWpositionElement( element, Xpos , Ypos , relative ){
	if (DHTML){
	var relative = (relative == true || relative == "relative") ? true : false ;
		if (relative && relative == true){
			//eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + "position=relative");	
			eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlrelativePosX + "=" + Xpos);		
			eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlrelativePosX + "=" + Xpos);		
		} else {
			eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlpagePosX + "=" + Xpos);
			eval("styleObj = " + dhtmlPrefix + "element" + styleSuffix + dhtmlpagePosY + "=" + Ypos);
		}
	}
}

// Image Swap Function
function swapImage(image, swapImageURL){
	var objectString,imageObject;
	if(document.images){
		if (typeof(image) == 'string') {
			objectString = 'document.' + image;
			imageObject = eval(objectString);
			if (imageObject && imageObject.src);{
				imageObject.src = swapImageURL;
			}
		} else if ((typeof(image) == 'object') && image && image.src) {
      		image.src = swapImageURL;
    	} else if ((typeof(image) == 'number') && document.images[image] && document.images[image].src) {
			document.images[image].src = swapImageURL;
		}
  	}
}

// Image Pre Loading Function supply the images as a series of string
// arguments representing the url's to pre-load
function preLoadImages(){
	if(document.images){
		imageObjects = new Array();
		for (i=0; i < arguments.length; i++){
			imageObjects[i] = new Image();
			imageObjects[i].src = arguments[i];
		}
	}
}


// Window Opener function
function openWindow(pageURL, windowName, width, height, atts){
	// set sensible default values
	if (!windowName) windowName = "popup";
	if (!width) width = 450;
	if (!height) height = 300;
	
	attributes = "width=" + width + ",height=" + height;
	attributes = (atts) ? attributes + "," + atts : attributes;
	
	// open window saving a reference to the window object
	_popup = window.open(pageURL, windowName, attributes);
	
	// ensure window is brought to front after 1/4 second once opened
	// this will also give focus to an already open window.
	popupTimer=setTimeout("_popup.focus()",250);
	
	//return the window object reference
	return _popup
}