// 2004 01 25 , igd, start of 'common.js',  start from Gail and Macromedia
//    Sets   'this' with Browser version numbers
//    doc  = This Document reference;
//    sty  = This Document reference for style;
//    htm  = This document reference for HTML code
//    unit = px units ? 
//
//    getObj = For referencing objects...
//    Is     = Gather and set browser information


// alert('COMMON.JS');

var is = new Is();

// Initialize all Global variables
var	doc  = "";
var	sty  = "";
var	htm  = "";
var     unit = "";
var	xpos = ""; 
var	ypos = "";

var	available_width		= -1;
var	available_height	= -1;
var	page_height		= -1;

// BROWSER SNIFFER (Sniff out the good and bad browsers)
// 2004 01 25, lifted from Gail who got it from dhtmlguru.com website
//  (and then updated/modified by IGD :)

// /////////////////////////////////////////////////////////////
function Is(vis) {
    agent  = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
//    this.major = navigator.appVersion.substring(0,1);  // 3 or 4 
    this.minor = parseFloat(navigator.appVersion);

// Netscape
    this.ns    = ((agent.indexOf('mozilla')   !=   -1) &&
                 ((agent.indexOf('spoofer')   ==   -1) &&
                 (agent.indexOf('compatible') ==   -1)));
    this.ns2   = (this.ns && (this.major      ==    3));
    this.ns3   = (this.ns && (this.major      ==    3));
    this.ns4b  = (this.ns && (this.major      ==    4) &&
                 (this.minor                  <= 4.03));
    this.ns4   = (this.ns && (this.major      ==   4));
    this.ns6   = (this.ns && (this.major     >=    5));

// Microsoft Internet Explorer
    this.ie    = (agent.indexOf("msie")       !=   -1);
    this.ie3   = (this.ie && (this.major      <     4));
    this.ie4   = (this.ie && (this.major      ==    4));
    this.ie5   = (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 5.0")   !=   -1));
    this.ie55  = (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 5.5")   !=   -1));
    this.ie6   =  (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 6.0")   !=   -1));

// os
    this.win		= (agent.indexOf("win")   != -1);
    this.mac		= (agent.indexOf("mac")   != -1);
    this.unix  		= (agent.indexOf("x11")   != -1);

// Other oddities
    this.opera		= (agent.indexOf("opera") != -1);
    this.op3		= (agent.indexOf("opera") != -1);
    this.opera6		= (agent.indexOf("opera") != -1);
    this.firebird	= (agent.indexOf("firebird")!=-1);
    this.firefox	= (agent.indexOf("firefox")!=-1);

// Mozilla

    this.browserName    = navigator.appName.substring(0,8);

// 2006 11 14, igd, New style checking
//  from page  http://meyerweb.com/eric/tools/s5/ui/default/slides.js
//
// this.ie = navigator.appName == 'Microsoft Internet Explorer' ? 1 : 0;
// this.opera = agent.indexOf('Opera') > -1 ? 1 : 0;
// this.gecko = agent.indexOf('Gecko') > -1 && agent.indexOf('Safari') < 1 ? 1 : 0;
// this.safari = agent.indexOf('Safari') < 1 ? 1 : 0;

// DOCUMENT OBJECT SWITCH (Used for building cross_browser functions)
// IE5, IE6, FIREFOX, NETSCAPE, MOZILLA, OPERA, KONQUERER, Etc
//////////////////////////////////////////////////////////////
if(this.ns4) {
    doc = "document";
    sty = "";
    htm = ".document";
   xpos = "e.pageX";
   ypos = "e.pageY";
} else if(this.ie4 || this.opera6 ) {
    doc = "document.all";
    sty = ".style";
    htm = "";
   xpos = "event.x"; 
   ypos = "event.y";
}

if (this.ns4 && !this.ns6) {
  unit = "";
} else {
  unit = "px";
}


}

// ******************************************************************
// GetSize()  
//    -> sets	scrollHeight     = Page Height
//		available_width  = 
//		available_height =
///////////////////////////////////////////////////////////////////
function getSize() {
//  alert('BGSTARS.JS :  getSize: Start');

  if(is.ns4 || is.ns6) {
    available_width  = innerWidth;
    available_height = innerHeight;
  }   else if (is.ie6) {
    available_width  = document.documentElement.clientWidth;
    available_height = document.documentElement.clientHeight;
  }   else if (is.ie4 || is.opera6) {
    available_width  = document.body.clientWidth;
    available_height = document.body.clientHeight;
  }  else {
     ALERT(" YOU FOUND A BROWSER THAT I DON'T SUPPORT! \n" +
		" PLEASE NOTIFY IAIN WITH THE DETAILS \n" +
		navigator.userAgent.toLowerCase()
	);
    displayIS();
  }
//  alert('BGSTARS.JS :  getSize: IF');

//igd: todo: OLD? page_height = (is.ns4) ? contentHTML.height : contentHTML.clientHeight;

// TESTING - DEBUG CODE
// alert('Width=' + available_width + ',Height=' 	+ available_height );
}

// displayIS();

function displayIS() {
//  getSize();
    alert('IS: agent=['  + agent      		+ '],\n' 
	    +' major=['  + is.major 		+ '],'
	    +' minor=['  + is.minor 		+ '],'
	    +' AppVer=[' + navigator.appVersion + '], \n'
	    +' Name:' + is.browserName 
	    +', Win:' + is.win + ' Unix:' + is.unix + ' Mac:' + is.mac + '\n'
	    +' NS:' + is.ns + ', (4)='   + is.ns4 + ', (6) =' + is.ns6 + '\n '
	    +' IE:' + is.ie + ', (4)='   + is.ie4 + ', (6) =' + is.ie6 + '\n '
	    +' Firebird: ' + is.firebird  +' Firefox: ' + is.firefox + '\n '
	    +' SIZE: Width=' 	+ available_width 
	    +', Height=' 	+ available_height
	);
}

///////////////////////////////////////////////////////////////////
function getObj(objName, style) {
  if (is.ns6 || is.ie6 || is.opera6) {
    if (style) {
      return eval('document.getElementById("' + objName + '").style');
    }
    else {
      return eval('document.getElementById("' + objName + '")');
    }
  }
  else {
    if (style) {
      return eval(doc + '["' + objName + '"]' + sty);
    }
    else {
      return eval(doc + '["' + objName + '"]');
    }
  }
}

///////////////////////////////////////////////////////////////////
function setObjXY(objNam, X, Y, vis) {
  obj  = getObj(objNam,true);
  Obj.position="absolute";
  Obj.left = X + unit;
  Obj.top  = Y + unit;
  Obj.visibility = vis ? "visible" : "hidden";
}

///////////////////////////////////////////////////////////////////
function clipValues(top,right,bottom,left,move) {
	this.top = top;
	this.right = right;
	this.bottom = bottom;
	this.left = left;
	this.move = move;
}


function createControls() {
	var controlsDiv = document.getElementById("controls");
	if (!controlsDiv) return;
	var hider = ' onmouseover="showHide(\'s\');" onmouseout="showHide(\'h\');"';
	var hideDiv, hideList = '';
	if (controlVis == 'hidden') {
		hideDiv = hider;
	} else {
		hideList = hider;
	}
	controlsDiv.innerHTML = '<form action="#" id="controlForm"' + hideDiv + '>' +
	'<div id="navLinks">' +
	'<a accesskey="t" id="toggle" href="javascript:toggle();">&#216;<\/a>' +
	'<a accesskey="z" id="prev" href="javascript:go(-1);">&laquo;<\/a>' +
	'<a accesskey="x" id="next" href="javascript:go(1);">&raquo;<\/a>' +
	'<div id="navList"' + hideList + '><select id="jumplist" onchange="go(\'j\');"><\/select><\/div>' +
	'<\/div><\/form>';
	if (controlVis == 'hidden') {
		var hidden = document.getElementById('navLinks');
	} else {
		var hidden = document.getElementById('jumplist');
	}
	addClass(hidden,'hideme');
}

function mouseMove(e) {
    mouse_y = eval(ypos);
    mouse_x = eval(xpos);
}

function mouseDown(e) {
//    shieldLyr.visibility = "visible";
//    page_height = (is.ns4) ? contentHTML.height : contentHTML.clientHeight;
}

function mouseUp(e) {

}

///////////////////////////////////////////////////////////////////
function prepareKeyDown() {
   if(is.ns4) {
        document.onkeydown = keyDown;
        document.captureEvents(Event.KEYDOWN);
        choreographer('layerSetup',null);
    } else if(is.ie4 || is.ie5) {
       document.onkeydown = keyDown;
        choreographer('layerSetup',null);

    }
}

function keyDown(e) {
    var key_press = (is.ns4) ? e.which : window.event.keyCode;
    if(key_press == 97 || key_press == 65 || key_press == 56 || key_press == 57) {
        pageScroll(true);
    }
    if(key_press == 122 || key_press == 90 || key_press == 32 || key_press == 50 || key_press == 51) {
        pageScroll(false);
    }
    if(key_press == 115 || key_press == 83 || key_press == 55) {
        contentLyr.top = 20;
    }
    if(key_press == 49) {
        contentLyr.top = -(page_height-20);
    }
}

// 'keys' code adapted from MozPoint (http://mozpoint.mozdev.org/)
function keys(key) {
	if (!key) {
		key = event;
		key.which = key.keyCode;
	}
	if (key.which == 84) {
		toggle();
		return;
	}
	if (s5mode) {
		switch (key.which) {
			case 10: // return
			case 13: // enter
				if (window.event && isParentOrSelf(window.event.srcElement, 'controls')) return;
				if (key.target && isParentOrSelf(key.target, 'controls')) return;
				if(number != undef) {
					goTo(number);
					break;
				}
			case 32: // spacebar
			case 34: // page down
			case 39: // rightkey
			case 40: // downkey
				if(number != undef) {
					go(number);
				} else if (!incrementals[snum] || incpos >= incrementals[snum].length) {
					go(1);
				} else {
					subgo(1);
				}
				break;
			case 33: // page up
			case 37: // leftkey
			case 38: // upkey
				if(number != undef) {
					go(-1 * number);
				} else if (!incrementals[snum] || incpos <= 0) {
					go(-1);
				} else {
					subgo(-1);
				}
				break;
			case 36: // home
				goTo(0);
				break;
			case 35: // end
				goTo(smax-1);
				break;
			case 67: // c
				showHide('k');
				break;
		}
		if (key.which < 48 || key.which > 57) {
			number = undef;
		} else {
			if (window.event && isParentOrSelf(window.event.srcElement, 'controls')) return;
			if (key.target && isParentOrSelf(key.target, 'controls')) return;
			number = (((number != undef) ? number : 0) * 10) + (key.which - 48);
		}
	}
	return false;
}



function NewWindow(flag)
{
    url  = 'http://www.dystopia.org';
    title = 'copy';
    params = 'width=415,height=460,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,left=0,top=20,screenX=0,screenY=20';
 window.open(url, title, params );

}

function writeMesg(mesg) {
        document.write(msg);
        document.close();
}

/////////////////////////////////
function onerror() {
    document.location.href = "javascript:";
}

//alert('COMMON.JS: End');

////////  E O F ///////////////////////////////////////////////////