/**
 * Font Controller
 * For creating a font size changer interface with minimum effort
 * Copyright (c) 2009 Hafees (http://cool-javascripts.com)
 * License: Free to use, modify, distribute as long as this header is kept :)
 * Modified by Shirley (sap@velvetblues.com)
 */

function fontSize(container, target, minSize, defSize, maxSize) {
	/*Editable settings*/
	var minCaption = "Make font size smaller"; //title for smallFont button
	var defCaption = "Make font size default"; //title for defaultFont button
	var maxCaption = "Make font size larger"; //title for largefont button
	
	
	//Now we'll add the font size changer interface in container
	smallFontHtml = "<a href='javascript:void(0);' class='smallFont' title='" + minCaption +"'>" + minCaption + "</a> ";
	//defFontHtml = "<a href='javascript:void(0);' class='defaultFont' title='" + defCaption +"'>" + defCaption + "</a> ";
	largeFontHtml = "<a href='javascript:void(0);' class='largeFont' title='" + maxCaption +"'>" + maxCaption + "</a> ";
	$j(container).html(smallFontHtml + "Text Size" + largeFontHtml);
	
	//Read cookie & sets the fontsize
	if ($j.cookie != undefined) {
		var cookie = target.replace(/[#. ]/g,'');
		var value = $j.cookie(cookie);
		if (value !=null) {
			$j(target).css('font-size', parseInt(value));
		}
	}
		
	//on clicking small font button, font size is decreased by 1px
	$j(container + " .smallFont").click(function(){ 
		curSize = parseInt($j(target).css("font-size"));
		newSize = curSize - 1;
		if (newSize >= minSize) {
			$j(target).css('font-size', newSize);
		} 
		if (newSize <= minSize) {
			$j(container + " .smallFont").addClass("sdisabled");
		}
		if (newSize < maxSize) {
			$j(container + " .largeFont").removeClass("ldisabled");
		}
		updatefontCookie(target, newSize); //sets the cookie 
		
	});

	//on clicking default font size button, font size is reset
	$j(container + " .defaultFont").click(function(){
		$j(target).css('font-size', defSize);
		$j(container + " .smallFont").removeClass("sdisabled");
		$j(container + " .largeFont").removeClass("ldisabled");
		updatefontCookie(target, defSize);
	});

	//on clicking large font size button, font size is incremented by 1 to the maximum limit
	$j(container + " .largeFont").click(function(){
		curSize = parseInt($j(target).css("font-size"));
		newSize = curSize + 1;
		if (newSize <= maxSize) {
			$j(target).css('font-size', newSize);
		} 
		if (newSize > minSize) {
			$j(container + " .smallFont").removeClass("sdisabled");
		}
		if (newSize >= maxSize) {
			$j(container + " .largeFont").addClass("ldisabled");
		}
		updatefontCookie(target, newSize);
	});

	function updatefontCookie(target, size) {
		if ($j.cookie != undefined) { //If cookie plugin available, set a cookie
			var cookie = target.replace(/[#. ]/g,'');
			$j.cookie(cookie, size);
		} 
	}
}