//INITIALIZATION
///////////////////////////
var html = document.getElementsByTagName('html')[0];
jscss('add',html,'hasScript');

var hasReqdDOM = (document.getElementsByTagName && document.getElementById);

function PageInit() {
	if ( !(hasReqdDOM) ) return;
	var FS = new FontSizer('body','upsize','downsize','reset');
}
addEvent(window,'load',PageInit);


// FONT SIZE HANDLER
////////////////////////////////
FontSizer.Instances = new Object();
var baseSize = 10;
function FontSizer(type,upid,downid,resetid) {
	this.cookieName = type+"FontSizer";
	var x = readCookie(this.cookieName);
	this.currentSize = (x) ? parseInt(x) : baseSize;
	if (this.currentSize!=baseSize) this.ChangeSize();
	this.stepSize = 2;
	
	FontSizer.Instances[type] = this;
	
	var upObj = getObj(upid);
	if (upObj) upObj.onclick =  function() { FontSizer.Instances[type].UpSize(); return false; };
	var downObj = getObj(downid);
	if (downObj) downObj.onclick =  function() { FontSizer.Instances[type].DownSize(); return false; };
	var rsObj = getObj(resetid);
	if (rsObj) rsObj.onclick = function() { FontSizer.Instances[type].Reset(); return false; }
}

var FSP = FontSizer.prototype;
FSP.Reset = function() { eraseCookie(this.cookieName); this.currentSize = baseSize; this.ChangeSize(); }
FSP.ChangeSize = function() { createCookie(this.cookieName,this.currentSize,7); 
	var form = document.getElementsByTagName('form')[0];
	form.style.fontSize = (this.currentSize / baseSize) + 'em';
 }
FSP.UpSize = function() { var cs = this.currentSize; cs += this.stepSize; if (cs > 20) cs = 20; this.currentSize = cs; this.ChangeSize(); }
FSP.DownSize = function() { var cs = this.currentSize; cs -= this.stepSize; if (cs < 9) cs = 9; this.currentSize = cs; this.ChangeSize(); }

//UTILITY FUNCTIONS
///////////////////////////
var wini; //Reference to a window
function PopWin(url, name, width, height) {
	var s;
	if (width || height) {
		h = (height) ? height : 600;
		w = (width) ? width : 800;
		lp = (screen.width) ? (screen.width-w)/2 : 0;
		tp = (screen.height) ? (screen.height-h)/2 : 0;
		sc = (name == 'gallery') ? 'no' : 'yes';
		s = 'height='+ h +',width='+ w +',top='+tp+',left='+lp+',scrollbars='+sc+',resizable,menubar=1';
	}
	else s = '';
	wini = window.open(url,name,s);
	if (wini) wini.focus();
	return ( !wini )
}// PopWin()

function getObj(objId) {
	if (document.getElementById)
		obj = document.getElementById(objId);
	else if (document.all)
		obj = document.all[objId];
	else
		obj = null;
	return obj;
}

function addEvent(obj,evType,fn){
	if(obj.addEventListener){
		obj.addEventListener(evType,fn,false);
	}
	else if(obj.attachEvent){
		var r=obj.attachEvent("on"+evType,fn);
	}
	else{
		if ( typeof( obj['on'+evType] ) == 'function' ) {
			var last = obj['on'+evType] ;
			obj['on'+evType] = null;
			obj['on'+evType] = function() { last(); fn(); };
		}
		else obj['on'+evType] = fn;
	}
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win -jps disabled
  //obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}



function includeCSS(file,media){
	var head = document.getElementsByTagName('head').item(0);
	var scriptTag = document.getElementById('loadCSS');
	if(scriptTag) head.removeChild(scriptTag);
	css = document.createElement('link');
	css.rel = "stylesheet";
	css.href = file;
	css.type = 'text/css';
	css.id = 'loadCSS';
	css.media = media;
	head.appendChild(css)
}

// found at http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
function jscss(a,o,c1,c2) {
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

// COOKIE HANDLERS
//////////////////////////////
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function eraseCookie(name) { createCookie(name,"",-1); }

// FUNCTIONS NEEDED FOR VARIOUS DETECTIONS
///////////////////////////////////////////
function getIEVersion() {
  var rv = -1;
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.mimeTypes.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			var y = x.description;
   			flashversion = y.charAt(y.indexOf('.')-1);
		}
	} else {
		result = false;
		for(var i = 15; i >= 3 && result != true; i--){
			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
			flashversion = i;
		}
	}
	return flashversion;
}
function detectFlash(ver) { return (getFlashVersion() >= ver) ? true:false; }
