/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       observe changing font size.
 *
 *    @version 2.0.20090607
 *    @requires jquery.js
 *    @requires bajl.js
 */
/* -------------------------------------------------------------------------- */
(function($) {



/* -------------------- Class : BAJL.FontSizeObserver -------------------- */
/**
 * observe changing font size (please use this as a singleton).
 * @class font size observer
 * @extends BAJL.Observable
 */
BAJL.FontSizeObserver = function() {
	/** observed node.
	    @type Element
	    @private */
	this.node        = null;
	/** current height of the observed node.
	    @type Number
	    @private */
	this.currentSize = 0;
	/** interval timer to observe changing font size.
	    @type BAJL.Interval
	    @private */
	this.timer       = null;
	/** observe interval (ms)
	    @type Number
	    @private
	    @constant */
	this.interval    = 500;
	
	if (BAJL.env.isDOMReady) {
		this.init();
	}
}

BAJL.FontSizeObserver.prototype = new BAJL.Observable;

/**
 * initialize.
 * @private
 */
BAJL.FontSizeObserver.prototype.init = function() {
	var id  = 'BAJL_FontSizeObserver_TestNode';
	var css = {
		  'position'       : 'absolute'
		, 'left'           : '-10000px'
		, 'top'            : '-10000px'
		, 'display'        : 'block'
		, 'visibility'     : 'hidden'
		, 'border'         : 'none'
		, 'margin'         : '0'
		, 'padding'        : '0'
		, 'font'           : 'normal normal normal 100%/1 inherit'
		, 'lineHeight'     : '1'
		, 'textDecoration' : 'none'
	};
	var $node = $('#' + id);
	if (!$node.get(0)) {
		$node = $(document.createElement('ins')).attr('id', id).css(css).text('M').appendTo(document.body);
	}
	this.node = $node.get(0);
	
	this.startObserve();
}

/**
 * get observed node's height.
 * @return observed node's height in px unit, it's nearly 'current font size'
 * @type Number
 */
BAJL.FontSizeObserver.prototype.getSize = function() {
	return this.node.offsetHeight;
}

/** start observing. */
BAJL.FontSizeObserver.prototype.startObserve = function() {
	if (!this.timer) {
		this.currentSize  = this.getSize();
		this.timer = new BAJL.Interval(this.observe, this.interval, this);
	}
}

/** stop observing. */
BAJL.FontSizeObserver.prototype.stopObserve = function() {
	if (this.timer) {
		this.timer.clear();
		this.timer = null;
	}
}

/**
 * observe font size changing.
 * @private
 */
BAJL.FontSizeObserver.prototype.observe = function() {
	var size = this.getSize();
	var diff = size - this.currentSize; 
	if (diff != 0) {
		this.doCallback('onChange', size, diff);
	}
	this.currentSize = size;
}



/* -------------------- for JSDoc toolkit output -------------------- */
/**
 * callback functions for {@link BAJL.FontSizeObserver}
 * @name BAJL.FontSizeObserver.callback
 * @namespace callback functions for {@link BAJL.FontSizeObserver}
 */
/**
 * a callback for when the display font size is changed.
 * @name BAJL.FontSizeObserver.callback.onChange
 * @function
 * @param {Number} size    current font size on the display (in px unit; nearly)
 * @param {Number} diff    difference from recent font size (in px unit)
 */



/* -------------------- for backward compatibilities -------------------- */

if (BAJL.settings.common.useBackCompat) {
	BAJL.CreateBackCompat({ 'BAFontSizeObserver' : BAJL.FontSizeObserver });
}



})(jQuery);
