function extractPageName(hrefString)
{
	var arr = hrefString.split('.');
	arr = arr[arr.length-2].split('/');
	return arr[arr.length-1].toLowerCase();		
}

function setActiveMenu(arr, crtPage)
{
	for(var i=0; i < arr.length; i++)
		if(extractPageName(arr[i].href) == crtPage)
		{
			arr[i].className = "current";
			arr[i].parentNode.className = "current";
		}
}

function setPage()
{
	if(document.location.href) 
		hrefString = document.location.href;
	else
		hrefString = document.location;

	if (document.getElementById("left")!=null) 
		setActiveMenu(document.getElementById("left").getElementsByTagName("a"), extractPageName(hrefString));
		
}


function ss_fixAllLinks() {
 // Get a list of all links in the page
 var allLinks = document.getElementsByTagName('a');
 // Walk through the list
 for (var i=0;i<allLinks.length;i++) {
   var lnk = allLinks[i];
   if ((lnk.href && lnk.href.indexOf('#') != -1) &&  
       ( (lnk.pathname == location.pathname) ||
   ('/'+lnk.pathname == location.pathname) ) &&  
       (lnk.search == location.search)) {
     // If the link is internal to the page (begins in #)
     // then attach the smoothScroll function as an onclick
     // event handler
     ss_addEvent(lnk,'click',smoothScroll);
   }
 }
}

function smoothScroll(e) {
 // This is an event handler; get the clicked on element,
 // in a cross-browser fashion
 if (window.event) {
   target = window.event.srcElement;
 } else if (e) {
   target = e.target;
 } else return;
 
 // Make sure that the target is an element, not a text node
 // within an element
 if (target.nodeType == 3) {
   target = target.parentNode;
 }
 
 // Paranoia; check this is an A tag
 if (target.nodeName.toLowerCase() != 'a') return;
 
 // Find the <a name> tag corresponding to this href
 // First strip off the hash (first character)
 anchor = target.hash.substr(1);
 // Now loop all A tags until we find one with that name
 var allLinks = document.getElementsByTagName('a');
 var destinationLink = null;
 for (var i=0;i<allLinks.length;i++) {
   var lnk = allLinks[i];
   if (lnk.name && (lnk.name == anchor)) {
     destinationLink = lnk;
     break;
   }
 }
 
 // If we didn't find a destination, give up and let the browser do
 // its thing
 if (!destinationLink) return true;
 
 // Find the destination's position
 var destx = destinationLink.offsetLeft;  
 var desty = destinationLink.offsetTop;
 var thisNode = destinationLink;
 while (thisNode.offsetParent &&  
       (thisNode.offsetParent != document.body)) {
   thisNode = thisNode.offsetParent;
   destx += thisNode.offsetLeft;
   desty += thisNode.offsetTop;
 }
 
 // Stop any current scrolling
 clearInterval(ss_INTERVAL);
 
 cypos = ss_getCurrentYPos();
 
 ss_stepsize = parseInt((desty-cypos)/ss_STEPS);
 ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
 
 // And stop the actual click happening
 if (window.event) {
   window.event.cancelBubble = true;
   window.event.returnValue = false;
 }
 if (e && e.preventDefault && e.stopPropagation) {
   e.preventDefault();
   e.stopPropagation();
 }
}

function ss_scrollWindow(scramount,dest,anchor) {
 wascypos = ss_getCurrentYPos();
 isAbove = (wascypos < dest);
 window.scrollTo(0,wascypos + scramount);
 iscypos = ss_getCurrentYPos();
 isAboveNow = (iscypos < dest);
 if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
   // if we've just scrolled past the destination, or
   // we haven't moved from the last scroll (i.e., we're at the
   // bottom of the page) then scroll exactly to the link
   window.scrollTo(0,dest);
   // cancel the repeating timer
   clearInterval(ss_INTERVAL);
   // and jump to the link directly so the URL's right
   location.hash = anchor;
 }
}

function ss_getCurrentYPos() {
 if (document.body && document.body.scrollTop)
   return document.body.scrollTop;
 if (document.documentElement && document.documentElement.scrollTop)
   return document.documentElement.scrollTop;
 if (window.pageYOffset)
   return window.pageYOffset;
 return 0;
}

function ss_addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
}  

var ss_INTERVAL;
var ss_STEPS = 25;

ss_addEvent(window,"load",ss_fixAllLinks);


/**
The JavaScript functions below will gradually enlarge or shrink an image
on the current page. I use this for mouseover effects, but there might be
some other interesting applications of it as well.

You can use these scripts in any way you'd like, just don't pretend like
you wrote them yourself.

version 1.0
March 17, 2005
Julian Robichaux, http://www.nsftools.com
*/

/**** adjust these two parameters to control how fast or slow
 **** the images grow/shrink ****/
// how many milliseconds we should wait between resizing events
var resizeDelay = 10;
// how many pixels we should grow or shrink by each time we resize
var resizeIncrement = 5;

