/*******************\
  GSSI DOM devices
  Requires: Base
\*******************/

if (! GSSI) { alert("gssi.dom.js: gssi.base.js not loaded") };
GSSI.DOM = {};

GSSI.Base.Extend( GSSI.ElementPrototype,  {
	prependChild: function ( node ) {
		if ( this.hasChildNodes ) {
			this.insertBefore( node, this.childNodes[0] );
		} else {
			this.appendChild( node );
		}
		return this;
	},
	
	trueOffset: function () {
		var T = 0;
		var L = 0;
		
		var e = this;
		do {
		  T += e.offsetTop  || 0;
		  L += e.offsetLeft  || 0;
		  // Safari fix ?
		  if ((e.offsetParent==document.body) && (e.style.position == 'absolute')) break;	
		} while (e = e.offsetParent);
		
		return {top:T, left:L};
	},
	
	trueLeft: function () {
		return this.trueOffset().left;
	},

	trueTop: function () {
		return this.trueOffset().top;
	},
	
	moveTo: function( fix, position, options ) {
		options = GSSI.Base.Extend({ offsetTop:0, offsetLeft:0 }, options);
		var pfn;
		fix = GSSI.$(fix);
		this.style.margin = 0;
		
		var fixobj = fix.trueOffset();
		fixobj.height = fix.offsetHeight;
		fixobj.width = fix.offsetWidth;

		var thisobj =  this.trueOffset();
		thisobj.height = this.offsetHeight;
		thisobj.width = this.offsetWidth;

		if(position) {
			if (typeof(position)=='string') { position = position.split(' '); };
			for( var i=0; i<position.length; i++ ) {
				pfn = GSSI.DOM.Positions[position[i]];
				if(pfn) {
					pfn( thisobj, fixobj );
				}
			}
		}
		
		thisobj.left += options.offsetLeft;
		thisobj.top +=  options.offsetTop;
		var trueoff = this.trueOffset();
		this.style.top = thisobj.top + 'px';
		this.style.left = thisobj.left + 'px';
	}, 
	
	sizeTo: function( src, widthdelta, heightdelta, options ) {
		options = options || {};
		widthdelta = widthdelta || 0;
		heightdelta = heightdelta || 0;
		if(!options.skipHeight) { this.style.height = (src.offsetHeight + heightdelta) + "px"; };
		if(!options.skipWidth) { this.style.width = (src.offsetWidth + widthdelta) + "px"; };
	},
	
	selectable: function( ok ) {
		var n;
		if (arguments.length < 1) { var ok = true; }
		if (ok) {
			if ( this.unselectable == 'on' ) {
				this.unselectable = null;
				this.style.MozUserSelect = 'inherit';
				for( var i = 0; i< this.childNodes.length; i++ ) {
					n = this.childNodes[i];
					if (n.nodeType != 3) { GSSI.$(n).selectable(true); };
				}
			}
		} else {
			if ( this.unselectable != 'on' ) {
				this.unselectable = 'on';
				this.style.MozUserSelect = 'none';
				for( var i = 0; i< this.childNodes.length; i++ ) {
					n = this.childNodes[i];
					if (n.nodeType != 3) { GSSI.$(n).selectable(false); };
				}
			}
		}
	}
});

GSSI.Base.WrapElement(document.body);

GSSI.DOM.GetBody = function() {
	return GSSI.Base.WrapElement(document.body);
};

GSSI.DOM.createElement = function ( tagname, attribs, styles, text, children ) {
	if (! tagname) { return null; }
	tagname = tagname.toString();
	attribs = GSSI.Base.Clone( attribs );
	
	var cssclass = null;
	if(GSSI.Base.HasProperty( attribs, "class" )) {
		cssclass = attribs["class"];
		attribs["class"]=null;
	}
	attribs["style"]=null;  // Can't support this crossbrowserly yet.
	
	e = document.createElement(tagname);
	if (!e ) { return null; }
	
	var a;
	for( p in attribs ) {
		a = attribs[p];
		if( (a === null) || (a === undefined) ) { continue; }
		if(GSSI.Base.HasProperty( e, p )) {
			e[p] = a;
		} else {
			e.setAttribute( p, a );
		}
	}

	for( p in styles ) {
		a = styles[p];
		if( (a === null) || (a === undefined) ) { continue; }
		e.style[p] = a;
	}
	e = GSSI.Base.WrapElement(e);
	if( cssclass ) { e.className = cssclass; };
	
	if (text) {
		e.appendChild(GSSI.DOM.createTextNode(text));
	};
	
	if (children) {
		for(var i=0; i<children.length; i++) {
			e.appendChild(children[i]);
		}
	}
	
	return e;
}

GSSI.DOM.createTextNode = function ( content ) {
	return document.createTextNode( content );
}

GSSI.DOM.UID = function() {
	var id;
	do {
		id = GSSI.Base.UID();
	} while( GSSI.$(id) )
	return id;
}

GSSI.DOM.Positions = {
	above:  function( targ, fix ) { targ.top = ( fix.top - targ.height ); },
	top:    function( targ, fix ) { targ.top = ( fix.top ); },
	middle: function( targ, fix ) { targ.top = ( fix.top + (fix.height/2) - (targ.height/2) ); },
	bottom: function( targ, fix ) { targ.top = ( fix.top + fix.height - targ.height ); },
	below:  function( targ, fix ) { targ.top = ( fix.top + fix.height ); },
	before: function( targ, fix ) { targ.left = ( fix.left - targ.width ); },
	left:   function( targ, fix ) { targ.left = ( fix.left ); },
	center: function( targ, fix ) { targ.left = ( fix.left + (fix.width/2) - (targ.width/2) ); },
	right:  function( targ, fix ) { targ.left = ( fix.left + fix.width - targ.width ); },
	after:  function( targ, fix ) { targ.left = ( fix.left + fix.width ); }
};



