// Object.inheritance.js - Allows for the addition of methods
// to an object without using JavaScript prototype methods
//
// Copyright C 2007 Craig Pearson <thepearson@gmail.com>  
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
// Usage
// 
// var MyObject(+ additional Methods) = 
//    getObj( name, frame, window ); or
//    getObj( id );                  or
//    getObj( Object );              etc
//

function objectInit( Obj ) {

	Obj.addEventHandler = function( eventType, fnHandler ) {
		if ( this.addEventListener ) {
			this.addEventListener( eventType, fnHandler, false );
		} else if ( this.attachEvent ) {
	 		this.attachEvent( "on" + eventType, fnHandler );
	 	} else {
			this[ "on" + eventType ] = fnHandler;
	  }
	}


	Obj.removeEventHandler = function( eventType, fnHandler ) {
		if ( this.removeEventListener ) {
			this.removeEventListener( eventType, fnHandler, false );
		} else if ( this.detachEvent ) {
			this.detachEvent( "on" + eventType, fnHandler );
		} else {
			this[ "on" + eventType ] = null;
		}
	}


	Obj.nextObject = function() {
		var n = this;
		do n = n.nextSibling;
		while ( n && n.nodeType != 1 );
		return n;
	}

	Obj.previousObject = function() {
		var p = this;
		do p = p.previousSibling;
		while ( p && p.nodeType != 1 );
		return p;
	}


	Obj.appendAfter = function( obj ) {
		var parent = this.parentNode;
		var next = this.nextObject();
		parent.insertBefore( obj, next );
		return true;
	}

	Obj.getNumberSiblings = function( obj ) {
		var cnt = 0;
		var n = this.parentNode.firstChild;
		while ( n.nextSibling ) {
			if ( n.nextSibling.nodeType == this.nodeType ) {
				cnt++;
				n = n.nextSibling;
			}
		}
		return cnt;
	}

	Obj.getPosition = function() {
		return [ this.getPositionLeft(), this.getPositionTop() ];
	}

	Obj.getPositionLeft = function() {
		var curleft = 0;
		var obj = this;
		if ( obj.offsetParent )
			while ( obj.offsetParent ) {
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}			
		else if ( obj.x ) 
			curleft += obj.x;
		return curleft;
	}

	Obj.getPositionTop = function() {
		var curtop = 0;
		var obj = this;
		if ( obj.offsetParent )
			while ( obj.offsetParent ) {
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		else if ( obj.y ) 
			curtop += obj.y;
		
		return curtop
	}

	Obj.getHeight = function() {
		if ( this.offsetHeight )
			return this.offsetHeight;
		else if ( this.style.height != "" )
			return this.style.height;
		else
			return false;
	}

	Obj.getWidth = function() {
		if ( this.offsetWidth )
			return this.offsetWidth;
		else if ( this.style.width != "" )
			return this.style.width;
		else
			return false;
	}
	return Obj;
}

function _getObject( name, frame, doc ) {
	try {
		if( ! doc ) 
			if( frame ) doc = frame.document;
			else doc = window.document;
		if ( doc[ name ] ) return doc[ name ];
		if ( doc.all && doc.all[ name ] ) return doc.all[name];
		if ( doc.getElementById && doc.getElementById( name ) ) return doc.getElementById(name);
		for ( var x = 0; x < doc.forms.length; x++ ) if( doc.forms[ x ][ name ] ) return doc.forms[x][name];
		for ( var x = 0; x < doc.anchors.length; x++ ) if( doc.anchors[ x ].name == name ) return doc.anchors[ x ];
		for ( var x = 0; document.layers && x < doc.layers.length; x++ ) {
			var theOb = _getObject( name, null, doc.layers[ x ].document );
			if ( theOb ) return theOb;
		}
		if ( ! frame && window[ name ] ) return window[ name ];
		if ( frame && frame[ name ] ) return oFrame[ name ];
		for( var x = 0; frame && frame.frames && x < frame.frames.length; x++ ) {
			var theOb = _getObject( name, frame.frames[ x ], frame.frames[ x ].document );
			if ( theOb ) return theOb;
		}
		return false;
	} catch ( E ) {
		return false;
	}
}

function getObj( name, frame, doc ) {
	var o;
	if ( typeof( name ) == "object" )
		o = name;
	else 
		o = _getObject( name, frame, doc );
	return 	objectInit( o );
}
