/* onResizeFont plugin
 * one unworking case:
 *   IE: can't catch changing text size through the top menu
 * TODO: try to escape from multiple #resizeIndicators
 ***/
(function($){
	var allcollection = $();
	var resizeIndicator;

	$.fn.resizefont = function(fn) {
		var collection = $(this);
		allcollection.push(collection);

		function forceResizeFont() {
			allcollection.each(function(i){
				$(this).trigger('resizefont');
			});
		}

		function ResizeIndicatorClass(callFunction, refreshTime) {
			$('body').prepend('<div id="resizeIndicator">resizeIndicator</div>')
				.find('#resizeIndicator').css({visibility: 'hidden', position: 'absolute'});

			this.testObject = $('#resizeIndicator');
			this.width = (this.testObject).width();
			this.height = (this.testObject).height();
			this.zoom = ($.browser.msie)?((this.testObject).get(0).style.zoom):(1);

			this.callFunction = callFunction;
			this.refreshTime = refreshTime;

			var obj = this;
			this.listener = setInterval(function(){obj.functionProcess();}, obj.refreshTime);

			this.functionProcess = function(){
				var tw = (this.testObject).width();
				var th = (this.testObject).height();
				var tz = ($.browser.msie)?((this.testObject).get(0).style.zoom):(1);

				if((tw != this.width) || (th != this.height) || (tz != this.zoom)){
					this.width = tw;
					this.height = th;
					/*this.zoom = tz;*/

					this.callFunction();
				}
			}
		}


		collection.each(function(i){
			$(this).bind('resizefont', function(){
				fn.apply(this);
			});
		});

		/*if($.browser.msie) {
			$(document).keydown(function(e){
				if (e.ctrlKey && (
					e.which == 187 ||
					e.which == 189 ||
					e.which == 107 ||
					e.which == 109 ||
					e.which == 96  ||
					e.which == 48
				)) forceResizeFont();
			});
			$(document).bind('mousewheel', function(e){
				if (e.ctrlKey) forceResizeFont();
			});

		} else {*/
			if(!resizeIndicator) {
				resizeIndicator = new ResizeIndicatorClass(
					function(){
						forceResizeFont();
					}, 100
				);
			}

		/*}*/

		setTimeout(function() {
			forceResizeFont();
		}, 100);

		return $(this);
	};
})(jQuery);
