var d=document;

handleLinks = function() {
	var a = document.getElementsByTagName('a');
	for(var loop=0;loop<a.length;loop++) {
		if(a[loop].rel == "external") {
			a[loop].onclick = bumperPage;
		}	
	}
}

function bumperPage() {
	var result = popUpWindow("bumper.html?URL=" + escape(this.href), "bumper", 580, 370);
	return (result == false);
}

popUpWindow = function(URL,windowName,width,height) {
	var w = screen.availWidth;
	var h = screen.availHeight;
	var leftPos = Math.round((w-width)/2);
	var topPos = Math.round((h-height)/2);
	var defaults = "scrollbars, resizable";
	var centerOnScreen = "top="+topPos+", left="+leftPos+", width="+width+", height="+height;
	var options = centerOnScreen + " ," + defaults;
	var msgWindow = window.open(URL,windowName,options);
	if(!msgWindow) {
		return false;
	} else {
		msgWindow.creator=self;
		msgWindow.focus();
	}
	return true;
};

var pageName = ""; // optional value for Manticore tracking
var MonitorFlash = function(sPageName, sPageTitle, sContentCat) {
    if (typeof mtcGO == "function") {
	  	try {
			//console.log('tracking', arguments);
		} catch(err) {}
	    mtcSetAbsolutePageName(sPageName);
		mtcSetPageTitle(sPageTitle);
		sTMContentCategory = (typeof sContentCat != "undefined") ? sContentCat : "";
	    mtcGO();
	} else { // wait for manticore to load
		try {
			//console.log('waiting', arguments);
		} catch(err) {}
		setTimeout(function() {
			MonitorFlash(sPageName, sPageTitle, sContentCat);
		}, 200);
	}
};

// page init functions
////////////////////////////////////////
// Dean Edwards/Matthias Miller/John Resig

function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) {
        clearInterval(_timer);
        _timer = null;
    }
	
	selectNav();
	fontControls();
	handleLinks();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

// expode and parseQuery are functions to deal with query string
function explode(item,delimiter) {
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);
	while (tempString.indexOf(delimiter)>0) {
		tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
		tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
		Count=Count+1
	}
	tempArray[Count]=tempString;
	return tempArray;
}
function parseQuery() {
	var returnVals = new Array();
	var qString = new String(window.location);
	var queryStart = qString.indexOf('?');
	if (queryStart==-1) {
		return returnVals;
	}
	var query = qString.substring(queryStart + 1, qString.length);
	parts = explode(query, "&");
	for (i in parts) {
		bits = explode(parts[i], "=");
		returnVals[bits[0].toLowerCase()] = bits[1]; // query[] indexes are now lowercase!
	}
	return returnVals;
}

var query = parseQuery();

function selectNav() {
	secBase = (typeof(secBase) != 'undefined') ? secBase : '/01.aspx';
	var d = document.getElementById('selNav');
	if (d) {
		var l = d.length;
		for(var x=0; x<l; x++) {
			if(secBase == d[x].value) {
				d.selectedIndex = x;
				break;
			}
		}
		
		d.onchange=function() {
			document.location=d.value;
		}
	}
}

setActiveStyleSheet = function(title) {
	if(d.getElementById && d.createTextNode) {
		var i, a;
		for(i=0; (a = d.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;
			}
		}
		imgOff(d.getElementById("txtsize_small"));
		imgOff(d.getElementById("txtsize_medium"));
		imgOff(d.getElementById("txtsize_large"));
		imgOn(d.getElementById("txtsize_" + title));
	}
}

fontControls = function() {
	if(d.getElementById("font-small")) {
		d.getElementById("font-small").onclick = function(){fontSize('small'); this.blur(); return false;};
		d.getElementById("font-medium").onclick = function(){fontSize('medium'); this.blur(); return false;};
		d.getElementById("font-large").onclick = function(){fontSize('large'); this.blur(); return false;};
	}
	setStyles();
}

setStyles = function() {
var cookie = readCookie("kcoStyles");
var title = cookie ? cookie : "small";
createCookie("kcoStyles", title, 365);
setActiveStyleSheet(title);
}

fontSize = function(fs) {
 createCookie("kcoStyles", fs, 365);
 setActiveStyleSheet(fs);
}

imgOff = function (img) {
	if (img) img.src = img.src.replace(/_on\.(gif|jpg)/, ".$1");
}

imgOn = function (img) {
	if (img) img.src = img.src.replace(/\.(gif|jpg)/, "_on.$1");
}

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 = "";
 d.cookie = name+"="+value+expires+"; path=/";
}

readCookie = function(name) {
 var nameEQ = name + "=";
 var ca = d.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;
}