/* begin MiniStats */
(function($)
{	
	// constructor
	function v1ArticleTools(root)
	{	
		// Private fields ------------------------------------------------------------------
	
		var _root = $(root),
			_self = this,
			_scaleButtons,
			_opts = {
				debug: false
			}
			;
	
		// Public methods ------------------------------------------------------------------
		
		$.extend(_self, {
			setCopySize: function(copySize, dontDispatch)
			{
				debug("setCopySize");
				
				var articleBody = $("div.v1-article-body");
				
				_self.clearButtonStates();
				_root.find('.sizer-' + copySize).addClass("on");
				
				
				if (articleBody.length) {
					articleBody.removeClass("small");
					articleBody.removeClass("medium");
					articleBody.removeClass("large");
					
					articleBody.addClass(copySize);
				} else {
					setTimeout('$("div.v1-article-body").removeClass("small");$("div.v1-article-body").removeClass("medium");$("div.v1-article-body").removeClass("large");$("div.v1-article-body").addClass("'+copySize+'");', 600)
				}

//				if (dontDispatch != true)
//				{
				$.cookie('sn_articleTextSize', copySize, {path:'/'});
//					$(document.body).trigger('setCopySize', copySize);
//				}
			},
			
//			onSetCopySize: function(e, copySize)
//			{
//				debug("setCopySize");
//				_self.setCopySize(copySize, true /* don't dispatch again to avoid loops */);
//			},
			
			clearButtonStates: function()
			{
				debug("clearButtonStates");
				_scaleButtons.each(function(){
					var _this = $(this);
					_this.removeClass("on");	
				}
				);
			}
		});
	
		// Private methods -----------------------------------------------------------------
	
		function debug(str)
		{
			if (_opts.debug == true) {
				try {
					console.log(str);
				} 
				catch (e) {
					alert("debug: " + str);
				}
			}
		};
		
		function init()
		{	
			debug("ArticleTools init");
			_scaleButtons = _root.find("a");

			_scaleButtons.each(function(){
				var size = /\btext-sizer:\s*(\S+)\b/.exec(this.className)[1];
				$(this)
					.data('size', size)
					.addClass('sizer-' + size)
					.click(function()
					{
						_self.setCopySize($(this).data('size'));
						return false;
					})
				;
			}
			);
			
			//$(document.body).bind('setCopySize', _self.onSetCopySize);
			
			var cookieSize = $.cookie('sn_articleTextSize');
			if (cookieSize != null) {
				debug("found cookie:" + cookieSize);
				_self.setCopySize(cookieSize);
			} else {
				debug("no cookie - sn_articleTextSize");
			}
		};
	
		// Initialization ------------------------------------------------------------------
		init();
	};

	// jQuery plugin implementation
	$.fn.v1articleTools = function()
	{
		this.each(function()
		{
			var $instance = new v1ArticleTools($(this));
		});
		return this; 
	};
})(jQuery);
/* end MiniStats */

