/*
** Ad hoc js for fonts:font-picker
** v0.2
** $Revision: 2 $
** $Date: 2008-07-22 02:09:14 +0100 (Tue, 22 Jul 2008) $
** Copyright 2008 Musaul Karim. All rights reserved 
** http://mk.vftw.com
*/


function FrmFontSelector()
{
	var tmpEl = document.getElementById('form');
	var tmpStr = tmpEl.getAttribute('className');
	this.strClass = null === tmpStr ? 'class' : 'className';

	this.defaultSampleText = 'A Quick Brown Fox Jumped Over The Lazy Dog.';
	this.selectedFontName = "";
	this.selectedFont = -1;

	this.fontList = 0;
	this.familyFontIndex = 0;
	this.foundryFontIndex = 0;
	this.availFontIndex = 0;

	this.oldFontEl = 0;

	this.elFontFamily = document.getElementById('selFontFamily');
	this.elFontAvailabilty = document.getElementById('selFontAvail');
	this.elFontSize = document.getElementById('selFontSize');

	this.LoadFonts();
}


FrmFontSelector.prototype.PopulateFonts = function()
{
	// reset the selection details
	this.selectedFontName = "";
	this.selectedFont = -1;

	// get the chosen options
	var family = this.elFontFamily.value;
	var avail = this.elFontAvailabilty.value;
	var famFonts = this.familyFontIndex[family];
	var avlFonts = this.availFontIndex[avail];

	// prepare the font checker
	var fc = document.getElementById("fontChecker");
	fc.style.display="inline";
	fc.innerHTML = "Test Text.";


	// Populate the font list
	//
	var fontPane = document.getElementById('fontpane');
	var fonts = '';
	for (var i=0; i<this.fontList.length; ++i)
	{
		var thefont = this.fontList[i];

		if (  ( family == -1 || famFonts[1].find(i, 0) != -1 )
		   && ( avail == -1 || avlFonts[1].find(i, 0) != -1 )
		   )
		{
			var fontPresent = IsFontPresent(thefont[0]);
			fonts += '<div class="fontentry" id="fontentry_';
			fonts += i;
			fonts += '" onclick="FrmFontSelector_SetSelectedFont(';
			fonts += i;
			fonts += ', \'' + thefont[0] + '\'';
			fonts += ')"';
			fonts += ( fontPresent ? '' : ' title="This font may not be available in this computer."' );
			fonts += '><div class="fontname">';
			fonts += thefont[0];
			fonts += ( fontPresent ? '' : '<strong style="color:red">*</strong>' );
			fonts += '</div><div class="fontsample" style="font:';
			fonts += this.elFontSize.value;
			fonts += 'px \'';
			fonts += thefont[0];
			fonts += '\';">';
			fonts += this.defaultSampleText;
			fonts += '</div></div>';
		}
	}

	fontPane.innerHTML = fonts;

	// reset the font checker
	fc.style.display="none";
}


FrmFontSelector.prototype.Show = function(oldFontEl)
{
	this.oldFontEl = oldFontEl;

	this.elFontFamily.value = -1;
	this.elFontAvailabilty.value = -1;

	this.PopulateFonts();

	document.getElementById('sneezeguard').style.display = 'block';
	document.getElementById('fontpickerdlg').style.display = 'block';
	document.getElementsByTagName('body')[0].style.overflow = "hidden";
}

FrmFontSelector.prototype.Hide = function()
{
	document.getElementById('fontpickerdlg').style.display = 'none';
	document.getElementById('sneezeguard').style.display = 'none';
	document.getElementsByTagName('body')[0].style.overflow = "auto";
}

FrmFontSelector.prototype.SetSelectedFont = function(idx, family)
{
	if (-1 != this.selectedFont)
	{
		var elFontEntry;
		elFontEntry = document.getElementById('fontentry_' + this.selectedFont);
		elFontEntry.setAttribute(this.strClass,"fontentry");
	}
	
	this.selectedFont = idx;
	this.selectedFontName = family;
	elFontEntry = document.getElementById('fontentry_' + this.selectedFont);
	elFontEntry.setAttribute(this.strClass,"fontentrySelected");
}

