﻿/********************************************************************************
Copyright (C) 1999 Thomas Brattli
These 4 functions made by Thomas Brattli at www.bratta.com
********************************************************************************/
//Default browsercheck, added to all scripts!
function checkBrowser() {
	this.ver=navigator.appVersion;
	this.dom=document.getElementById ? 1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom) ? 1:0;
	this.ie4=(document.all && !this.dom) ? 1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ? 1:0;
	this.ns4=(document.layers && !this.dom) ? 1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5);
	return this;
}
var bw=new checkBrowser();
//With nested layers for Netscape, this function hides the layer if it's visible and visa versa

function showHide(div,nest ) {
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if(obj.visibility=='visible' || obj.visibility=='show') obj.visibility='hidden'
	else obj.visibility='visible'
} //ENDFUNCTION [showHide]

function showByID(div,nest) {
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if ( obj ) {
		obj.visibility='visible';
	}
} //ENDFUNCTION [showByID]

function hideByID(div,nest) {
if(!document.getElementById(div) && console.log) {console.log('ShowHide:: DIV not available: '+div); }
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if ( obj ) {
		obj.visibility='hidden';
	}
} //ENDFUNCTION [hideByID]

function noDisplay(objID) {
	if (!document.getElementById(objID) ) { 
		alert('debug: noDisplay:: ' +objID+ ' is not available'); 
	} else { 
		document.getElementById(objID).style.display = 'none';
	}
} //ENDFUNCTION [noDisplay]

function yesDisplay(objID) {
	if (!document.getElementById(objID) ) { 
		alert('debug: yesDisplay:: '+objID+' is not available'); 
	} else { 
		document.getElementById(objID).style.display = 'block';
	}
} //ENDFUNCTION [yesDisplay]


//Browser indicator
var ns4 = (document.layers) ? true : false;  //browser info
var ie4 = (document.all) ? true : false;
var ng5 = (document.getElementById) ? true : false;

// Places the specified hidden DIV and correctly positions it relative to document boundaries and mouse position
// uses global variables winW, winH, mouseXposn and mouseYposn (from jsToolbox)
function placeDivByMouse(divID, xDelta, yDelta, itemOnPageID, topLimit, bottomLimit) {
//console.log('divID='+divID+', itemOnPageID='+itemOnPageID+', topLimit='+topLimit+', bottomLimit='+bottomLimit );//debug
	if ( !winW || winW == 0 ) { winW = document.body ? document.body.offsetWidth : 735; }  //set window width
	if ( !winH || winH == 0 ) { winH = document.body ? getDocHeight() : 700 ; }  //get window height, default to 700 if not available
	if ( winH > 800) { winH = Math.floor(winH*.7); }  //restrict winH to compensate for layout bugs
	if ( !topLimit ) { topLimit = 0; }
	if ( !bottomLimit ) { bottomLimit = winH; }
	obj = document.getElementById(divID);
	xDelta = xDelta != null ? xDelta : 2;
	yDelta = yDelta != null ? yDelta : 2;
	var xPos = mouseXposn + xDelta; //mouseXposn from jsToolbox
	var yPos = !itemOnPageID ? mouseYposn + yDelta : mouseYposn; //mouseYposn (from jsToolbox)
	obj.style.display = "block";  //set DIV on to enable size measurement
	objWidth=obj.scrollWidth;
	objHeight=obj.scrollHeight;
//X Coordinates [depends on whether itemOnPageID provided]
	if (itemOnPageID) { // IF [item on Page IS provided]  (use itemOnPage coordinate to avoid the object if possible)
		var itemOnPage = document.getElementById(itemOnPageID);
		if (findPos(itemOnPageID)[0]+itemOnPage.scrollWidth < winW-objWidth) {  //IF [object will fit to RIGHT of itemOnPage]
			xPos = findPos(itemOnPageID)[0]+itemOnPage.scrollWidth+xDelta; //place left side of object abutting right side of itemOnPage
		} else {  //ELSEIF [object will fit to LEFT of itemOnPage]
			if (findPos(itemOnPageID)[0] > objWidth) {  //IF [object will fit to LEFT of itemOnPage]
				xPos = findPos(itemOnPageID)[0]-objWidth-xDelta; //place right side of object abutting left side of itemOnPage
			} else {  //ELSEIF [object won't fit to LEFT of itemOnPage]
				xPos = 0; //place the object abutting the left edge knowing that it will overlap itemOnPage 
			}  //ENDIF [object will (or won't) fit to LEFT of itemOnPage]
		}  //ENDIF [object will fit to RIGHT of itemOnPage]
	} else {  // ELSEIF [item on Page NOT provided] (use mouse coordinates only)
		if (xPos + objWidth > winW ) { xPos = mouseXposn-objWidth-xDelta; }  //winW is global supplied by jsToolbox
	}  // ENDIF [item on Page provided]
//console.log('xPos=' +xPos+ ' vs. itemOnPageID[x]=' +findPos(itemOnPageID)[0]+ ' and objWidth=' +objWidth);  //debug
//Y Coordinates [independent of whether itemOnPageID provided]
	var yNewPos = -1;
	var topOverlap = (yPos - .5*objHeight) - topLimit;  
	if ( topOverlap < 0 ) {  //IF [object overlaps topLimit, lower yPos] 
		yNewPos = topLimit; 
	}  //ENDIF [object overlaps topLimit, lower yPos] 
	var bottomOverlap = (yPos + .5*objHeight) - bottomLimit;
	if ( bottomOverlap > 0 ) {  //IF [object overlaps bottom raise yPos]
		yNewPos = bottomLimit - objHeight-5; 
	}    //ENDIF [object overlaps bottom raise yPos] //note: bottomOverlap is positive if extends below bottomLimit
	if ( bottomOverlap <= 0 && topOverlap >= 0 ) {  //IF [object fits, center it]
		yNewPos = yPos - .5*objHeight; 
	}  //ENDIF [object fits, center it]
	if ( objHeight > (bottomLimit-topLimit) ) {  //IF [object bigger than space available push it to top of page]
		yNewPos = 1; 
	}  //ENDIF [object bigger than space available push it to top of page]
	yPos=yNewPos;
	
	placeObj(divID, xPos, yPos ); 
}  //ENDFUNCTION [placeDIVbyMouse]

