﻿var FontDetector = function(){
	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");
	d.appendChild(s);
	d.style.fontFamily = "serif";			//font for the parent element DIV.
	s.style.fontFamily = "serif";			//serif font used as a comparator.
	s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
	s.innerHTML        = "mmmmmmmmmmlil";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
	h.appendChild(d);
	var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
	var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
	h.removeChild(d);
	/* test
	 * params:
	 * font - name of the font you wish to detect
	 * return: 
	 * f[0] - Input font name.
	 * f[1] - Computed width.
	 * f[2] - Computed height.
	 * f[3] - Detected? (true/false).
	 */
	function debug(font) {
		h.appendChild(d);
		var f = [];
		f[0] = s.style.fontFamily = font;	// Name of the font
		f[1] = s.offsetWidth;				// Width
		f[2] = s.offsetHeight;				// Height
		h.removeChild(d);
		font = font.toLowerCase();
		if (font == "serif") {
			f[3] = true;	// to set arial and sans-serif true
		} else {
			f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
		}
		return f;
	}
	function test(font){
		f = debug(font);
		return f[3];
	}
	function getFonts(node){
		docBody = node.innerHTML;
		var usedFonts = [];
		regex = /font\-family[\s]*:[\s]*([\s\S]*?)[;|"|'|>]/ig;
		var matchStr;
		while (matchStr = regex.exec(docBody)){
			usedFonts.push(matchStr[1]);
		}
		
		usedFonts = uniq(usedFonts);

		var fontsRequire = [];
		for (var i=0;i<usedFonts.length;i++)	{
			if( !this.test(usedFonts[i]) ){ // font Not Install
				fontsRequire.push(usedFonts[i]);
			}
		}
		if(chkBrowserIE9() && typeof window.top.Editor == "undefined")
			fontsRequire = usedFonts;
		return fontsRequire;
	}
	function getFontString(node){
		return this.getFonts(node).join("|").replace(/ /g, "+");
	}
	this.detailedTest = debug;
	this.test = test;	
	this.getFontString = getFontString;
	this.getFonts = getFonts;
}
/* ======= functions ======= */
function uniq(arr){
	var arrUniq = new Array();
	for (var i=0;i<arr.length;i++)	{
		var xFlag = true;
		for (var j=0;j<arrUniq.length;j++) {
			if( arrUniq[j] == arr[i] ) {
				xFlag = false;
				continue;
			}else{
				xFlag = true;
			}
		}
		if(xFlag) {
			arrUniq.push(arr[i]);
		}
	}
	return arrUniq;
}

function createLinkTag(){
	var libPath = libPath||"http://static.webstarts.com/library/";
	var FontObj = new FontDetector();
	if( chkBrowserIEVer() )
		var fontArr = FontObj.getFonts(document.body);
	else{
		var fontArr = new Array();
		fontArr[0] = FontObj.getFontString(document.body);
	}
	
	var fontInterval = setInterval(function(){
		ele = document.createElement("LINK");
		ele.rel = "stylesheet";
		ele.type = "text/css";
		ele.href = libPath+"users/fonts/f.css?family="+fontArr.pop();
		
		document.documentElement.childNodes[0].appendChild(ele);
		if(fontArr.length==0)
			clearInterval(fontInterval);
	},100);
}

function chkBrowserIEVer(){
	var pattern = /MSIE [7|8][\d\w.]+/i;
	return pattern.test(navigator.userAgent);
}

function chkBrowserIE9(){
	var pattern = /MSIE 9[\d\w.]+/i;
	return pattern.test(navigator.userAgent);
}

if( typeof window.top.Editor == "undefined"){
	if (window.addEventListener) {
		window.addEventListener("load", createLinkTag, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", createLinkTag);
	}
}else{
	pNode = document.body;
	g=0;
	while( dd_menu = window.top.document.getElementById( "Menu_"+(g++) ) ){
		if(dd_menu.className == "Menu_FontName"){
			pNode = dd_menu;
			break;
		}
	}
	var FontObj = new FontDetector();
	var fontArr = FontObj.getFonts(pNode);
	window.top.Editor.requireFonts = fontArr;
}
/* ======= functions ======= */