FrmFontSelector.prototype.ParseDataBlock = function(dataBlockId)
{
	var outArray = new Array();
	var outcount = 0;

	var src = document.getElementById(dataBlockId);
	if (!src)
	{
		throw "DataBlock '" + dataBlockId + "' does not exist!";
	}

	var content = src.textContent || src.innerText;
	var strList = content.split('¦');
	for (var i = 0; i < strList.length; ++i)
	{
		var str = strList[i].trim();
		if (str.length > 0)
		{
			var cols = str.split('|');
			var colCount = cols.length;

			if (colCount > 0)
			{
				outArray[outcount] = new Array();
				for (j = 0; j < colCount; ++j)
				{
					outArray[outcount][j] = cols[j].trim();
				}
				++outcount;
			}
		}
	}

        //alert(dataBlockId + ".size=" + outArray.length);
	return outArray;
}


FrmFontSelector.prototype.CreateIndex = function(dataBlockId)
{
	var index = new Array();
	var keyblock = this.ParseDataBlock(dataBlockId);

	for (var i = 0; i < keyblock.length; ++i)
	{
		var fi = keyblock[i][0];
		index[fi] = new Array();
		index[fi][0] = keyblock[i][1];
		index[fi][1] = new Array();
	}

	return index;
}


FrmFontSelector.prototype.LoadFonts = function()
{
	// Create indeces
	this.familyFontIndex = this.CreateIndex('data_fontfamilies');
	this.foundryFontIndex = this.CreateIndex('data_fontfoundries');
	this.availFontIndex = this.CreateIndex('data_fontavail');


	// Populate drop-down boxes
	//
	for (var i = 0; i < this.familyFontIndex.length; ++i)
	{
		if (this.familyFontIndex[i].length > 0)
		{
			this.elFontFamily[this.elFontFamily.options.length] = new Option(this.familyFontIndex[i][0], i, false, false);
		}
	}
	for (var i = 0; i < this.availFontIndex.length; ++i)
	{
		if (this.availFontIndex[i] != null)
		{
			this.elFontAvailabilty.options[this.elFontAvailabilty.options.length] = new Option(this.availFontIndex[i][0], i, false, false);
		}
	}
	

	var indeces = new Array();
	indeces[0] = this.familyFontIndex;
	indeces[1] = this.foundryFontIndex;
	indeces[2] = this.availFontIndex;

	// Load Font Families
	this.fontList = this.ParseDataBlock('data_fontlist');
	for (var i = 0; i < this.fontList.length; ++i)
	{
		for (idx = 0; idx < 3; ++idx)
		{
			var fld = idx + 1;
			
			// Poulate the index
			var fontIndex = indeces[idx];
			var keys = this.fontList[i][fld].split(',');
			for (ki = 0; ki < keys.length; ++ki)
			{
				var key = keys[ki].trim();
				if (key.length > 0)
				{
					fontIndex[key][1].push(i);
				}
			}
		}
	}
}


function FrmFontSelector_SetSelectedFont(idx, family)
{
	g_fontSelector.SetSelectedFont(idx, family);
}


function FrmFontSelector_RefreshFontList()
{
	g_fontSelector.PopulateFonts();
}




function IsFontPresent(fontName)
{
	if (fontName.indexOf(" ") > -1)
	{
		fontName = "'" + fontName + "'";
	}

	var fs = document.getElementById("fontChecker");

	fs.style.fontFamily = fontName + ", serif";
	var width = fs.offsetWidth;

	fs.style.fontFamily = fontName + ", sans-serif";
	if (fs.offsetWidth != width)
		return false;

	fs.style.fontFamily = fontName + ", cursive";
	if (fs.offsetWidth != width)
		return false;

	fs.style.fontFamily = fontName + ", fantasy";
	if (fs.offsetWidth != width)
		return false;

	fs.style.fontFamily = fontName + ", monospace";
	if (fs.offsetWidth != width)
		return false;

	return true;
}
