// display.js | ver 1.0
// JavaScript Font Format Setter
//
// Originally inspired by the persistant font settings in sites such as
// FanFiction.net and FanficAuthors.net
//
// Programming resources:
// The free tutorials available at W3Schools
// http://www.w3schools.com/
// The how-to at Alternative Style: Working With Alternate Style Sheets by Paul Sowden
// http://www.alistapart.com/stories/alternate/
// Browser compatablility issues from QuirksMode by Peter-Paul Koch
// http://www.quirksmode.org/
// The getElementById workaround from:
// The JavaScript Source!! http://javascript.internet.com
// Created by: Ultimater :: http://ultimiacian.tripod.com/
// And many, many thanks go to Google for performing so many, many searches!
// http://www.google.com/


// this first part isn't a function; it runs as soon as this file loads
// downside: page display is held up until this javascript module loads

  var today=new Date();
  var EndDate=new Date(today.getTime()+730*24*60*60*1000);
  var FontCode=7;                        // default font-face code for new visitors
  var FontName=new Array(9);             // array that will hold available font names
  var FontSize='medium';                 // default font-size for new visitors
  var TxtWidth=95;                       // default width for new visitors
  var PageStyle='Amber';                 // default style for new visitors
  var BackColor='#110000';
  var ForeColor='#cccc00';
  var DefaultBackColor='#110000';
  var DefaultForeColor='#cccc00';

// the next three lines check to see if the variables are already stored in local cookies
// if they are, then those values are used instead

  if(parseInt(GetCookie('TheBearFontSize'))>0){FontSize=GetCookie('TheBearFontSize');};
  if(parseInt(GetCookie('TheBearFontCode'))>0){FontCode=GetCookie('TheBearFontCode');};
  if(parseInt(GetCookie('TheBearTxtWidth'))>0){TxtWidth=GetCookie('TheBearTxtWidth');};
  if(GetCookie('TheBearBackColor').length>0){BackColor=GetCookie('TheBearBackColor');};
  if(GetCookie('TheBearForeColor').length>0){ForeColor=GetCookie('TheBearForeColor');};
  if(GetCookie('TheBearDefaultBackColor').length>0)
    {DefaultBackColor=GetCookie('TheBearDefaultBackColor');};
  if(GetCookie('TheBearDefaultForeColor').length>0)
    {DefaultForeColor=GetCookie('TheBearDefaultForeColor');};

  setCurrentBackColor(BackColor);
  setCurrentForeColor(ForeColor);

// the selects and buttons are written in JavaScript because they require JavaScript to work
// if the browser can't use JavaScript, no useless controls are displayed
// they won't even see a blank space showing that controls are missing

  document.write("<div id='format'>");
  document.write("<div class='ftxt'>Display and <a href='print.html' onClick='return popup()'>Print</a> Settings</div>");
  document.write("<div class='fsel'>");
  WriteFontChanger();
  document.write("<select id='backchanger' onChange=setCurrentBackColor(value)>");
  ColorSelector(BackColor,DefaultBackColor,'Back');
  document.write("</select>");
  document.write("<select id='forechanger' onChange=setCurrentForeColor(value)>");
  ColorSelector(ForeColor,DefaultForeColor,'Text');
  document.write("</select>");
  WriteWidthSelector();
  document.write("</div>");
  document.write("</div>");


// end of the execute-on-load segment
// below are functions


function WriteWidthSelector() // the width options only are written to the HTML
{ var i,sel;
document.write("<select id='widthchanger' onChange=setCurrentWidth(value)>");
  for(i=40;i<=100;i=i+5)
   {sel='';
    if(TxtWidth==i){sel=' selected="selected"'};
    document.write("<option value='"+i+"'"+sel+">Width "+i+"% ");
  };
document.write("</select>");
};

function WriteFontChanger()   // this writes the font-size buttons and font-face selector
{ var i,sel;
document.write("<select id='sizechanger' onChange=setCurrentSize(value)>");
if(FontSize=='medium'){document.write("<option value='medium' selected='selected'>Default ")};
  for(i=6;i<=30;i++)
   {
    sel='';
    if(FontSize==i){sel=' selected="selected"'};
    document.write("<option value='"+i+"'"+sel+">"+i+"px Font ");
   };
document.write("</select>");

document.write("<select id='fontchanger' onChange=setCurrentFontCode(value)>");

i=2;sel='';
FontName[i]='\'Comic Sans MS\',cursive';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Comic Sans ");

i=8;sel='';
FontName[i]='Corbel,sans-serif';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Corbel ");

i=7;sel='';
FontName[i]='Eurostile,Eurostile-Roman-DTC,Arial,Helvetica,sans-serif';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Eurostile ");

i=3;sel='';
FontName[i]='Consolas,\'Lucida Console\',\'Courier New\',courier,monospace';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Fixed Width ");

i=1;sel='';
FontName[i]='Calibri,Arial,Helvetica,sans-serif';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Sans-Serif ");

i=5;sel='';
FontName[i]='Cambria,\'Times New Roman\',times,serif';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Serif ");

i=6;sel='';
FontName[i]='Verdana,sans-serif';
if(FontCode==i){sel=' selected="selected"'};
document.write("<option value='"+i+"'"+sel+">Verdana ");

document.write("</select>");
};


