/*   ________________________
 *  |    ____  ____  ____    |	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 = 10; // initial font-size in px
    var minFontSize = 10; // minimum font-size in px
    var maxFontSize = 28; // maximum font-size in px
    var siteName = "DAF_Dealer"; // site name for the cookie
    var eParent = "#menu"; // FATHER IDENTIFIER inside I want the [-a a a+] box (body,div:first,etc.)
    var eSon = "li:first";// 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", "relative")
			.css("top", 0)
			.css("z-index",999)
		// format the [-a a a+] box container
			.css("width","auto")
			.css("height","auto")
			.css("margin", "0 auto")
			.css("background-color", "#946244")
			.css("text-align","center")
			.css("border", 1)
			.css("border-style", "solid")
			.css("border-color", "#996666");
    // 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","black")
			.css("background-color", "#F5E0BC")
			.css("border", 1)
			.css("border-style", "solid")
			.css("border-color", "#996666");*/
    
/* 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
    	}
    }
/* Not change nothing from here down ====================================================================================================================================================================================================== */
   // $('body').css("font-size", "62.5%");// stands font-size 10px=1em
    
	var ThingToChange = '#TextAreaEdit ,#TextAreaEdit a, #TextAreaEdit p, #TextAreaEdit ul, #TextAreaEdit li, #TextAreaEdit span, .MiddleSectionResult, .MiddleSectionResult a , .MiddleSectionResult span, .MiddleSectionResult p, .MiddleSectionResultTitle, .MiddleSectionResultDate, .MiddleSectionResultText, .MiddleSectionOffer, .MiddleSectionOfferDate , .MiddleSectionOfferTitle a, .MiddleSectionOfferTitle, .MiddleSectionOfferText, .ContactDetails, .ContactDetails p, .ContactDetails span, .ContactDetails a, #ForSaleTop, #ForSaleLeft, #ForSaleRight, #SpecificationDiv, .RowLeft, .RowRight'
	// 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);
			
			//console.log($getSize);
			
        	$(ThingToChange).css( 'fontSize', $getSize + ($getSize.indexOf("px") != -1 ? "" : "px") ); 
    	}
    	else {
        	$.cookie($cookieName, fontHeight, { path:'/' ,  expires: 30 });
    	}
    // reset link 
    /*$("#resetFont").bind("click", function(){
        $("html").css("font-size", fontHeight);
        $.cookie($cookieName, fontHeight, { expires: 30 });
    });*/
    // increase link
    $("#linkLarge").bind("click", function(){
        var actualFontHeight = $(ThingToChange).css("font-size");
        var actualFontHeightNum = parseFloat(actualFontHeight, 10);
        var newFontHeight = actualFontHeightNum * 1.1;
        if (newFontHeight < maxFontSize) {
            $(ThingToChange).css("font-size", newFontHeight);
            $.cookie($cookieName, newFontHeight, { path:'/' , expires: 30 });
        }
        return false;
    });
    // decrease link
    $("#linkSmall").bind("click", function(){
        var actualFontHeight = $(ThingToChange).css("font-size");
        var actualFontHeightNum = parseFloat(actualFontHeight, 10);
        var newFontHeight = actualFontHeightNum * 0.9;
        if (newFontHeight > minFontSize) {
            $(ThingToChange).css("font-size", newFontHeight);
            $.cookie($cookieName, newFontHeight, {   path:'/' , 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
        $(ThingToChange).css("font-size", prevFontHeight);
        $.cookie($cookieName, prevFontHeight, {  path:'/' , expires: 30 });
    });
    if ($.cookie($cookieName)) {
        $("#initialFontSize").click();// get cookie font-size
    }
    else {
        $("#resetFont").click();// reset font-size to resolution width calculated size 
    }
});
