/**
 * @class HD.Widget
 * @description A reusable class that other classes can extend with to provide widget lifecycle functionality
 * @constructor
 */
HD.Widget = function(config) {
	this.config = config;
	this.loadTemplates(arguments.callee);
	if (this.config.parent) {
		this.config.parent = HD.get(this.config.parent) || this.config.parent;
	}
};

HD.Widget.prototype = {
	/**
	 * Retrieves the parent element of the widget
	 * @returns {HTMLElement}
	 */
    getParent : function() {
    	return HD.get(this.config.parent);
    },

	/**
	 * Renders the widget given contextual data
	 * @param {object} data Contextual data to render the widget with
	 */
    render : function(data) {
    	var parentEl = this.getParent();
    	parentEl.innerHTML = this.getHtml(data);
    	this.setListeners(data);
		if (this.printable) {
			this.setPrintListeners(parentEl);
		}
    },

	/**
	 * Attaches event listeners to HTML elements after rendering is finished
	 * @param {object} data Contextual data to render the widget with
	 */
    setListeners : function(data) {
    	
    },

	/**
	 * Destroys all child content within the widget
	 */
    destroy : function () {
    	var parentEl = this.getParent();
    	parentEl.innerHTML = "";
    },
    
	/**
	 * Sets the loading state of the widget
	 * @param {boolean} isShow Whether the widget is currently loading
	 */
    loading : function(isShow) {
		var parentEl = this.getParent();
		if(isShow) {
			HD.addClass(parentEl, HD.CSS_CLASSES.LOADING);
		} else {
			HD.removeClass(parentEl, HD.CSS_CLASSES.LOADING);
		}
    },
    
	/**
	 * Listens for event notifications
	 * @param {string} eventName The event that was thrown
	 * @param {object} eventData Contextual event data
	 */
    update : function(eventName, eventData) {
    
    },
    
	/**
	 * Generate HTML content given contextual data
	 * @param {object} data Contextual event data
	 * @returns {string}
	 */
    getHtml : function(data) {
    	return "";
    },
    
	/**
	 * An analytics event string to fire (should be overridden)
	 * @returns {string}
	 */
    analyticsString : function() {
    	return "";
    }
};

HD.extend(HD.Widget, [HD.util.Observable, HD.util.Template]);
if (HD.util.Printable) {
	HD.extend(HD.Widget, [HD.util.Printable]);
}

HD.register('hd_widget', HD.Widget, {version: "1.0", build: "1"});