// this will hold information about the images we're dealing with
var imgCache = new Object();


/**
The getCacheTag function just creates a (hopefully) unique identifier for
each <img> that we resize.
*/
function getCacheTag (imgElement) {
	return imgElement.src + "~" + imgElement.offsetLeft + "~" + imgElement.offsetTop;
}


/**
We're using this as a class to hold information about the <img> elements
that we're manipulating.
*/
function cachedImg (imgElement, increment) {
	this.img = imgElement;
	this.cacheTag = getCacheTag(imgElement);
	this.originalSrc = imgElement.src;
	
	var h = imgElement.height;
	var w = imgElement.width;
	this.originalHeight = h;
	this.originalWidth = w;
	
	increment = (!increment) ? resizeIncrement : increment;
	this.heightIncrement = Math.ceil(Math.min(1, (h / w)) * increment);
	this.widthIncrement = Math.ceil(Math.min(1, (w / h)) * increment);
}


/**
This is the function that should be called in the onMouseOver and onMouseOut
events of an <img> element. For example:

<img src='onesmaller.gif' onMouseOver='resizeImg(this, 150, "onebigger.gif")' onMouseOut='resizeImg(this)'>

The only required parameter is the first one (imgElement), which is a
reference to the <img> element itself. If you're calling from onMousexxx, 
you can just use "this" as the value.

The second parameter specifies how much larger or smaller we should resize
the image to, as a percentage of the original size. In the example above,
we want to resize it to be 150% larger. If this parameter is omitted, we'll
assume you want to resize the image to its original size (100%).

The third parameter can specify another image that should be used as the
image is being resized (it's common for "rollover images" to be similar but
slightly different or more colorful than the base images). If this parameter
is omitted, we'll just resize the existing image.
*/
function resizeImg (imgElement, percentChange, newImageURL) {
	// convert the percentage (like 150) to an percentage value we can use
	// for calculations (like 1.5)
	var pct = (percentChange) ? percentChange / 100 : 1;
	
	// if we've already resized this image, it will have a "cacheTag" attribute
	// that should uniquely identify it. If the attribute is missing, create a
	// cacheTag and add the attribute
	var cacheTag = imgElement.getAttribute("cacheTag");
	if (!cacheTag) {
		cacheTag = getCacheTag(imgElement);
		imgElement.setAttribute("cacheTag", cacheTag);
	}
	
	// look for this image in our image cache. If it's not there, create it.
	// If it is there, update the percentage value.
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal) {
		imgCache[cacheTag] = new Array(new cachedImg(imgElement), pct);
	} else {
		cacheVal[1] = pct;
	}
	
	// if we're supposed to be using a rollover image, use it
	if (newImageURL)
		imgElement.src = newImageURL;
	
	// start the resizing loop. It will continue to call itself over and over
	// until the image has been resized to the proper value.
	resizeImgLoop(cacheTag);
	return true;
}


/**
This is the function that actually does all the resizing. It calls itself
repeatedly with setTimeout until the image is the right size.
*/
function resizeImgLoop (cacheTag) {
	// get information about the image element from the image cache
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal)
		return false;
	
	var cachedImageObj = cacheVal[0];
	var imgElement = cachedImageObj.img;
	var pct = cacheVal[1];
	var plusMinus = (pct > 1) ? 1 : -1;
	var hinc = plusMinus * cachedImageObj.heightIncrement;
	var vinc = plusMinus * cachedImageObj.widthIncrement;
	var startHeight = cachedImageObj.originalHeight;
	var startWidth = cachedImageObj.originalWidth;
	
	var currentHeight = imgElement.height;
	var currentWidth = imgElement.width;
	var endHeight = Math.round(startHeight * pct);
	var endWidth = Math.round(startWidth * pct);
	
	// if the image is already the right size, we can exit
	if ( (currentHeight == endHeight) || (currentWidth == endWidth) )
		return true;
	
	// increase or decrease the height and width, making sure we don't get
	// larger or smaller than the final size we're supposed to be
	var newHeight = currentHeight + hinc;
	var newWidth = currentWidth + vinc;
	if (pct > 1) {
		if ((newHeight >= endHeight) || (newWidth >= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	} else {
		if ((newHeight <= endHeight) || (newWidth <= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	}
	
	// set the image element to the new height and width
	imgElement.height = newHeight;
	imgElement.width = newWidth;
	
	// if we've returned to the original image size, we can restore the
	// original image as well (because we may have been using a rollover
	// image in the original call to resizeImg)
	if ((newHeight == cachedImageObj.originalHeight) || (newWidth == cachedImageObj.originalwidth)) {
		imgElement.src = cachedImageObj.originalSrc;
	}
	
	// shrink or grow again in a few milliseconds
	setTimeout("resizeImgLoop('" + cacheTag + "')", resizeDelay);
}
