/*/// these variables set the bar preferences ////*/

var bartype="screenbottom";
//var bartype="pagebottom";

var bardisplay="permanent";
//var bardisplay="onoff";

var barmargintop="50px";

//var bardictionary="off";
var bardictionary="off";

//URL of toolbar_css including bar.css
var cssbaseurl='http://www.brainhe.com/';
//var cssbaseurl='http://localhost/brainhe/';

/*///////////////////////////////////////////////*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) 
  browser = "Safari"
else if (checkIt('omniweb')) 
  browser = "OmniWeb"
else if (checkIt('opera')) 
  browser = "Opera"
else if (checkIt('webtv')) 
  browser = "WebTV";
else if (checkIt('icab')) 
  browser = "iCab"
else if (checkIt('msie')) 
  browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else 
  browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) 
    OS = "Linux";
	else if (checkIt('x11')) 
    OS = "Unix";
	else if (checkIt('mac')) 
    OS = "Mac"
	else if (checkIt('win')) 
    OS = "Windows"
	else 
    OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function dictionary()
{
  theword=document.getElementById('toolbarform').word.value
  thestring="http://dictionary.reference.com/search?q=" + theword;
  open(thestring,"dictionary");void(0);
}

function thesaurus()
{
  theword=document.getElementById('toolbarform').word.value
  thestring="http://thesaurus.reference.com/search?q=" + theword;
  open(thestring,"thesaurus");void(0);
}

//can change stylesheets depending on browser
if(browser=="Internet Explorer")
  cssurl=cssbaseurl+'toolbar_css/';
else
  cssurl=cssbaseurl+'toolbar_css/';

window.onbeforeprint=beforeprint
window.onafterprint=afterprint

function beforeprint(){ 
  document.getElementById('fixbottom').style.display='none';
}

function afterprint(){ 
  document.getElementById('fixbottom').style.display='inline';
}  

//write bar html (escape single quotes \')
document.write(''+
'<!--TEXTIC Toolbar. Copyright Textic Ltd. 2005. Unauthorised use is prohibited-->'+
'<form action="" id="toolbarform" onsubmit="">'+
'<div id="fixbottom" >'+
'<div id="toolbar">');

if(bardisplay!="permanent")
{
  document.write(''+
  '<div class="tool"><img class="tool" src="'  + cssbaseurl + 'lib/off_button.gif" alt="TEXTIC Toolbar off" width="19" height="20" onclick="hide_bar();" /></div>');
}

document.write(''+
'		<div class="tool"><img onclick="window.open(\'http://www.textic.com\',\'textic\',\'width=850,height=650,scrollbars=yes,resizable=yes,status=yes,directories=yes,menubar=yes,location=yes,toolbar=yes\');" class="tool" src="'  + cssbaseurl + 'lib/textic_logo.gif" alt="TEXTIC Toolbar" width="88" height="15" /></div>'+
'<!-- zoom -->'+
'		<div class="tool" >'+
'		<input type="hidden" name="sizeselect" value="1" />'+
'		<img onclick="javascript:zoom(1/1.2, browser);"" class="tool" src="'  + cssbaseurl + 'lib/icon_minus.gif" alt="Zoom out" width="20" height="20" /> '+
'		<img onclick="javascript:zoom(1.2, browser);" class="tool" src="'  + cssbaseurl + 'lib/icon_fsize.gif" alt="Zoom in" width="20" height="20" />'+
'    </div>'+
'		'+
'		<!-- font face -->'+
'		<div class="tool"><img class="tool" src="'  + cssbaseurl + 'lib/icon_fchoice.gif"  alt="Font choice" width="20" height="20" />'+
'		<select id="faceselectid" name="faceselect" class="tool" onchange="setChoice(\'face\',this.options[this.selectedIndex].value)">'+
'		<option class="tool" value="">Font choice</option>'+
'		<option class="tool" value="">---------</option>'+
'		<option class="tool" value="arial">Arial</option>'+
'		<option class="tool" value="verdana">Verdana</option>'+
'		<option class="tool" value="times">Times</option>'+
'		<option class="tool" value="comic">Comic</option>'+
'			</select></div>'+
'		'+
'		<!-- design scheme -->'+
'		<div class="tool"><img class="tool" src="'  + cssbaseurl + 'lib/icon_design.gif" alt="Design scheme" width="20" height="20" />'+
'		<select id="designselectid" name="designselect" class="tool" onchange="setChoice(\'design\',this.options[this.selectedIndex].value)">'+
'		<option value="">Design</option>'+
'		<option value="">---------</option>'+
'		<option value="">Default</option>'+
'		<option value="styleb">Reverse</option>'+
'		<option value="stylec">Slate</option>'+
'		<option value="styled">Simple</option>'+
'		<option value="stylee">Soft</option>'+
'		<option value="stylef">Red</option>'+
'		<option value="styleg">Orange</option>'+
'		<option value="styleh">Yellow</option>'+
'		<option value="stylei">Green</option>'+
'		<option value="stylej">Blue</option>'+
'		<option value="stylek">Violet</option>'+
'		</select></div>'+
'		'+
'<!-- font colour -->'+
'		<div class="tool"><img class="tool" src="'  + cssbaseurl + 'lib/icon_fcolour.gif"  alt="Font colour" width="20" height="20" />'+
'		<select id="colorselectid" name="colourselect" class="tool" onchange="setChoice(\'colour\',this.options[this.selectedIndex].value)">'+
'		<option value="">Font colour</option>'+
'		<option value="">---------</option>'+
'		<option value="black">Black</option>'+
'		<option value="red">Red</option>'+
'		<option value="orange">Orange</option>'+
'		<option value="yellow">Yellow</option>'+
'		<option value="green">Green</option>'+
'		<option value="blue">Blue</option>'+
'		<option value="violet">Violet</option>'+
'		</select></div>'+
''+
'<!-- tick and reset -->'+
'		<div class="tool"><img class="tool" src="'  + cssbaseurl + 'lib/icon_tick.gif" alt="BDA dyslexia-friendly" width="20" height="20" /> <input class="tool" type="button" value="Reset" onclick="resetall();" style="font-size: 8pt;" /></div>');
//onclick="window.open(\'http://www.textic.com/bdadyslexiafriendly.html\',\'textic\',\'width=850,height=650,scrollbars=yes,resizable=yes,status=yes,directories=yes,menubar=yes,location=yes,toolbar=yes\');"

if(bardictionary=="on")
{
  document.write(''+
'   <div class="tool"><table id="dictionary_table" cellpadding="0px" cellspacing="0px"><tr><td rowspan="2"><input style="float:left;" class="tool" name="word" size="8" /></td>'+
'   <td><a class="tool" style="font-size:9px;margin: 0px 0px 0px 2px;" href="javascript:dictionary();" title="type or drag a word into the box to access the Dictionary">&gt;&nbsp;Dictionary</a></td></tr>'+
'   <tr><td><a class="tool" style="font-size:9px;margin: 0px 0px 0px 2px;" href="javascript:thesaurus();"  title="type or drag a word into the box to access the Thesaurus">&gt;&nbsp;Thesaurus</a></td></tr></table>'+
'   </div>');
}

document.write(''+
'<!-- help -->'+
'		<div class="tool"><a href="http://www.textic.com/textic_toolbar_help.html"><img class="tool" src="'  + cssbaseurl + 'lib/icon_help.gif" alt="Help" width="20" height="20" /></a></div>'+
'<!-- copyright -->'+
'		<div class="tool" ><a href="#" class="tool" title="&copy; 2005 TEXTIC Ltd.">&copy;</a></div>'+
''+
''+
'</div>'+
'</div>'+
''+
'</form>'+
'<!--End toolbar-->');

//set width of bar depending on whats in it
if ( (bardictionary=="on") && (bardisplay=="onoff") )
  document.getElementById('toolbar').style.width="826px";
if( (bardisplay=="permanent") && (bardictionary=="off") )
  document.getElementById('toolbar').style.width="680px";
if( (bardisplay=="permanent") && (bardictionary=="on") )
  document.getElementById('toolbar').style.width="799px";


//Add stylesheet bar.css
if(browser=="Internet Explorer")
{  
  if(bartype=="screenbottom")
    setStyle('ie_bar/bar');
  if(bartype=="pagebottom")
  {
    setStyle('ie_bar/fixedbar');
    document.getElementById('fixbottom').style.position="relative";
    document.getElementById('fixbottom').style.marginTop=barmargintop;
  }

}
else
{ 
  if(bartype=="screenbottom")
    setStyle('other_bar/bar');
  if(bartype=="pagebottom")
  {
    setStyle('other_bar/fixedbar') 
    document.getElementById('fixbottom').style.position="absolute";
    document.getElementById('fixbottom').style.marginTop=barmargintop;
  }
}

  
//Add stylesheets and set dropdown selections stored in cookies
setChoices();

function gotourl(url) 
{
  location=url;
}

// Add stylesheet to head
function setStyle(value)
{

  if (value)
  {
    var csshref = cssurl+value+'.css';

    try 
    {
      if(document.createStyleSheet) 
      {
        document.createStyleSheet(csshref);
      } 
      else 
      {
        var objHead = document.getElementsByTagName('head');
        var objCSS = document.createElement('link');
        objCSS.rel = 'stylesheet'; 
        objCSS.href = csshref;
        objCSS.type = 'text/css';
        objHead[0].appendChild(objCSS);
      }
    }
    catch(e)
    {
	    alert (e+'could not add stylesheet')	
    }

  }
  
}


function setChoices()
{
  designc=getCookie('design');
  setStyle(designc);
  setchoice(document.getElementById('toolbarform').designselect,designc);

  sizec=getCookie('size');
  setzoom(sizec);

  colourc=getCookie('colour');
  setStyle(colourc);
  setchoice(document.getElementById('toolbarform').colourselect,colourc);

  facec=getCookie('face');
  setStyle(facec);
  setchoice(document.getElementById('toolbarform').faceselect,facec);

}


function setChoice(name,value)
{
  setCookie(name,value);
  if (value)
    setStyle(value);
  else 
    location.reload();

}

//set choice for dropdown
function setchoice(cobj,cvalue)
{

  for (var i=0;i<cobj.options.length;i++)
  {
    if(cobj.options[i].value==cvalue) 
      cobj.selectedIndex=i;break;
  }
  
}


function setzoom(z)
{
  
    if (z=='2') setStyle('medium');
    else if (z=='3') setStyle('large');
    else if (z=='4') setStyle('largest');

}


function zoom(f, browser)
{
  
  var z;
  z=document.getElementById('toolbarform').sizeselect.value;

  var agent=navigator.userAgent.toLowerCase();
  
  //alert(browser)
  
  if(browser!="Opera")
  {
  
    //if (agent.indexOf("msie")==-1 || agent.indexOf("mac")!=-1 )
    //{
      if (f>1 && z<4) 
        z++;
    
      else if (f<1 && z>1) 
      {
        z--;
        if (z==1) 
          location.reload();
      }
  
    //} 
    //else z*=f;

    //stop zoom below 100%
    if(z>=1)
    {
      document.getElementById('toolbarform').sizeselect.value=z;
      setCookie('size',z);
      setzoom(z);
    }

  }
  
}


function resetall()
{

  deleteCookie('design');
  setchoice(document.getElementById('toolbarform').designselect,'');
  
  deleteCookie('size');
  
  deleteCookie('face');
  setchoice(document.getElementById('toolbarform').faceselect,'');
  
  deleteCookie('colour');
  setchoice(document.getElementById('toolbarform').colourselect,'');
  
  location.reload();

}


function setCookie(name, value, expires, path, domain, secure)
{

  //cookie is valid throughout dir's
  path='/';

  if (expires) 
  {
  var expireDate = new Date();
  expireDate.setTime(expireDate.getTime() + 3600000*24*expires);
  }
    
  document.cookie= name + "=" + escape(value) +
  ((expires) ? "; expires=" + expireDate.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");

}

function getCookie(name)
{

  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);

  if (begin == -1)
  {
    begin = dc.indexOf(prefix);
    
    if (begin != 0) 
      return null;
  }
  else
  {
    begin += 2;
  }
  
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
  {
    end = dc.length;
  }
  
  return unescape(dc.substring(begin + prefix.length, end));

}

function deleteCookie(name, path, domain)
{
  
  //cookie is valid throughout dir's
  path='/';
  
  if (getCookie(name))
  {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  
}



window.onload=function init(){
  resize();
  display_bar();
}

//keep toolbar 100% of screen width when resized
function resize(){
  document.getElementById('fixbottom').style.width='100%';
}

function display_bar(){
  display=getCookie('toolbar_display');
  
  //toolbar is permanently on
  if(bardisplay=='permanent')
    display='on';
  
  if(display=='off' || display==null)
    document.getElementById('fixbottom').style.display="none";
  else
  { 
  
    if(browser=="Internet Explorer")
    {
      opstart=100;
      document.getElementById('fixbottom').style.filter = "alpha(opacity=opstart)";
    }
    else
    {
      opstart=95;
      document.getElementById('fixbottom').style.MozOpacity = opstart;
    }
    
    document.getElementById('fixbottom').style.filter = "alpha(opacity=opstart)";  
    document.getElementById('faceselectid').style.display="inline";
    document.getElementById('faceselectid').style.width="87px";
    document.getElementById('colorselectid').style.display="inline";
    document.getElementById('colorselectid').style.width="87px";
    document.getElementById('designselectid').style.display="inline";
    document.getElementById('designselectid').style.width="87px";
    document.getElementById('toolbar').style.margin="auto";
  }
}


//keep toolbar 100% of screen width
window.onresize=function resize(){
  document.getElementById('fixbottom').width='100%';
}

//keep toolbar 100% of screen width
document.getElementById('fixbottom').width='100%';

//set body to height of toolbar so nothing is obscured
if(bartype=="screenbottom")
{
  bodybottommargin=window.document.body.style.getAttribute('marginBottom');
  bodybottommargin+=65;
  window.document.body.style.setAttribute('marginBottom', bodybottommargin);
}

function hide_bar(){
  
  if(browser=="Internet Explorer")
    opstart=100;
  else
    opstart=69;
  
  done=opacity('fixbottom', opstart, 0, 500, true);
  setCookie('toolbar_display', 'off');
  
}

function show_bar(){
  

  if(document.getElementById('fixbottom').style.display=="none"){
  
  document.getElementById('fixbottom').style.display="inline";
  
  if(browser=="Internet Explorer")
    opstart=100;
  else
  {
    document.getElementById('fixbottom').style.MozOpacity = 0;
    opstart=95;
  }
  
  opacity('fixbottom', 0, opstart, 500, false);
  
  document.getElementById('faceselectid').style.display="inline";
  document.getElementById('colorselectid').style.display="inline";
  document.getElementById('designselectid').style.display="inline";

  
  setCookie('toolbar_display', 'on');
  
  }
  
}

function opacity(id, opacStart, opacEnd, millisec, hide) { 
    //speed for each frame 
    var speed = Math.round(millisec / 70); 
    var timer = 0; 
 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }    
  
  if(hide)
  setTimeout("document.getElementById('fixbottom').style.display='none'",(timer * speed));

} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

//this displays the on button - lib/ should be full path incase of subdirectories
//document.write('<div><img src="lib/textic-button.jpg" onclick="show_bar();" /></div>');