function ColorSelector(value,defaultcolor,tag) // the style options are written to the HTML
{ var sel;
sel='';
if(value==defaultcolor){sel=' selected="selected"'};
document.write("<option value='default'"+sel+">Default "+tag+" ");
sel='';
if(value=='aqua'){sel=' selected="selected"'};
document.write("<option value='aqua'"+sel+">Aqua "+tag+" ");
sel='';
if(value=='black'){sel=' selected="selected"'};
document.write("<option value=black"+sel+">Black "+tag+" ");
sel='';
if(value=='blue'){sel=' selected="selected"'};
document.write("<option value='blue'"+sel+">Blue "+tag+" ");
sel='';
if(value=='fuchsia'){sel=' selected="selected"'};
document.write("<option value='fuchsia'"+sel+">Fuchsia "+tag+" ");
sel='';
if(value=='gray'){sel=' selected="selected"'};
document.write("<option value='gray'"+sel+">Grey "+tag+" ");
sel='';
if(value=='green'){sel=' selected="selected"'};
document.write("<option value='green'"+sel+">Green "+tag+" ");
sel='';
if(value=='lime'){sel=' selected="selected"'};
document.write("<option value='lime'"+sel+">Lime "+tag+" ");
sel='';
if(value=='maroon'){sel=' selected="selected"'};
document.write("<option value='maroon'"+sel+">Maroon "+tag+" ");
sel='';
if(value=='navy'){sel=' selected="selected"'};
document.write("<option value='navy'"+sel+">Navy "+tag+" ");
sel='';
if(value=='olive'){sel=' selected="selected"'};
document.write("<option value='olive'"+sel+">Olive "+tag+" ");
sel='';
if(value=='purple'){sel=' selected="selected"'};
document.write("<option value='purple'"+sel+">Purple "+tag+" ");
sel='';
if(value=='red'){sel=' selected="selected"'};
document.write("<option value='red'"+sel+">Red "+tag+" ");
sel='';
if(value=='silver'){sel=' selected="selected"'};
document.write("<option value='silver'"+sel+">Silver "+tag+" ");
sel='';
if(value=='teal'){sel=' selected="selected"'};
document.write("<option value='teal'"+sel+">Teal "+tag+" ");
sel='';
if(value=='white'){sel=' selected="selected"'};
document.write("<option value='white'"+sel+">White "+tag+" ");
sel='';
if(value=='yellow'){sel=' selected="selected"'};
document.write("<option value='yellow'"+sel+">Yellow "+tag+" ");
};

function GetCookieValue(offset)
{
  var endstr=document.cookie.indexOf(";",offset);
  if(endstr==-1){endstr=document.cookie.length;};
return unescape(document.cookie.substring(offset,endstr));
};

function GetCookie(name)
{
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while(i<clen)
  {
    var j=i+alen;
    if(document.cookie.substring(i,j)==arg)
      {return GetCookieValue(j);};
    i=document.cookie.indexOf(" ",i)+1;
    if(i==0)
      {break;};
  };
  return-1;
};

function SetCookie(WhichCookie,CookieValue)
{
  document.cookie=WhichCookie+"="+escape(CookieValue)+";path=/;expires="+EndDate.toGMTString();
};

function setCurrentSize(value)
{
  if(document.getElementById('sizechanger').options.length==26)
    {document.getElementById('sizechanger').remove(0);};
  SetCookie('TheBearFontSize',value);
  document.getElementById('ChangeMe').style.fontSize=value+'px';
};

function setCurrentFontCode(value)
{
  SetCookie('TheBearFontCode',value);
  document.getElementById('ChangeMe').style.fontFamily=FontName[value];
};

function setCurrentWidth(value)
{
  SetCookie('TheBearTxtWidth',value);
  document.getElementById('ChangeMe').style.width=value+'%';
};

function setCurrentBackColor(value)
{ var temp=value;
  if(value=='default'){temp=DefaultBackColor};
  SetCookie('TheBearBackColor',temp);
  document.bgColor=temp;
};

function setCurrentForeColor(value)
{ var temp=value;
  if(value=='default'){temp=DefaultForeColor};
  SetCookie('TheBearForeColor',temp);
  document.fgColor=temp;
};

function popup()
{
if (! window.focus)return true;
window.open('print.html','','width=300,height=300');
return false;
}