﻿var baseFontSize = 0.89;

function h1Replacer(tweakMultiplier, kerning){
    var ctls = document.getElementsByTagName("h1");
            for (var i = 0; i < ctls.length; i++) { 
                var source = ctls[i];
                if (source != null){
                    //H1 Width
                    var width = source.offsetWidth;
                    
                    //H1 Content
                    var text = source.innerHTML.replace("&amp;","&"); 
                    
                    //H1 Font
                    var textFont = getCSSProp(source, "fontFamily");
                    if (textFont.indexOf(",") != -1)
                    {
                        textFont = textFont.slice(0,textFont.indexOf(","));
                    }
                    
                    //H1 Color
                    var textColor = RGB2HEX(getCSSProp(source, "color")); 
                    
                    //H1 Background Color
                    var backColor = RGB2HEX(getCSSProp(source, "backgroundColor"));
                    
                    //H1 Text Size
                    var textSize = getCSSProp(source, "fontSize");
                    textSize = textSize.replace("px","");
                    
                    textSize = percentToPoint(textSize, baseFontSize, tweakMultiplier);
                    
                    //H1 Text Weight
                    var textWeight = getCSSProp(source, "fontWeight");
                    if (textWeight == "700")
                    {
                        textWeight = "bold";
                    }
                    
                    //H1 Text Style (Italic?)
                    var textStyle = getCSSProp(source, "fontStyle");
                    
                    //H1 Leading (Line Height)
//                    var textLeading = getCSSProp(source, "lineHeight"); 
//                    
//                    if (textLeading.indexOf('px')>-1)
//                    {
//                        textLeading = textLeading.replace("px","");
//                        textLeading = textLeading * (72/96);
//                    }
//                    else if (textLeading.indexOf('pt')>-1)
//                    {
//                        textLeading = textLeading.replace("pt","");
//                    }

                    //H1 Kerning
                    var textKerning = kerning;
                    
                    //H1 Case
                    var textCase = getCSSProp(source, "textTransform");
                                                                     
                    /* new page fragment */
                    var fragment= document.createDocumentFragment();
                    var divCont = document.createElement("div");
                    divCont.setAttribute("class","h1Container");
                    
                    /* Clear Div */
                    var clearDiv = document.createElement("div");
                    clearDiv.style.clear = "both";
                    
                    /* Add In-line Style */
                    divCont.style.position = "relative";
                    divCont.style.overflow = "hidden";
                    divCont.style.padding = "0 0 3px 0";                    
                    
                    var newh1 = document.createElement("h1");
                    newh1.appendChild(document.createTextNode(text));
                    divCont.appendChild(newh1);
                    
                    var div = document.createElement("div");
                    div.id = "flashcontent"+i;
                    divCont.appendChild(div);
                    divCont.appendChild(clearDiv);
                    
                    fragment.appendChild(divCont);
                    
                    // Replace the source h1 with new h1 and div
                    source.parentNode.replaceChild(fragment, source);
          
                    // Load flash into new div
		            var fo = new SWFObject("/flash/h1Replacer2.swf", "h1Replacer", width, "1000", "7", "#ffffff");	
		            fo.addParam("wmode", "transparent");
		            fo.addVariable("myText", text.replace("&","[[ampersand]]"));
		            fo.addVariable("textColor", textColor);
		            fo.addVariable("backColor", backColor);
		            fo.addVariable("textWeight", textWeight);
		            fo.addVariable("textStyle", textStyle);
		            fo.addVariable("textSize", textSize);
		            fo.addVariable("textKerning", textKerning);
		            fo.addVariable("textCase", textCase);
		            fo.addVariable("textFont", textFont);
		            fo.addVariable("textWidth", width);
		            fo.write("flashcontent"+i);
		            
		            var rep = document.getElementById("h1Replacer");
		            if(rep!=null)
		            {
		                newh1.style.top="-1000px";
		                newh1.style.position="relative";
		            }
		        }
            }
}

function getCSSProp (element, prop) {
  if (element.style[prop]) {
    // inline style property
    return element.style[prop];
  } else if (element.currentStyle) {
    // external stylesheet for Explorer
    return element.currentStyle[prop];
  } else if (document.defaultView && document.defaultView.getComputedStyle) {
    // external stylesheet for Mozilla and Safari 1.3+
    prop = prop.replace(/([A-Z])/g,"-$1");
    prop = prop.toLowerCase();
    return document.defaultView.getComputedStyle(element,"").getPropertyValue(prop);
  } else {
    // Safari 1.2
    return null;
  }
}

function RGB2HEX(colour)
{
    var red;
    var green;
    var blue;
    
    try
    {
        if (colour.indexOf('rgba')>-1)
        {
            colour = colour.replace('rgba(','');
            colour = colour.replace(')','');
            
            var colours = new Array();
            colours = colour.split(",");
            
            if (colours[3] != 0)
            {
                red = colours[0].replace(" ", "");
                green = colours[1].replace(" ", "");
                blue = colours[2].replace(" ", "");
                colour = "0x"+ toHex(red) + toHex(green) + toHex(blue);
            }
            else
            {
                return "transparent";
            }
        }
        else if (colour.indexOf('rgb')>-1)
        {
            colour = colour.replace('rgb(','');
            colour = colour.replace(')','');
            
            var colours = new Array();
            colours = colour.split(",");
            
            red = colours[0].replace(" ", "");
            green = colours[1].replace(" ", "");
            blue = colours[2].replace(" ", "");
            colour = "0x"+ toHex(red) + toHex(green) + toHex(blue);
        }
        else if (colour.indexOf('#')>-1)
        {
            colour = colour.replace("#","0x");
        }
    }
    catch(e)
    {
        return "transparent";
    }
    
    return colour;
}

function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function percentToPoint(value, baseFontSize, tweakMultiplier)
{
    if (value.indexOf('%')>-1)
    {
        value = value.replace("%","");
        
        value = (value*0.01)*baseFontSize;  
        value = value / 0.0626;
        value = value * tweakMultiplier;
        //value = value * (72/96);
        return value;
    }
    else if (value.indexOf('em')>-1)
    {
        value = value.replace("em","");
        value = value * baseFontSize;
        value = value / 0.0625;
        value = value * tweakMultiplier;
        //value = value * (72/96);
        return value;
    }
    else
    {
        //value = value * (72/96);
        value = value * tweakMultiplier;
        return value;
    }
}




