/*   ________________________
 *  |    ____  ____  ____    |	jQuery Plugin by Jorge H. Morales www.jhmorales.es 17/01/2009 
 *  |   |    ||    ||    |   |	 * Copyright (c) 2009 Jorge H. Morales (www.jhmorales.es). GPL License.
 *  |   | -a ||  a || a+ |   |   * http://www.gnu.org/licenses/gpl.html
 *  |   |____||____||____|   |	   (original version in spanish fontReSizer.js)
 *  |________________________|	  based on http://www.shopdev.co.uk/blog/text-resizing-with-jquery/
 *  							It dynamically creates a full configurable [-a a a+] box to control the screen
 *   font-size, it also calculates the initial font-size for new visitors according to screen resolution to show
 *   the text in the same proportion. So in all resolutions font-size to screen-width proportion is near the same. 
 *   Also it keeps chosen font-size in cookie for future navigation.
 * 
 * EXAMPLES:
 * $("body);$("div:first").before and $("#changeFontSize").css("position", "absolute").css("top", "0").css("right", "0")
 *    creates [-a a a+] box at the top right corner with no changes in the page design.
 * $("#menu");$("li:first").before and $("#changeFontSize").css("position", "relative")
 *    creates [-a a a+] box at the top of a vertical menu.
 * $("#menu);$("li:last").after y $("#changeFontSize").css("position", "relative")
 *    creates [-a a a+] box at the bottom of a vertical menu.
 * OPTIONS:
 * Edit first variable values and IDENTIFIERS, change, add and/or delete css("...","...") format tags to change 
 *    the design of the [-a a a+] box and a links. Save and reload page to see changes.
 * INSTALATION:
 * Needs jQuery and jquery.cookie.js (Klaus Hartl http://plugins.jquery.com/project/Cookie)
 * Before </head> place this lines:
 * <script src="jquery-1.2.6.js" type="text/javascript"></script>
 * <script src="jquery.cookie.js" type="text/javascript"></script>
 * <script src="fontReSizer.js" type="text/javascript"></script>
 *
*/
$(function(){
	var initialFontSize = 16; // initial font-size in px
    var minFontSize = 6; // minimum font-size in px
    var maxFontSize = 48; // maximum font-size in px
    var siteName = "KINDERSCHUTZ"; // site name for the cookie
    var eParent = "#wrapper"; // FATHER IDENTIFIER inside I want the [-a a a+] box (body,div:first,etc.)
    var eSon = "#header";// SON IDENTIFIER (sibling) to place [-a a a+] box use :first o :last
    $(eSon,(eParent))
	// -a a a+ box will be placed 
	// before or after of the SON
    	.before(/*not change this=>*/"<div id='changeFontSize'><p><a href='#' id='initialFontSize'></a><a href='#' id='decreaseFont'>-A&nbsp;</a><a href='#' id='resetFont'>&nbsp;A&nbsp;</a><a href='#' id='increaseFont'>&nbsp;A+</a></p></div>");
	// [-a a a+] box 
    	$("#changeFontSize")
    		.css("position", "fixed")
			.css("top", 0)
			.css("right", "50%")
			.css("z-index",999)
		// format the [-a a a+] box container
			.css("width","8em")
			.css("height","auto")
			.css("margin", "0 -420px 0 0")
			.css("background-color", "transparent")
			.css("text-align","center");
    // format the a links
    	$("#changeFontSize a")
			.css("display", "inline-block")
			.css("padding", 2)
			.css("margin", "1%")
			.css("width", "auto")
			.css("text-decoration","none")
			.css("text-align","center")
			.css("color","white")
			.css("background-color", "#ce0045");
    
/* Change the divisor number of the CalcQuotient only if necessary (maybe in IE cases)==================== */
    var screenWidth = screen.availWidth;
	var CalcQuotient = screenWidth / initialFontSize;

    if (screenWidth < 1100) {
        fontHeight = screenWidth / CalcQuotient;//font-size for standard browsers with =< 1024px width screen resolution
    }
    else {
        fontHeight = screenWidth / (CalcQuotient/1.2);//font-size for standard browsers with >= 1024px width screen resolution
    }
    if ($.browser.msie) {
        if (screenWidth < 1100) {
            fontHeight = screenWidth / CalcQuotient;//font-size for ie browsers with =< 1024px width screen resolution
        }
	    else {
    	    fontHeight = screenWidth / (CalcQuotient/1.2);//font-size for ie browsers with >= 1024px width screen resolution
    	}
    }
    fontHeight = "10px";
/* Not change nothing from here down ====================================================================================================================================================================================================== */
    $("html").css("font-size", "62.5%");// stands font-size 10px=1em
    // show text resizing links
	$("a:last",("#changeFontSize")).after("<div></div>").css("clear","both");
    $("#changeFontSize").show();
    var $cookieName = siteName + "-changeFontSize";
    // if exists load saved value, otherwise store it
    	if ($.cookie($cookieName)) {
        	var $getSize = $.cookie($cookieName);
        	$("html").css({
            fontSize: $getSize + ($getSize.indexOf("px") != -1 ? "" : "px")// IE fix for double "pxpx" error
        	}); 
    	}
    	else {
        	$.cookie($cookieName, fontHeight, { expires: 30 });
    	}
    // reset link 
    $("#resetFont").bind("click", function(){
        $("html").css("font-size", "62.5%");
        $.cookie($cookieName, "62.5%", { expires: 30 });
    });
    // increase link
    $("#increaseFont").bind("click", function(){
        var actualFontHeight = $("html").css("font-size");
        var actualFontHeightNum = parseFloat(actualFontHeight, 10);
        var newFontHeight = actualFontHeightNum * 1.25;
        if (newFontHeight < maxFontSize) {
            $("html").css("font-size", newFontHeight);
            $.cookie($cookieName, newFontHeight, { expires: 30 });
        }
        return false;
    });
    // decrease link
    $("#decreaseFont").bind("click", function(){
        var actualFontHeight = $("html").css("font-size");
        var actualFontHeightNum = parseFloat(actualFontHeight, 10);
        var newFontHeight = actualFontHeightNum / 1.25;
        if (newFontHeight > minFontSize) {
            $("html").css("font-size", newFontHeight);
            $.cookie($cookieName, newFontHeight, { expires: 30 });
        }
        return false;
    });
    // invisible link for calculated font-size
    $("#initialFontSize").css("visibility", "hidden").css("font-size", 0).css("padding", 0).css("margin", 0).css("width",0);
    $("#initialFontSize").bind("click", function(){
        var $getSize = $.cookie($cookieName);
        var prevFontHeight = $getSize + ($getSize.indexOf("px") != -1 ? "" : "px");// IE fix for double "pxpx" error
        $("html").css("font-size", prevFontHeight);
        $.cookie($cookieName, prevFontHeight, { expires: 30 });
    });
    if ($.cookie($cookieName)) {
        $("#initialFontSize").click();// get cookie font-size
    }
    else {
        $("#resetFont").click();// reset font-size to resolution width calculated size 
    }
});
