﻿// JScript File
//  For this script to work successfully when changing Class font sizes,
//  the class names for the font have to have to the font size in the class name.
//  For example a class could be names Black9CR where the font size for the class is 9.
//  To also prevent unwanted style changes, it would be beneficial to give the style a unique
//  letter code to search for to verify that you are only changing the classes you want to change.

//  Jeremy Kuhlman
//  Phase2 Interactive
//  10/17/2007


//What size is currently shown
var fontSize = "normal";

//Max and Min font size values
var fontMinSize = 7;
var fontMaxSize = 17;

//Set font to small size
function smallFont(){
     
    //Amount to change font by
    var sizeAmount;
    
    if (fontSize != "small"){
    
        //Detemine what font is currenlty displayed
        if (fontSize == "normal"){
            sizeAmount = 1;
        } else if (fontSize == "large"){
            sizeAmount = 2;
        }
        
        modifyClassFontSize("div", "decrease", sizeAmount, true, "CR");
        modifyClassFontSize("span", "decrease", sizeAmount, true, "CR");
        modifyClassFontSize("td", "decrease", sizeAmount, true, "CR");
        modifyClassFontSize("p", "decrease", sizeAmount, true, "CR");
        modifyClassFontSize("font", "decrease", sizeAmount, true, "CR");
        
        modifyStyleFontSize("p", "decrease", sizeAmount);
        modifyStyleFontSize("font", "decrease", sizeAmount);
        
        fontSize = "small";
    }
}

//Set font to normal size
function normalFont(){

    if (fontSize != "normal"){
        var fontDirection;
        
        if (fontSize == "small"){
            fontDirection = "increase";
        } else if (fontSize == "large") {
            fontDirection = "decrease";
        }
        
        //Detemine what font is currenlty displayed
        sizeAmount = 1;
        
        modifyClassFontSize("div", fontDirection, sizeAmount, true, "CR");
        modifyClassFontSize("span", fontDirection, sizeAmount, true, "CR");
        modifyClassFontSize("td", fontDirection, sizeAmount, true, "CR");
        modifyClassFontSize("p", fontDirection, sizeAmount, true, "CR");
        modifyClassFontSize("font", fontDirection, sizeAmount, true, "CR");
        
        modifyStyleFontSize("p", "increase", sizeAmount);
        modifyStyleFontSize("font", "increase", sizeAmount);
        
        fontSize = "normal";
    }
}

//Set font to large size
function largeFont(){

    if (fontSize != "large"){
    
        //Detemine what font is currenlty displayed
        if (fontSize == "small"){
            sizeAmount = 2;
        } else if (fontSize == "normal"){
            sizeAmount = 1;
        }
         
        modifyClassFontSize("div", "increase", sizeAmount, true, "CR");
        modifyClassFontSize("span", "increase", sizeAmount, true, "CR");
        modifyClassFontSize("td", "increase", sizeAmount, true, "CR");
        modifyClassFontSize("p", "increase", sizeAmount, true, "CR");
        modifyClassFontSize("font", "increase", sizeAmount, true, "CR");
        
        modifyStyleFontSize("p", "increase", sizeAmount);
        modifyStyleFontSize("font", "increase", sizeAmount);
        
        fontSize = "large";
    }
}
    
//Modify all font sizes that are declared through a class reference
function modifyClassFontSize(Tag, Direction, Amount, useLetterCode, letterCode) {

    //Tag to find
    var htmlTag = document.getElementsByTagName(Tag);
    
    for(i = 0; i < htmlTag.length; i++) {
        
        var tagClass;
        tagClass = htmlTag[i].className;
        
        //Check to see if the letterCode is in this class
        if ((tagClass.indexOf(letterCode) >= 0) && (useLetterCode == true)) {
            //Get the number out of the Class
            var fontCurrentSize = tagClass.match(/[\d\.]+/g);
            var newFontSize;
            
            //Modify the font size
            if (Direction == "increase"){
                if(fontCurrentSize < fontMaxSize) {
                    newFontSize = parseInt(fontCurrentSize) + Amount;
                } else {
                    newFontSize = fontMaxSize;
                }
            } else if (Direction == "decrease"){
                if(fontCurrentSize > fontMinSize) {
                    newFontSize = parseInt(fontCurrentSize) - Amount;
                } else {
                    newFontSize = fontMinSize;
                } 
            }

            //Update the class name
            tagClass = tagClass.replace(fontCurrentSize, newFontSize);
            htmlTag[i].className = tagClass;
        }
    }
}

//Modify all font sizes declared inline throught an attribute or style
function modifyStyleFontSize(Tag, Direction, Amount) {
    //Tag to find
   var htmlTag = document.getElementsByTagName('Tag');
   
   var fontCurrentSize;
   
   for(i = 0; i < htmlTag.length; i++) {
        if(htmlTag[i].style.fontSize) {
            fontCurrentSize = parseInt(htmlTag[i].style.fontSize.replace("px",""));
        }
      
        //Modify the font size
        if (Direction == "increase"){
            if(fontCurrentSize < fontMaxSize) {
                 fontCurrentSize += Amount;
            } else {
                    newFontSize = fontMaxSize;
                }  
        } else if (Direction == "decrease"){
            if(fontCurrentSize > fontMinSize) {
                 fontCurrentSize -= Amount;
            } else {
                    newFontSize = fontMinSize;
                } 
        }
      
        htmlTag[i].style.fontSize = fontCurrentSize + "px"
   }
}
    
    
