// Modified from Bill Dortch's Cookie Functions (hidaho.com)
// (found in JavaScript Bible)
function setCookie(cname,cvalue,days,path,cdomain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = cname + "=" + escape(cvalue) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((cdomain) ? "; domain=" + cdomain : "") +
    ((secure) ? "; secure" : "");
}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(cname) {
  var nameq = cname + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com)
function deleteCookie(cname,path,cdomain) {
  if (getCookie(cname)) {
    document.cookie = cname + "=" +
      ((path) ? "; path=" + path : "") +
      ((cdomain) ? "; domain=" + cdomain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth(){
    if (window.innerWidth != null)
        return window.innerWidth;
    if (document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    if (document.body != null)
        return document.body.clientWidth;
    return null;
}

function pageHeight() {
    if (window.innerHeight != null)
        return window.innerHeight;
    if (document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;
    if (document.body != null)
        return document.body.clientHeight;
    return null;
}

function posLeft() {
    if (typeof window.pageXOffset != 'undefined')
        return window.pageXOffset;
    if (document.documentElement && document.documentElement.scrollLeft)
        return document.documentElement.scrollLeft;
    if (document.body.scrollLeft)
        return document.body.scrollLeft;
    return 0;
}

function posTop() {
    if (typeof window.pageYOffset != 'undefined')
        return window.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (document.body.scrollTop)
        return document.body.scrollTop;
    return 0;
}
function posRight() {return posLeft()+pageWidth();}
function posBottom() {return posTop()+pageHeight();}


function adjustLayout(scaling) {

    // determine the page (window) size
    var pwidth = parseFloat(pageWidth());
    if (pwidth <= 0)
        return false;
    var pheight = parseFloat(pageHeight());
    if (pheight <= 0)
        return false;

    // dimensions of the banner - as defined in flash
    // these also determine its aspect ratio which cannot be changed.
    var flashwidth=687;
    var flashheight=321;
    var aspect = flashwidth / flashheight;


    // bwidth and bheight are the dimensions of the banner to be used.
    var bwidth = flashwidth * ((parseFloat(scaling) + 0.35) * 2);
    var bheight = flashheight * ((parseFloat(scaling) + 0.35) * 2);
    // constrain these by the page dimensions
    if(bwidth > pwidth)
    {
        bwidth = pwidth;
        bheight = bwidth / aspect;
    }
    var min_lm = 0;  // minimum left margin

	// position the resize buttons at the top left of the screen
	// then ensure that the min left margin leaves space for them
    var obj = GetStyleObject("resize");
    if (obj)
    {
		obj.left = '0px';
		obj.top = '0px';
	}
    var o = document.getElementById("resize");
	if (o)
	{
        min_lm = parseInt(o.offsetWidth)+ parseInt(o.offsetLeft) + 5;
	}
    // we now have a sensible size for the banner
	// - centre it as the top of the screen
	// but ensure we have a bit of space for the left margin.
    var lm;
    lm = (pwidth / 2) - (bwidth / 2);
    if (lm < min_lm)
	{
        lm = min_lm;
		// reduce the banner width to ensure centring
		bwidth = pwidth - (2 * lm);
		// preserve the aspect ratio of the banner
        bheight = bwidth / aspect;
	}
    //alert('scaling='+scaling+' min='+minheight+' max='+maxheight+'fn (width='+pageWidth()+' height='+pageHeight()+' top='+posTop()+' bottom='+posBottom()+'). bwidth='+bwidth+' bheight='+bheight+' lm='+lm);
    // IE is not case-sensitive on IDs, other browsrs are
    // most browsers require style parameters to be suffixed 'px'
    // (IE will accept either format).
    obj = GetStyleObject("mainheader");
    //alert('got style');
    if (obj)
    {
        //alert('mainheader');
        //alert('obj top='+obj.top+' width='+obj.width+' height='+obj.height+' left='+obj.left);
		obj.position = 'absolute';
		obj.visibility = 'visible';
        obj.top = 0;
        obj.width = parseInt(bwidth) + 'px';
        obj.height = parseInt(bheight) + 'px';
        obj.left = lm + 'px';
        /*alert('obj top='+obj.top+' '+getStyle('mainheader','top')
             +' width='+obj.width+' '+getStyle('mainheader','width')
             +' height='+obj.height+' '+getStyle('mainheader','height')
             +' left='+obj.left+' '+getStyle('mainheader','left'));
        */
    }

    /* the main panel will either be centred or left
	    These are essentially handled the same way
		The only difference is the width and left margin of the panel
	*/
    var footer_top = 0;
	var lobj;
    obj = GetStyleObject("main_panel_centred");
    if (obj)
    {
        var o = document.getElementById("main_panel_centred");
        //alert('mp height='+o.offsetHeight+' top='+o.offsetTop+' width='+o.offsetWidth+ ' '+bwidth);
		obj.position = 'absolute';
		obj.visibility = 'visible';
        obj.width = parseInt(bwidth) + 'px';
        obj.top = (parseInt(bheight) + 5) + 'px';
        obj.left = lm + 'px';
        //alert('obj top='+obj.top+' width='+obj.width+' height='+obj.height+' left='+obj.left);
        footer_top = parseInt(o.offsetHeight)+ parseInt(o.offsetTop) + 5;
	}
    lobj = GetStyleObject("main_panel_left");
    if (lobj)
    {
        var o = document.getElementById("main_panel_left");
		var mpmargin = 50;  // a small margin
		if (pwidth <= mpmargin)
			mpmargin = 0;  // absurdity check
		lobj.position = 'absolute';
		lobj.visibility = 'visible';
        lobj.width = (pwidth - (2 * mpmargin)) + 'px';
        lobj.top = (parseInt(bheight) + 5) + 'px';
        lobj.left = mpmargin +'px';
        footer_top = parseInt(o.offsetHeight)+ parseInt(o.offsetTop) + 5;
        //alert('obj top='+lobj.top+' width='+lobj.width+' height='+lobj.height+' left='+lobj.left+' footer top='+footer_top);
		/*  Positioning the top of the footer is difficult. For some reason, it
		    seems to go wrong with this left panel rather than the centred panel
			(why should that make a difference). 
			Trouble is, it overlaps the readable text.
			Try adding a percentage to the position of the footer, might come out
			a bit low, but at least the text is readable
		*/
		footer_top += 0.05 * footer_top;
	}

    var fobj = GetStyleObject("mainfooter");
    if (fobj)
    {
       if (document.getElementById)
       {
          var o = document.getElementById("mainfooter");
          fobj.position = 'absolute';
		  fobj.visibility = 'visible';
          fobj.width = parseInt(bwidth) + 'px';
          fobj.top = footer_top + 'px';
          fobj.left = lm +'px';
          //alert('fheight='+o.offsetHeight+' top='+o.offsetTop+' width='+o.offsetWidth+ ' '+bwidth);
       }
   }
    return false;
}

function GetStyleObject(oid)
{
    if (document.getElementById)
    {
	    if (document.getElementById(oid))
	    {
           return document.getElementById(oid).style;
	    }
    }
    else if (document.layers)
    {
        //alert('doc layers');
        return document.layers[oid];
    }
    else if (document.all)
    {
        //alert('doc all');
        return document.all[oid].style;
    }
    else
	{
        return null;
    }
}

function init_layout()
{
    var scale = parseFloat(text_size_control.init());
	adjustLayout(scale);
}

function resize_layout()
{
    adjustLayout(text_size_control.value());
}

function adjust_layout(n)
{
	var scale = parseFloat(text_size_control.value()) + parseFloat(n);
	//alert('adjust_layout='+n+' scale='+scale);
	
    scale=text_size_control.set(scale);
    adjustLayout(scale);
}

function inc()
{
   adjust_layout(0.05);
}

function dec()
{
    adjust_layout(-0.05);
}

var text_size_control = {
    units:          "pt",
    default_size:    10,
    max_size:        28,
    min_size:        8,
    current_factor:  0.5,

    denormalise:function(factor)
    {
        return (factor*(this.max_size - this.min_size)) + this.min_size;
    },

    normalise:function(size)
    {
        return (size - this.min_size)/(this.max_size - this.min_size);
    },

    init:function()
	  {
        if(!document.getElementById||!document.getElementsByTagName)
            return;

        this.current_factor=getCookie("textSize");
		//alert('init cf='+this.current_factor);
        if(  isNaN(parseFloat(this.current_factor)))
        {
            this.current_factor = this.normalise(this.default_size);
        }
        this.set(this.current_factor);
        return this.current_factor;
    },

    set:function(new_factor)
	  {
        var size = this.denormalise(new_factor);
        if(size > this.max_size)
        {
            size = this.max_size;
            this.current_factor = this.normalise(this.max_size);
        }
        else if(size < this.min_size)
        {
            size = this.min_size;
            this.current_factor = this.normalise(this.min_size);
        }
        else
        {
            this.current_factor = new_factor;
        }

        var body_list = document.getElementsByTagName('body');
        if (body_list)
        {
             if (body_list.length > 0)
            {
                 body_list[0].style.fontSize=size+this.units;
            }
        }
		//alert('set nf='+new_factor+' cf='+this.current_factor+' size='+size);
        setCookie("textSize",this.current_factor,180,"/");
        return this.current_factor;
    },

    value:function()
    {
		//alert('value='+this.current_factor);
        return parseFloat(this.current_factor);
    }
};

