// define String.trim() method 
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

String.method('trim', function (  ) {
    return this.replace(/^\s+|\s+$/g, '');
});

// this function will be called by the flash script
var getFontList = function(user_fonts) {
    var obj = document.getElementById('font_getter'),
        fonts;
    if (typeof(user_fonts) != 'undefined') {
        fonts = unescape(user_fonts);
    }
    if (fonts) match_fonts(fonts);
};

// backup function. getURL works well in Safari, Opera and Firefox, but poorly in IE */
var getFontListie6 = function(){
    var obj = document.getElementById('font_getter'),
        fonts;
    if (typeof(obj.GetVariable) != 'undefined') {
        fonts = obj.GetVariable('/:user_fonts');
    }
    if (fonts) match_fonts(fonts);
};

function match_fonts(fonts) {
  var haveMatch = false;
  var requestedFonts = getFontFamily(document.body).split(',');
  //alert(requestedFonts); 
  var installedFonts = fonts.split(',');
  //alert(installedFonts);
  for (var j=0; j<requestedFonts.length && !haveMatch; j++) {
    //alert("Trying: " + requestedFonts[j]);
    for (var i=0;i<installedFonts.length;i++){
      if ( installedFonts[i].toLowerCase().trim() === requestedFonts[j].toLowerCase().trim())
	{ haveMatch = true;  break; }	
    }
  }
  if (haveMatch) {
    document.getElementById('whatfont').src = "whatfont.php?font=" + installedFonts[i] /*@cc_on + "&jscript=" + @_jscript_version + "&build=" + @_jscript_build@*/;
  }
}

function getFontFamily(elm){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
	  strValue = document.defaultView.getComputedStyle(elm, null).fontFamily;
	}
	else if(elm.currentStyle){
	  strValue = elm.currentStyle.fontFamily;
	}
	// Safari&Chrome return: 'font' (first), Opera: "font" (first match)
	  return strValue.replace(/['"]{1}/gi, "");
}

