/*
	File:			Common.js
	Author:			Alistair "Boz" Bowness
	Chronology:		v1.2 created 5-Mar-2002 for Apache / PHP
	Description:	Provides "back-end" JavaScript support for my "visual library" web design
	Notes:			Please don't nick this without asking! It took a lot of sweat and tears to create...
	Dependencies:	xbStyle.js, ua.js
	
	History:		5-Mar-2002: Started work on PHP version -- very similar to old ASP version, so no worries!


	Any variables that are "user-editable" will be at the top of this file. You usually won't need to tweak anything
	else unless you're overriding any of my default behaviours.	
*/

	var ABSOLUTE_RIGHT = 774;	// Change this to the MAXIMUM screen-width you're going to support, minus 26.
								// So, 640 would be (640-26)=614, 800=774, 1024=998, 1152=1116, 1280=1254.
								// As at time of writing, I go for the safe bet of 800 (therefore 774).

	var ctrlPopup;				// Holds current menu that's popped-up, so we know what to "pop-down" later

	

var iEMail = new Image();
var iEMailOver = new Image();

iEMail.src = "images/btnEMail.gif";
iEMailOver.src = "images/btnEMailOver.gif";

	

// Main Functions
	
// ColorChange; useful for changing URL color when user hovers over it.
// <a href="summat.htm" onmouseover="colorChange( this, "#ff00ff" )....>
function colorChange( ctrl, cColor ) {	
	oCtrl = new xbStyle( ctrl );
	oCtrl.setBackgroundColor( cColor );
	//ctrl.style.backgroundColor = cColor;
}

// Useful for changing image when user rolls over
// <img src="..." onmouseover="imageChange( this, "gfxGen.php?over=1")...>
function imageChangeNew( ctrl, newSrc ) {
	ctrl.src = newSrc;
}

// Backwards compatibility: in the ASP days I used to "preload" the images and then point to the variables.
// If you still want to do that, use this function.
function imageChange( ctrl, newPic ) {
	ctrl.src = newPic.src;
}

// Also a rollover function. Changes window status (at bottom of browser). Also, if the "BozTicker" has been
// set up, it will use that as well. (Doesn't need to be, though.)
function winStatus( cStatus ) {
	window.status = cStatus;

	// Kludge -- can produce an error if user rolls mouse when document still loading:
	elem = document.getElementById( 'status' );
	if ( elem ) {
		oStatus = new xbStyle( elem );
		oTicker = new xbStyle( document.getElementById( 'bozTicker' ) );
		if ( cStatus == '' ) {
			oStatus.setVisibility( 'hidden' );
			oTicker.setVisibility( 'visible' );
		} else {
			oStatus.setInnerHTML( cStatus );
			oTicker.setVisibility( 'hidden' );
			oStatus.setVisibility( 'visible' );
		}
	}
	
	return true;
}

// Click function -- pops up a "hidden" window (NOT a menu!). Useful for those "help" windows that I use!
// <cSource> is the element ID of the "window" (usually a table within a DIV)
// <cTarget> is the element ID of the control that the source is "attached" to
// <dx> and <dy> are delta-offsets (in pixels) to offset the source from the target (can be negative)
// Example: <a href="JavaScript:void(0);" onclick="popUp( 'myMenu', 'myButton', -10, -10 );"...>
function popUp( cSource, cTarget, dx, dy ) {
	oSource = new xbStyle( document.getElementById( cSource ) );
	oTarget = new xbStyle( document.getElementById( cTarget ) );
	oSource.setPageX( oTarget.getPageX() + dx );
	oSource.setPageY( oTarget.getPageY() + dy );

	oSource.setVisibility( 'visible' );
}

// Popdown removes a control that has been popped-up from the previous function
// <control> - the control to pop down
function popDown( control ) {
	oControl = new xbStyle( document.getElementById( control ) );
	oControl.setVisibility( 'hidden' );
}

// menuPopUp is almost the same as popUp() above, but it "remembers" the control once it is popped up. We can
// then unpop the control using menuPopDown() without having to remember ourselves what the control to pop down is!
// This also caters for an idea by Kenz -- if you have a menu popped up, there should be some way of making it
// disappear if it's getting in the way. My solution was to allow a user to click on an inactive part of the current
// document, by simply changing the BODY definition to:
// <body onclick="menuPopDown()"... >
function menuPopUp( cSource, cTarget, dx, dy ) {
	// Why do I position the window on the fly, and not before, like on an ONLOAD event?
	// because Netscape is utter CRAP and ignores it, that's bloody well why!
	// Use dx and dy as delta positions to the drop-target

	oSource = new xbStyle( document.getElementById( cSource ) );
	oTarget = new xbStyle( document.getElementById( cTarget ) );
	oParent = new xbStyle( document.getElementById( 'WINPARENT' ) ); // The controlling window

	oSource.setPageX( oTarget.getPageX() + dx );
	oSource.setPageY( oTarget.getPageY() + dy );

	// What's all this? Well, if it detects that a menu would go off-screen when it appears, it's
	// automagically positioned to right-justify itself against the parent button's right edge.

	if ( ( oSource.getPageX() + oSource.getWidth() - oParent.getPageX() ) > ABSOLUTE_RIGHT ) 
		oSource.setPageX( ( oTarget.getPageX() - oSource.getWidth() ) + oTarget.getWidth() );

	oSource.setVisibility( 'visible' );
	ctrlPopup = cSource;
}

// menuPopDown() -- unpops a previous "menu" control that was popped up.
function menuPopDown() {
	if ( ctrlPopup != null ) {
		oControl = new xbStyle( document.getElementById( ctrlPopup ) );
		oControl.setVisibility( 'hidden' );
		ctrlPopup = null;
	}
}


