/* 
 * dhLib: Simple Dynamic-HTML-Library
 * Konrad Schandera X / MMVII 
 * 
 * base-section
 *
 * functions for returning consistent values for 
 * cross-browser-inconsistent methods, such as:
 * events,
 * event's firing elements
 * event's mouse-offsets in elements
 * etc.
 */ 




/* allocated by dhLib_init() and filled with the browser-string */
dhLib_browser = "undefined"; 
dhLib_debug = false;



/*
 ** ==== INITIALIZING ==== **
 */

/*
 * initializes the dhLib, sets dhLib_browser-string
 */
function dhLib_init() {
	nav = navigator.userAgent;
	if	( nav.indexOf("MSIE") != -1 ) 	{ dhLib_browser = "IE";		}
	else if	( nav.indexOf("KHTML") != -1 )	{ dhLib_browser = "KHTML";	}
	else if	( nav.indexOf("Gecko") != -1 )	{ dhLib_browser = "GECKO";	}
	else if	( nav.indexOf("Opera") != -1 )	{ dhLib_browser = "PRESTO";	}
} // end.init






/*
 ** ==== REAL BASE FUNCTIONS AND STUFF UND SO ==== **
 */

/*
 * gets an element by id, without terminating the script when the element is not found,
 * thus, we can simply get the element in any function like e = dhLib_getElementById(), 
 * and when the element is found, use it as e., if not, the function simply returns false
 * and the function can abort without error. 
 */
function dhLib_getElementById(id) {
	if ( document.getElementById(id) ) {
		return( document.getElementById(id) );
	} // end.elementFound
	else {
		return(false);
	} // end.false
} // end.checkElement





/*
 ** ==== EVENT-FUNCTIONS ==== **
 */

/*
 * returns the event-object wich was fired from the element,
 * that is, it simply returns the first argument, wich should be an event in 
 * case of gecko/opera/khtml, or gets the global variable 'event', wich is 
 * used when InternetExplorer is used. always to be called when events from
 * elements are used to get event-data (such as mouse-positions etc.)
 */
function dhLib_getEvent(e) {
	if ( dhLib_browser == "IE" ) {
		return( event );
	} // detected IE, get globally fired 'event'
	else {
		return( e );
	} // end.other browsers, simply return the event
} // end.getEvent



/* 
 * returns the event's offsets from the originating element
 */
function dhLib_getOffsetX(e) {
	if ( dhLib_browser=="IE" || dhLib_browser=="PRESTO" || dhLib_browser=="KHTML" ) {
		return( e.offsetX );
	} // end.get by event.offsetX (IE,OPERA,SAFARI);
	else if ( dhLib_browser=="GECKO" ) {
		return( e.layerX );
	} // end.get by event.layerX (GECKO);
} // end.getOffsetX

function dhLib_getOffsetY(e) {
	if ( dhLib_browser=="IE" || dhLib_browser=="PRESTO" || dhLib_browser=="KHTML" ) {
		return( e.offsetY );
	} // end.get by event.offsetY (IE,OPERA,SAFARI);
	else if ( dhLib_browser=="GECKO" ) {
		return( e.layerY );
	} // end.get by event.layerY (GECKO);
} // end.getOffsetX

/*
 * returns the event's offset inside the page, that is, the mousecursor-position
 */
function dhLib_getCursorX(e) {
	
} // end.getCursorX

function dhLib_getCursorY(e) {
	
} // end.getCursorY



/*
 * returns the element wich fired the event given as argument
 */
function dhLib_getEventElement(e) {
	if ( dhLib_browser=="IE" ) {
		return( e.srcElement );
	} // end.ie, return srcElement
	else {
		return( e.target );
	} // end.all others return target
} // end.getEventElement



/*
 ** ==== NAVIGATION ==== **
 */

/* returns the scrolling inside the window */
function dhLib_getWindowScrollOffsetX() {
	if ( dhLib_browser=="IE" ) { return( document.body.scrollLeft );	}	 // end.detected IE
	else { return( window.pageXOffset ); }
} // end.getScrollX

function dhLib_getWindowScrollOffsetY() {
	if ( dhLib_browser=="IE" ) { return( document.body.scrollTop ); } // end.ie
	else { return( window.pageYOffset ); }
} // end.getScrollY





/*
 ** ==== GRAPHICS ==== **
 */

/*
 * set opacity for element with id
 */
function dhLib_setOpacity(id,opacity) {
	e = dhLib_getElementById(id);
	if ( e ) {
		dhLib_opacities[id] = opacity;
		w3cOpacity = opacity / 100;
		e.style.opacity = w3cOpacity;
		e.style.filter = "Alpha(opacity=" + opacity + "');";

		/*debug*/ dhLib_writeDebug( id+"_o_ie" , opacity );
		/*debug*/ dhLib_writeDebug( id+"_o_ge" , w3cOpacity );

	} // end.element found
} // end.setOpacity






/*
 ** ==== DEBUGGING ==== ** 
 */

/*
 * writes a debug-message to an element specified by id. element must exist in the document
 * and dhLib_debug must not be false. throws warning-alert when element doesnt exist
 */
function dhLib_writeDebug(id,val) {
	if ( dhLib_debug == false ) { return(0); }
	e = dhLib_getElementById(id);
	if ( e ) {
		if ( e.nodeName == "INPUT" ) {
			e.value = val;
		} // end.debug-output-element is input, use value
		else {
			e.innerHTML = val;
		} // end.debug-output-element is other, try html
	} // end.element found
	else {
		alert("ERROR: Debug-element with id '" + id + "' could not be found");
	} // end.error
} // end.debugWrite






/* initialises the base */
dhLib_init();

