// menu.js
//
function init() {
	var slash = location.href.lastIndexOf('/') + 1;
	var dot = location.href.lastIndexOf('.');
	var id = location.href.substring(slash, dot);
	if(dot == -1) id = "index";
	var li = document.getElementById(id);
	if(li) {
		var y = getTopOffset(li);
		li.className = "sel";
		li.style.borderRight = "none";
		li.style.background = "#89d9d8 url(img/gradient.png) 0px -" + y + "px repeat-x";
	}
}

function getTopOffset(obj) {
	// Get an object's y-coordinate
	if(!obj) return 0;
	var totalOffset = obj.offsetTop;
	while(obj = obj.offsetParent) totalOffset += obj.offsetTop;
	return totalOffset;
}

function topCorners(img) {
	// Write the top left and right corners
	corners(img, "TL,TR");
}

function bottomCorners(img) {
	// Write the bottom left and right corners
	corners(img, "BL,BR");
}

function corners(img, corners) {
	// Create rounded corners as specified
	var border = parseInt(img.substring(0,3));
	img = "img/corners/" + img.replace(' ', '_');
	var width = 6;
	var height= 6;
	
	// IE is weird with the positioning sometimes...
	var ie = (navigator.appVersion.indexOf('MSIE') > -1);
	if(!border || isNaN(border)) border = 1;
	var ieTL = 2;
	var ieTR = width - 1;
	var ieBL = 0;
	var ieBR = width - 3;

	// Position each corner
	var r = "";
	if(corners.indexOf("TL") > -1) {
		var style = "float: left; position: relative; left: -2px; top: -2px; ";
		if(ie) style += "left: -" + ieTL + "px; ";
  	r += '<img src="' + img + '_tl.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	if(corners.indexOf("TR") > -1) {
		var style = "float: right; position: relative; left: 2px; top: -2px; ";
		if(ie) style += "left: " + ieTR + "px; ";
  	r += '<img src="' + img + '_tr.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}	
	if(corners.indexOf("BL") > -1) {
		var style = "float: left; position: relative; left: -2px; top: -" + (height - border) + "px; ";
		if(ie) style += "left: -" + ieBL + "px; top: -" + height + "px";
		r += '<img src="' + img + '_bl.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	if(corners.indexOf("BR") > -1) {
		var style = "float: right; position: relative; left: 2px; top: -" + (height - border) + "px; ";
		if(ie) style += "left: " + ieBR + "px; top: -" + height + "px";
		r += '<img src="' + img + '_br.gif" height="' + height + '" width="'+ width + '" style="' + style + '" />';
	}
	
	document.write(r);
}