/* 
	Library: Gainsborough - web client framework
	Component: common functions
	
	Hextra Digital 
	http://www.hextra-digital.com
*/

// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;
	if( window.addEventListener ) { // Standard
		element.addEventListener( type, expression, bubbling );
		return true;
	} else if( window.attachEvent ) { // IE
		element.attachEvent( 'on' + type, expression );
		return true;
	} else return false;
}

function hx_image_preloader( _img_src, _callback, _notify ) {
	this.source		= _img_src;
	this.notify		= '';
	this.notifyID	= _notify;
	this.callback	= _callback;
	this.notify		= ge( this.notifyID );	

	re( this.notify, '<img style="margin-right: .5em" src="images/circle_ball.gif />Retrieving image...' );
};

hx_image_preloader.prototype.load = function() {	
	var img 			= new Image;	
	img.onload 			= hx_image_preloader.prototype.imageLoaded;
	img.onerror 		= hx_image_preloader.prototype.imageError;
	img.parent			= this;
	img.src 			= this.source;
};

hx_image_preloader.prototype.imageError = function() {
	re( this.notify, '<strong>Error</strong>' );
	delete img;
};

hx_image_preloader.prototype.imageLoaded = function() {
	this.parent.imageCompleted();
	if( this.parent.callback ) this.parent.callback ( this );
};

hx_image_preloader.prototype.imageCompleted = function() {
	re( this.notify, '' );
	delete img;
};

function cursor_wait()  {
	var cursor = 
	document.layers ? document.cursor :
	document.all ? document.all.cursor :
	document.getElementById ? document.getElementById( 'cursor' ) : null;

	if( cursor ) cursor = 'wait';
};

function cursor_clear() {
	var cursor = 
	document.layers ? document.cursor :
	document.all ? document.all.cursor :
	document.getElementById ? document.getElementById( 'cursor' ) : null;

	if( cursor ) cursor = 'default';
};

/*
	ge( _id ) - useful shortword for getElementById
*/
function ge( _id ) {
    var obj;
    if( document.getElementById ) {
        obj = document.getElementById( _id );
    }else if( document.all ) {
        obj = document.all[ _id ];
    }else if( document.layers ) {
        obj = document.layers[ _id ];
    }
    return obj || null;
};

/*
	gd( xml, node ) - useful shortword for getElementById
*/
function gd( xml, node ) {
    if( !xml ) return '';
    var x = xml.getElementsByTagName( node );
    if( !x ) return '';
    if( !x[ 0 ] ) return '';
    if( !x[ 0 ].firstChild ) return '';
    return x[ 0 ].firstChild.data;    
};

/*
	gds( xml, node ) - useful shortword for getElementsByTagName
*/
function gds( xml, node ) {
    if( !xml ) return null;
    return xml.getElementsByTagName( node );
};

/*
	ga( _xml_node, _attribute ) - retrieves an attribute from a specific node
*/
function ga( _node, _attribute ) {
    if( !_node ) return '';	
    var attr = _node.getAttribute( _attribute );	
	return attr;
};

/*
	clear( _node ) - erases all contents in a specified DOM node
*/
function clear( node ) {

    if( node.childNodes ) {
	
        while( node.childNodes.length > 0 ) {
		
            clear( node.childNodes[ 0 ] );
            if( node.childNodes[ 0 ].nodeName == 'IMG' ) {
                var img = node.childNodes[ 0 ];
                if( img ) img.src = '';
            }
            node.removeChild( node.childNodes[ 0 ] );        
        }
    }
};

/*function clearimg( node )
{    
    var imgs = node.getElementsByTagName( "img" );
    for( i = 0; i < imgs.length; i++ )
    { 
        imgs[ i ].src = ""; 
    }
};*/

/*
	re( _node, _content ) - replaces DOM node content
*/
function re( node, content ) {
    if( node ) {
        clear( node );
        node.innerHTML = content;
    }
};