/*
** musaul/lab : main.js
** v0.0.1
** $Revision: 1 $
** $Date: 2008-02-11 01:25:34 -0800 (Mon, 11 Feb 2008) $
** Copyright 2009 Musaul Karim. All rights reserved
** http://musaul.net/
*/


//globals
var g_fontSelector;
var g_fontSamples;
var g_elInfo;
var g_elInfoButton;
var g_elSampleText;
var g_elOrientation;


function GetClassStr()
{
	var tmpEl = document.getElementById('fontfamily1');
	var tmpStr = tmpEl.getAttribute('className');
	var strClass = (null === tmpStr) ? 'class' : 'className';
	return strClass;
}


function CFontSample()
{
	this.elFontHolder = null;
	this.elSample = null;
	this.elFamily = null;
	this.elSize = null;
	this.elSzUnit = null;
	this.elColour = null;
	this.elBold = null;
	this.elItalic = null;
	this.isBold = false;
	this.isItalic = false;
}

function UpdateSample(idx)
{
	var src = g_fontSamples[idx];
	var targ = g_fontSamples[idx].elSample;

	targ.style.fontFamily = '\'' + src.elFamily.value + '\'';
	targ.style.fontSize = src.elSize.value + src.elSzUnit.value;
	targ.style.fontWeight = src.isBold ? 'bold' : 'normal';
	targ.style.fontStyle = src.isItalic ? 'italic' : 'normal';
	targ.style.color = src.elColour.style.color;
	targ.style.backgroundColor = src.elColour.style.backgroundColor;
	targ.innerHTML = g_elSampleText.value.replace(/\n/g, '<br/>');
}

function UpdateBothSamples()
{
	UpdateSample(0);
	UpdateSample(1);
}


function SetToggleButton(btn, sel)
{
	var cls = GetClassStr();

	if (sel)
	{
		btn.setAttribute(cls, 'formatbtnSel');
	}
	else
	{
		btn.setAttribute(cls, 'formatbtn');
	}
}

function ToggleBold(idx)
{
	g_fontSamples[idx].isBold = !g_fontSamples[idx].isBold;
	SetToggleButton(g_fontSamples[idx].elBold, g_fontSamples[idx].isBold);
}

function ToggleItalics(idx)
{
	g_fontSamples[idx].isItalic = !g_fontSamples[idx].isItalic;
	SetToggleButton(g_fontSamples[idx].elItalic, g_fontSamples[idx].isItalic);
}


function SetColour(idx)
{
	alert("Colour selection not yet implemented.");
}


function SetLayout(l)
{
	var cls = GetClassStr();

	if (l==0)
	{
		g_fontSamples[0].elFontHolder.style.cssFloat = "left";
		g_fontSamples[1].elFontHolder.style.cssFloat = "left";
		g_fontSamples[0].elFontHolder.style.width = "50%";
		g_fontSamples[1].elFontHolder.style.width = "50%";
		g_elOrientation[0].setAttribute(cls, 'orientationSel');
		g_elOrientation[1].setAttribute(cls, 'orientation');
	}
	else
	{
		g_fontSamples[0].elFontHolder.style.cssFloat = "none";
		g_fontSamples[1].elFontHolder.style.cssFloat = "none";
		g_fontSamples[0].elFontHolder.style.width = "100%";
		g_fontSamples[1].elFontHolder.style.width = "100%";
		g_elOrientation[1].setAttribute(cls, 'orientationSel');
		g_elOrientation[0].setAttribute(cls, 'orientation');
	}
}

function ShowFontSelector(src)
{
	g_fontSelector.Show(src);
}

function ChangeFontAccept()
{
	if (g_fontSelector.selectedFontName.length > 0)
	{
		document.getElementById(g_fontSelector.oldFontEl).value = g_fontSelector.selectedFontName;
	}
	g_fontSelector.Hide();
}

function ChangeFontCancel()
{
	g_fontSelector.Hide();
}


function ShowInfo()
{
	g_elInfo.style.display = 'block';
	g_elInfoButton.setAttribute('onclick', 'HideInfo()');
}

function HideInfo()
{
	g_elInfo.style.display = 'none';
	g_elInfoButton.setAttribute('onclick', 'ShowInfo()');
}


function StartApp()
{
	g_fontSelector = new FrmFontSelector();
	g_elInfo = document.getElementById('info');
	g_elInfoButton = document.getElementById('btnAbout');
	g_elSampleText = document.getElementById('textcontent');

	g_fontSamples = new Array;
	g_fontSamples.push(new CFontSample);
	g_fontSamples.push(new CFontSample);

	g_fontSamples[0].elFontHolder = document.getElementById('fontholder1');
	g_fontSamples[0].elSample = document.getElementById('fontsample1');
	g_fontSamples[0].elFamily = document.getElementById('fontfamily1');
	g_fontSamples[0].elSize = document.getElementById('fontsize1');
	g_fontSamples[0].elSzUnit = document.getElementById('fontunit1');
	g_fontSamples[0].elColour = document.getElementById('btnFont1Colour');
	g_fontSamples[0].elBold = document.getElementById('tglFont1Bold');
	g_fontSamples[0].elItalic = document.getElementById('tglFont1Italic');

	g_fontSamples[1].elFontHolder = document.getElementById('fontholder2');
	g_fontSamples[1].elSample = document.getElementById('fontsample2');
	g_fontSamples[1].elFamily = document.getElementById('fontfamily2');
	g_fontSamples[1].elSize = document.getElementById('fontsize2');
	g_fontSamples[1].elSzUnit = document.getElementById('fontunit2');
	g_fontSamples[1].elColour = document.getElementById('btnFont2Colour');
	g_fontSamples[1].elBold = document.getElementById('tglFont2Bold');
	g_fontSamples[1].elItalic = document.getElementById('tglFont2Italic');

	g_elOrientation = new Array;
	g_elOrientation.push( document.getElementById('horizPanes') );
	g_elOrientation.push( document.getElementById('vertPanes') );
}


function PageReadyHandler()
{
    StartApp();
}

window.onload=PageReadyHandler;
