/**
 * 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)
 * Usage: (For more details visit 
 * This function can be called inside a $(document).ready()
 * Eg: fontSize("#controls", "#content", 9, 12, 20); 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
 * 9 - minimum font size
 * 12 - default font size
 * 20 - maximum font size
 * 
 */

function fontSize(container, target, minSize, maxSize) {
	
//	//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 small font button, font size is decreased by 1px
	$(container + " .smallFont").click(function(){ 	 
	
        applyFontSize(container, target, "-", minSize, maxSize)
        
//		updatefontCookie(target, newSize); //sets the cookie 
	});

	//on clicking large font size button, font size is incremented by 1 to the maximum limit
	$(container + " .largeFont").click(function(){
	
	    //alert($(target).css("font-size"));
	
	    applyFontSize(container, target, "+", minSize, maxSize)
		
//		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);
//	    } 
//    }

    function applyFontSize(container, target, action, minSize, maxSize)
    {   
        var curSize = parseInt($(target).css("font-size"));
		var newSize;
		
		if (action == "-")
		    newSize = curSize - 1;
		else 
		    newSize = curSize + 1;    
		    
		//alert(newSize);
		
		if (newSize >= minSize && newSize <= maxSize) {		    
            //$(target).css('font-size', newSize);
            applyTargetTagFontSize(target, action, minSize, maxSize);
            /*
            debugger;
            $(target).find("*").each(function(){
               
                applyTargetTagFontSize(this, action, minSize, maxSize);
            });    
            
            // Classes            
            applyTargetTagFontSize(target + " .gvDefaultRowAlignCenterVerticalBorder", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .gvDefaultHeaderAlignLeft", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .gvDefaultHeaderAlignCenterVerticalBorder", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .gvDefaultHeaderAlignCenter", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .gvDefaultHeaderAlignRight", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .gvDefaultRowAlignLeft", action, minSize, maxSize);           
            applyTargetTagFontSize(target + " .gvDefaultRowAlignCenter", action, minSize, maxSize);           
            applyTargetTagFontSize(target + " .gvDefaultRowAlignRight", action, minSize, maxSize);           
            applyTargetTagFontSize(target + " .AdvisorCalendarDefault", action, minSize, maxSize);
            applyTargetTagFontSize(target + " .subtitle_Section", action, minSize, maxSize);                        
            applyTargetTagFontSize(target + " .subtitle_small", action, minSize, maxSize);           
            applyTargetTagFontSize(target + " .text_Normal", action, minSize, maxSize);
            //applyTargetTagFontSize(target + " .title_FundLeftMenu", action, minSize, maxSize);           
            //applyTargetTagFontSize(target + " .newsPublishedDate", action, minSize, maxSize);   
            //applyTargetTagFontSize(target + " .text_Small", action, minSize, maxSize); 
            //applyTargetTagFontSize(target + " .title_Level2", action, minSize, maxSize); 
                        
                      
            // Tags   
            applyTargetTagFontSize(target + " p", action, minSize, maxSize);
            //applyTargetTagFontSize(target + " span", action, minSize, maxSize);
            applyTargetTagFontSize(target + " a", action, minSize, maxSize);            
            applyTargetTagFontSize(target + " input", action, minSize, maxSize);
            applyTargetTagFontSize(target + " select", action, minSize, maxSize);
            applyTargetTagFontSize(target + " h1", action, minSize, maxSize);
            applyTargetTagFontSize(target + " h2", action, minSize, maxSize);
            applyTargetTagFontSize(target + " h3", action, minSize, maxSize);
            applyTargetTagFontSize(target + " h4", action, minSize, maxSize);
            applyTargetTagFontSize(target + " h5", action, minSize, maxSize);                        */
        }
        
        if (action == "-" && newSize <= minSize) {
			$(container + " .smallFont").addClass("sdisabled");
		}
		if (action == "-" && newSize < maxSize) {
			$(container + " .largeFont").removeClass("ldisabled");
		}		
		if (action == "+" && newSize > minSize) {
			$(container + " .smallFont").removeClass("sdisabled");
		}
		if (action == "+" && newSize >= maxSize) {
			$(container + " .largeFont").addClass("ldisabled");
		}
    }
    
    function applyTargetTagFontSize(target, action, minSize, maxSize)
    {              
        if($(target).children().length > 0)
        {
            $(target).children().each(function(){
                applyTargetTagFontSize(this, action, minSize, maxSize)
            });
        }
        //debugger;
        var curSize = parseInt($(target).css("font-size"));
		var newSize;
		
		if(isNaN(curSize)){ return; }
		
		if (action == "-")
		    newSize = curSize - 1;
		else 
		    newSize = curSize + 1;
            
		$(target).css('font-size', newSize);    			
    }
}
