/**
 * 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 :)
 *
 */

/**
 * Required: jQuery 1.x library, 
 * Optional: jQuery Cookie Plugin (if used, the last used font size will be saved)
 * This function it's called inside the $(document).ready() ***inside the Masterpage***
 * Eg: fontSize("#controls", "#content", 9); where,
 * #controls - where control is the element id, where the controllers will be created.
 * #content - for which element the font size changes to apply. In this case font size of content div will be changed
 * 13 - default font size
 * 
 */

function fontSize(container, target, defSize) {
	/*Editable settings*/
	var minCaption = "Default Text Size"; //title for mediumFont button
	var medCaption = "Medium Text Size"; //title for defaultFont button
	var maxCaption = "Large Text Size"; //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 +"'></a> ";
	mediumFontHtml = "<a href='javascript:void(0);' class='mediumFont' title='" + medCaption +"'></a> ";
	largeFontHtml = "<a href='javascript:void(0);' class='largeFont' title='" + maxCaption +"'></a> ";
	$(container).html(smallFontHtml + mediumFontHtml + largeFontHtml);
	
	//Read cookie & sets the fontsize
	if ($.cookie != undefined) {
		var cookie = target.replace(/[#. ]/g,'');
		var value = $.cookie(cookie);
		if (value !=null) {
			$(target).css('font-size', parseInt(value));
		}
	}
	
	//on clicking default font size button, font size is reset
	$(container + " .smallFont").click(function(){
		$(target).css('font-size', defSize);
		$(container + " .mediumFont").removeClass("sdisabled");
		$(container + " .largeFont").removeClass("ldisabled");
		updatefontCookie(target, defSize);
	});
		
	//on clicking small font button, font size is decreased by 2px
	$(container + " .mediumFont").click(function(){ 
		newSize = (target, defSize + 1);
		$(target).css('font-size', newSize);
	    updatefontCookie(target, newSize); //sets the cookie 
    });

	//on clicking large font size button, font size is incremented by 2 to the maximum limit
	$(container + " .largeFont").click(function(){
		curSize = parseInt($(target).css("font-size"));
		newSize = (target, defSize + 3);
		$(target).css('font-size', newSize);
		updatefontCookie(target, newSize);
	});

	function updatefontCookie(target, size) {
		if ($.cookie != undefined) { //If cookie plugin available, set a cookie
			var cookie = target.replace(/[#. ]/g,'');
			$.cookie(cookie, size);
		} 
	}
}