// Shows the specified hidden DIV and correctly positions it relative to document boundaries and mouse position
// uses global variables winW, winH, mouseXposn and mouseYposn (from jsToolbox)
function showDIVbyMouse(divID, xDelta, yDelta, itemOnPageID, topLimit, bottomLimit) {
	placeDivByMouse(divID, xDelta, yDelta, itemOnPageID, topLimit, bottomLimit);
	showByID(divID); 
}  //ENDFUNCTION [showDIVbyMouse]

function placeDivByID(divID, xPos, yPos, topLimit, bottomLimit) {
	if ( !winW || winW == 0 ) { winW = document.body ? document.body.offsetWidth : 735; }  //set window width
	if ( !winH || winH == 0 ) { winH = document.body ? getDocHeight() : 700 ; }  //get window height, default to 700 if not supplied
	winH = (winH > 800) ? Math.floor(winH*.7) : winH;  //restrict winH to compensate for layout bugs
	if ( !topLimit ) { topLimit = 0; }
	if ( !bottomLimit ) { bottomLimit = winH; }

	var objToShow = $$(divID);
	objToShow.style.display = "block";  //set DIV on to enable size measurement

//X Coordinates
	var objWidth = objToShow.scrollWidth;
	if (xPos+objWidth > winW ) {  //IF [object extends beyond right border]   //winW is global supplied by jsToolbox
		xPos = winW - objWidth;  //push it left
	}  //ENDIF [object extends beyond right border]
	
//Y Coordinates
	var objHeight = objToShow.scrollHeight;
	var yNewPos = -1;
	var topOverlap = (yPos - .5*objHeight) - topLimit;  
	if ( topOverlap < 0 ) {  //IF [object overlaps topLimit, lower yPos] 
		yNewPos = topLimit; 
	}  //ENDIF [object overlaps topLimit, lower yPos] 
	var bottomOverlap = (yPos + .5*objHeight) - bottomLimit;
	if ( bottomOverlap > 0 ) {  //IF [object overlaps bottom raise yPos]
		yNewPos = bottomLimit - objHeight-25; 
	}    //ENDIF [object overlaps bottom raise yPos] //note: bottomOverlap is positive if extends below bottomLimit
	if ( bottomOverlap <= 0 && topOverlap >= 0 ) {  //IF [object fits, center it]
		yNewPos = yPos - .5*objHeight; 
	}  //ENDIF [object fits, center it]
	if ( objHeight > (bottomLimit-topLimit) ) {  //IF [object bigger than space available push it to top of page]
		yNewPos = 5; 
	}  //ENDIF [object bigger than space available push it to top of page]
	yPos=yNewPos;
	
	placeObj(divID, xPos, yPos); 
	return [xPos, yPos]; 
}  //ENDFUNCTION [placeDIVbyMouse]

function showDivByID(divID, xPos, yPos, topLimit, bottomLimit) {
	placeDivByID(divID, xPos, yPos, topLimit, bottomLimit);
	showByID(divID); 
}  //ENDFUNCTION [showDivByID]
 
function hideDIV(divID) {
if (ng5) document.getElementById(divID).style.display = "none";
else if (ns4) document.layers[divID].display = "none";
else if (ie4) document.all[divID].style.display = "none";
}

//Fades the selected ID the div

/*
function fadeByID(itemID) {
	$(#(eval(itemID)).fadeto('normal',.1);
}
*/
/***********These functions must travel together************************/