
//////////////////////////////////////////////////////////////////////
function addWindowOnLoad(func) {
	var cur_func = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function(){
			cur_func();
			func();
		}
	}
}
//////////////////////////////////////////////////////////////////////
function addWindowOnUnLoad(func) {
	var cur_func = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function(){
			cur_func();
			func();
		}
	}
}

//////////////////////////////////////////////////////////////////////
// Roll Over Images for navigations
function chimg(n){
	if (!n || !document.images[n]) return;

	var imgsrc = document.images[n].src;
	if (imgsrc.indexOf("_ov.gif") < 0) {
		document.images[n].src=imgsrc.substring(0,imgsrc.length-4) +"_ov.gif";
	} else{
		document.images[n].src=imgsrc.substring(0,imgsrc.length-7) +".gif";
	}
}


//////////////////////////////////////////////////////////////////////
// Popup Images
function popup(url,name,width,height) {
	var data = "width="+width+",height="+height+",scrollbars=1,resizable=1";
	popwin = window.open(url,name,data);
	if (popwin) popwin.focus();
}

//////////////////////////////////////////////////////////////////////
// Popup Window
function popupWin(aobj, width, height) {
	var url = aobj.href;
	var trg = aobj.target;
	var opt = "width="+width+",height="+height+",scrollbars=1,resizable=1";
	popwin = window.open(url, trg, opt);
	if (popwin) popwin.focus();
}


//link function for Opener
function openerLink(url) {
	if ( !url ) return;

	if ( window.opener ) {
		window.opener.location.href = url;
		window.opener.focus();
	}
	else {
		var newwin = window.open(url, '_blank', '');
		if ( newwin ) newwin.focus();
	}
}


//////////////////////////////////////////////////////////////////////
function printPage(){
	
	if (typeof preHookPrintPage == 'function') {
		preHookPrintPage();
	}
	
	var data = "width=672,height=750,scrollbars=1,resizable=1";
	printwin = window.open('/js/print.html','printable',data);
	printwin.focus();
}


//////////////////////////////////////////////////////////////////////
var StyleSwitcher = {

	buttonImages: [
		{'style': 'small',  'id': 'fontSmall'},
		{'style': 'normal', 'id': 'fontNormal'},
		{'style': 'large',  'id': 'fontLarge'}],

	setActive: function(title) {
		var i, a, main;
		for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				a.disabled = true;
				if (a.getAttribute("title") == title) a.disabled = false;
			}
		}
		
		var imgs = StyleSwitcher.buttonImages;
		var len  = imgs.length;
		for (i=0; i<len; i++) {
			var id = imgs[i].id;

			if (!document.images[id]) continue;

			var imgsrc = document.images[id].src;
			if (title == imgs[i].style) {
				if (imgsrc.indexOf("_cr.gif") < 0) {
					document.images[id].src = imgsrc.substring(0, imgsrc.length-4) +"_cr.gif";
				}
			} else {
				if (imgsrc.indexOf("_cr.gif") > -1) {
					document.images[id].src = imgsrc.substring(0, imgsrc.length-7) +".gif";
				}
			}
		}
	},

	getActive: function() {
		var i, a;
		for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
		}
		return null;
	},

	getPreferred: function() {
		var i, a;
		for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("rel").indexOf("alt") == -1
			&& a.getAttribute("title")
			) return a.getAttribute("title");
		}
		return null;
	},

	createCookie: function(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=/";
	},

	readCookie: function(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;
	},

	init: function() {
		addWindowOnLoad(function() {
			var cookie = StyleSwitcher.readCookie("style");
			var title = cookie ? cookie : StyleSwitcher.getPreferred();
			StyleSwitcher.setActive(title);
		});

		addWindowOnUnLoad(function() {
			var title = StyleSwitcher.getActive();
			if (title) {
			  StyleSwitcher.createCookie("style", title, 365);
			}
		});

		var cookie = StyleSwitcher.readCookie("style");
		var title = cookie ? cookie : StyleSwitcher.getPreferred();
		StyleSwitcher.setActive(title);
	}
}

StyleSwitcher.init();

// wrapper function 
function setActiveStyleSheet(title) {
	StyleSwitcher.setActive(title);
}




