/* 
 * fontScaler - flexible font-sizing plugin  
 * Examples and documentation at: http://www.marcup.nl/projects/fontScaler 
 * Version 0.2 (13/09/2009) * Copyright (c) 2009 Marc Bruisten 
 * Licensed under the GPL License: http://www.gnu.org/licenses/gpl.txt 
 * Requires: jQuery v1.3+ 
 *
 * Changelog 0.2
 * - removed obsolete class "fontScaler" from fontscaler anchor buttons
 * - fixed cookie storage functionality
 * - added cookie lifetime (storetime) setting
 *
 */
	
$(document).ready(function(){
	$(".fontScaler").fontScaler({
		target: 'a,p,th,td'
	});
});


 
 (function($) {

	$.fn.fontScaler = function(options) {
		var defaultSettings = {
			increment: 'fixed'				// fixed || variable
		, fixed: {							// fixed increment options
					size1: '80'
			, size2: '100'
			, size3: '120'
			, id1: 'fs_normal'
			, id2: 'fs_medium'
			, id3: 'fs_large'
			, title1: '小'
			, title2: '中'
			, title3: '大'
		}
		, store: false						// store user font-size in cookie
		, storetime: 10						// lifetime of cookie in minutes
		, target: eval(document.body)
		, containerclass: "fontScaler"
		, fx: "normal"						// normal || smooth
		, fxspeed: 100
		};

		if (options && options.fixed)
			options.fixed = $.extend({}, defaultSettings.fixed, options.fixed);
		
		var settings = new Array();
		settings = $.extend({}, defaultSettings, options);			
		
		// increment constructor

		function newfontScaler(i) {
			
			var fixed = settings.fixed;
			var variable = settings.variable;

			if (settings.increment == "fixed") {
				var fsid = fixed["id" + i];
				var fstitle = fixed["title" + i];
			}
			if (settings.increment == "variable") {
				var fsid = variable["id" + i];
				var fstitle = variable["title" + i];
				var fscharacter = variable["character" + i];
			}

			if ((settings.increment == "variable" && variable["id" + i] != null) || (settings.increment == "fixed" && fixed["id" + i] != null)) {
				return $(document.createElement('a'))
				.attr('href', 'javascript:void(0)')
				.attr('id', fsid)
				.attr('title', fstitle)
				.bind("click", function(e) { changeFontSize(i,e) })
			}
		}

		var fontSizeLinks = $(document.createElement('div'))
		.attr('class', settings.containerclass)
		.append(
			newfontScaler(1), newfontScaler(2), newfontScaler(3)
		);

		$(this).prepend(fontSizeLinks);
		
	
		
		// fontScaler function
		
		function changeFontSize(i,event) {
			
			i = i * 1;
			var target = settings.target;			
			
			if (settings.increment == "fixed") {

				if (settings.fx == "normal") {
					$(target).css('font-size', settings.fixed["size" + i] + '%');
				}
				if (settings.fx == "smooth") {
					$(target).animate({
						fontSize: settings.fixed["size" + i] + '%'
					}, settings.fxspeed);
				}
				if ((event) && (settings.store == true)) { //case button clicked //create a cookie to store font-size
					var duration = ((settings.storetime /24) /60);
					createCookie('fontScalerFixed', (i), duration);
				}
			
			}
			
		}
		
		var cookievalueFixed = readCookie('fontScalerFixed');
		if ((cookievalueFixed != null) && (settings.store == true)) {
			changeFontSize(cookievalueFixed);
		}
	
		/* cookie functions */
		function createCookie(name, value, days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
				var expires = "; expires=" + date.toGMTString();
			}
			else var expires = "";
			document.cookie = name + "=" + value + expires + "; path=/";
		}

		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for (var i = 0; i < ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0) == ' ') c = c.substring(1, c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
			}
			return null;
		}

		function eraseCookie(name) {
			createCookie(name, "", -1);
		}


	};
})(jQuery);
