// ==========
//  my font
// ==========

if ("undefined" == typeof myFont)
{
	myFont = {
		init : function(callbackFunc) {
			myFont.list = new Array();
			myFont.totalList = new Array();
			myFont.selectedFontFamily = "";
			myFont.selectedSize = "";
			myFont.hasBase = false;
			myFont.baseFontFamily = "";
			myFont.baseSize = "";
			myFont.callbackFunc = callbackFunc;
			myFont.getList();
		},
		getList : function() {
			var url = _staticUrl+"/ajax/json/common/myinfo/getUseFont";
			$.getJSON(url,{},function(data) {
				if (data.result && data.result.myFont)
				{
					var fontlist = data.result.myFont;
					var fontFamily = "";
					for (var key in fontlist)
					{
						fontFamily = fontlist[key].filename.split("_")[2];
						myFont.set(fontlist[key].title,fontlist[key].filename,fontFamily,fontlist[key].arrFont);
						if (fontlist[key].base_font > 0)
						{
							myFont.hasBase = true;
							myFont.baseFontFamily = fontFamily;
							myFont.baseSize = fontlist[key].base_font;
						} 
					}
					myFont.selectFontFamily(myFont.baseFontFamily);
					myFont.selectSize(myFont.baseSize);
				}
				myFont.callback();								
			});
		},
		set : function(title,filename,fontFamily,arrFont) {
			if (!myFont.exist(fontFamily))
			{
				var obj = new Object();
				obj.title = title;
				obj.filename = filename;
				obj.fontFamily = fontFamily;
				obj.arrFont = arrFont;
				obj.size = 9;
				obj.isGetEot = false;
				myFont.list[fontFamily] = obj;
				
				for (var key in arrFont)
				{
					myFont.setCss(fontFamily,key);
				}				
			}
		},
		setTotal : function(fontFamily,size){			
			if(!myFont.existTotal(fontFamily+"_"+size))
			{				
				myFont.totalList[fontFamily+"_"+size] = fontFamily+"_"+size;
			}
		},
		existTotal : function(totalKey){
			if (typeof(myFont.totalList[totalKey]) != "undefined")
				return true;
			return false;
		},
		get : function() {
			if (myFont.selectedFontFamily && myFont.exist(myFont.selectedFontFamily))
			{
				myFont.list[myFont.selectedFontFamily].size = myFont.selectedSize;
				return myFont.list[myFont.selectedFontFamily];
			}
			return false;
		},
		getBase : function() {
			if (myFont.baseFontFamily)
			{
				myFont.list[myFont.baseFontFamily].size = myFont.baseSize;
				return myFont.list[myFont.baseFontFamily];
			}
			return false;
		},
		getAll : function() {
			return myFont.list;
		},
		getCnt : function() {
			var i=0;
			for (var key in myFont.list)
				i++;
			return i;
		},
		getHasBase : function() {
			return myFont.hasBase;
		},		
		getSelectedFontFamily : function() {
			return myFont.selectedFontFamily;
		},
		getSelectedSize : function() {
			return myFont.selectedSize;
		},
		getBaseFontFamily : function() {
			return myFont.baseFontFamily;
		},
		getBaseSize : function() {
			return myFont.baseSize;
		},
		exist : function(fontFamily) {
			if (typeof(myFont.list[fontFamily]) != "undefined")
				return true;
			return false;
		},
		selectFontFamily : function(fontFamily) {
			myFont.selectedFontFamily = fontFamily;						
		},
		selectSize : function(size) {
			myFont.selectedSize = size;
		},
		callback : function() {
			if (myFont.callbackFunc) {
				if(typeof(myFont.callbackFunc) == 'function') myFont.callbackFunc();
				else eval(myFont.callbackFunc+"();");
			}
		},
		setCss : function(fontFamily,size) {
			var css = "<style>@font-face {";
			css += "font-family: "+fontFamily+"_"+size+"; font-style:  normal; font-weight: normal; ";
			css += "src: url(http://file.sayclub.co.kr/charimg/font/m_f_"+fontFamily+"_"+size+".eot); }</style>";
			$("head").append(css);			
			
			myFont.setTotal(fontFamily,size);
		},
		setStyleAtElement : function(el) {
			var font = myFont.get();
			if (font)
			{
				el.css("fontFamily",font.fontFamily+"_"+font.size);
				el.css("fontSize",font.size+"pt");
			}
			else
			{
				el.css("fontFamily",myFont.selectedFontFamily);
				el.css("fontSize",myFont.selectedSize+"pt");
			}
		},
		setStyleDirect : function(el,fontFamily,size)
		{
			if (!fontFamily || !size) return false;
			if (!myFont.exist(fontFamily))
				myFont.setCss(fontFamily,size);
			el.css("fontFamily",fontFamily);
			el.css("fontSize",size+"pt");
		},
		encode : function(msg) {
			var font = myFont.get();
			if (font)
				var tag = "--MYFONT "+font.fontFamily+"_"+font.size+"--";
			else
				var tag = "--NORMALFONT "+myFont.selectedFontFamily+"_"+myFont.selectedSize+"--";
			return tag+msg;
		},
		decode : function(msg) {
			if (msg.search("--MYFONT") != -1)
			{			
				var temp = msg.split("--MYFONT ")[1].split("--");
				var fontFamily = temp[0].split("_")[0];
				var size = temp[0].split("_")[1];
				msg = msg.substr(msg.indexOf("--",8) + 2);
				if (!myFont.existTotal(fontFamily+"_"+size))
				{
					myFont.setCss(fontFamily,size);
				}
				return "<font style=\"font-family:"+fontFamily+"_"+size+"; font-size:"+size+"pt;\">"+msg+"</font>";
			}
			else if (msg.search("--NORMALFONT") != -1)
			{
				var temp = msg.split("--NORMALFONT ")[1].split("--");
				var fontFamily = temp[0].split("_")[0];
				var size = temp[0].split("_")[1];
				msg = msg.substr(msg.indexOf("--",12) + 2);
				return "<font style=\"font-family:"+fontFamily+"; font-size:"+size+"pt;\">"+msg+"</font>";
			}
			return msg;
		}
	}
}