

// Copyright (c) NimbleCat, 2008

/*
* this object wraps a DOM element
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("element.js requires the NC JavaScript framework");
 }
 
/*
 * create an attribute name space
 */
NC.widget.Attribute = {}
NC.widget.Attribute.ActorId = 'NCactorid' ;
NC.widget.Attribute.OwnerId = 'NCownerid';
NC.widget.Attribute.SelectedOption = 'NCSelectedOption';

NC.widget.Attribute.set = function( el, name, value) {
	el.setAttribute( name, value);
}

NC.widget.Attribute.get = function( el, name) {
	return el.getAttribute( name);
}

/* constructor
 * 
 */ 
 NC.widget.Element = function( id) {
 	this.domId = id;
 }
 
 
  	/*
 	 * add a document DOM event
 	 */
 	
 	 NC.widget.Element.addDocumentDomEvent = function( eventType, eventFn, capture) {
    	if (window.addEventListener) { // check if the DOM2 model is available
			document.addEventListener(eventType, eventFn, (capture));
        } else if (window.attachEvent) { // else use the IE model
            document.attachEvent("on" + eventType, eventFn);
        }
    }
 	
 	/*
 	 * removes a document DOM event
 	 */
 	NC.widget.Element.removeDocumentDomEvent = function(eventType, eventFn, capture) {
    	if (window.removeEventListener) { // check if the DOM2 model is available
			document.removeEventListener(eventType, eventFn, (capture));
        } else if (window.detachEvent) { // else use the IE model
            document.detachEvent("on" + eventType, eventFn);
        }
 	}
 	
 	 	/*
 	 * adds a DOM event
 	 * eventType: the type/name of the function. event.js lists all the names
 	 */
 	NC.widget.Element.addElementDomEvent = function( element, eventType, eventFn, capture) {
    	if (window.addEventListener) { // check if the DOM2 model is available
			element.addEventListener(eventType, eventFn, (capture));
        } else if (window.attachEvent) { // else use the IE model
            element.attachEvent("on" + eventType, eventFn);
        }
    }
 	
 	/*
 	 * removes a DOM event
 	 */
 	NC.widget.Element.removeElementDomEvent = function(element, eventType, eventFn, capture) {
    	if (window.removeEventListener) { // check if the DOM2 model is available
			element.removeEventListener(eventType, eventFn, (capture));
        } else if (window.detachEvent) { // else use the IE model
            element.detachEvent("on" + eventType, eventFn);
        }
 	}
 	
 	
 
 
 NC.widget.Element.prototype = {

 	/* gets the DOM elementID
 	 * 
 	 */
 	init: function() {
 	},
 
 	getId: function() {
 		return this.domId;
 	},
 	
 	/*
 	 * renders the element
 	 */
 	render:function() {
 		return 'Override Element.render';
 	},
 	
 	enable: function( state) {
 		if (this.domId != "-1") {
 			var element = document.getElementById( this.domId);
 			state = state ? false: true; // reverse the meaning
 			element.disabled = state;
 		}
 	},
 	
 	/*
 	 * adds a DOM event
 	 * eventType: the type/name of the function. event.js lists all the names
 	 */
 	addDomEvent: function( eventType, eventFn, capture) {
 		var element = document.getElementById( this.domId);
    	if (window.addEventListener) { // check if the DOM2 model is available
			element.addEventListener(eventType, eventFn, (capture));
        } else if (window.attachEvent) { // else use the IE model
            element.attachEvent("on" + eventType, eventFn);
        }
    },
 	
 	/*
 	 * removes a DOM event
 	 */
 	removeDomEvent: function(target, eventType, eventFn, capture) {
  		var element = document.getElementById( target);
    	if (window.removeEventListener) { // check if the DOM2 model is available
			element.removeEventListener(eventType, eventFn, (capture));
        } else if (window.detachEvent) { // else use the IE model
            element.detachEvent("on" + eventType, eventFn);
        }
 	},
 	
 	fireEvent: function( eventType) {
 		var bReturn;
  		var element = document.getElementById( this.domId);
		if (NC.env.ua.ie) {
			bReturn = element.fireEvent("on" + eventType);
        }
        else {  // Gecko, Opera, and Safari
            oEvent = document.createEvent("HTMLEvents");
            oEvent.initEvent(eventType, true, true);
            bSubmitForm = element.dispatchEvent(oEvent);
         }
 	},
 	
 	onSelect: function( selectedItemId) {
 		alert( "Element.onSelect called. Override this");
 	}
 	
 	
 }
 
 